-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconvtranspose2d.py
430 lines (367 loc) · 15.3 KB
/
convtranspose2d.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from typing import Any, Literal, Union
import cupy as cp
import numpy as np
import neunet
from neunet.autograd import Tensor
from neunet.nn.modules import Module
from neunet.nn.parameter import Parameter
class _ConvTranspose2dTensor(Tensor): # tensor for static backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def prepare_grad(grad, padding, stride, dilated_kernel_size, output_padding):
padded_grad = set_padding(
grad, padding
) # ADD set padding that we removed in forward #in conv2dTranspose set padding equals remove padding
grad = padded_grad[
:,
:,
dilated_kernel_size[0] - 1 : padded_grad.shape[2]
- (dilated_kernel_size[0] - 1)
- output_padding[0], # remove kernel padding that we added
dilated_kernel_size[1] - 1 : padded_grad.shape[3]
- (dilated_kernel_size[1] - 1)
- output_padding[1],
].copy()
unstrided_grad = remove_stride(grad, stride)
return unstrided_grad
def grad_fn(
X: Tensor,
weight: Tensor,
bias: Tensor,
out_channels,
padding,
stride,
dilation,
output_padding,
prepared_input_size,
conv_size,
dilated_kernel_size,
windows,
grad
):
batch_size, _, _, _ = X.shape
grad_pattern = X.xp.zeros(
(
batch_size,
out_channels,
int(
prepared_input_size[0]
+ X.xp.max(X.xp.array([conv_size[0], dilated_kernel_size[0]]))
- 1
),
int(
prepared_input_size[1]
+ X.xp.max(X.xp.array([conv_size[1], dilated_kernel_size[1]]))
- 1
),
),
dtype=grad.dtype,
)
grad_pattern[
:,
:,
dilated_kernel_size[0] - 1 : conv_size[0] + dilated_kernel_size[0] - 1,
dilated_kernel_size[1] - 1 : conv_size[1] + dilated_kernel_size[1] - 1,
] = grad
batch_str, channel_str, kern_h_str, kern_w_str = grad_pattern.strides
grad_windows = X.xp.lib.stride_tricks.as_strided(
grad_pattern,
(
batch_size,
out_channels,
prepared_input_size[0],
prepared_input_size[1],
dilated_kernel_size[0],
dilated_kernel_size[1],
),
(batch_str, channel_str, kern_h_str, kern_w_str, kern_h_str, kern_w_str),
)
weight_rot_180 = X.xp.rot90(weight.data, 2, axes=(2, 3))
grad_weight = X.xp.einsum("bihwkl,bohw->oikl", windows, grad)
grad_bias = X.xp.sum(grad, axis=(0, 2, 3))
grad_X = X.xp.einsum("bohwkl,oikl->bihw", grad_windows, weight_rot_180)
grad_X = prepare_grad(grad_X, padding, stride, dilated_kernel_size, output_padding)
weight.data = remove_stride(weight.data, dilation)
grad_weight = remove_stride(grad_weight, dilation)
X.apply_grad(grad_X)
weight.apply_grad(grad_weight)
if bias is not None:
bias.apply_grad(grad_bias)
self.grad_fn = grad_fn
class ConvTranspose2d(Module): # layer with static backpropagation
"""
Add 2d transposed convolutional layer
-------------------------------------
Args:
`kernels_num`: number of kernels
`kernel_shape` (tuple) of size 2 or (int): height and width of kernel
`padding` (tuple) of size 2 or (int) or `"same"`, `"real same"`, `"valid"` string value: the "inverted" padding of the input window (removing padding)
{
`"valid"`: padding is 0
`"same"`: keras "same" implementation, that returns the output of size "input_size + stride_size"
`"real same"`: my "same" implementation, that returns the output of size "input_size"
}
`stride` (tuple) of size 2 or (int): the transposed stride (operation similar to dilation) of the 2d input window
`dilation` (tuple) of size 2 or (int): the dilation of the sliding kernel
`use_bias` (bool): `True` if used. `False` if not used
Returns:
output: output_array (numpy.ndarray): the output array with shape: (batch_size, channels_num, conv_height, conv_width)
References:
https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose2d.html
"""
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: Union[int, tuple[int, int]],
stride: Union[int, tuple[int, int]]=(1, 1),
padding: Union[int, tuple[int, int]]=(0, 0),
dilation: Union[int, tuple[int, int]]=(1, 1),
output_padding: Union[int, tuple[int, int]]=(0, 0),
bias: bool=True,
device: Literal["cpu", "cuda"] = "cpu",
):
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = (
kernel_size if isinstance(kernel_size, tuple) else (kernel_size, kernel_size)
)
self.padding = padding if isinstance(padding, tuple) else (padding, padding)
self.stride = stride if isinstance(stride, tuple) else (stride, stride)
self.dilation = dilation if isinstance(dilation, tuple) else (dilation, dilation)
self.output_padding = (
output_padding
if isinstance(output_padding, tuple)
else (output_padding, output_padding)
)
stdv = 1.0 / np.sqrt(self.in_channels * self.kernel_size[0] * self.kernel_size[1])
self.weight = Parameter(
neunet.tensor(
np.random.uniform(
-stdv,
stdv,
(
self.out_channels,
self.in_channels,
self.kernel_size[0],
self.kernel_size[1],
),
),
dtype=np.float32,
)
)
if bias == True:
self.bias: Union[Tensor, None] = Parameter(neunet.tensor(np.zeros(self.out_channels), dtype=np.float32))
else:
self.bias = None
self.input_size: Any = None
self.to(device)
def build(self):
self.kernel_height, self.kernel_width = self.kernel_size
self.input_height, self.input_width = self.input_size[2:]
if self.padding == "valid":
self.padding = (0, 0, 0, 0)
elif self.padding == "same" or self.padding == "real same":
if self.padding == "same":
padding_up_down = (
(1 - self.stride[0])
+ self.dilation[0] * (self.kernel_height - 1)
+ self.output_padding[0]
)
padding_left_right = (
(1 - self.stride[1])
+ self.dilation[1] * (self.kernel_width - 1)
+ self.output_padding[1]
)
elif self.padding == "real same":
padding_up_down = (
(self.stride[0] - 1) * (self.input_height - 1)
+ self.dilation[0] * (self.kernel_height - 1)
+ self.output_padding[0]
)
padding_left_right = (
(self.stride[1] - 1) * (self.input_width - 1)
+ self.dilation[1] * (self.kernel_width - 1)
+ self.output_padding[1]
)
if padding_up_down % 2 == 0:
padding_up, padding_down = padding_up_down // 2, padding_up_down // 2
else:
padding_up, padding_down = (
padding_up_down // 2,
padding_up_down - padding_up_down // 2,
)
if padding_left_right % 2 == 0:
padding_left, padding_right = (
padding_left_right // 2,
padding_left_right // 2,
)
else:
padding_left, padding_right = (
padding_left_right // 2,
padding_left_right - padding_left_right // 2,
)
self.padding = (padding_up, padding_down, padding_left, padding_right)
elif len(self.padding) == 2:
self.padding = (
self.padding[0],
self.padding[0],
self.padding[1],
self.padding[1],
) # (top, bottom, left, right) padding ≃ (2 * vertical, 2 *horizontal) padding
self.conv_height = (
(self.input_height - 1) * self.stride[0]
- (self.padding[0] + self.padding[1])
+ self.dilation[0] * (self.kernel_height - 1)
+ self.output_padding[0]
+ 1
)
self.conv_width = (
(self.input_width - 1) * self.stride[1]
- (self.padding[2] + self.padding[3])
+ self.dilation[1] * (self.kernel_width - 1)
+ self.output_padding[1]
+ 1
)
self.conv_size = (self.conv_height, self.conv_width)
self.dilated_kernel_height = self.dilation[0] * (self.kernel_height - 1) + 1
self.dilated_kernel_width = self.dilation[1] * (self.kernel_width - 1) + 1
self.dilated_kernel_size = (
self.dilated_kernel_height,
self.dilated_kernel_width,
)
self.prepared_input_height = (
(self.input_height - 1) * self.stride[0]
+ 1
- (self.padding[0] + self.padding[1])
+ self.output_padding[0]
+ 2 * self.dilated_kernel_height
- 2
)
self.prepared_input_width = (
(self.input_width - 1) * self.stride[1]
+ 1
- (self.padding[2] + self.padding[3])
+ self.output_padding[1]
+ 2 * self.dilated_kernel_width
- 2
)
self.prepared_input_size = (
self.prepared_input_height,
self.prepared_input_width,
)
def forward(self, X: Tensor) -> Tensor:
if not isinstance(X, Tensor):
raise TypeError("Input must be a tensor")
if X.device != self.device:
raise ValueError("Tensors must be on the same device")
self.input_size = X.shape
self.build()
X_data = self.prepare_inputs(X.data)
self.weight.data = set_stride(self.weight.data, self.dilation)
batch_size = len(X_data)
batch_str, channel_str, kern_h_str, kern_w_str = X_data.strides
windows = self.xp.lib.stride_tricks.as_strided(
X_data,
(
batch_size,
self.in_channels,
self.conv_size[0],
self.conv_size[1],
self.dilated_kernel_size[0],
self.dilated_kernel_size[1],
),
(batch_str, channel_str, kern_h_str, kern_w_str, kern_h_str, kern_w_str),
)
O = self.xp.einsum("bihwkl,oikl->bohw", windows, self.weight.data)
if self.bias is not None:
O += self.bias.data[:, None, None]
return _ConvTranspose2dTensor(
O,
(
X,
self.weight,
self.bias,
self.out_channels,
self.padding,
self.stride,
self.dilation,
self.output_padding,
self.prepared_input_size,
self.conv_size,
self.dilated_kernel_size,
windows,
),
"convtranspose2d",
self.device,
)
def prepare_inputs(self, input_data):
xp = np if isinstance(input_data, np.ndarray) else cp
temp_strided = set_stride(input_data, self.stride) # ADD STRIDING
# add output_padding here #WARNING output padding must be smaller than either stride or dilation,
temp_out = xp.zeros(
(
temp_strided.shape[0],
temp_strided.shape[1],
temp_strided.shape[2] + self.output_padding[0],
temp_strided.shape[3] + self.output_padding[1],
),
dtype=input_data.dtype,
)
temp_out[:, :, : temp_strided.shape[2], : temp_strided.shape[3]] = (
temp_strided # ADD output_padding
)
input_data = xp.zeros(
( # add kernel padding
input_data.shape[0],
input_data.shape[1],
temp_out.shape[2] + 2 * (self.dilated_kernel_size[0] - 1),
temp_out.shape[3] + 2 * (self.dilated_kernel_size[1] - 1),
),
dtype=input_data.dtype,
)
input_data[
:,
:,
self.dilated_kernel_size[0] - 1 : temp_out.shape[2] + self.dilated_kernel_size[0] - 1,
self.dilated_kernel_size[1] - 1 : temp_out.shape[3] + self.dilated_kernel_size[1] - 1,
] = temp_out
input_data = remove_padding(
input_data, self.padding
) # ADD remove padding #in conv2dTranspose set padding equals remove padding
return input_data
def __call__(self, X):
return self.forward(X)
def set_padding(array, padding):
# New shape: (_, _, H + P[0] + P[1], W + P[2] + P[3])
xp = np if isinstance(array, np.ndarray) else cp
return xp.pad(
array,
((0, 0), (0, 0), (padding[0], padding[1]), (padding[2], padding[3])),
constant_values=0,
)
def remove_padding(array, padding):
# New shape: (_, _, H - P[0] - P[1], W - P[2] - P[3])
return array[
:,
:,
padding[0] : array.shape[2] - padding[1],
padding[2] : array.shape[3] - padding[3],
]
def set_stride(array, stride):
# New shape: (_, _, S[0] * H - (S[0] - 1), S[1] * W - (S[1] - 1)
xp = np if isinstance(array, np.ndarray) else cp
strided_array = xp.zeros(
(
array.shape[0],
array.shape[1],
stride[0] * array.shape[2] - (stride[0] - 1),
stride[1] * array.shape[3] - (stride[1] - 1),
),
dtype=array.dtype,
)
strided_array[:, :, :: stride[0], :: stride[1]] = array
return strided_array
def remove_stride(array, stride):
# New shape: (_, _, (H + S[0] - 1) // S[0], (W + S[1] - 1) // S[1])
return array[:, :, :: stride[0], :: stride[1]]