blob: c99844b6bb6fe78df141c95ffe186d8600e28e8d [file] [log] [blame]
Aron Virginas-Tard089b742019-01-29 11:09:51 +00001//
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>
Aron Virginas-Tard089b742019-01-29 11:09:51 +000011#include <boost/numeric/conversion/cast.hpp>
12#include <boost/test/tools/floating_point_comparison.hpp>
13
14#include <vector>
15
16namespace
17{
18
19template<typename Model>
20class MobileNetSsdTestCase : public InferenceModelTestCase<Model>
21{
22public:
23 MobileNetSsdTestCase(Model& model,
24 unsigned int testCaseId,
25 const MobileNetSsdTestCaseData& testCaseData)
26 : InferenceModelTestCase<Model>(model,
27 testCaseId,
28 { std::move(testCaseData.m_InputData) },
29 { k_OutputSize1, k_OutputSize2, k_OutputSize3, k_OutputSize4 })
30 , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f))
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000031 , m_DetectedObjects(testCaseData.m_ExpectedDetectedObject)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000032 {}
33
34 TestCaseResult ProcessResult(const InferenceTestOptions& options) override
35 {
Derek Lambertieb1fce02019-12-10 21:20:10 +000036 boost::ignore_unused(options);
37
Ferran Balaguerc602f292019-02-08 17:09:55 +000038 const std::vector<float>& output1 = boost::get<std::vector<float>>(this->GetOutputs()[0]); // bounding boxes
Aron Virginas-Tard089b742019-01-29 11:09:51 +000039 BOOST_ASSERT(output1.size() == k_OutputSize1);
40
Ferran Balaguerc602f292019-02-08 17:09:55 +000041 const std::vector<float>& output2 = boost::get<std::vector<float>>(this->GetOutputs()[1]); // classes
Aron Virginas-Tard089b742019-01-29 11:09:51 +000042 BOOST_ASSERT(output2.size() == k_OutputSize2);
43
Ferran Balaguerc602f292019-02-08 17:09:55 +000044 const std::vector<float>& output3 = boost::get<std::vector<float>>(this->GetOutputs()[2]); // scores
Aron Virginas-Tard089b742019-01-29 11:09:51 +000045 BOOST_ASSERT(output3.size() == k_OutputSize3);
46
Ferran Balaguerc602f292019-02-08 17:09:55 +000047 const std::vector<float>& output4 = boost::get<std::vector<float>>(this->GetOutputs()[3]); // valid detections
Aron Virginas-Tard089b742019-01-29 11:09:51 +000048 BOOST_ASSERT(output4.size() == k_OutputSize4);
49
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000050 const size_t numDetections = boost::numeric_cast<size_t>(output4[0]);
51
52 // Check if number of valid detections matches expectations
53 const size_t expectedNumDetections = m_DetectedObjects.size();
54 if (numDetections != expectedNumDetections)
55 {
Derek Lamberti08446972019-11-26 16:38:31 +000056 ARMNN_LOG(error) << "Number of detections is incorrect: Expected (" <<
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000057 expectedNumDetections << ")" << " but got (" << numDetections << ")";
58 return TestCaseResult::Failed;
59 }
60
Aron Virginas-Tard089b742019-01-29 11:09:51 +000061 // Extract detected objects from output data
62 std::vector<DetectedObject> detectedObjects;
63 const float* outputData = output1.data();
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000064 for (unsigned int i = 0u; i < numDetections; i++)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000065 {
66 // NOTE: Order of coordinates in output data is yMin, xMin, yMax, xMax
67 float yMin = *outputData++;
68 float xMin = *outputData++;
69 float yMax = *outputData++;
70 float xMax = *outputData++;
71
72 DetectedObject detectedObject(
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000073 output2.at(i),
Aron Virginas-Tard089b742019-01-29 11:09:51 +000074 BoundingBox(xMin, yMin, xMax, yMax),
75 output3.at(i));
76
77 detectedObjects.push_back(detectedObject);
78 }
79
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000080 std::sort(detectedObjects.begin(), detectedObjects.end());
81 std::sort(m_DetectedObjects.begin(), m_DetectedObjects.end());
Aron Virginas-Tard089b742019-01-29 11:09:51 +000082
83 // Compare detected objects with expected results
84 std::vector<DetectedObject>::const_iterator it = detectedObjects.begin();
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000085 for (unsigned int i = 0; i < numDetections; i++)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000086 {
87 if (it == detectedObjects.end())
88 {
Derek Lamberti08446972019-11-26 16:38:31 +000089 ARMNN_LOG(error) << "No more detected objects found! Index out of bounds: " << i;
Aron Virginas-Tard089b742019-01-29 11:09:51 +000090 return TestCaseResult::Abort;
91 }
92
93 const DetectedObject& detectedObject = *it;
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000094 const DetectedObject& expectedObject = m_DetectedObjects[i];
95
96 if (detectedObject.m_Class != expectedObject.m_Class)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000097 {
Derek Lamberti08446972019-11-26 16:38:31 +000098 ARMNN_LOG(error) << "Prediction for test case " << this->GetTestCaseId() <<
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000099 " is incorrect: Expected (" << expectedObject.m_Class << ")" <<
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000100 " but predicted (" << detectedObject.m_Class << ")";
101 return TestCaseResult::Failed;
102 }
103
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000104 if(!m_FloatComparer(detectedObject.m_Confidence, expectedObject.m_Confidence))
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000105 {
Derek Lamberti08446972019-11-26 16:38:31 +0000106 ARMNN_LOG(error) << "Confidence of prediction for test case " << this->GetTestCaseId() <<
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000107 " is incorrect: Expected (" << expectedObject.m_Confidence << ") +- 1.0 pc" <<
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000108 " but predicted (" << detectedObject.m_Confidence << ")";
109 return TestCaseResult::Failed;
110 }
111
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000112 if (!m_FloatComparer(detectedObject.m_BoundingBox.m_XMin, expectedObject.m_BoundingBox.m_XMin) ||
113 !m_FloatComparer(detectedObject.m_BoundingBox.m_YMin, expectedObject.m_BoundingBox.m_YMin) ||
114 !m_FloatComparer(detectedObject.m_BoundingBox.m_XMax, expectedObject.m_BoundingBox.m_XMax) ||
115 !m_FloatComparer(detectedObject.m_BoundingBox.m_YMax, expectedObject.m_BoundingBox.m_YMax))
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000116 {
Derek Lamberti08446972019-11-26 16:38:31 +0000117 ARMNN_LOG(error) << "Detected bounding box for test case " << this->GetTestCaseId() <<
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000118 " is incorrect";
119 return TestCaseResult::Failed;
120 }
121
122 ++it;
123 }
124
125 return TestCaseResult::Ok;
126 }
127
128private:
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000129 static constexpr unsigned int k_Shape = 10u;
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000130
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000131 static constexpr unsigned int k_OutputSize1 = k_Shape * 4u;
132 static constexpr unsigned int k_OutputSize2 = k_Shape;
133 static constexpr unsigned int k_OutputSize3 = k_Shape;
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000134 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
140template <typename Model>
141class MobileNetSsdTestCaseProvider : public IInferenceTestCaseProvider
142{
143public:
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(),
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000155 "Path to directory containing test data");
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000156
157 Model::AddCommandLineOptions(options, m_ModelCommandLineOptions);
158 }
159
Matthew Bentham3e68b972019-04-09 13:10:46 +0100160 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000161 {
162 if (!ValidateDirectory(m_DataDir))
163 {
164 return false;
165 }
166
Matthew Bentham3e68b972019-04-09 13:10:46 +0100167 m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000168 if (!m_Model)
169 {
170 return false;
171 }
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000172 std::pair<float, int32_t> qParams = m_Model->GetInputQuantizationParams();
Narumol Prangnawaratc8bab1b2019-02-15 17:34:51 +0000173 m_Database = std::make_unique<MobileNetSsdDatabase>(m_DataDir.c_str(), qParams.first, qParams.second);
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000174 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
193private:
194 typename Model::CommandLineOptions m_ModelCommandLineOptions;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100195 std::function<std::unique_ptr<Model>(const InferenceTestOptions &,
196 typename Model::CommandLineOptions)> m_ConstructModel;
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000197 std::unique_ptr<Model> m_Model;
198
199 std::string m_DataDir;
200 std::unique_ptr<MobileNetSsdDatabase> m_Database;
201};
202
203} // anonymous namespace