將focal loss新增到你的網路框架當中(caffe 版本)
阿新 • • 發佈:2019-02-07
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檔案
(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:開始訓練
}