-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmultibox_focal_loss_layer.hpp
114 lines (99 loc) · 3.41 KB
/
multibox_focal_loss_layer.hpp
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
#ifndef CAFFE_MULTIBOX_LOSS_LAYER_HPP_
#define CAFFE_MULTIBOX_LOSS_LAYER_HPP_
#include <map>
#include <utility>
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/bbox_util.hpp"
#include "caffe/layers/loss_layer.hpp"
namespace caffe {
/**
* @brief Perform MultiBox operations. Including the following:
*
* - decode the predictions.
* - perform matching between priors/predictions and ground truth.
* - use matched boxes and confidences to compute loss.
*
*/
template <typename Dtype>
class MultiBoxFocalLossLayer : public LossLayer<Dtype> {
public:
explicit MultiBoxFocalLossLayer(const LayerParameter& param)
: LossLayer<Dtype>(param) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual inline const char* type() const { return "MultiBoxFocalLoss"; }
// bottom[0] stores the location predictions.
// bottom[1] stores the confidence predictions.
// bottom[2] stores the prior bounding boxes.
// bottom[3] stores the ground truth bounding boxes.
virtual inline int ExactNumBottomBlobs() const { return 4; }
virtual inline int ExactNumTopBlobs() const { return 1; }
protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
// The internal localization loss layer.
shared_ptr<Layer<Dtype> > loc_loss_layer_;
LocLossType loc_loss_type_;
float loc_weight_;
// bottom vector holder used in Forward function.
vector<Blob<Dtype>*> loc_bottom_vec_;
// top vector holder used in Forward function.
vector<Blob<Dtype>*> loc_top_vec_;
// blob which stores the matched location prediction.
Blob<Dtype> loc_pred_;
// blob which stores the corresponding matched ground truth.
Blob<Dtype> loc_gt_;
// localization loss.
Blob<Dtype> loc_loss_;
// The internal confidence loss layer.
shared_ptr<Layer<Dtype> > conf_loss_layer_;
ConfLossType conf_loss_type_;
// bottom vector holder used in Forward function.
vector<Blob<Dtype>*> conf_bottom_vec_;
// top vector holder used in Forward function.
vector<Blob<Dtype>*> conf_top_vec_;
// blob which stores the confidence prediction.
Blob<Dtype> conf_pred_;
// blob which stores the corresponding ground truth label.
Blob<Dtype> conf_gt_;
// confidence loss.
Blob<Dtype> conf_loss_;
MultiBoxLossParameter multibox_loss_param_;
int num_classes_;
bool share_location_;
MatchType match_type_;
float overlap_threshold_;
bool use_prior_for_matching_;
int background_label_id_;
bool use_difficult_gt_;
bool do_neg_mining_;
float neg_pos_ratio_;
float neg_overlap_;
CodeType code_type_;
bool encode_variance_in_target_;
bool map_object_to_agnostic_;
bool ignore_cross_boundary_bbox_;
bool bp_inside_;
MiningType mining_type_;
int loc_classes_;
int num_gt_;
int num_;
int num_priors_;
int num_matches_;
int num_conf_;
vector<map<int, vector<int> > > all_match_indices_;
vector<vector<int> > all_neg_indices_;
float alpha_;
float gamma_;
// How to normalize the loss.
LossParameter_NormalizationMode normalization_;
};
} // namespace caffe
#endif // CAFFE_MULTIBOX_LOSS_LAYER_HPP_