1010
1111namespace 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+
1330template <typename Dtype>
1431void 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