blob: 7beedf8a62f1971f4256b8f0e7ee5605fd43e14b [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>
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
17namespace
18{
19
20template<typename Model>
21class MobileNetSsdTestCase : public InferenceModelTestCase<Model>
22{
23public:
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))
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000032 , m_DetectedObjects(testCaseData.m_ExpectedDetectedObject)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000033 {}
34
35 TestCaseResult ProcessResult(const InferenceTestOptions& options) override
36 {
Ferran Balaguerc602f292019-02-08 17:09:55 +000037 const std::vector<float>& output1 = boost::get<std::vector<float>>(this->GetOutputs()[0]); // bounding boxes
Aron Virginas-Tard089b742019-01-29 11:09:51 +000038 BOOST_ASSERT(output1.size() == k_OutputSize1);
39
Ferran Balaguerc602f292019-02-08 17:09:55 +000040 const std::vector<float>& output2 = boost::get<std::vector<float>>(this->GetOutputs()[1]); // classes
Aron Virginas-Tard089b742019-01-29 11:09:51 +000041 BOOST_ASSERT(output2.size() == k_OutputSize2);
42
Ferran Balaguerc602f292019-02-08 17:09:55 +000043 const std::vector<float>& output3 = boost::get<std::vector<float>>(this->GetOutputs()[2]); // scores
Aron Virginas-Tard089b742019-01-29 11:09:51 +000044 BOOST_ASSERT(output3.size() == k_OutputSize3);
45
Ferran Balaguerc602f292019-02-08 17:09:55 +000046 const std::vector<float>& output4 = boost::get<std::vector<float>>(this->GetOutputs()[3]); // valid detections
Aron Virginas-Tard089b742019-01-29 11:09:51 +000047 BOOST_ASSERT(output4.size() == k_OutputSize4);
48
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000049 const size_t numDetections = boost::numeric_cast<size_t>(output4[0]);
50
51 // Check if number of valid detections matches expectations
52 const size_t expectedNumDetections = m_DetectedObjects.size();
53 if (numDetections != expectedNumDetections)
54 {
55 BOOST_LOG_TRIVIAL(error) << "Number of detections is incorrect: Expected (" <<
56 expectedNumDetections << ")" << " but got (" << numDetections << ")";
57 return TestCaseResult::Failed;
58 }
59
Aron Virginas-Tard089b742019-01-29 11:09:51 +000060 // Extract detected objects from output data
61 std::vector<DetectedObject> detectedObjects;
62 const float* outputData = output1.data();
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000063 for (unsigned int i = 0u; i < numDetections; i++)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000064 {
65 // NOTE: Order of coordinates in output data is yMin, xMin, yMax, xMax
66 float yMin = *outputData++;
67 float xMin = *outputData++;
68 float yMax = *outputData++;
69 float xMax = *outputData++;
70
71 DetectedObject detectedObject(
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000072 output2.at(i),
Aron Virginas-Tard089b742019-01-29 11:09:51 +000073 BoundingBox(xMin, yMin, xMax, yMax),
74 output3.at(i));
75
76 detectedObjects.push_back(detectedObject);
77 }
78
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000079 std::sort(detectedObjects.begin(), detectedObjects.end());
80 std::sort(m_DetectedObjects.begin(), m_DetectedObjects.end());
Aron Virginas-Tard089b742019-01-29 11:09:51 +000081
82 // Compare detected objects with expected results
83 std::vector<DetectedObject>::const_iterator it = detectedObjects.begin();
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000084 for (unsigned int i = 0; i < numDetections; i++)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000085 {
86 if (it == detectedObjects.end())
87 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000088 BOOST_LOG_TRIVIAL(error) << "No more detected objects found! Index out of bounds: " << i;
Aron Virginas-Tard089b742019-01-29 11:09:51 +000089 return TestCaseResult::Abort;
90 }
91
92 const DetectedObject& detectedObject = *it;
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000093 const DetectedObject& expectedObject = m_DetectedObjects[i];
94
95 if (detectedObject.m_Class != expectedObject.m_Class)
Aron Virginas-Tard089b742019-01-29 11:09:51 +000096 {
97 BOOST_LOG_TRIVIAL(error) << "Prediction for test case " << this->GetTestCaseId() <<
Narumol Prangnawarat4628d052019-02-25 17:26:05 +000098 " is incorrect: Expected (" << expectedObject.m_Class << ")" <<
Aron Virginas-Tard089b742019-01-29 11:09:51 +000099 " but predicted (" << detectedObject.m_Class << ")";
100 return TestCaseResult::Failed;
101 }
102
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000103 if(!m_FloatComparer(detectedObject.m_Confidence, expectedObject.m_Confidence))
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000104 {
105 BOOST_LOG_TRIVIAL(error) << "Confidence of prediction for test case " << this->GetTestCaseId() <<
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000106 " is incorrect: Expected (" << expectedObject.m_Confidence << ") +- 1.0 pc" <<
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000107 " but predicted (" << detectedObject.m_Confidence << ")";
108 return TestCaseResult::Failed;
109 }
110
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000111 if (!m_FloatComparer(detectedObject.m_BoundingBox.m_XMin, expectedObject.m_BoundingBox.m_XMin) ||
112 !m_FloatComparer(detectedObject.m_BoundingBox.m_YMin, expectedObject.m_BoundingBox.m_YMin) ||
113 !m_FloatComparer(detectedObject.m_BoundingBox.m_XMax, expectedObject.m_BoundingBox.m_XMax) ||
114 !m_FloatComparer(detectedObject.m_BoundingBox.m_YMax, expectedObject.m_BoundingBox.m_YMax))
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000115 {
116 BOOST_LOG_TRIVIAL(error) << "Detected bounding box for test case " << this->GetTestCaseId() <<
117 " is incorrect";
118 return TestCaseResult::Failed;
119 }
120
121 ++it;
122 }
123
124 return TestCaseResult::Ok;
125 }
126
127private:
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000128 static constexpr unsigned int k_Shape = 10u;
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000129
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000130 static constexpr unsigned int k_OutputSize1 = k_Shape * 4u;
131 static constexpr unsigned int k_OutputSize2 = k_Shape;
132 static constexpr unsigned int k_OutputSize3 = k_Shape;
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000133 static constexpr unsigned int k_OutputSize4 = 1u;
134
135 boost::math::fpc::close_at_tolerance<float> m_FloatComparer;
136 std::vector<DetectedObject> m_DetectedObjects;
137};
138
139template <typename Model>
140class MobileNetSsdTestCaseProvider : public IInferenceTestCaseProvider
141{
142public:
143 template <typename TConstructModelCallable>
144 explicit MobileNetSsdTestCaseProvider(TConstructModelCallable constructModel)
145 : m_ConstructModel(constructModel)
146 {}
147
148 virtual void AddCommandLineOptions(boost::program_options::options_description& options) override
149 {
150 namespace po = boost::program_options;
151
152 options.add_options()
153 ("data-dir,d", po::value<std::string>(&m_DataDir)->required(),
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000154 "Path to directory containing test data");
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000155
156 Model::AddCommandLineOptions(options, m_ModelCommandLineOptions);
157 }
158
Matthew Bentham3e68b972019-04-09 13:10:46 +0100159 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000160 {
161 if (!ValidateDirectory(m_DataDir))
162 {
163 return false;
164 }
165
Matthew Bentham3e68b972019-04-09 13:10:46 +0100166 m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000167 if (!m_Model)
168 {
169 return false;
170 }
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000171 std::pair<float, int32_t> qParams = m_Model->GetInputQuantizationParams();
Narumol Prangnawaratc8bab1b2019-02-15 17:34:51 +0000172 m_Database = std::make_unique<MobileNetSsdDatabase>(m_DataDir.c_str(), qParams.first, qParams.second);
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000173 if (!m_Database)
174 {
175 return false;
176 }
177
178 return true;
179 }
180
181 std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override
182 {
183 std::unique_ptr<MobileNetSsdTestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId);
184 if (!testCaseData)
185 {
186 return nullptr;
187 }
188
189 return std::make_unique<MobileNetSsdTestCase<Model>>(*m_Model, testCaseId, *testCaseData);
190 }
191
192private:
193 typename Model::CommandLineOptions m_ModelCommandLineOptions;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100194 std::function<std::unique_ptr<Model>(const InferenceTestOptions &,
195 typename Model::CommandLineOptions)> m_ConstructModel;
Aron Virginas-Tard089b742019-01-29 11:09:51 +0000196 std::unique_ptr<Model> m_Model;
197
198 std::string m_DataDir;
199 std::unique_ptr<MobileNetSsdDatabase> m_Database;
200};
201
202} // anonymous namespace