将focal loss添加到你的网络框架当中(caffe 版本)

1. https://github.com/Longqi-S/Focal-Loss 下载focal-loss-master.zip
2.解压focal-loss-master.zip,得到softmax_focal_loss_layer.cpp, softmax_focal_loss_layer.cu和 softmax_focal_loss_layer.hpp
3.将softmax_focal_loss_layer.hpp放到caffe-master/include/caffe/layers里面
4.将softmax_focal_loss_layer.cpp, softmax_focal_loss_layer.cu放到caffe-master/src/caffe/layers里面
5.修改caffe-master/src/caffe/proto/caffe.proto文件
参考 http://blog.csdn.net/shuzfan/article/details/51322976
(1)由于我们的层有一个focal_loss_param参数,因此我们首先应该在message LayerParameter {}中添加新参数信息。添加信息时,首先要制定一个唯一ID,这个ID的可选值可以由这句话看出:
// NOTE
// Update the next available ID when you add a new LayerParameter field.
//
// LayerParameter next available layer-specific ID: 143 (last added: scale_param)
所以添加下面这句话:
// Focal Loss layer
optional SoftmaxFocalLossParameter softmax_focal_loss_param = XXX; (XXX is determined by your own caffe)

(2)在任意位置添加消息函数
// Focal Loss for Dense Object Detection
message SoftmaxFocalLossParameter{
optional float alpha = 1 [default = 0.25]; 
optional float gamma = 2 [default = 2];
}

5.重新编译caffe, make all -j16

6.重新编译pycaffe, make pycaffe

7.修改模型文件
这是之前的softmax层:
layer {
name: "loss_cls"
type: "SoftmaxWithLoss"
bottom: "cls_score"
bottom: "labels"
propagate_down: 1
propagate_down: 0
top: "loss_cls"
loss_weight: 1
}
这是修改后的softmax层:
layer {
name: "focal_loss_cls"
type: "SoftmaxWithFocalLoss"
bottom: "cls_score"
bottom: "labels"
propagate_down: 1
propagate_down: 0
top: "focal_loss"
softmax_focal_loss_param {
alpha: 1
gamma: 1
}

}

8:开始训练
将focal loss添加到你的网络框架当中(caffe 版本)_第1张图片

你可能感兴趣的:(目标检测)