2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
8 #include "InferenceTestImage.hpp"
9 #include "ObjectDetectionCommon.hpp"
11 #include <QuantizeHelper.hpp>
13 #include <armnn/TypesUtils.hpp>
15 #include <boost/numeric/conversion/cast.hpp>
25 struct MobileNetSsdTestCaseData
27 MobileNetSsdTestCaseData(
28 const std::vector<uint8_t>& inputData,
29 const std::vector<DetectedObject>& expectedDetectedObject,
30 const std::vector<std::vector<float>>& expectedOutput)
31 : m_InputData(inputData)
32 , m_ExpectedDetectedObject(expectedDetectedObject)
33 , m_ExpectedOutput(expectedOutput)
36 std::vector<uint8_t> m_InputData;
37 std::vector<DetectedObject> m_ExpectedDetectedObject;
38 std::vector<std::vector<float>> m_ExpectedOutput;
41 class MobileNetSsdDatabase
44 explicit MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset);
46 std::unique_ptr<MobileNetSsdTestCaseData> GetTestCaseData(unsigned int testCaseId);
49 std::string m_ImageDir;
54 constexpr unsigned int k_MobileNetSsdImageWidth = 300u;
55 constexpr unsigned int k_MobileNetSsdImageHeight = k_MobileNetSsdImageWidth;
58 const std::array<ObjectDetectionInput, 1> g_PerTestCaseInput =
64 DetectedObject(16.0f, BoundingBox(0.216785252f, 0.079726994f, 0.927124202f, 0.939067304f), 0.79296875f)
69 MobileNetSsdDatabase::MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset)
70 : m_ImageDir(imageDir)
75 std::unique_ptr<MobileNetSsdTestCaseData> MobileNetSsdDatabase::GetTestCaseData(unsigned int testCaseId)
77 const unsigned int safeTestCaseId =
78 testCaseId % boost::numeric_cast<unsigned int>(g_PerTestCaseInput.size());
79 const ObjectDetectionInput& testCaseInput = g_PerTestCaseInput[safeTestCaseId];
81 // Load test case input
82 const std::string imagePath = m_ImageDir + testCaseInput.first;
83 std::vector<uint8_t> imageData;
86 InferenceTestImage image(imagePath.c_str());
88 // Resize image (if needed)
89 const unsigned int width = image.GetWidth();
90 const unsigned int height = image.GetHeight();
91 if (width != k_MobileNetSsdImageWidth || height != k_MobileNetSsdImageHeight)
93 image.Resize(k_MobileNetSsdImageWidth, k_MobileNetSsdImageHeight, CHECK_LOCATION());
96 // Get image data as a vector of floats
97 std::vector<float> floatImageData = GetImageDataAsNormalizedFloats(ImageChannelLayout::Rgb, image);
98 imageData = armnnUtils::QuantizedVector<uint8_t>(floatImageData, m_Scale, m_Offset);
100 catch (const InferenceTestImageException& e)
102 ARMNN_LOG(fatal) << "Failed to load image for test case " << testCaseId << ". Error: " << e.what();
106 std::vector<float> numDetections = { static_cast<float>(testCaseInput.second.size()) };
108 std::vector<float> detectionBoxes;
109 std::vector<float> detectionClasses;
110 std::vector<float> detectionScores;
112 for (DetectedObject expectedObject : testCaseInput.second)
114 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMin);
115 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMin);
116 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMax);
117 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMax);
119 detectionClasses.push_back(expectedObject.m_Class);
121 detectionScores.push_back(expectedObject.m_Confidence);
124 // Prepare test case expected output
125 std::vector<std::vector<float>> expectedOutputs;
126 expectedOutputs.reserve(4);
127 expectedOutputs.push_back(detectionBoxes);
128 expectedOutputs.push_back(detectionClasses);
129 expectedOutputs.push_back(detectionScores);
130 expectedOutputs.push_back(numDetections);
132 return std::make_unique<MobileNetSsdTestCaseData>(imageData, testCaseInput.second, expectedOutputs);
135 } // anonymous namespace