假设输入:4通道的featureMap 大小为4*28*28
输出:96通道的featureMap
4通道的featureMap 卷积后变为96个通道的featureMap
需要96个卷积核,每个卷积核的大小为4*kernel_h*kernel_w(卷积核是有深度的,切记!!!)
需要bias 为96个
卷积过程:
1、计算卷积后的featureMap大小:
top_n = bottom_n
top_c = num_output
top_h = (bottom_h +2*pad_h-kernel_h)/stride_h+1
top_w = (bottom_w+2*pad_w-kernel_w)/stride_w+1
2、计算卷积过程中响应点对应的输入坐标矩阵
offset_h = top_h*stride_h-pad_h
offset_w = top_w*stride_w-pad_w
响应矩阵为:[offset_h:offset_h+kernel_h,offset_w:offset_w+kernel_w]
3、卷积计算
把相应点对应的响应矩阵与对应的卷积核分别卷积后,进行相加,再加上对应的bias(切记!!!bias一个响应点输出只加一次)
卷积代码如下:
def __conv(self,bottom_blob,top_blob):
bottom_n,bottom_c,bottom_h,bottom_w = bottom_blob.shape
top_n,top_c,top_h,top_w = top_blob.shape
for t_n in range(top_n):
for t_c in range(top_c):
for h in range(top_h):
for w in range(top_w):
for b_c in range(bottom_c):
for row in range(self.__kernel_h):
offset_h = self.__stride_h*h-self.__pad_h + row
if offset_h < 0 or offset_h >= bottom_h:
continue
for col in range(self.__kernel_w):
offset_w = self.__stride_w*w-self.__pad_w+col
if offset_w<0 or offset_w>=bottom_w:
continue
top_blob[t_n][t_c][h][w] += bottom_blob[t_n][b_c][offset_h][offset_w]*self._w[t_c][b_c][row][col]
top_blob[t_n][t_c][h][w] += self._b[t_c]