注释:
输入为x: (10, 3, 36, 36), self.weight: (36, 3, 5, 5) 输出为卷积结果 (10, 36, 32, 32),其中r_range和c_range是提前计算好的输出尺寸如(32,32)
for b in range(0, x.shape[0]):
for c in range(self.weight.shape[0]):
output = []
for i in range(0, x.shape[2], self.stride):
for j in range(0, x.shape[3], self.stride):
if i+self.weight.shape[2] <= x.shape[2] and j+self.weight.shape[3] <= x.shape[3]:
value = np.sum(x[b,:,i:i+self.weight.shape[2],j:j+self.weight.shape[3]] * self.weight[c])
output.append(value)
out[b,c] = np.array(output).reshape(r_range,c_range)
主要思路是利用np.sum这里,将输入图像中(3,5,5) 的patch与(3,5,5) 的卷积核直接做乘法然后相加即可得到一个输出特征图中的像素从而避免套入更多for循环。
除此之外更简便的方式是,在卷积之前先整理数据形状,将卷积运算转换成两个矩阵相乘,详见cs231n。