16 #include <boost/assert.hpp> 17 #include <boost/multi_array.hpp> 18 #include <boost/test/tools/floating_point_comparison.hpp> 22 template <
typename Model>
27 unsigned int testCaseId,
30 , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f))
31 , m_TopObjectDetections(std::move(testCaseData.m_TopObjectDetections))
39 using Boost3dArray = boost::multi_array<float, 3>;
41 const std::vector<float>& output = boost::get<std::vector<float>>(this->
GetOutputs()[0]);
44 constexpr Boost3dArray::index gridSize = 7;
45 constexpr Boost3dArray::index numClasses = 20;
46 constexpr Boost3dArray::index numScales = 2;
48 const float* outputPtr = output.data();
51 Boost3dArray classProbabilities(boost::extents[gridSize][gridSize][numClasses]);
52 for (Boost3dArray::index y = 0; y < gridSize; ++y)
54 for (Boost3dArray::index x = 0; x < gridSize; ++x)
56 for (Boost3dArray::index c = 0; c < numClasses; ++c)
58 classProbabilities[y][x][c] = *outputPtr++;
64 Boost3dArray scales(boost::extents[gridSize][gridSize][numScales]);
65 for (Boost3dArray::index y = 0; y < gridSize; ++y)
67 for (Boost3dArray::index x = 0; x < gridSize; ++x)
69 for (Boost3dArray::index s = 0; s < numScales; ++s)
71 scales[y][x][s] = *outputPtr++;
77 constexpr
float imageWidthAsFloat =
static_cast<float>(
YoloImageWidth);
78 constexpr
float imageHeightAsFloat =
static_cast<float>(
YoloImageHeight);
80 boost::multi_array<float, 4> boxes(boost::extents[gridSize][gridSize][numScales][4]);
81 for (Boost3dArray::index y = 0; y < gridSize; ++y)
83 for (Boost3dArray::index x = 0; x < gridSize; ++x)
85 for (Boost3dArray::index s = 0; s < numScales; ++s)
87 float bx = *outputPtr++;
88 float by = *outputPtr++;
89 float bw = *outputPtr++;
90 float bh = *outputPtr++;
92 boxes[y][x][s][0] = ((bx +
static_cast<float>(x)) / 7.0f) * imageWidthAsFloat;
93 boxes[y][x][s][1] = ((by +
static_cast<float>(y)) / 7.0f) * imageHeightAsFloat;
94 boxes[y][x][s][2] = bw * bw *
static_cast<float>(imageWidthAsFloat);
95 boxes[y][x][s][3] = bh * bh *
static_cast<float>(imageHeightAsFloat);
101 std::vector<YoloDetectedObject> detectedObjects;
102 detectedObjects.reserve(gridSize * gridSize * numScales * numClasses);
104 for (Boost3dArray::index y = 0; y < gridSize; ++y)
106 for (Boost3dArray::index x = 0; x < gridSize; ++x)
108 for (Boost3dArray::index s = 0; s < numScales; ++s)
110 for (Boost3dArray::index c = 0; c < numClasses; ++c)
113 const float confidence = classProbabilities[y][x][c] * scales[y][x][s];
117 box.
m_X = boxes[y][x][s][0];
118 box.
m_Y = boxes[y][x][s][1];
119 box.
m_W = boxes[y][x][s][2];
120 box.
m_H = boxes[y][x][s][3];
122 detectedObjects.emplace_back(c, box, confidence);
129 std::sort(detectedObjects.begin(), detectedObjects.end(),
138 auto outputIt = detectedObjects.begin();
139 auto outputEnd = detectedObjects.end();
143 if (outputIt == outputEnd)
146 return TestCaseResult::Abort;
150 if (detectedObject.
m_Class != expectedDetection.m_Class)
153 " is incorrect: Expected (" << expectedDetection.m_Class <<
")" <<
154 " but predicted (" << detectedObject.
m_Class <<
")";
155 return TestCaseResult::Failed;
158 if (!m_FloatComparer(detectedObject.
m_Box.
m_X, expectedDetection.m_Box.m_X) ||
159 !m_FloatComparer(detectedObject.
m_Box.
m_Y, expectedDetection.m_Box.m_Y) ||
160 !m_FloatComparer(detectedObject.
m_Box.
m_W, expectedDetection.m_Box.m_W) ||
161 !m_FloatComparer(detectedObject.
m_Box.
m_H, expectedDetection.m_Box.m_H) ||
162 !m_FloatComparer(detectedObject.
m_Confidence, expectedDetection.m_Confidence))
166 return TestCaseResult::Failed;
172 return TestCaseResult::Ok;
176 boost::math::fpc::close_at_tolerance<float> m_FloatComparer;
177 std::vector<YoloDetectedObject> m_TopObjectDetections;
180 template <
typename Model>
184 template <
typename TConstructModelCallable>
186 : m_ConstructModel(constructModel)
192 namespace po = boost::program_options;
194 options.add_options()
195 (
"data-dir,d", po::value<std::string>(&m_DataDir)->required(),
196 "Path to directory containing test data");
198 Model::AddCommandLineOptions(options, m_ModelCommandLineOptions);
208 m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
214 m_Database = std::make_unique<YoloDatabase>(m_DataDir.c_str());
223 virtual std::unique_ptr<IInferenceTestCase>
GetTestCase(
unsigned int testCaseId)
override 225 std::unique_ptr<YoloTestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId);
231 return std::make_unique<YoloTestCase<Model>>(*m_Model, testCaseId, *testCaseData);
235 typename Model::CommandLineOptions m_ModelCommandLineOptions;
237 typename Model::CommandLineOptions)> m_ConstructModel;
238 std::unique_ptr<Model> m_Model;
240 std::string m_DataDir;
241 std::unique_ptr<YoloDatabase> m_Database;
constexpr unsigned int YoloImageHeight
virtual TestCaseResult ProcessResult(const InferenceTestOptions &options) override
const std::vector< TContainer > & GetOutputs() const
#define ARMNN_LOG(severity)
void IgnoreUnused(Ts &&...)
unsigned int GetTestCaseId() const
virtual void AddCommandLineOptions(boost::program_options::options_description &options) override
constexpr size_t YoloOutputSize
YoloTestCase(Model &model, unsigned int testCaseId, YoloTestCaseData &testCaseData)
virtual std::unique_ptr< IInferenceTestCase > GetTestCase(unsigned int testCaseId) override
virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override
std::vector< float > m_InputImage
YoloTestCaseProvider(TConstructModelCallable constructModel)
constexpr unsigned int YoloImageWidth
bool ValidateDirectory(std::string &dir)
armnn::Runtime::CreationOptions::ExternalProfilingOptions options