Fix style issues in accuracy & argmax layer
authorKai Li <kaili_kloud@163.com>
Sat, 19 Jul 2014 16:42:12 +0000 (00:42 +0800)
committerKai Li <kaili_kloud@163.com>
Sat, 19 Jul 2014 16:42:12 +0000 (00:42 +0800)
src/caffe/layers/accuracy_layer.cpp
src/caffe/layers/argmax_layer.cpp

index b244c51..3ccba3b 100644 (file)
@@ -3,6 +3,7 @@
 #include <algorithm>
 #include <cmath>
 #include <cfloat>
+#include <functional>
 #include <utility>
 #include <vector>
 
@@ -31,12 +32,6 @@ void AccuracyLayer<Dtype>::SetUp(
   (*top)[0]->Reshape(1, 1, 1, 1);
 }
 
-template<typename Dtype>
-static bool int_Dtype_pair_greater(std::pair<int, Dtype> a,
-                            std::pair<int, Dtype> b) {
-  return a.second > b.second;
-}
-
 template <typename Dtype>
 Dtype AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
     vector<Blob<Dtype>*>* top) {
@@ -49,20 +44,21 @@ Dtype AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
   vector<int> max_id(top_k_+1);
   for (int i = 0; i < num; ++i) {
     // Top-k accuracy
-    std::vector<std::pair<int, Dtype> > bottom_data_vector;
+    std::vector<std::pair<Dtype, int> > bottom_data_vector;
     for (int j = 0; j < dim; ++j) {
       bottom_data_vector.push_back(
-          std::make_pair(j, bottom_data[i * dim + j]));
+          std::make_pair(bottom_data[i * dim + j], j));
     }
     std::partial_sort(
         bottom_data_vector.begin(), bottom_data_vector.begin() + top_k_,
-        bottom_data_vector.end(), int_Dtype_pair_greater<Dtype>);
+        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].first == static_cast<int>(bottom_label[i])) {
+    for (int k = 0; k < top_k_; k++) {
+      if (bottom_data_vector[k].second == static_cast<int>(bottom_label[i])) {
         ++accuracy;
         break;
       }
+    }
   }
 
   // LOG(INFO) << "Accuracy: " << accuracy;
index 580e4b0..bd1a268 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <algorithm>
 #include <cfloat>
+#include <functional>
 #include <queue>
 #include <utility>
 #include <vector>
@@ -29,12 +30,6 @@ void ArgMaxLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
   }
 }
 
-template<typename Dtype>
-static bool int_Dtype_pair_greater(std::pair<int, Dtype> a,
-                            std::pair<int, Dtype> b) {
-  return a.second > b.second;
-}
-
 template <typename Dtype>
 Dtype ArgMaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
     vector<Blob<Dtype>*>* top) {
@@ -43,22 +38,20 @@ Dtype ArgMaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
   int num = bottom[0]->num();
   int dim = bottom[0]->count() / bottom[0]->num();
   for (int i = 0; i < num; ++i) {
-    std::vector<std::pair<int, Dtype> > bottom_data_vector;
+    std::vector<std::pair<Dtype, int> > bottom_data_vector;
     for (int j = 0; j < dim; ++j) {
       bottom_data_vector.push_back(
-          std::make_pair(j, bottom_data[i * dim + j]));
+          std::make_pair(bottom_data[i * dim + j], j));
     }
     std::partial_sort(
         bottom_data_vector.begin(), bottom_data_vector.begin() + top_k_,
-        bottom_data_vector.end(), int_Dtype_pair_greater<Dtype>);
+        bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
+    for (int j = 0; j < top_k_; ++j) {
+      top_data[(*top)[0]->offset(i, 0, j)] = bottom_data_vector[j].second;
+    }
     if (out_max_val_) {
       for (int j = 0; j < top_k_; ++j) {
-        top_data[(*top)[0]->offset(i, 0, j)] = bottom_data_vector[j].first;
-        top_data[(*top)[0]->offset(i, 1, j)] = bottom_data_vector[j].second;
-      }
-    } else {
-      for (int j = 0; j < top_k_; ++j) {
-        top_data[(*top)[0]->offset(i, 0, j)] = bottom_data_vector[j].first;
+        top_data[(*top)[0]->offset(i, 1, j)] = bottom_data_vector[j].first;
       }
     }
   }