From a9281488f0d263667204db0ab4b538cfb9ac1ccc Mon Sep 17 00:00:00 2001 From: Kai Li Date: Sun, 20 Jul 2014 00:42:12 +0800 Subject: [PATCH] Fix style issues in accuracy & argmax layer --- src/caffe/layers/accuracy_layer.cpp | 18 +++++++----------- src/caffe/layers/argmax_layer.cpp | 23 ++++++++--------------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/caffe/layers/accuracy_layer.cpp b/src/caffe/layers/accuracy_layer.cpp index b244c51..3ccba3b 100644 --- a/src/caffe/layers/accuracy_layer.cpp +++ b/src/caffe/layers/accuracy_layer.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -31,12 +32,6 @@ void AccuracyLayer::SetUp( (*top)[0]->Reshape(1, 1, 1, 1); } -template -static bool int_Dtype_pair_greater(std::pair a, - std::pair b) { - return a.second > b.second; -} - template Dtype AccuracyLayer::Forward_cpu(const vector*>& bottom, vector*>* top) { @@ -49,20 +44,21 @@ Dtype AccuracyLayer::Forward_cpu(const vector*>& bottom, vector max_id(top_k_+1); for (int i = 0; i < num; ++i) { // Top-k accuracy - std::vector > bottom_data_vector; + std::vector > 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); + bottom_data_vector.end(), std::greater >()); // 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(bottom_label[i])) { + for (int k = 0; k < top_k_; k++) { + if (bottom_data_vector[k].second == static_cast(bottom_label[i])) { ++accuracy; break; } + } } // LOG(INFO) << "Accuracy: " << accuracy; diff --git a/src/caffe/layers/argmax_layer.cpp b/src/caffe/layers/argmax_layer.cpp index 580e4b0..bd1a268 100644 --- a/src/caffe/layers/argmax_layer.cpp +++ b/src/caffe/layers/argmax_layer.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -29,12 +30,6 @@ void ArgMaxLayer::SetUp(const vector*>& bottom, } } -template -static bool int_Dtype_pair_greater(std::pair a, - std::pair b) { - return a.second > b.second; -} - template Dtype ArgMaxLayer::Forward_cpu(const vector*>& bottom, vector*>* top) { @@ -43,22 +38,20 @@ Dtype ArgMaxLayer::Forward_cpu(const vector*>& bottom, int num = bottom[0]->num(); int dim = bottom[0]->count() / bottom[0]->num(); for (int i = 0; i < num; ++i) { - std::vector > bottom_data_vector; + std::vector > 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); + bottom_data_vector.end(), std::greater >()); + 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; } } } -- 2.7.4