14 #include <boost/assert.hpp> 15 #include <boost/multi_array.hpp> 16 #include <boost/test/tools/floating_point_comparison.hpp> 20 template <
typename Model>
25 unsigned int testCaseId,
28 , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f))
29 , m_TopObjectDetections(std::move(testCaseData.m_TopObjectDetections))
35 boost::ignore_unused(options);
37 using Boost3dArray = boost::multi_array<float, 3>;
39 const std::vector<float>& output = boost::get<std::vector<float>>(this->
GetOutputs()[0]);
42 constexpr Boost3dArray::index gridSize = 7;
43 constexpr Boost3dArray::index numClasses = 20;
44 constexpr Boost3dArray::index numScales = 2;
46 const float* outputPtr = output.data();
49 Boost3dArray classProbabilities(boost::extents[gridSize][gridSize][numClasses]);
50 for (Boost3dArray::index y = 0; y < gridSize; ++y)
52 for (Boost3dArray::index x = 0; x < gridSize; ++x)
54 for (Boost3dArray::index c = 0; c < numClasses; ++c)
56 classProbabilities[y][x][c] = *outputPtr++;
62 Boost3dArray scales(boost::extents[gridSize][gridSize][numScales]);
63 for (Boost3dArray::index y = 0; y < gridSize; ++y)
65 for (Boost3dArray::index x = 0; x < gridSize; ++x)
67 for (Boost3dArray::index s = 0; s < numScales; ++s)
69 scales[y][x][s] = *outputPtr++;
75 constexpr
float imageWidthAsFloat =
static_cast<float>(
YoloImageWidth);
76 constexpr
float imageHeightAsFloat =
static_cast<float>(
YoloImageHeight);
78 boost::multi_array<float, 4> boxes(boost::extents[gridSize][gridSize][numScales][4]);
79 for (Boost3dArray::index y = 0; y < gridSize; ++y)
81 for (Boost3dArray::index x = 0; x < gridSize; ++x)
83 for (Boost3dArray::index s = 0; s < numScales; ++s)
85 float bx = *outputPtr++;
86 float by = *outputPtr++;
87 float bw = *outputPtr++;
88 float bh = *outputPtr++;
90 boxes[y][x][s][0] = ((bx +
static_cast<float>(x)) / 7.0f) * imageWidthAsFloat;
91 boxes[y][x][s][1] = ((by +
static_cast<float>(y)) / 7.0f) * imageHeightAsFloat;
92 boxes[y][x][s][2] = bw * bw *
static_cast<float>(imageWidthAsFloat);
93 boxes[y][x][s][3] = bh * bh *
static_cast<float>(imageHeightAsFloat);
99 std::vector<YoloDetectedObject> detectedObjects;
100 detectedObjects.reserve(gridSize * gridSize * numScales * numClasses);
102 for (Boost3dArray::index y = 0; y < gridSize; ++y)
104 for (Boost3dArray::index x = 0; x < gridSize; ++x)
106 for (Boost3dArray::index s = 0; s < numScales; ++s)
108 for (Boost3dArray::index c = 0; c < numClasses; ++c)
111 const float confidence = classProbabilities[y][x][c] * scales[y][x][s];
115 box.
m_X = boxes[y][x][s][0];
116 box.
m_Y = boxes[y][x][s][1];
117 box.
m_W = boxes[y][x][s][2];
118 box.
m_H = boxes[y][x][s][3];
120 detectedObjects.emplace_back(c, box, confidence);
127 std::sort(detectedObjects.begin(), detectedObjects.end(),
136 auto outputIt = detectedObjects.begin();
137 auto outputEnd = detectedObjects.end();
141 if (outputIt == outputEnd)
144 return TestCaseResult::Abort;
148 if (detectedObject.
m_Class != expectedDetection.m_Class)
151 " is incorrect: Expected (" << expectedDetection.m_Class <<
")" <<
152 " but predicted (" << detectedObject.
m_Class <<
")";
153 return TestCaseResult::Failed;
156 if (!m_FloatComparer(detectedObject.
m_Box.
m_X, expectedDetection.m_Box.m_X) ||
157 !m_FloatComparer(detectedObject.
m_Box.
m_Y, expectedDetection.m_Box.m_Y) ||
158 !m_FloatComparer(detectedObject.
m_Box.
m_W, expectedDetection.m_Box.m_W) ||
159 !m_FloatComparer(detectedObject.
m_Box.
m_H, expectedDetection.m_Box.m_H) ||
160 !m_FloatComparer(detectedObject.
m_Confidence, expectedDetection.m_Confidence))
164 return TestCaseResult::Failed;
170 return TestCaseResult::Ok;
174 boost::math::fpc::close_at_tolerance<float> m_FloatComparer;
175 std::vector<YoloDetectedObject> m_TopObjectDetections;
178 template <
typename Model>
182 template <
typename TConstructModelCallable>
184 : m_ConstructModel(constructModel)
190 namespace po = boost::program_options;
192 options.add_options()
193 (
"data-dir,d", po::value<std::string>(&m_DataDir)->required(),
194 "Path to directory containing test data");
196 Model::AddCommandLineOptions(options, m_ModelCommandLineOptions);
206 m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
212 m_Database = std::make_unique<YoloDatabase>(m_DataDir.c_str());
221 virtual std::unique_ptr<IInferenceTestCase>
GetTestCase(
unsigned int testCaseId)
override 223 std::unique_ptr<YoloTestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId);
229 return std::make_unique<YoloTestCase<Model>>(*m_Model, testCaseId, *testCaseData);
233 typename Model::CommandLineOptions m_ModelCommandLineOptions;
235 typename Model::CommandLineOptions)> m_ConstructModel;
236 std::unique_ptr<Model> m_Model;
238 std::string m_DataDir;
239 std::unique_ptr<YoloDatabase> m_Database;
virtual TestCaseResult ProcessResult(const InferenceTestOptions &options) override
virtual std::unique_ptr< IInferenceTestCase > GetTestCase(unsigned int testCaseId) override
constexpr size_t YoloOutputSize
#define ARMNN_LOG(severity)
YoloTestCase(Model &model, unsigned int testCaseId, YoloTestCaseData &testCaseData)
virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override
virtual void AddCommandLineOptions(boost::program_options::options_description &options) override
YoloTestCaseProvider(TConstructModelCallable constructModel)
const std::vector< TContainer > & GetOutputs() const
constexpr unsigned int YoloImageHeight
unsigned int GetTestCaseId() const
constexpr unsigned int YoloImageWidth
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
std::vector< float > m_InputImage
bool ValidateDirectory(std::string &dir)