ArmNN  NotReleased
YoloTestCase< Model > Class Template Reference

#include <YoloInferenceTest.hpp>

Inheritance diagram for YoloTestCase< Model >:
InferenceModelTestCase< Model > IInferenceTestCase

Public Member Functions

 YoloTestCase (Model &model, unsigned int testCaseId, YoloTestCaseData &testCaseData)
 
virtual TestCaseResult ProcessResult (const InferenceTestOptions &options) override
 
- Public Member Functions inherited from InferenceModelTestCase< Model >
 InferenceModelTestCase (Model &model, unsigned int testCaseId, const std::vector< TContainer > &inputs, const std::vector< unsigned int > &outputSizes)
 
virtual void Run () override
 
- Public Member Functions inherited from IInferenceTestCase
virtual ~IInferenceTestCase ()
 

Additional Inherited Members

- Public Types inherited from InferenceModelTestCase< Model >
using TContainer = boost::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char > >
 
- Protected Member Functions inherited from InferenceModelTestCase< Model >
unsigned int GetTestCaseId () const
 
const std::vector< TContainer > & GetOutputs () const
 

Detailed Description

template<typename Model>
class YoloTestCase< Model >

Definition at line 21 of file YoloInferenceTest.hpp.

Constructor & Destructor Documentation

◆ YoloTestCase()

YoloTestCase ( Model model,
unsigned int  testCaseId,
YoloTestCaseData testCaseData 
)
inline

Definition at line 24 of file YoloInferenceTest.hpp.

References YoloTestCaseData::m_InputImage, and YoloOutputSize.

27  : InferenceModelTestCase<Model>(model, testCaseId, { std::move(testCaseData.m_InputImage) }, { YoloOutputSize })
28  , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f))
29  , m_TopObjectDetections(std::move(testCaseData.m_TopObjectDetections))
30  {
31  }
std::vector< YoloDetectedObject > m_TopObjectDetections
constexpr size_t YoloOutputSize
std::vector< float > m_InputImage

Member Function Documentation

◆ ProcessResult()

virtual TestCaseResult ProcessResult ( const InferenceTestOptions options)
inlineoverridevirtual

Implements IInferenceTestCase.

Definition at line 33 of file YoloInferenceTest.hpp.

References ARMNN_LOG, InferenceModelTestCase< Model >::GetOutputs(), InferenceModelTestCase< Model >::GetTestCaseId(), YoloDetectedObject::m_Box, YoloDetectedObject::m_Class, YoloDetectedObject::m_Confidence, YoloBoundingBox::m_H, YoloBoundingBox::m_W, YoloBoundingBox::m_X, YoloBoundingBox::m_Y, YoloImageHeight, YoloImageWidth, and YoloOutputSize.

34  {
35  boost::ignore_unused(options);
36 
37  using Boost3dArray = boost::multi_array<float, 3>;
38 
39  const std::vector<float>& output = boost::get<std::vector<float>>(this->GetOutputs()[0]);
40  BOOST_ASSERT(output.size() == YoloOutputSize);
41 
42  constexpr Boost3dArray::index gridSize = 7;
43  constexpr Boost3dArray::index numClasses = 20;
44  constexpr Boost3dArray::index numScales = 2;
45 
46  const float* outputPtr = output.data();
47 
48  // Range 0-980. Class probabilities. 7x7x20
49  Boost3dArray classProbabilities(boost::extents[gridSize][gridSize][numClasses]);
50  for (Boost3dArray::index y = 0; y < gridSize; ++y)
51  {
52  for (Boost3dArray::index x = 0; x < gridSize; ++x)
53  {
54  for (Boost3dArray::index c = 0; c < numClasses; ++c)
55  {
56  classProbabilities[y][x][c] = *outputPtr++;
57  }
58  }
59  }
60 
61  // Range 980-1078. Scales. 7x7x2
62  Boost3dArray scales(boost::extents[gridSize][gridSize][numScales]);
63  for (Boost3dArray::index y = 0; y < gridSize; ++y)
64  {
65  for (Boost3dArray::index x = 0; x < gridSize; ++x)
66  {
67  for (Boost3dArray::index s = 0; s < numScales; ++s)
68  {
69  scales[y][x][s] = *outputPtr++;
70  }
71  }
72  }
73 
74  // Range 1078-1469. Bounding boxes. 7x7x2x4
75  constexpr float imageWidthAsFloat = static_cast<float>(YoloImageWidth);
76  constexpr float imageHeightAsFloat = static_cast<float>(YoloImageHeight);
77 
78  boost::multi_array<float, 4> boxes(boost::extents[gridSize][gridSize][numScales][4]);
79  for (Boost3dArray::index y = 0; y < gridSize; ++y)
80  {
81  for (Boost3dArray::index x = 0; x < gridSize; ++x)
82  {
83  for (Boost3dArray::index s = 0; s < numScales; ++s)
84  {
85  float bx = *outputPtr++;
86  float by = *outputPtr++;
87  float bw = *outputPtr++;
88  float bh = *outputPtr++;
89 
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);
94  }
95  }
96  }
97  BOOST_ASSERT(output.data() + YoloOutputSize == outputPtr);
98 
99  std::vector<YoloDetectedObject> detectedObjects;
100  detectedObjects.reserve(gridSize * gridSize * numScales * numClasses);
101 
102  for (Boost3dArray::index y = 0; y < gridSize; ++y)
103  {
104  for (Boost3dArray::index x = 0; x < gridSize; ++x)
105  {
106  for (Boost3dArray::index s = 0; s < numScales; ++s)
107  {
108  for (Boost3dArray::index c = 0; c < numClasses; ++c)
109  {
110  // Resolved confidence: class probabilities * scales.
111  const float confidence = classProbabilities[y][x][c] * scales[y][x][s];
112 
113  // Resolves bounding box and stores.
114  YoloBoundingBox box;
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];
119 
120  detectedObjects.emplace_back(c, box, confidence);
121  }
122  }
123  }
124  }
125 
126  // Sorts detected objects by confidence.
127  std::sort(detectedObjects.begin(), detectedObjects.end(),
128  [](const YoloDetectedObject& a, const YoloDetectedObject& b)
129  {
130  // Sorts by largest confidence first, then by class.
131  return a.m_Confidence > b.m_Confidence
132  || (a.m_Confidence == b.m_Confidence && a.m_Class > b.m_Class);
133  });
134 
135  // Checks the top N detections.
136  auto outputIt = detectedObjects.begin();
137  auto outputEnd = detectedObjects.end();
138 
139  for (const YoloDetectedObject& expectedDetection : m_TopObjectDetections)
140  {
141  if (outputIt == outputEnd)
142  {
143  // Somehow expected more things to check than detections found by the model.
144  return TestCaseResult::Abort;
145  }
146 
147  const YoloDetectedObject& detectedObject = *outputIt;
148  if (detectedObject.m_Class != expectedDetection.m_Class)
149  {
150  ARMNN_LOG(error) << "Prediction for test case " << this->GetTestCaseId() <<
151  " is incorrect: Expected (" << expectedDetection.m_Class << ")" <<
152  " but predicted (" << detectedObject.m_Class << ")";
153  return TestCaseResult::Failed;
154  }
155 
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))
161  {
162  ARMNN_LOG(error) << "Detected bounding box for test case " << this->GetTestCaseId() <<
163  " is incorrect";
164  return TestCaseResult::Failed;
165  }
166 
167  ++outputIt;
168  }
169 
170  return TestCaseResult::Ok;
171  }
YoloBoundingBox m_Box
constexpr size_t YoloOutputSize
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
const std::vector< TContainer > & GetOutputs() const
constexpr unsigned int YoloImageHeight
constexpr unsigned int YoloImageWidth
unsigned int m_Class

The documentation for this class was generated from the following file: