更改caffe中的最后一层
Changing the final layer in caffe
这是关于如何在训练前将现有权重添加到模型的问题 的扩展。
我想使用现有的权重,但我的最终层输出 50 而不是 1000(因为网络被训练为对 1000 个项目进行分类)。从之前的 post 开始,通过更改输出层的名称,我能够添加权重。但后来我意识到还有其他层依赖于最后一层。这是来自 VGG 网络的片段:
layer {
name: "loss3/classifier"
type: "InnerProduct"
bottom: "pool5/7x7_s1"
top: "loss3/classifier"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
inner_product_param {
num_output: 50
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "loss3/loss3"
type: "SoftmaxWithLoss"
bottom: "loss3/classifier"
bottom: "label"
top: "loss3/loss3"
loss_weight: 1
}
layer {
name: "loss3/top-1"
type: "Accuracy"
bottom: "loss3/classifier"
bottom: "label"
top: "loss3/top-1"
include {
phase: TEST
}
}
layer {
name: "loss3/top-5"
type: "Accuracy"
bottom: "loss3/classifier"
bottom: "label"
top: "loss3/top-5"
include {
phase: TEST
}
accuracy_param {
top_k: 5
}
}
我的问题是:
底部和顶部参数到底是什么?
后面两层的"loss3/classifier"需要改名吗?
这是一个非常基本的问题。我强烈建议您阅读一些文档和基本的 caffe 教程,以了解 caffe 的基础知识。 This tutorial 可以是一个很好的起点。
深度网络有一个底层图表,描述了从网络输入到预测输出的 "flow" 数据。您在问题中附加的片段描述了这样一个图表。
每层代表 "processing unit" 沿 "data path":它的输入是 "bottom"
blob,层输出其处理后的数据作为 "top"
blob。
所以,如果你有一层
layer {
name: "loss3/classifier"
type: "InnerProduct"
bottom: "pool5/7x7_s1"
top: "loss3/classifier"
...
}
该层执行 "InnerProduct"
操作(由层的 type
定义)。它对输入 blob "pool5/7x7_s1"
(定义为 bottom
)执行操作,并将结果输出到 blob "loss3/classifier"
(定义为 top
blob)。该层的可学习参数(权重和偏差)由 caffe 使用层名称 "loss3/classifier"
.
存储和访问
因此,如果您将 layyer 的 name 更改为 "loss3/classifier_50"
而无需 更改 top
您将获得所需的效果:caffe不会复制该层的权重,但会保持该层的输出连接到其他层。
顺便说一句,
您确定您使用的是 VGG 网络吗?这个 prototxt 看起来像 GoogLeNet 架构...
请注意,AlexNet、VGG 和 GoogLeNet 是三个完全不同的网络。
这是关于如何在训练前将现有权重添加到模型的问题
我想使用现有的权重,但我的最终层输出 50 而不是 1000(因为网络被训练为对 1000 个项目进行分类)。从之前的 post 开始,通过更改输出层的名称,我能够添加权重。但后来我意识到还有其他层依赖于最后一层。这是来自 VGG 网络的片段:
layer {
name: "loss3/classifier"
type: "InnerProduct"
bottom: "pool5/7x7_s1"
top: "loss3/classifier"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
inner_product_param {
num_output: 50
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "loss3/loss3"
type: "SoftmaxWithLoss"
bottom: "loss3/classifier"
bottom: "label"
top: "loss3/loss3"
loss_weight: 1
}
layer {
name: "loss3/top-1"
type: "Accuracy"
bottom: "loss3/classifier"
bottom: "label"
top: "loss3/top-1"
include {
phase: TEST
}
}
layer {
name: "loss3/top-5"
type: "Accuracy"
bottom: "loss3/classifier"
bottom: "label"
top: "loss3/top-5"
include {
phase: TEST
}
accuracy_param {
top_k: 5
}
}
我的问题是:
底部和顶部参数到底是什么?
后面两层的"loss3/classifier"需要改名吗?
这是一个非常基本的问题。我强烈建议您阅读一些文档和基本的 caffe 教程,以了解 caffe 的基础知识。 This tutorial 可以是一个很好的起点。
深度网络有一个底层图表,描述了从网络输入到预测输出的 "flow" 数据。您在问题中附加的片段描述了这样一个图表。
每层代表 "processing unit" 沿 "data path":它的输入是 "bottom"
blob,层输出其处理后的数据作为 "top"
blob。
所以,如果你有一层
layer {
name: "loss3/classifier"
type: "InnerProduct"
bottom: "pool5/7x7_s1"
top: "loss3/classifier"
...
}
该层执行 "InnerProduct"
操作(由层的 type
定义)。它对输入 blob "pool5/7x7_s1"
(定义为 bottom
)执行操作,并将结果输出到 blob "loss3/classifier"
(定义为 top
blob)。该层的可学习参数(权重和偏差)由 caffe 使用层名称 "loss3/classifier"
.
存储和访问
因此,如果您将 layyer 的 name 更改为 "loss3/classifier_50"
而无需 更改 top
您将获得所需的效果:caffe不会复制该层的权重,但会保持该层的输出连接到其他层。
顺便说一句,
您确定您使用的是 VGG 网络吗?这个 prototxt 看起来像 GoogLeNet 架构...
请注意,AlexNet、VGG 和 GoogLeNet 是三个完全不同的网络。