Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions include/caffe/loss_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ class AccuracyLayer : public Layer<Dtype> {
return LayerParameter_LayerType_ACCURACY;
}

virtual inline int ExactNumBottomBlobs() const { return 2; }
virtual inline int ExactNumTopBlobs() const { return 1; }
// AccuracyLayer takes 2-3 bottom Blobs; if there are 3 the second and third
// are compared to compute a 0/1 label. (Otherwise the label comes directly
// from the second.)
virtual inline int ExactNumBottomBlobs() const { return -1; }
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int MaxBottomBlobs() const { return 3; }

private:
int ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i);

protected:
/**
Expand Down
22 changes: 20 additions & 2 deletions src/caffe/layers/accuracy_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

namespace caffe {

template <typename Dtype>
int AccuracyLayer<Dtype>::ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i) {
int label;
if (bottom.size() == 2) {
label = static_cast<int>(bottom[1]->cpu_data()[i]);
} else { // bottom.size() == 3
// label == 1 if bottom[1] == bottom[2] (same)
// label == 0 if bottom[1] != bottom[2] (not same)
label = (bottom[1]->cpu_data()[i] ==
bottom[2]->cpu_data()[i]) ? 1 : 0;

if (i == 0) {
}
}
return label;
}

template <typename Dtype>
void AccuracyLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
Expand All @@ -34,12 +51,13 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
Dtype accuracy = 0;
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* bottom_label = bottom[1]->cpu_data();
int num = bottom[0]->num();
int dim = bottom[0]->count() / bottom[0]->num();
vector<Dtype> maxval(top_k_+1);
vector<int> max_id(top_k_+1);
int label;
for (int i = 0; i < num; ++i) {
label = ComputeLabel(bottom, i);
// Top-k accuracy
std::vector<std::pair<Dtype, int> > bottom_data_vector;
for (int j = 0; j < dim; ++j) {
Expand All @@ -51,7 +69,7 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
// check if true label is in top k predictions
for (int k = 0; k < top_k_; k++) {
if (bottom_data_vector[k].second == static_cast<int>(bottom_label[i])) {
if (bottom_data_vector[k].second == label) {
++accuracy;
break;
}
Expand Down