正如在这个问题中所讨论的,反卷积只是一个卷积层,但具有特定的填充、步幅和滤波器大小选择。
例如,如果您当前的图像大小是,您可以应用与和55x55
的卷积来获得图像,然后padding=20
以此类推。(我并不是说这种数字选择给出了输出图像的所需质量,只是大小。实际上,我认为从to下采样然后再上采样回过于激进,但您可以自由尝试任何架构)。stride=1
filter=[21x21]
75x75
95x95
227x227
55x55
227x227
这是任何步幅和填充的前向传递的实现。它执行im2col 转换,但使用stride_tricks
from numpy. 它不像现代 GPU 实现那样优化,但绝对比4 个内部循环快:
import numpy as np
def conv_forward(x, w, b, stride, pad):
N, C, H, W = x.shape
F, _, HH, WW = w.shape
# Check dimensions
assert (W + 2 * pad - WW) % stride == 0, 'width does not work'
assert (H + 2 * pad - HH) % stride == 0, 'height does not work'
# Pad the input
p = pad
x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
# Figure out output dimensions
H += 2 * pad
W += 2 * pad
out_h = (H - HH) / stride + 1
out_w = (W - WW) / stride + 1
# Perform an im2col operation by picking clever strides
shape = (C, HH, WW, N, out_h, out_w)
strides = (H * W, W, 1, C * H * W, stride * W, stride)
strides = x.itemsize * np.array(strides)
x_stride = np.lib.stride_tricks.as_strided(x_padded,
shape=shape, strides=strides)
x_cols = np.ascontiguousarray(x_stride)
x_cols.shape = (C * HH * WW, N * out_h * out_w)
# Now all our convolutions are a big matrix multiply
res = w.reshape(F, -1).dot(x_cols) + b.reshape(-1, 1)
# Reshape the output
res.shape = (F, N, out_h, out_w)
out = res.transpose(1, 0, 2, 3)
out = np.ascontiguousarray(out)
return out