-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv2d_transpose_numpy.py
256 lines (209 loc) · 8.99 KB
/
conv2d_transpose_numpy.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import numpy as np
def numpy_conv2d(inputs, kernels, strides, padding, return_kernel=False):
'''
Convolute `inputs` with `kernels`.
Args:
inputs: A `numpy.ndarray` of shape (number of samples, number of
channels, height, width)
kernels: A `numpy.ndarray` of shape (number of kernels,
number of channels, height, width). The number of channels
of `kernels` must match with that of `inputs`.
strides: A tuple of two positive `int`.
padding: A non-negative `int`.
return_kernel: A `bool`. If `True`, return the unrolled kernel
and the convoluted outputs, otherwise, return only the
convoluted outputs.
Returns:
outputs: A `numpy.ndarray`. The convoluted outputs.
'''
_inputs = _padding(inputs, padding, 'normal')
outputs_shape = compute_outputs_shape(_inputs, kernels, strides, 'normal')
_kernels = _unroll_kernel(_inputs, kernels, outputs_shape, 'normal')
_inputs = _flatten(_inputs)
outputs = np.tensordot(_inputs, _kernels, ((1, 2), (0, 1)))
outputs = outputs.transpose(0,2,1).reshape(outputs_shape)
outputs = _stride(outputs, strides, 'normal')
if return_kernel:
return (outputs, _kernels)
return outputs
def numpy_conv2d_transpose(inputs, kernels, strides, outputs_shape, padding,
output_padding, return_kernel=False):
'''
Transposed convolute `inputs` with `kernels`.
Args:
inputs: A `numpy.ndarray` of shape (number of samples, number of
channels, height, width)
kernels: A `numpy.ndarray` of shape (number of channels, number
of kernels, height, width). The number of channels of
`kernels` must match with that of `inputs`.
strides: A tuple of two positive `int`.
outputs_shape: A `numpy.ndarray` of shape (number of samples,
number of channels, height, width). It is computed by
`compute_outputs_shape()`.
padding: A non-negative `int`.
return_kernel: A `bool`. If `True`, return the unrolled kernel
and the transposed-convoluted outputs, otherwise, return only
the transposed-convoluted outputs.
Returns:
outputs: A `numpy.ndarray`. The convoluted outputs.
'''
_inputs = _stride(inputs, strides, 'transposed', output_padding)
_kernels = _unroll_kernel(_inputs, kernels, outputs_shape, 'transposed')
_inputs = _flatten(_inputs)
outputs = np.tensordot(_inputs, _kernels, ((1, 2), (0, 1)))
outputs = outputs.transpose(0,2,1).reshape(outputs_shape)
outputs = _padding(outputs, padding, 'transposed')
if return_kernel:
return (outputs, _kernels)
return outputs
#################
## utils
#################
def _padding(inputs, padding, mode):
'''
Pad (`mode=='normal'`) or crop (`mode=='transposed'`).
Args:
inputs: A `numpy.ndarray` of shape (number of samples, number of
channels, height, width).
padding: A non-negative `int`.
mode: A `str`. Either 'normal' or 'tranposed'.
Returns:
outputs: A `numpy.ndarray`. Padded or cropped `inputs`.
'''
p = padding
m, c, h, w = inputs.shape
if mode == 'normal': # do padding
outputs = np.zeros((m, c, h+2*p, w+2*p), dtype=np.float32)
outputs[:,:,p:h+p,p:w+p] = inputs
elif mode == 'transposed': # do cropping
outputs = inputs[:,:,p:h-p,p:w-p]
return outputs
def _flatten(inputs):
'''
Flatten the axes for height and width.
Args:
inputs: A `numpy.ndarray` of shape (number of samples, number of
channels, height, width).
Returns:
outputs: A `numpy.ndarray`. Flattened `inputs`.
'''
m, c, h, w = inputs.shape
return inputs.reshape((m, c, -1))
def _stride(inputs, strides, mode, output_padding=None):
'''
If `mode=='normal'`, keep an element of `inputs`every `strides` step
along both height and width axes. This shrinks `inputs`. If
`mode='transposed'`, add `strides` zeros between every element of
`inputs` along both height and width axes. This expands `inputs`.
Args:
inputs: A `numpy.ndarray` of shape (number of samples, number of
channels, height, width).
strides: A tuple of two positive `int`.
output_padding: A non-negative `int` for specifying how many
additional rows and columns are added to one side of the
output. Only valid when `mode=='transposed'`.
mode: A `str`. Either 'normal' or 'tranposed'.
Returns:
outputs: A `numpy.ndarray`. Expanded or shrinked.
'''
sh, sw = strides
m, c, h, w = inputs.shape
if mode == 'normal': # get every other N rows/columns
outputs = inputs[:,:,::sh,::sw]
elif mode == 'transposed': # add N rows/columns of 0 between two rows/columns
_h = h + (h - 1) * (sh - 1) + output_padding
_w = w + (w - 1) * (sw - 1) + output_padding
outputs = np.zeros((m,c,_h,_w), dtype=np.float32)
outputs[:,:,::sh,::sw] = inputs
return outputs
def _unroll_kernel(inputs, kernels, outputs_shape, mode):
'''
Unroll `kernels`
Args:
inputs: A `numpy.ndarray` of shape (number of samples, number of
channels, height, width)
kernels: A `numpy.ndarray`. If `mode=='normal'`, it has the
shape (number of kernels, number of channels, height,
width). If `mode=='transposed'`, it has the shape of (
number of channels, number of kernels, height, width). The
number of channels of `kernels` must match with that of
`inputs`.
outputs_shape: A `numpy.ndarray` of shape (number of samples,
number of channels, height, width). It is computed by
`compute_outputs_shape()`
mode: A `str`. Either 'normal' or 'tranposed'
Returns:
outputs: A `numpy.ndarray`. Unrolled kernel.
'''
# __, __, oh, ow = outputs_shape
# __, ic, ih, iw = inputs.shape
# kn, kc, kh, kw = kernels.shape
# if mode == 'normal':
# assert ic == kc
# _kernel = np.zeros((kc, ih, iw, kn), dtype=np.float32)
# _kernel[:,:kh,:kw,:] = kernels.transpose(1,2,3,0)
# _kernel = _kernel.reshape(kc, -1, kn)
# outputs = np.stack([
# np.roll(_kernel, iw * x + y, axis=1) for x in range(oh) for y in range(ow)
# ], axis=2)
# elif mode == 'transposed':
# assert ic == kn
# _kernel = np.zeros((kn, oh, ow, kc), dtype=np.float32)
# _kernel[:,:kh,:kw,:] = kernels.transpose(0,2,3,1)
# _kernel = _kernel.reshape(kn, -1, kc)
# outputs = np.stack([
# np.roll(_kernel, ow * x + y, axis=1) for x in range(ih) for y in range(iw)
# ], axis=1)
inputs_shape = inputs.shape
if mode == 'transposed':
inputs_shape, outputs_shape = outputs_shape, inputs_shape
kernels = kernels.transpose(1,0,2,3)
__, ic, ih, iw = inputs_shape
__, oc, oh, ow = outputs_shape
kn, kc, kh, kw = kernels.shape
_kernel = np.zeros((kc, ih, iw, kn), dtype=np.float32)
_kernel[:,:kh,:kw,:] = kernels.transpose(1,2,3,0)
_kernel = _kernel.reshape(kc, -1, kn)
r = np.arange(oh*ow) // ow * (kw - 1)
y, x = np.ogrid[:ih*iw,:oh*ow]
idx = (y - (x + r)) % (ih*iw)
if mode == 'transposed':
idx = idx.T
outputs = _kernel[:,idx,:]
return outputs
def compute_outputs_shape(inputs, kernels, strides, mode, output_padding=None):
'''
Compute the output shape of the normal/transposed convolution.
Args:
inputs: A `numpy.ndarray` of shape (number of samples, number of
channels, height, width)
kernels: A `numpy.ndarray`. If `mode=='normal'`, it has the
shape (number of kernels, number of channels, height,
width). If `mode=='transposed'`, it has the shape of (
number of channels, number of kernels, height, width). The
number of channels of `kernels` must match with that of
`inputs`.
strides: A tuple of two positive `int`.
mode: A `str`. Either 'normal' or 'tranposed'
output_padding: A non-negative `int` for specifying how many
additional rows and columns are added to one side of the
output. Only valid when `mode=='transposed'`.
Returns:
outputs: A `numpy.ndarray`. Output shape.
'''
sh, sw = strides
im, ic, ih, iw = inputs.shape
kn, kc, kh, kw = kernels.shape
if mode == 'normal':
outputs_shape = (
im,
kn,
ih - kh + 1,
iw - kw + 1)
elif mode == 'transposed':
outputs_shape = (
im,
kc,
(ih - 1) * sh + kh + output_padding,
(iw - 1) * sw + kw + output_padding)
return outputs_shape