2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include "ObjectDetectionCommon.hpp"
13 #include <armnn/TypesUtils.hpp>
14 #include <backendsCommon/test/QuantizeHelper.hpp>
16 #include <boost/log/trivial.hpp>
17 #include <boost/numeric/conversion/cast.hpp>
22 #include "InferenceTestImage.hpp"
27 struct MobileNetSsdTestCaseData
29 MobileNetSsdTestCaseData(
30 const std::vector<uint8_t>& inputData,
31 const std::vector<DetectedObject>& expectedDetectedObject,
32 const std::vector<std::vector<float>>& expectedOutput)
33 : m_InputData(inputData)
34 , m_ExpectedDetectedObject(expectedDetectedObject)
35 , m_ExpectedOutput(expectedOutput)
38 std::vector<uint8_t> m_InputData;
39 std::vector<DetectedObject> m_ExpectedDetectedObject;
40 std::vector<std::vector<float>> m_ExpectedOutput;
43 class MobileNetSsdDatabase
46 explicit MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset);
48 std::unique_ptr<MobileNetSsdTestCaseData> GetTestCaseData(unsigned int testCaseId);
51 std::string m_ImageDir;
56 constexpr unsigned int k_MobileNetSsdImageWidth = 300u;
57 constexpr unsigned int k_MobileNetSsdImageHeight = k_MobileNetSsdImageWidth;
60 const std::array<ObjectDetectionInput, 1> g_PerTestCaseInput =
66 DetectedObject(16.0f, BoundingBox(0.216785252f, 0.079726994f, 0.927124202f, 0.939067304f), 0.79296875f)
71 MobileNetSsdDatabase::MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset)
72 : m_ImageDir(imageDir)
77 std::unique_ptr<MobileNetSsdTestCaseData> MobileNetSsdDatabase::GetTestCaseData(unsigned int testCaseId)
79 const unsigned int safeTestCaseId =
80 testCaseId % boost::numeric_cast<unsigned int>(g_PerTestCaseInput.size());
81 const ObjectDetectionInput& testCaseInput = g_PerTestCaseInput[safeTestCaseId];
83 // Load test case input
84 const std::string imagePath = m_ImageDir + testCaseInput.first;
85 std::vector<uint8_t> imageData;
88 InferenceTestImage image(imagePath.c_str());
90 // Resize image (if needed)
91 const unsigned int width = image.GetWidth();
92 const unsigned int height = image.GetHeight();
93 if (width != k_MobileNetSsdImageWidth || height != k_MobileNetSsdImageHeight)
95 image.Resize(k_MobileNetSsdImageWidth, k_MobileNetSsdImageHeight, CHECK_LOCATION());
98 // Get image data as a vector of floats
99 std::vector<float> floatImageData = GetImageDataAsNormalizedFloats(ImageChannelLayout::Rgb, image);
100 imageData = QuantizedVector<uint8_t>(m_Scale, m_Offset, floatImageData);
102 catch (const InferenceTestImageException& e)
104 BOOST_LOG_TRIVIAL(fatal) << "Failed to load image for test case " << testCaseId << ". Error: " << e.what();
108 std::vector<float> numDetections = { static_cast<float>(testCaseInput.second.size()) };
110 std::vector<float> detectionBoxes;
111 std::vector<float> detectionClasses;
112 std::vector<float> detectionScores;
114 for (DetectedObject expectedObject : testCaseInput.second)
116 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMin);
117 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMin);
118 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMax);
119 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMax);
121 detectionClasses.push_back(expectedObject.m_Class);
123 detectionScores.push_back(expectedObject.m_Confidence);
126 // Prepare test case expected output
127 std::vector<std::vector<float>> expectedOutputs;
128 expectedOutputs.reserve(4);
129 expectedOutputs.push_back(detectionBoxes);
130 expectedOutputs.push_back(detectionClasses);
131 expectedOutputs.push_back(detectionScores);
132 expectedOutputs.push_back(numDetections);
134 return std::make_unique<MobileNetSsdTestCaseData>(imageData, testCaseInput.second, expectedOutputs);
137 } // anonymous namespace