-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathfocal_loss_layer.cpp
155 lines (143 loc) · 5.18 KB
/
focal_loss_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <vector>
#include "caffe/layers/focal_loss_layer.hpp"
#include "caffe/util/math_functions.hpp"
namespace caffe {
template <typename Dtype>
void FocalLossLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
LossLayer<Dtype>::LayerSetUp(bottom, top);
sigmoid_bottom_vec_.clear();
sigmoid_bottom_vec_.push_back(bottom[0]);
sigmoid_top_vec_.clear();
sigmoid_top_vec_.push_back(sigmoid_output_.get());
sigmoid_layer_->SetUp(sigmoid_bottom_vec_, sigmoid_top_vec_);
has_ignore_label_ =
this->layer_param_.loss_param().has_ignore_label();
if ( has_ignore_label_ ) {
ignore_label_ = this->layer_param_.loss_param().ignore_label();
}
valid_num_ = 0;
alpha_ = this->layer_param().focal_loss_param().alpha();
gamma_ = this->layer_param().focal_loss_param().gamma();
if ( this->layer_param_.loss_param().has_normalization() ) {
normalization_ = this->layer_param_.loss_param().normalization();
}
else if ( this->layer_param_.loss_param().has_normalize() ) {
normalization_ = this->layer_param_.loss_param().normalize() ?
LossParameter_NormalizationMode_VALID : LossParameter_NormalizationMode_BATCH_SIZE;
}
else {
normalization_ = LossParameter_NormalizationMode_BATCH_SIZE;
}
}
template <typename Dtype>
void FocalLossLayer<Dtype>::Reshape(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
LossLayer<Dtype>::Reshape(bottom, top);
outer_num_ = bottom[ 0 ]->shape(0); // batch size
inner_num_ = bottom[ 0 ]->count(1); // instance size: |output| == |target|
CHECK_EQ(bottom[0]->count(), bottom[1]->count()) <<
"SIGMOID_CROSS_ENTROPY_LOSS layer inputs must have the same count.";
sigmoid_layer_->Reshape(sigmoid_bottom_vec_, sigmoid_top_vec_);
scaler_.ReshapeLike(*bottom[0]);
}
template <typename Dtype>
Dtype FocalLossLayer<Dtype>::get_normalizer(
LossParameter_NormalizationMode normalization_mode, int valid_count) {
Dtype normalizer;
switch ( normalization_mode ) {
case LossParameter_NormalizationMode_FULL:
normalizer = Dtype(outer_num_ * inner_num_);
break;
case LossParameter_NormalizationMode_VALID:
if ( valid_count == -1 ) {
normalizer = Dtype(outer_num_ * inner_num_);
}
else {
normalizer = Dtype(valid_count);
}
break;
case LossParameter_NormalizationMode_BATCH_SIZE:
normalizer = Dtype(outer_num_);
break;
case LossParameter_NormalizationMode_NONE:
normalizer = Dtype(1);
break;
default:
LOG(FATAL) << "Unknown normalization mode: "
<< LossParameter_NormalizationMode_Name(normalization_mode);
}
// Some users will have no labels for some examples in order to 'turn off' a
// particular loss in a multi-task setup. The max prevents NaNs in that case.
return std::max(Dtype(1.0), normalizer);
}
template <typename Dtype>
void FocalLossLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
if ( bottom[ 0 ]->count() < 1 ){
top[ 0 ]->mutable_cpu_data()[ 0 ] = Dtype(0);
return;
}
// The forward pass computes the sigmoid outputs.
sigmoid_bottom_vec_[0] = bottom[0];
sigmoid_layer_->Forward(sigmoid_bottom_vec_, sigmoid_top_vec_);
// Compute the loss (negative log likelihood)
const int count = bottom[0]->count();
const int num = bottom[0]->num();
// Stable version of loss computation from input data
const Dtype* input_data = bottom[0]->cpu_data();
const Dtype* target = bottom[1]->cpu_data();
Dtype loss = 0;
for (int i = 0; i < count; ++i) {
if ( target[ i ] == ignore_label_ )
loss = 0;
else{
valid_num_+=1;
loss -= input_data[ i ] * ( target[ i ] - ( input_data[ i ] >= 0 ) ) -
log(1 + exp(input_data[ i ] - 2 * input_data[ i ] * ( input_data[ i ] >= 0 )));
}
}
top[0]->mutable_cpu_data()[0] = loss / num;
if ( top.size() >= 2 ) {
for ( int i = 0; i < count; ++i ){
if ( target[ i ] == ignore_label_ ){
top[ 1 ]->mutable_cpu_data()[ i ] = 0;
}
else{
top[ 1 ]->mutable_cpu_data()[ i ] = -( input_data[ i ] * ( target[ i ] - ( input_data[ i ] >= 0 ) ) -
log(1 + exp(input_data[ i ] - 2 * input_data[ i ] * ( input_data[ i ] >= 0 ))) );
// Output per-instance loss
}
}
}
}
template <typename Dtype>
void FocalLossLayer<Dtype>::Backward_cpu(
const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
if (propagate_down[1]) {
LOG(FATAL) << this->type()
<< " Layer cannot backpropagate to label inputs.";
}
if (propagate_down[0]) {
if ( bottom[ 0 ]->count() < 1 ){
return;
}
// First, compute the diff
const int count = bottom[0]->count();
const int num = bottom[0]->num();
const Dtype* sigmoid_output_data = sigmoid_output_->cpu_data();
const Dtype* target = bottom[1]->cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
caffe_sub(count, sigmoid_output_data, target, bottom_diff);
// Scale down gradient
const Dtype loss_weight = top[0]->cpu_diff()[0];
caffe_scal(count, loss_weight / num, bottom_diff);
}
}
#ifdef CPU_ONLY
STUB_GPU_BACKWARD(FocalLossLayer, Backward);
#endif
INSTANTIATE_CLASS(FocalLossLayer);
REGISTER_LAYER_CLASS(FocalLoss);
} // namespace caffe