Caffe 转换层的权重和维度
Caffe conv layer weights and dimensions
我遇到了这个很好的article,它直观地解释了卷积神经网络的工作原理。
现在试图了解 caffe conv 层内部到底发生了什么:
使用形状为 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 具有所有权重:每个平面一个 7x7 矩阵 (13) 每个过滤器 (128)。
在 1 x 13 x 19 x 19 数据上用 blob 0 进行卷积,如果我理解正确的话,我们最终得到 128 x 13 x 19 x 19 输出(有填充,所以每个 7x7 矩阵为每个像素生成一个数字)
128 x 13 x 19 x 19如何变成层的1 x 128 x 19 x 19输出?
blob 1 中的 128 个权重是多少?
奖金问题:什么是 blobs_lr
?
您引用的是旧版本的 caffe prototxt 格式。适应新格式会给你
layer { # layer and not layer*s*
name: "conv1_7x7_128"
type: "Convolution" # tyoe as string
param { lr_mult: 1. } # instead of blobs_lr
param { lr_mult: 2. } # instead of 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 的通道。
应用 7 x 7 的 128 个滤波器(每个滤波器应用于所有 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 项 - 每个过滤器结果的附加标量。您可以通过添加
来关闭偏差项
bias_term: false
到你层的convolution_param
。
您可以阅读更多关于卷积层的内容here。
关于bonus的问题,Eliethesaiyan已经在.
中回答的很好了
我遇到了这个很好的article,它直观地解释了卷积神经网络的工作原理。
现在试图了解 caffe conv 层内部到底发生了什么:
使用形状为 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 具有所有权重:每个平面一个 7x7 矩阵 (13) 每个过滤器 (128)。
在 1 x 13 x 19 x 19 数据上用 blob 0 进行卷积,如果我理解正确的话,我们最终得到 128 x 13 x 19 x 19 输出(有填充,所以每个 7x7 矩阵为每个像素生成一个数字)
128 x 13 x 19 x 19如何变成层的1 x 128 x 19 x 19输出?
blob 1 中的 128 个权重是多少?
奖金问题:什么是 blobs_lr
?
您引用的是旧版本的 caffe prototxt 格式。适应新格式会给你
layer { # layer and not layer*s*
name: "conv1_7x7_128"
type: "Convolution" # tyoe as string
param { lr_mult: 1. } # instead of blobs_lr
param { lr_mult: 2. } # instead of 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 的通道。
应用 7 x 7 的 128 个滤波器(每个滤波器应用于所有 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 项 - 每个过滤器结果的附加标量。您可以通过添加
来关闭偏差项bias_term: false
到你层的convolution_param
。
您可以阅读更多关于卷积层的内容here。
关于bonus的问题,Eliethesaiyan已经在