赞
踩
- out_h = (H + 2*pad - filter_h)//stride + 1
- out_w = (W + 2*pad - filter_w)//stride + 1
现在,我们使用这个算式,试着做几个计算。
• pad―填充
- import sys, os
- import numpy as np
-
- 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
-
- x1 = np.random.rand(1, 3, 7, 7)
- col1 = im2col(x1, 5, 5, stride=1, pad=0)
- print(col1.shape) # (9, 75)
- x2 = np.random.rand(10, 3, 7, 7) # 10个数据
- col2 = im2col(x2, 5, 5, stride=1, pad=0)
- print(col2.shape) # (90, 75)
- class Convolution:
- def __init__(self, W, b, stride=1, pad=0):
-
- self.W = W
- self.b = b
- self.stride = stride
- self.pad = pad
-
- def forward(self, x):
-
- FN, C, FH, FW = self.W.shape
- N, C, H, W = x.shape
- out_h = int(1 + (H + 2 * self.pad - FH) / self.stride)
- out_w = int(1 + (W + 2 * self.pad - FW) / self.stride)
- col = im2col(x, FH, FW, self.stride, self.pad)
- col_W = self.W.reshape(FN, -1).T # 滤波器的展开
- out = np.dot(col, col_W) + self.b
- out = out.reshape(N, out_h, out_w, -1).transpose(0, 3, 1, 2)
- return out
- def col2im(col, input_shape, filter_h, filter_w, stride=1, pad=0):
- """
- Parameters
- ----------
- col :
- input_shape : 输入数据的形状(例:(10, 1, 28, 28))
- filter_h :
- filter_w
- stride
- pad
- Returns
- -------
- """
- N, C, H, W = input_shape
- out_h = (H + 2*pad - filter_h)//stride + 1
- out_w = (W + 2*pad - filter_w)//stride + 1
- col = col.reshape(N, out_h, out_w, C, filter_h, filter_w).transpose(0, 3, 4, 5, 1, 2)
-
- img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1))
- for y in range(filter_h):
- y_max = y + stride*out_h
- for x in range(filter_w):
- x_max = x + stride*out_w
- img[:, :, y:y_max:stride, x:x_max:stride] += col[:, :, y, x, :, :]
-
- return img[:, :, pad:H + pad, pad:W + pad]
- class Pooling:
- def __init__(self, pool_h, pool_w, stride=1, pad=0):
- self.pool_h = pool_h
- self.pool_w = pool_w
- self.stride = stride
- self.pad = pad
- def forward(self, x):
- N, C, H, W = x.shape
- out_h = int(1 + (H - self.pool_h) / self.stride)
- out_w = int(1 + (W - self.pool_w) / self.stride)
- # 展开(1)
- col = im2col(x, self.pool_h, self.pool_w, self.stride, self.pad)
- col = col.reshape(-1, self.pool_h*self.pool_w)
- # 最大值(2)
- out = np.max(col, axis=1)
- # 转换(3)
- out = out.reshape(N, out_h, out_w, C).transpose(0, 3, 1, 2)
- return out
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。