Skip to content

Commit 1df369d

Browse files
committed
modify accuracy layer to take 1 or 2 label blobs; if 2, compute same/not same
1 parent aa4564c commit 1df369d

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

include/caffe/loss_layers.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ class AccuracyLayer : public Layer<Dtype> {
4141
return LayerParameter_LayerType_ACCURACY;
4242
}
4343

44-
virtual inline int ExactNumBottomBlobs() const { return 2; }
45-
virtual inline int ExactNumTopBlobs() const { return 1; }
44+
// AccuracyLayer takes 2-3 bottom Blobs; if there are 3 the second and third
45+
// are compared to compute a 0/1 label. (Otherwise the label comes directly
46+
// from the second.)
47+
virtual inline int ExactNumBottomBlobs() const { return -1; }
48+
virtual inline int MinBottomBlobs() const { return 2; }
49+
virtual inline int MaxBottomBlobs() const { return 3; }
50+
51+
private:
52+
int ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i);
4653

4754
protected:
4855
/**

src/caffe/layers/accuracy_layer.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@
1010

1111
namespace caffe {
1212

13+
template <typename Dtype>
14+
int AccuracyLayer<Dtype>::ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i) {
15+
int label;
16+
if (bottom.size() == 2) {
17+
label = static_cast<int>(bottom[1]->cpu_data()[i]);
18+
} else { // bottom.size() == 3
19+
// label == 1 if bottom[1] == bottom[2] (same)
20+
// label == 0 if bottom[1] != bottom[2] (not same)
21+
label = (bottom[1]->cpu_data()[i] ==
22+
bottom[2]->cpu_data()[i]) ? 1 : 0;
23+
24+
if (i == 0) {
25+
}
26+
}
27+
return label;
28+
}
29+
1330
template <typename Dtype>
1431
void AccuracyLayer<Dtype>::LayerSetUp(
1532
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
@@ -34,12 +51,13 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
3451
const vector<Blob<Dtype>*>& top) {
3552
Dtype accuracy = 0;
3653
const Dtype* bottom_data = bottom[0]->cpu_data();
37-
const Dtype* bottom_label = bottom[1]->cpu_data();
3854
int num = bottom[0]->num();
3955
int dim = bottom[0]->count() / bottom[0]->num();
4056
vector<Dtype> maxval(top_k_+1);
4157
vector<int> max_id(top_k_+1);
58+
int label;
4259
for (int i = 0; i < num; ++i) {
60+
label = ComputeLabel(bottom, i);
4361
// Top-k accuracy
4462
std::vector<std::pair<Dtype, int> > bottom_data_vector;
4563
for (int j = 0; j < dim; ++j) {
@@ -51,7 +69,7 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
5169
bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
5270
// check if true label is in top k predictions
5371
for (int k = 0; k < top_k_; k++) {
54-
if (bottom_data_vector[k].second == static_cast<int>(bottom_label[i])) {
72+
if (bottom_data_vector[k].second == label) {
5573
++accuracy;
5674
break;
5775
}

0 commit comments

Comments
 (0)