准备工作:
1,首先你需要有一个可以make编译通过的caffe(这个过程在第一篇blog中已经写出)和一个带有center_loss层的caffe-face(可以直接在github上clone下来)
caffe-face网址:
https://github.com/ydwen/caffe-face
下载命令如下:
git clone https://github.com/ydwen/caffe-face.git
添加原因:
1,在分类问题上,center_loss可以很好的减小类类间距,增加类间距离。
参考blog网址:
https://blog.csdn.net/qq_14845119/article/details/53308996
前言:
添加center_loss层需要有它的.cpp 、.cu以及相应的.hpp文件才能生成center_loss层,同时,需要修改caffe.proto。
这种‘搬砖’法也可以应用在其他层的替换。
具体过程如下:
1,复制caffe-face的src/caffe/layers目录下的center_loss_layer.cpp和center_loss_layer.cu文件到caffe的相同位置的目录下;
2,复制caffe-face的include/caffe/layers目录下的center_loss_layer.hpp到caffe相同位置的目录下;
3,修改caffe.proto文件
>>进入caffe的src/caffe/proto目录下打开caffe.proto文件,在网络层的定义部分,ID为148,last added:xxx,将网络名替换为center_loss_param
>>并设置替换engine的默认值,将xxx的ID为147的设置替换为center_loss,如下:
optional CenterLossParameter center_loss_param = 147;
>>在caffe.proto文件的末尾添加center_loss层的message CenterLossParameter:
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];
}
4,删掉src/caffe/layers以及include/caffe/layers目录下的xxx.cpp,xxx.cu以及xxx.hpp文件,或者进行压缩备份。
5,最后一步复制caffe-face的src/caffe/test目录下的test_neuron_layer.cpp文件替换到caffe下,或者修改caffe的src/caffe/test目录下的test_neuron_layer.cpp文件的内容(将文件内所有关于xxx_layer的内容都删掉)
修改完成后就可以编译caffe了,依次执行以下命令编译(如果你想加速可以在命令行末尾追加-jxx,其中xx(数值,如-j16)是看你内核数量的多少,不建议添加-jxx,经验教训是手动添加替换网络层后,高速编译可能会出现不必要的bug):
make all
make test
make runtest
make pycaffe
没有bug出现即为编译成功。