[英]col2im implementation in ConvNet
我正在尝试仅使用 numpy 来实现CNN 。
在进行反向传播时,我发现我必须使用col2im来重塑dx ,所以我检查了https://github.com/huyouare/CS231n/blob/master/assignment2/cs231n/im2col.py的实现。
import numpy as np
def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1):
# First figure out what the size of the output should be
N, C, H, W = x_shape
assert (H + 2 * padding - field_height) % stride == 0
assert (W + 2 * padding - field_height) % stride == 0
out_height = (H + 2 * padding - field_height) / stride + 1
out_width = (W + 2 * padding - field_width) / stride + 1
i0 = np.repeat(np.arange(field_height), field_width)
i0 = np.tile(i0, C)
i1 = stride * np.repeat(np.arange(out_height), out_width)
j0 = np.tile(np.arange(field_width), field_height * C)
j1 = stride * np.tile(np.arange(out_width), out_height)
i = i0.reshape(-1, 1) + i1.reshape(1, -1)
j = j0.reshape(-1, 1) + j1.reshape(1, -1)
k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1)
return (k, i, j)
def im2col_indices(x, field_height, field_width, padding=1, stride=1):
""" An implementation of im2col based on some fancy indexing """
# Zero-pad the input
p = padding
x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding,
stride)
cols = x_padded[:, k, i, j]
C = x.shape[1]
cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1)
return cols
def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1,
stride=1):
""" An implementation of col2im based on fancy indexing and np.add.at """
N, C, H, W = x_shape
H_padded, W_padded = H + 2 * padding, W + 2 * padding
x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype)
k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding,
stride)
cols_reshaped = cols.reshape(C * field_height * field_width, -1, N)
cols_reshaped = cols_reshaped.transpose(2, 0, 1)
np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped)
if padding == 0:
return x_padded
return x_padded[:, :, padding:-padding, padding:-padding]
pass
我预计当我将X放入im2col_indices并将该输出放回col2im_indices 时将返回相同的X ,但事实并非如此。
我不明白 col2im 实际上是做什么的。
如果我是对的,输出不是相同的 X,因为 X 的每个单元格都被转换为多个col
,并且在im2col_indices
期间已经相乘。
假设你有一个像这样的简单图像X
1 2 3
4 5 6
7 8 9
并使用内核大小 3、步长 1 和same
填充对其进行转换,结果将是
0 0 0 0 1 2 0 4 5
0 0 0 1 2 3 4 5 6
0 0 0 2 3 0 5 6 0
0 1 2 0 4 5 0 7 8
1 2 3 4 5 6 7 8 9
2 3 0 5 6 0 8 9 0
0 4 5 0 7 8 0 0 0
4 5 6 7 8 9 0 0 0
5 6 0 8 9 0 0 0 0
* * * *
如您所见,第一个值为 1 的单元格显示在四个col
: col
。
im2col_indices
首先零初始化具有填充大小的图像,然后将每个col
添加到其中。 关注第一个单元格,过程应该是这样的
1.零初始化图像
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
2.添加col 0
0 0 0 0 0 0 0 0 - - 0 0 0 0 0
0 0 0 0 0 0 1 2 - - 0 1 2 0 0
0 0 0 0 0 + 0 4 5 - - = 0 4 5 0 0
0 0 0 0 0 - - - - - 0 0 0 0 0
0 0 0 0 0 - - - - - 0 0 0 0 0
3.添加第col 1
0 0 0 0 0 - 0 0 0 - 0 0 0 0 0
0 1 2 0 0 - 1 2 3 - 0 2 4 3 0
0 4 5 0 0 + - 4 5 6 - = 0 8 10 6 0
0 0 0 0 0 - - - - - 0 0 0 0 0
0 0 0 0 0 - - - - - 0 0 0 0 0
4.添加第col 3
0 0 0 0 0 - - - - - 0 0 0 0 0
0 2 4 3 0 0 1 2 - - 0 3 6 3 0
0 8 10 6 0 + 0 4 5 - - = 0 12 15 6 0
0 0 0 0 0 0 7 8 - - 0 7 8 0 0
0 0 0 0 0 - - - - - 0 0 0 0 0
5.添加第col 4
0 0 0 0 0 - - - - - 0 0 0 0 0
0 3 6 3 0 - 1 2 3 - 0 4 8 6 0
0 12 15 6 0 + - 4 5 6 - = 0 16 20 12 0
0 7 8 0 0 - 7 8 9 - 0 14 16 9 0
0 0 0 0 0 - - - - - 0 0 0 0 0
转换回来时,第一个单元格乘以 4。 对于这个简单的图像, col2im_indices(im2col_indices(X))
应该给你
4 12 12
24 45 36
28 48 36
与原始图像相比,四个角单元格1 3 7 9
乘以 4,四个边缘单元格2 4 6 8
乘以 6,中心单元格5
乘以 9。
对于大图像,大多数单元格将乘以 9,我认为这大致意味着您的学习率实际上比您想象的要大 9 倍。
回复这个 2 年前的帖子,它可能会在未来帮助某人。
这是我的理解。 在 CNN 反向传播上下文中,col2im 矩阵是过滤器和反向传播误差 (dout) 的乘积。 必须注意,该矩阵已经是两个矩阵的乘积,这与前向传递中的 im2col 用例不同,我们刚刚将输入拉伸为 im2col 矩阵,准备进行乘法(卷积)。 由于 im2col 和 col2im 之间的这种差异,在 col2im 中,我们需要向所有贡献输入索引添加反向传播误差。
让我们考虑一个 1x5x5 输入、单个 1x3x3 过滤器、0 填充、步长 1 的示例。输入的索引将如下所示:
[0,0] [0,1] [0,2] [0,3] [0,4]
[1,0] [1,1] [1,2] [1,3] [1,4]
[2,0] [2,1] [2,2] [2,3] [2,4]
[3,0] [3,1] [3,2] [3,3] [3,4]
[4,0] [4,1] [4,2] [4,3] [4,4]
为前向传播矩阵乘法计算的结果 9x9 im2col 索引将如下所示:
im2col 索引
<----------------------- 9 ----------------------------->
[ 0] [0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2]
[ 1] [0,1] [0,2] [0,3] [1,1] [1,2] [1,3] [2,1] [2,2] [2,3]
[ 2] [0,2] [0,3] [0,4] [1,2] [1,3] [1,4] [2,2] [2,3] [2,4]
[ 3] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2] [3,0] [3,1] [3,2]
[ 4] [1,1] [1,2] [1,3] [2,1] [2,2] [2,3] [3,1] [3,2] [3,3]
[ 5] [1,2] [1,3] [1,4] [2,2] [2,3] [2,4] [3,2] [3,3] [3,4]
[ 6] [2,0] [2,1] [2,2] [3,0] [3,1] [3,2] [4,0] [4,1] [4,2]
[ 7] [2,1] [2,2] [2,3] [3,1] [3,2] [3,3] [4,1] [4,2] [4,3]
[ 8] [2,2] [2,3] [2,4] [3,2] [3,3] [3,4] [4,2] [4,3] [4,4]
在反向传播中,当我们通过将反向传播的误差 Dout 和滤波器相乘来生成 col2im 矩阵时,如上所示的结果索引已经是乘法的结果。 当我们将其转换回输入错误时,我们需要在输入错误数组中的给定位置添加相应的索引。
例如:
input_error[0,0] = im2col_error[0,0]
input_error[0,1] = im2col_error[0,1] + im2col_error[1,0]
input_error[0,2] = im2col_error[0,2] + im2col_error[1,1] + im2col_error[2,0]
....
....
从上面的指数矩阵可以明显看出这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.