我发现了这篇文章,它直观地解释了卷积网络的工作原理。
现在我试图理解Caffe卷积层内部到底发生了什么:
输入数据形状为1 x 13 x 19 x 19,使用128个滤波器的卷积层:
layers { name: "conv1_7x7_128" type: CONVOLUTION blobs_lr: 1. blobs_lr: 2. bottom: "data" top: "conv2" convolution_param { num_output: 128 kernel_size: 7 pad: 3 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}
如果我理解正确的话,层的输出形状是1 x 128 x 19 x 19。
查看层权重的形状在net->layers()[1]->blobs()
中:
layer 1: type Convolution 'conv1_7x7_128' blob 0: 128 13 7 7 blob 1: 128
看起来blob 0包含了所有的权重:每个滤波器(128个)每个平面(13个)有一个7×7的矩阵。
如果我理解正确的话,使用blob 0对1 x 13 x 19 x 19数据进行卷积运算,最终得到128 x 13 x 19 x 19的输出(由于有填充,每个7×7矩阵为每个像素产生一个数)。
-
128 x 13 x 19 x 19如何转变成层的1 x 128 x 19 x 19输出?
-
blob 1中的128个权重是什么?
附加问题:blobs_lr
是什么?
回答:
您引用的是Caffe的旧版本prototxt格式。调整为新格式后,您将得到
layer { # 使用layer而不是layers name: "conv1_7x7_128" type: "Convolution" # 类型作为字符串 param { lr_mult: 1. } # 代替blobs_lr param { lr_mult: 2. } # 代替blobs_lr bottom: "data" top: "conv2" convolution_param { num_output: 128 kernel_size: 7 pad: 3 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}
如果您的输入数据shape
为1 x 13 x 19 x 19,这意味着您的batch_size
为1,您有13个通道,空间维度为19 x 19。
应用128个7 x 7的滤波器(每个滤波器应用于所有13个输入通道)意味着您有128个shape
为13 x 7 x 7的滤波器(这是您的第一层参数的shape
)。应用每个滤波器会得到一个单一的输出通道1 x 1 x 19 x 19,由于您有128个这样的滤波器,最终得到1 x 128 x 19 x 19的输出。
第二层的参数是偏置项——对每个滤波器结果的加性标量。您可以通过添加
bias_term: false
到您的层的convolution_param
中来关闭偏置项。
您可以在这里阅读更多关于卷积层的介绍。
至于附加问题,Eliethesaiyan已经在他的评论中很好地回答了这个问题。