2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include "InferenceTest.hpp"
8 #include "MobileNetSsdDatabase.hpp"
10 #include <armnn/utility/IgnoreUnused.hpp>
12 #include <boost/assert.hpp>
13 #include <boost/numeric/conversion/cast.hpp>
14 #include <boost/test/tools/floating_point_comparison.hpp>
21 template<typename Model>
22 class MobileNetSsdTestCase : public InferenceModelTestCase<Model>
25 MobileNetSsdTestCase(Model& model,
26 unsigned int testCaseId,
27 const MobileNetSsdTestCaseData& testCaseData)
28 : InferenceModelTestCase<Model>(model,
30 { std::move(testCaseData.m_InputData) },
31 { k_OutputSize1, k_OutputSize2, k_OutputSize3, k_OutputSize4 })
32 , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f))
33 , m_DetectedObjects(testCaseData.m_ExpectedDetectedObject)
36 TestCaseResult ProcessResult(const InferenceTestOptions& options) override
38 armnn::IgnoreUnused(options);
40 const std::vector<float>& output1 = boost::get<std::vector<float>>(this->GetOutputs()[0]); // bounding boxes
41 BOOST_ASSERT(output1.size() == k_OutputSize1);
43 const std::vector<float>& output2 = boost::get<std::vector<float>>(this->GetOutputs()[1]); // classes
44 BOOST_ASSERT(output2.size() == k_OutputSize2);
46 const std::vector<float>& output3 = boost::get<std::vector<float>>(this->GetOutputs()[2]); // scores
47 BOOST_ASSERT(output3.size() == k_OutputSize3);
49 const std::vector<float>& output4 = boost::get<std::vector<float>>(this->GetOutputs()[3]); // valid detections
50 BOOST_ASSERT(output4.size() == k_OutputSize4);
52 const size_t numDetections = boost::numeric_cast<size_t>(output4[0]);
54 // Check if number of valid detections matches expectations
55 const size_t expectedNumDetections = m_DetectedObjects.size();
56 if (numDetections != expectedNumDetections)
58 ARMNN_LOG(error) << "Number of detections is incorrect: Expected (" <<
59 expectedNumDetections << ")" << " but got (" << numDetections << ")";
60 return TestCaseResult::Failed;
63 // Extract detected objects from output data
64 std::vector<DetectedObject> detectedObjects;
65 const float* outputData = output1.data();
66 for (unsigned int i = 0u; i < numDetections; i++)
68 // NOTE: Order of coordinates in output data is yMin, xMin, yMax, xMax
69 float yMin = *outputData++;
70 float xMin = *outputData++;
71 float yMax = *outputData++;
72 float xMax = *outputData++;
74 DetectedObject detectedObject(
76 BoundingBox(xMin, yMin, xMax, yMax),
79 detectedObjects.push_back(detectedObject);
82 std::sort(detectedObjects.begin(), detectedObjects.end());
83 std::sort(m_DetectedObjects.begin(), m_DetectedObjects.end());
85 // Compare detected objects with expected results
86 std::vector<DetectedObject>::const_iterator it = detectedObjects.begin();
87 for (unsigned int i = 0; i < numDetections; i++)
89 if (it == detectedObjects.end())
91 ARMNN_LOG(error) << "No more detected objects found! Index out of bounds: " << i;
92 return TestCaseResult::Abort;
95 const DetectedObject& detectedObject = *it;
96 const DetectedObject& expectedObject = m_DetectedObjects[i];
98 if (detectedObject.m_Class != expectedObject.m_Class)
100 ARMNN_LOG(error) << "Prediction for test case " << this->GetTestCaseId() <<
101 " is incorrect: Expected (" << expectedObject.m_Class << ")" <<
102 " but predicted (" << detectedObject.m_Class << ")";
103 return TestCaseResult::Failed;
106 if(!m_FloatComparer(detectedObject.m_Confidence, expectedObject.m_Confidence))
108 ARMNN_LOG(error) << "Confidence of prediction for test case " << this->GetTestCaseId() <<
109 " is incorrect: Expected (" << expectedObject.m_Confidence << ") +- 1.0 pc" <<
110 " but predicted (" << detectedObject.m_Confidence << ")";
111 return TestCaseResult::Failed;
114 if (!m_FloatComparer(detectedObject.m_BoundingBox.m_XMin, expectedObject.m_BoundingBox.m_XMin) ||
115 !m_FloatComparer(detectedObject.m_BoundingBox.m_YMin, expectedObject.m_BoundingBox.m_YMin) ||
116 !m_FloatComparer(detectedObject.m_BoundingBox.m_XMax, expectedObject.m_BoundingBox.m_XMax) ||
117 !m_FloatComparer(detectedObject.m_BoundingBox.m_YMax, expectedObject.m_BoundingBox.m_YMax))
119 ARMNN_LOG(error) << "Detected bounding box for test case " << this->GetTestCaseId() <<
121 return TestCaseResult::Failed;
127 return TestCaseResult::Ok;
131 static constexpr unsigned int k_Shape = 10u;
133 static constexpr unsigned int k_OutputSize1 = k_Shape * 4u;
134 static constexpr unsigned int k_OutputSize2 = k_Shape;
135 static constexpr unsigned int k_OutputSize3 = k_Shape;
136 static constexpr unsigned int k_OutputSize4 = 1u;
138 boost::math::fpc::close_at_tolerance<float> m_FloatComparer;
139 std::vector<DetectedObject> m_DetectedObjects;
142 template <typename Model>
143 class MobileNetSsdTestCaseProvider : public IInferenceTestCaseProvider
146 template <typename TConstructModelCallable>
147 explicit MobileNetSsdTestCaseProvider(TConstructModelCallable constructModel)
148 : m_ConstructModel(constructModel)
151 virtual void AddCommandLineOptions(boost::program_options::options_description& options) override
153 namespace po = boost::program_options;
155 options.add_options()
156 ("data-dir,d", po::value<std::string>(&m_DataDir)->required(),
157 "Path to directory containing test data");
159 Model::AddCommandLineOptions(options, m_ModelCommandLineOptions);
162 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override
164 if (!ValidateDirectory(m_DataDir))
169 m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
174 std::pair<float, int32_t> qParams = m_Model->GetInputQuantizationParams();
175 m_Database = std::make_unique<MobileNetSsdDatabase>(m_DataDir.c_str(), qParams.first, qParams.second);
184 std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override
186 std::unique_ptr<MobileNetSsdTestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId);
192 return std::make_unique<MobileNetSsdTestCase<Model>>(*m_Model, testCaseId, *testCaseData);
196 typename Model::CommandLineOptions m_ModelCommandLineOptions;
197 std::function<std::unique_ptr<Model>(const InferenceTestOptions &,
198 typename Model::CommandLineOptions)> m_ConstructModel;
199 std::unique_ptr<Model> m_Model;
201 std::string m_DataDir;
202 std::unique_ptr<MobileNetSsdDatabase> m_Database;
205 } // anonymous namespace