def test_forward(self): # filter_shape = (3, 3, 3, 3) # (n, c, ih, iw) # input_shape = (3, 3, 5, 5) input_shape = (3, 5, 5) conv = Conv2D( filter_num=3, filter_size=(3, 3), activation="identity", initialization="he", pad=0, stride=1, input_shape=input_shape, ) assert conv.biases.all() == 0.0 inputs = np.random.randn(3, 3, 5, 5) outputs = conv.forward(inputs) assert outputs.shape == (3, 3, 3, 3) # (n, fn, oh, ow) outputs = outputs.transpose(0, 2, 3, 1).reshape(3 * 3 * 3, 3) expansion = im2row(inputs, conv.fh, conv.fw, conv.pad, conv.stride) assert expansion.shape == (27, 27) # 这里设置为可逆矩阵 weight_reshaped = np.dot(np.linalg.inv(expansion), outputs) weights = weight_reshaped.T.reshape(3, 3, 3, 3) assert str(weights) == str(conv.weights)
def test_im2row_with_stride(self): # input_shape = (1, 5, 5, 1) input_shape = (1, 1, 5, 5) img = np.array([ [1, 2, 3, 0, 1], [0, 1, 2, 3, 2], [3, 0, 1, 2, 3], [2, 3, 0, 1, 0], [1, 0, 3, 2, 1], ]) inputs = np.zeros((input_shape)) # inputs[0, :, :, 0] = img inputs[0, 0, :, :] = img fh = fw = 3 pad = 0 stride = 2 expansion = im2row(inputs=inputs, fh=fh, fw=fw, pad=pad, stride=stride) target = np.array( [ [1, 2, 3, 0, 1, 2, 3, 0, 1], [3, 0, 1, 2, 3, 2, 1, 2, 3], [3, 0, 1, 2, 3, 0, 1, 0, 3], [1, 2, 3, 0, 1, 0, 3, 2, 1], ], dtype="float64", ) assert (expansion == target).all()
def test_im2row_loss_features(self): # 输入特征图形状为4x4 # 过滤器形状为3x3 # 填充为0, 滑动步长为2 # 此时,过滤器只能在输入特征图上活动一次,输入特征图中局部感受野(即活动窗口)外的像素将会丢失 fh = fw = 3 pad = 0 stride = 2 expansion = im2row(self.inputs, fh, fw, pad, stride) # print('\n', 'expansion are:') # print(expansion) # print('') # print(expansion.shape) target = np.array([[1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0]]) assert (expansion == target).all()
def test_im2row(self): fh = fw = 3 pad = 0 stride = 1 expansion = im2row(inputs=self.inputs, fh=fh, fw=fw, pad=pad, stride=stride) target = np.array([ [1, 2, 3, 0, 1, 2, 3, 0, 1], [2, 3, 0, 1, 2, 3, 0, 1, 2], [0, 1, 2, 3, 0, 1, 2, 3, 0], [1, 2, 3, 0, 1, 2, 3, 0, 1], ]) # print('=' * 80) # print(expansion) # print('=' * 80) assert (expansion == target).all()
def test_im2row_with_batch(self): # input_shape = (2, 2, 2, 3) input_shape = (2, 3, 2, 2) inputs = np.arange(1, 25).reshape(*input_shape) """ 第一批3个通道上的像素矩阵: [[1, 2], [[5, 6], [[9, 10], [3, 4]] [7, 8]] [11, 12]] 第二批3个通道上的像素矩阵: [[13, 14], [[17, 18], [[21, 22], [15, 16]] [19, 20]] [23, 24]] ---------------------------------- 将统一通道上的局部感受野展开之后: 第一批: 第二批: [[1, 5, 9], [[13, 17, 21], [2, 6, 10], [14, 18, 22], [3, 7, 11], [15, 19, 23], [4, 8, 12]] [16, 20, 24]] """ print("\n", "inputs are:") print(inputs) print("") expansion = im2row(inputs, fh=1, fw=1, pad=0, stride=1) # print('\n') # print(expansion) # print('') target = np.array([ [1.0, 5.0, 9.0], # 相同通道的像素都扩展到同一行 [2.0, 6.0, 10.0], [3.0, 7.0, 11.0], [4.0, 8.0, 12.0], # ----------- 第一批 [13.0, 17.0, 21.0], [14.0, 18.0, 22.0], [15.0, 19.0, 23.0], [16.0, 20.0, 24.0], ]) # ----------- 第二批 assert (expansion == target).all()
def test_im2row_with_padding(self): fh = fw = 3 pad = 1 stride = 1 expansion = im2row(self.inputs, fh, fw, pad, stride) # print('\n', 'expansion are:') # print(expansion) # print('') target = np.array([ [ 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 0.0, 3.0, 0.0, 1.0, ], [ 0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 0.0, 1.0, 2.0, ], [ 0.0, 0.0, 0.0, 0.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 0.0, ], [ 0.0, 1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 0.0, 3.0, 0.0, 1.0, 0.0, 2.0, 3.0, 0.0, ], [ 1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 0.0, 1.0, 2.0, 2.0, 3.0, 0.0, 1.0, ], [ 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 0.0, 3.0, 0.0, 1.0, 0.0, ], [ 0.0, 0.0, 1.0, 2.0, 0.0, 3.0, 0.0, 1.0, 0.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, ], [ 0.0, 1.0, 2.0, 3.0, 3.0, 0.0, 1.0, 2.0, 2.0, 3.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ], [ 1.0, 2.0, 3.0, 0.0, 0.0, 1.0, 2.0, 0.0, 3.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, ], ]).T assert (expansion == target).all()