原版的caffe是没有Centerloss这一层的。所以需要手动添加。
参考博客:http://blog.csdn.net/samylee/article/details/76640240
主要配置是一下几个文件:
src/caffe/proto/caffe.proto
include/caffe/layers/center_loss_layer.hpp
src/caffe/layers/center_loss_layer.cpp
src/caffe/layers/center_loss_layer.cu
1.下载这几个文件:https://github.com/ydwen/caffe-face 上下载caffe-face
从相关路径下吧center_loss_layer.hpp,复制到include/caffe/layers/ 下
吧center_loss_layer.cpp 和center_loss_layer.cu 复制到src/caffe/layers/ 下
2.在原本caffe/src/caffe/proto/caffe.proto 路径下对caffe.proto进行修改:
在message LayerParameter{}中添加如下代码:
optional CenterLossParameter center_loss_param = 147;
(其中147为当前最后一个ID,根据自己的提示来改)
在文档最后加入:
message CenterLossParameter {
optional uint32 num_output = 1; // The number of outputs for the layer
optional FillerParameter center_filler = 2; // The filler for the centers
// The first axis to be lumped into a single inner product computation;
// all preceding axes are retained in the output.
// May be negative to index from the end (e.g., -1 for the last axis).
optional int32 axis = 3 [default = 1];
}
3.重新编译caffe
make clean
make all -16j
make test -j16
make runtest -j16
make pycaffe -j16
make matcaffe -j16(使用Matlab的话需要)
二、关于CenterLoss的使用
CenterLoss不能单独使用,需要和SoftmaxWithLoss联合使用才行。
例如:将下面的网络复制http://ethereon.github.io/netscope/#/editor 中便可看到网络结构。
name: "LeNet"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/mnist/mnist_train_lmdb"
batch_size: 64
backend: LMDB
}
}
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/mnist/mnist_test_lmdb"
batch_size: 100
backend: LMDB
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
############# softmax loss ###############
layer {
name: "softmax_loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "softmax_loss"
}
############# center loss ###############
layer {
name: "center_loss"
type: "CenterLoss"
bottom: "ip1"
bottom: "label"
top: "center_loss"
param {
lr_mult: 1
decay_mult: 0
}
center_loss_param {
num_output: 10
center_filler {
type: "xavier"
}
}
loss_weight: 0.01
}