forked from HirokiNakahara/GUINNESS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_binary_conv2d.py
109 lines (88 loc) · 3.99 KB
/
link_binary_conv2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import math
import function_binary_conv2d
from chainer import initializers
from chainer import link
import numpy
class Convolution2D(link.Link):
"""Two-dimensional convolutional layer.
This link wraps the :func:`~chainer.functions.convolution_2d` function and
holds the filter weight and bias vector as parameters.
Args:
in_channels (int): Number of channels of input arrays. If None,
parameter initialization will be deferred until the first forward
data pass at which time the size will be determined.
out_channels (int): Number of channels of output arrays.
ksize (int or pair of ints): Size of filters (a.k.a. kernels).
``ksize=k`` and ``ksize=(k, k)`` are equivalent.
stride (int or pair of ints): Stride of filter applications.
``stride=s`` and ``stride=(s, s)`` are equivalent.
pad (int or pair of ints): Spatial padding width for input arrays.
``pad=p`` and ``pad=(p, p)`` are equivalent.
wscale (float): Scaling factor of the initial weight.
bias (float): Initial bias value.
nobias (bool): If ``True``, then this link does not use the bias term.
use_cudnn (bool): If ``True``, then this link uses cuDNN if available.
initialW (4-D array): Initial weight value. If ``None``, then this
function uses to initialize ``wscale``.
May also be a callable that takes ``numpy.ndarray`` or
``cupy.ndarray`` and edits its value.
initial_bias (1-D array): Initial bias value. If ``None``, then this
function uses to initialize ``bias``.
May also be a callable that takes ``numpy.ndarray`` or
``cupy.ndarray`` and edits its value.
.. seealso::
See :func:`chainer.functions.convolution_2d` for the definition of
two-dimensional convolution.
Attributes:
W (~chainer.Variable): Weight parameter.
b (~chainer.Variable): Bias parameter.
"""
def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0,
wscale=1, bias=0, nobias=False, use_cudnn=True,
initialW=None, initial_bias=None):
super(Convolution2D, self).__init__()
self.ksize = ksize
self.stride = _pair(stride)
self.pad = _pair(pad)
self.use_cudnn = use_cudnn
self.out_channels = out_channels
self.initialW = initialW
self.wscale = wscale
if in_channels is None:
self.add_uninitialized_param('W')
else:
self._initialize_params(in_channels)
kh, kw = _pair(self.ksize)
W_shape = (self.out_channels, in_channels, kh, kw)
#self.add_param('W', W_shape)
# For backward compatibility, the scale of weights is proportional to
# the square root of wscale.
initializers.init_weight(self.W.data, self.initialW,
scale=math.sqrt(self.wscale))
if nobias:
self.b = None
else:
self.add_param('b', out_channels)
if initial_bias is None:
initial_bias = bias
initializers.init_weight(self.b.data, initial_bias)
def _initialize_params(self, in_channels):
kh, kw = _pair(self.ksize)
W_shape = (self.out_channels, in_channels, kh, kw)
self.add_param('W', W_shape)
# For backward compatibility, the scale of weights is proportional to
# the square root of wscale.
initializers.init_weight(self.W.data, self.initialW,
scale=math.sqrt(self.wscale))
def __call__(self, x):
"""Applies the convolution layer.
Args:
x (~chainer.Variable): Input image.
Returns:
~chainer.Variable: Output of the convolution.
"""
return function_binary_conv2d.func_convolution_2d(x, self.W, self.b, self.stride, self.pad, self.use_cudnn)
def _pair(x):
if hasattr(x, '__getitem__'):
return x
return x, x