Skip to content

Commit 3dfca3b

Browse files
committed
add support for optional 2nd output scores in proposal layer
1 parent 71afe3f commit 3dfca3b

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

include/caffe/layers/proposal_layer.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class ProposalLayer : public Layer<Dtype> {
2929
virtual inline int ExactNumBottomBlobs() const { return 3; }
3030
//virtual inline int MinBottomBlobs() const { return 3; }
3131
//virtual inline int MaxBottomBlobs() const { return 3; }
32-
virtual inline int ExactNumTopBlobs() const { return 1; }
33-
//virtual inline int MinTopBlobs() const { return 1; }
34-
//virtual inline int MaxTopBlobs() const { return 1; }
32+
//irtual inline int ExactNumTopBlobs() const { return 1; }
33+
virtual inline int MinTopBlobs() const { return 1; } // output rois
34+
virtual inline int MaxTopBlobs() const { return 2; } // optional, output scores
3535

3636
protected:
3737

@@ -55,7 +55,7 @@ class ProposalLayer : public Layer<Dtype> {
5555

5656
virtual void bbox_transform_inv(int img_width, int img_height, vector<vector<float> > bbox, vector<vector<float> > select_anchor, vector<vector<float> > &pred);
5757

58-
virtual void apply_nms(vector<vector<float> > &pred_boxes, vector<float> &confidence);
58+
//virtual void apply_nms(vector<vector<float> > &pred_boxes, vector<float> &confidence);
5959

6060
virtual void filter_boxes(vector<vector<float> > &pred_boxes, vector<float> &confidence, float min_size);
6161
virtual void applynmsfast(vector<vector<float> > &pred_boxes,vector<pair<Dtype, int> > &score_index_vec,

src/caffe/layers/proposal_layer.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ void ProposalLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
4747
top_shape.push_back(1);
4848
top_shape.push_back(5);
4949
top[0]->Reshape(top_shape);
50+
if(top.size() > 1)
51+
{
52+
vector<int> top_shape1(2, 1);
53+
top_shape1.push_back(1);
54+
top_shape1.push_back(1);
55+
top[1]->Reshape(top_shape1);
56+
}
5057
}
5158

5259
template <typename Dtype>
5360
void ProposalLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
5461
const vector<Blob<Dtype>*>& top) {
55-
const Dtype* score = bottom[0]->cpu_data();
56-
const Dtype* bbox_deltas = bottom[1]->cpu_data();
57-
const Dtype* im_info = bottom[2]->cpu_data();
62+
const Dtype* score = bottom[0]->cpu_data(); // data order [n,c,h,w]
63+
const Dtype* bbox_deltas = bottom[1]->cpu_data(); // data order [n,c,h,w]
64+
const Dtype* im_info = bottom[2]->cpu_data(); // data order [h,w,c]
5865
int height = bottom[0]->height();
5966
int width = bottom[0]->width();
6067
//float thresh = 0.3; //
@@ -63,7 +70,7 @@ void ProposalLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
6370
vector<vector<float> > bbox;
6471
int anchor_num = anchor_scale_.size()*anchor_ratio_.size();
6572

66-
// TODO: data order is different from python version, may need adjustment
73+
// TODO: stored data order is different from python version, may need adjustment
6774
for (int k = 0; k < anchor_num; k++)
6875
{
6976
float w = anchor_boxes_[4 * k + 2] - anchor_boxes_[4 * k] + 1;
@@ -132,6 +139,20 @@ void ProposalLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
132139
top_data[5 * i + 3] = pred_boxes[index][2];
133140
top_data[5 * i + 4] = pred_boxes[index][3];
134141
}
142+
143+
if(top.size() > 1)
144+
{
145+
vector<int> score_shape;
146+
score_shape.push_back(num);
147+
score_shape.push_back(1);
148+
top[1]->Reshape(score_shape);
149+
Dtype* top_data1 = top[1]->mutable_cpu_data();
150+
for (int i = 0; i < num; i++)
151+
{
152+
int index = indices[i];
153+
top_data1[i] = confidence[index];
154+
}
155+
}
135156
}
136157

137158

@@ -302,6 +323,7 @@ void ProposalLayer<Dtype>::applynmsfast(vector<vector<float> > &pred_boxes, vect
302323
indices.resize(top_k);
303324
}
304325

326+
/*
305327
template <typename Dtype>
306328
void ProposalLayer<Dtype>::apply_nms(vector<vector<float> > &pred_boxes, vector<float> &confidence)
307329
{
@@ -342,7 +364,7 @@ void ProposalLayer<Dtype>::apply_nms(vector<vector<float> > &pred_boxes, vector<
342364
}
343365
}
344366
}
345-
367+
*/
346368

347369
INSTANTIATE_CLASS(ProposalLayer);
348370
REGISTER_LAYER_CLASS(Proposal);

0 commit comments

Comments
 (0)