用python实现卷积神经网络——im2col函数

卷积神经网络的核心之一——卷积运算,常用到函数im2col。它将包含批数量的4维数据转换成2维数据,以适应展开的滤波器。
im2col 的代码如下:

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
def im2col(input_data, filter_h, filter_w, stride=1, pad=0):
    """

    Parameters
    ----------
    input_data : 由(数据量, 通道, 高, 长)的4维数组构成的输入数据
    filter_h : 滤波器的高
    filter_w : 滤波器的长
    stride : 步幅
    pad : 填充

    Returns
    -------
    col : 2维数组
    """
    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

首先介绍一下卷积运算的基本过程:
为简单起见,假设我们现有一个2通道66的数据,相应的,有2个22的滤波器a、b。
2通道6*6的数据这里插入图片描述
以步长2进行卷积运算后,对应位置的数相加
在这里插入图片描述
最后得到一个3*3的矩阵
在这里插入图片描述
以上是卷积运算的基本过程。下面是代码实现部分的重点。

1
 col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]

y、x为滤波器的索引值,本例中y、x均为0或1。即(0,0)(0,1)(1,0)(1,1)。col的后两个冒号,即第4、5维数据,存储与滤波器(y,x)处的值进行卷积的img中的数据:
如图:阴影处表示与滤波器(0,0)处的值a1进行卷积的值
在这里插入图片描述共3*3个,存储在col[0,0,0,0,0:2,0:2]中,即

在这里插入图片描述同理,col[0,0,0,1,0:2,0:2]中存储与a2进行卷积的值:
在这里插入图片描述对于第2通道的数据也进行相同的过程。略。
下面将4维数据转化成2维数据

1
 col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N*out_h*out_w, -1)

由前面的过程可知,每一个滤波器的一个值与out_heightout_weight个值相乘,将这几个值排成1列,同一个滤波器的所有值乘完后排在同一行。所有通道排在同一行。所以一行共有channelfhfw,本例为222,共有Nout_heightout_weight行,本例为13*3