Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include "InferenceTest.hpp" |
| 8 | #include "MobileNetSsdDatabase.hpp" |
| 9 | |
| 10 | #include <boost/assert.hpp> |
| 11 | #include <boost/log/trivial.hpp> |
| 12 | #include <boost/numeric/conversion/cast.hpp> |
| 13 | #include <boost/test/tools/floating_point_comparison.hpp> |
| 14 | |
| 15 | #include <vector> |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | template<typename Model> |
| 21 | class MobileNetSsdTestCase : public InferenceModelTestCase<Model> |
| 22 | { |
| 23 | public: |
| 24 | MobileNetSsdTestCase(Model& model, |
| 25 | unsigned int testCaseId, |
| 26 | const MobileNetSsdTestCaseData& testCaseData) |
| 27 | : InferenceModelTestCase<Model>(model, |
| 28 | testCaseId, |
| 29 | { std::move(testCaseData.m_InputData) }, |
| 30 | { k_OutputSize1, k_OutputSize2, k_OutputSize3, k_OutputSize4 }) |
| 31 | , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f)) |
| 32 | , m_DetectedObjects(testCaseData.m_ExpectedOutput) |
| 33 | {} |
| 34 | |
| 35 | TestCaseResult ProcessResult(const InferenceTestOptions& options) override |
| 36 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 37 | const std::vector<float>& output1 = boost::get<std::vector<float>>(this->GetOutputs()[0]); // bounding boxes |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 38 | BOOST_ASSERT(output1.size() == k_OutputSize1); |
| 39 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 40 | const std::vector<float>& output2 = boost::get<std::vector<float>>(this->GetOutputs()[1]); // classes |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 41 | BOOST_ASSERT(output2.size() == k_OutputSize2); |
| 42 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 43 | const std::vector<float>& output3 = boost::get<std::vector<float>>(this->GetOutputs()[2]); // scores |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 44 | BOOST_ASSERT(output3.size() == k_OutputSize3); |
| 45 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 46 | const std::vector<float>& output4 = boost::get<std::vector<float>>(this->GetOutputs()[3]); // valid detections |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 47 | BOOST_ASSERT(output4.size() == k_OutputSize4); |
| 48 | |
| 49 | // Extract detected objects from output data |
| 50 | std::vector<DetectedObject> detectedObjects; |
| 51 | const float* outputData = output1.data(); |
| 52 | for (unsigned int i = 0u; i < k_NumDetections; i++) |
| 53 | { |
| 54 | // NOTE: Order of coordinates in output data is yMin, xMin, yMax, xMax |
| 55 | float yMin = *outputData++; |
| 56 | float xMin = *outputData++; |
| 57 | float yMax = *outputData++; |
| 58 | float xMax = *outputData++; |
| 59 | |
| 60 | DetectedObject detectedObject( |
| 61 | static_cast<unsigned int>(output2.at(i)), |
| 62 | BoundingBox(xMin, yMin, xMax, yMax), |
| 63 | output3.at(i)); |
| 64 | |
| 65 | detectedObjects.push_back(detectedObject); |
| 66 | } |
| 67 | |
| 68 | // Sort detected objects by confidence |
| 69 | std::sort(detectedObjects.begin(), detectedObjects.end(), |
| 70 | [](const DetectedObject& a, const DetectedObject& b) |
| 71 | { |
| 72 | return a.m_Confidence > b.m_Confidence || |
| 73 | (a.m_Confidence == b.m_Confidence && a.m_Class > b.m_Class); |
| 74 | }); |
| 75 | |
| 76 | // Check if number of valid detections matches expectations |
| 77 | const size_t numValidDetections = boost::numeric_cast<size_t>(output4[0]); |
| 78 | if (numValidDetections != m_DetectedObjects.size()) |
| 79 | { |
| 80 | BOOST_LOG_TRIVIAL(error) << "Number of valid detections is incorrect: Expected (" << |
| 81 | m_DetectedObjects.size() << ")" << " but got (" << numValidDetections << ")"; |
| 82 | return TestCaseResult::Failed; |
| 83 | } |
| 84 | |
| 85 | // Compare detected objects with expected results |
| 86 | std::vector<DetectedObject>::const_iterator it = detectedObjects.begin(); |
| 87 | for (const DetectedObject& expectedDetection : m_DetectedObjects) |
| 88 | { |
| 89 | if (it == detectedObjects.end()) |
| 90 | { |
| 91 | BOOST_LOG_TRIVIAL(info) << "No more detected objects to compare"; |
| 92 | return TestCaseResult::Abort; |
| 93 | } |
| 94 | |
| 95 | const DetectedObject& detectedObject = *it; |
| 96 | if (detectedObject.m_Class != expectedDetection.m_Class) |
| 97 | { |
| 98 | BOOST_LOG_TRIVIAL(error) << "Prediction for test case " << this->GetTestCaseId() << |
| 99 | " is incorrect: Expected (" << expectedDetection.m_Class << ")" << |
| 100 | " but predicted (" << detectedObject.m_Class << ")"; |
| 101 | return TestCaseResult::Failed; |
| 102 | } |
| 103 | |
| 104 | if(!m_FloatComparer(detectedObject.m_Confidence, expectedDetection.m_Confidence)) |
| 105 | { |
| 106 | BOOST_LOG_TRIVIAL(error) << "Confidence of prediction for test case " << this->GetTestCaseId() << |
| 107 | " is incorrect: Expected (" << expectedDetection.m_Confidence << ") +- 1.0 pc" << |
| 108 | " but predicted (" << detectedObject.m_Confidence << ")"; |
| 109 | return TestCaseResult::Failed; |
| 110 | } |
| 111 | |
| 112 | if (!m_FloatComparer(detectedObject.m_BoundingBox.m_XMin, expectedDetection.m_BoundingBox.m_XMin) || |
| 113 | !m_FloatComparer(detectedObject.m_BoundingBox.m_YMin, expectedDetection.m_BoundingBox.m_YMin) || |
| 114 | !m_FloatComparer(detectedObject.m_BoundingBox.m_XMax, expectedDetection.m_BoundingBox.m_XMax) || |
| 115 | !m_FloatComparer(detectedObject.m_BoundingBox.m_YMax, expectedDetection.m_BoundingBox.m_YMax)) |
| 116 | { |
| 117 | BOOST_LOG_TRIVIAL(error) << "Detected bounding box for test case " << this->GetTestCaseId() << |
| 118 | " is incorrect"; |
| 119 | return TestCaseResult::Failed; |
| 120 | } |
| 121 | |
| 122 | ++it; |
| 123 | } |
| 124 | |
| 125 | return TestCaseResult::Ok; |
| 126 | } |
| 127 | |
| 128 | private: |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 129 | static constexpr unsigned int k_NumDetections = 1u; |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 130 | |
| 131 | static constexpr unsigned int k_OutputSize1 = k_NumDetections * 4u; |
| 132 | static constexpr unsigned int k_OutputSize2 = k_NumDetections; |
| 133 | static constexpr unsigned int k_OutputSize3 = k_NumDetections; |
| 134 | static constexpr unsigned int k_OutputSize4 = 1u; |
| 135 | |
| 136 | boost::math::fpc::close_at_tolerance<float> m_FloatComparer; |
| 137 | std::vector<DetectedObject> m_DetectedObjects; |
| 138 | }; |
| 139 | |
| 140 | template <typename Model> |
| 141 | class MobileNetSsdTestCaseProvider : public IInferenceTestCaseProvider |
| 142 | { |
| 143 | public: |
| 144 | template <typename TConstructModelCallable> |
| 145 | explicit MobileNetSsdTestCaseProvider(TConstructModelCallable constructModel) |
| 146 | : m_ConstructModel(constructModel) |
| 147 | {} |
| 148 | |
| 149 | virtual void AddCommandLineOptions(boost::program_options::options_description& options) override |
| 150 | { |
| 151 | namespace po = boost::program_options; |
| 152 | |
| 153 | options.add_options() |
| 154 | ("data-dir,d", po::value<std::string>(&m_DataDir)->required(), |
| 155 | "Path to directory containing test data"); |
| 156 | |
| 157 | Model::AddCommandLineOptions(options, m_ModelCommandLineOptions); |
| 158 | } |
| 159 | |
| 160 | virtual bool ProcessCommandLineOptions() override |
| 161 | { |
| 162 | if (!ValidateDirectory(m_DataDir)) |
| 163 | { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | m_Model = m_ConstructModel(m_ModelCommandLineOptions); |
| 168 | if (!m_Model) |
| 169 | { |
| 170 | return false; |
| 171 | } |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 172 | std::pair<float, int32_t> qParams = m_Model->GetQuantizationParams(); |
| 173 | m_Database = std::make_unique<MobileNetSsdDatabase>(m_DataDir.c_str(), qParams.first, qParams.second); |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 174 | if (!m_Database) |
| 175 | { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override |
| 183 | { |
| 184 | std::unique_ptr<MobileNetSsdTestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId); |
| 185 | if (!testCaseData) |
| 186 | { |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
| 190 | return std::make_unique<MobileNetSsdTestCase<Model>>(*m_Model, testCaseId, *testCaseData); |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | typename Model::CommandLineOptions m_ModelCommandLineOptions; |
| 195 | std::function<std::unique_ptr<Model>(typename Model::CommandLineOptions)> m_ConstructModel; |
| 196 | std::unique_ptr<Model> m_Model; |
| 197 | |
| 198 | std::string m_DataDir; |
| 199 | std::unique_ptr<MobileNetSsdDatabase> m_Database; |
| 200 | }; |
| 201 | |
| 202 | } // anonymous namespace |