From 5f924d5d533831c29f1f5243eb1790467c9aac1a Mon Sep 17 00:00:00 2001 From: yangyaming Date: Mon, 19 Jun 2017 18:15:15 +0800 Subject: [PATCH] Follow comments. --- doc/api/v2/config/evaluators.rst | 9 +++ .../evaluators/DetectionMAPEvaluator.cpp | 66 +++++++++---------- .../trainer_config_helpers/evaluators.py | 6 +- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/doc/api/v2/config/evaluators.rst b/doc/api/v2/config/evaluators.rst index 39db51fa4ab..9ac972fb193 100644 --- a/doc/api/v2/config/evaluators.rst +++ b/doc/api/v2/config/evaluators.rst @@ -99,3 +99,12 @@ value_printer .. automodule:: paddle.v2.evaluator :members: value_printer :noindex: + +Detection +===== + +detection_map +------------- +.. automodule:: paddle.v2.evaluator + :members: detection_map + :noindex: diff --git a/paddle/gserver/evaluators/DetectionMAPEvaluator.cpp b/paddle/gserver/evaluators/DetectionMAPEvaluator.cpp index 7d326c2db14..9b825db574c 100644 --- a/paddle/gserver/evaluators/DetectionMAPEvaluator.cpp +++ b/paddle/gserver/evaluators/DetectionMAPEvaluator.cpp @@ -80,21 +80,20 @@ public: allGTBBoxes.push_back(bboxes); } - size_t imgId = 0; - for (size_t n = 0; n < cpuOutput_->getHeight();) { + size_t n = 0; + const real* cpuOutputData = cpuOutput_->getData(); + for (size_t imgId = 0; imgId < batchSize; ++imgId) { map>> bboxes; - while (cpuOutput_->getData()[n * 7] == imgId && - n < cpuOutput_->getHeight()) { + size_t curImgId = static_cast((cpuOutputData + n * 7)[0]); + while (curImgId == imgId && n < cpuOutput_->getHeight()) { vector label; vector score; vector bbox; - getBBoxFromDetectData( - cpuOutput_->getData() + n * 7, 1, label, score, bbox); + getBBoxFromDetectData(cpuOutputData + n * 7, 1, label, score, bbox); bboxes[label[0]].push_back(make_pair(score[0], bbox[0])); ++n; + curImgId = static_cast((cpuOutputData + n * 7)[0]); } - ++imgId; - if (imgId > batchSize) break; allDetectBBoxes.push_back(bboxes); } @@ -119,15 +118,14 @@ public: } // calcTFPos - calcTFPos( - batchSize, allGTBBoxes, allDetectBBoxes, &allTruePos_, &allFalsePos_); + calcTFPos(batchSize, allGTBBoxes, allDetectBBoxes); return 0; } virtual void printStats(std::ostream& os) const { real mAP = calcMAP(); - os << "Detection mAP=" << mAP * 100; + os << "Detection mAP=" << mAP; } virtual void distributeEval(ParameterClient2* client) { @@ -138,9 +136,7 @@ protected: void calcTFPos(const size_t batchSize, const vector>>& allGTBBoxes, const vector>>>& - allDetectBBoxes, - map>>* allTruePos, - map>>* allFalsePos) { + allDetectBBoxes) { for (size_t n = 0; n < allDetectBBoxes.size(); ++n) { if (allGTBBoxes[n].size() == 0) { for (map>>::const_iterator @@ -149,8 +145,8 @@ protected: ++it) { size_t label = it->first; for (size_t i = 0; i < it->second.size(); ++i) { - (*allTruePos)[label].push_back(make_pair(it->second[i].first, 0)); - (*allFalsePos)[label].push_back(make_pair(it->second[i].first, 1)); + allTruePos_[label].push_back(make_pair(it->second[i].first, 0)); + allFalsePos_[label].push_back(make_pair(it->second[i].first, 1)); } } } else { @@ -162,9 +158,8 @@ protected: vector> predBBoxes = it->second; if (allGTBBoxes[n].find(label) == allGTBBoxes[n].end()) { for (size_t i = 0; i < predBBoxes.size(); ++i) { - (*allTruePos)[label].push_back(make_pair(predBBoxes[i].first, 0)); - (*allFalsePos)[label].push_back( - make_pair(predBBoxes[i].first, 1)); + allTruePos_[label].push_back(make_pair(predBBoxes[i].first, 0)); + allFalsePos_[label].push_back(make_pair(predBBoxes[i].first, 1)); } } else { vector gtBBoxes = @@ -189,22 +184,21 @@ protected: if (evaluateDifficult_ || (!evaluateDifficult_ && !gtBBoxes[maxIdx].isDifficult)) { if (!visited[maxIdx]) { - (*allTruePos)[label].push_back( + allTruePos_[label].push_back( make_pair(predBBoxes[i].first, 1)); - (*allFalsePos)[label].push_back( + allFalsePos_[label].push_back( make_pair(predBBoxes[i].first, 0)); visited[maxIdx] = true; } else { - (*allTruePos)[label].push_back( + allTruePos_[label].push_back( make_pair(predBBoxes[i].first, 0)); - (*allFalsePos)[label].push_back( + allFalsePos_[label].push_back( make_pair(predBBoxes[i].first, 1)); } } } else { - (*allTruePos)[label].push_back( - make_pair(predBBoxes[i].first, 0)); - (*allFalsePos)[label].push_back( + allTruePos_[label].push_back(make_pair(predBBoxes[i].first, 0)); + allFalsePos_[label].push_back( make_pair(predBBoxes[i].first, 1)); } } @@ -274,7 +268,7 @@ protected: } } if (count != 0) mAP /= count; - return mAP; + return mAP * 100; } void getAccumulation(vector> inPairs, @@ -291,20 +285,22 @@ protected: std::string getTypeImpl() const { return "detection_map"; } - real getValueImpl() const { return calcMAP() * 100; } + real getValueImpl() const { return calcMAP(); } private: - real overlapThreshold_; - bool evaluateDifficult_; - size_t backgroundId_; - std::string apType_; + real overlapThreshold_; // overlap threshold when determining whether matched + bool evaluateDifficult_; // whether evaluate difficult ground truth + size_t backgroundId_; // class index of background + std::string apType_; // how to calculate mAP (Integral or 11point) MatrixPtr cpuOutput_; MatrixPtr cpuLabel_; - map numPos_; - map>> allTruePos_; - map>> allFalsePos_; + map numPos_; // counts of true objects each classification + map>> + allTruePos_; // true positive prediction + map>> + allFalsePos_; // false positive prediction }; REGISTER_EVALUATOR(detection_map, DetectionMAPEvaluator); diff --git a/python/paddle/trainer_config_helpers/evaluators.py b/python/paddle/trainer_config_helpers/evaluators.py index 1dcd8048032..44d52edfa7b 100644 --- a/python/paddle/trainer_config_helpers/evaluators.py +++ b/python/paddle/trainer_config_helpers/evaluators.py @@ -166,9 +166,9 @@ def detection_map_evaluator(input, ap_type="11point", name=None): """ - Detection mAP Evaluator. It will print mean Average Precision for detection. + Detection mAP Evaluator. It will print mean Average Precision (mAP) for detection. - The detection mAP Evaluator according to the detection_output's output count + The detection mAP Evaluator based on the output of detection_output layer counts the true positive and the false positive bbox and integral them to get the mAP. @@ -186,7 +186,7 @@ def detection_map_evaluator(input, :type overlap_threshold: float :param background_id: The background class index. :type background_id: int - :param evaluate_difficult: Wether evaluate a difficult ground truth. + :param evaluate_difficult: Whether evaluate a difficult ground truth. :type evaluate_difficult: bool """ if not isinstance(input, list): -- GitLab