我正在阅读一本深度学习书籍(第7章,CNN),其中包含了实现 im2col 的代码(链接)。它的目的是将四维数组转换为二维数组。但我不知道为什么该实现中有一个六维数组。我非常想知道作者使用该算法的思想背后的原理。
我已经尝试搜索很多关于 im2col 实现的论文,但没有一个像这样使用高维数组。我找到的有用于可视化 im2col 过程的材料是此论文的图片(链接)。
我已经尝试搜索很多关于 im2col 实现的论文,但没有一个像这样使用高维数组。我找到的有用于可视化 im2col 过程的材料是此论文的图片(链接)。
def im2col(input_data, filter_h, filter_w, stride=1, pad=0):
"""
Parameters
----------
input_data : (batch size, channel, height, width), or (N,C,H,W) at below
filter_h : kernel height
filter_w : kernel width
stride : size of stride
pad : size of padding
Returns
-------
col : two dimensional array
"""
N, C, H, W = input_data.shape
out_h = (H + 2*pad - filter_h)//stride + 1
out_w = (W + 2*pad - filter_w)//stride + 1
img = np.pad(input_data, [(0,0), (0,0), (pad, pad), (pad, pad)], 'constant')
col = np.zeros((N, C, filter_h, filter_w, out_h, out_w))
for y in range(filter_h):
y_max = y + stride*out_h
for x in range(filter_w):
x_max = x + stride*out_w
col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]
col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N*out_h*out_w, -1)
return col