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>
14 #include <armnn/utility/NumericCast.hpp>
24 struct MobileNetSsdTestCaseData
26 MobileNetSsdTestCaseData(
27 const std::vector<uint8_t>& inputData,
28 const std::vector<DetectedObject>& expectedDetectedObject,
29 const std::vector<std::vector<float>>& expectedOutput)
30 : m_InputData(inputData)
31 , m_ExpectedDetectedObject(expectedDetectedObject)
32 , m_ExpectedOutput(expectedOutput)
35 std::vector<uint8_t> m_InputData;
36 std::vector<DetectedObject> m_ExpectedDetectedObject;
37 std::vector<std::vector<float>> m_ExpectedOutput;
40 class MobileNetSsdDatabase
43 explicit MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset);
45 std::unique_ptr<MobileNetSsdTestCaseData> GetTestCaseData(unsigned int testCaseId);
48 std::string m_ImageDir;
53 constexpr unsigned int k_MobileNetSsdImageWidth = 300u;
54 constexpr unsigned int k_MobileNetSsdImageHeight = k_MobileNetSsdImageWidth;
57 const std::array<ObjectDetectionInput, 1> g_PerTestCaseInput =
63 DetectedObject(16.0f, BoundingBox(0.216785252f, 0.079726994f, 0.927124202f, 0.939067304f), 0.79296875f)
68 MobileNetSsdDatabase::MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset)
69 : m_ImageDir(imageDir)
74 std::unique_ptr<MobileNetSsdTestCaseData> MobileNetSsdDatabase::GetTestCaseData(unsigned int testCaseId)
76 const unsigned int safeTestCaseId =
77 testCaseId % armnn::numeric_cast<unsigned int>(g_PerTestCaseInput.size());
78 const ObjectDetectionInput& testCaseInput = g_PerTestCaseInput[safeTestCaseId];
80 // Load test case input
81 const std::string imagePath = m_ImageDir + testCaseInput.first;
82 std::vector<uint8_t> imageData;
85 InferenceTestImage image(imagePath.c_str());
87 // Resize image (if needed)
88 const unsigned int width = image.GetWidth();
89 const unsigned int height = image.GetHeight();
90 if (width != k_MobileNetSsdImageWidth || height != k_MobileNetSsdImageHeight)
92 image.Resize(k_MobileNetSsdImageWidth, k_MobileNetSsdImageHeight, CHECK_LOCATION());
95 // Get image data as a vector of floats
96 std::vector<float> floatImageData = GetImageDataAsNormalizedFloats(ImageChannelLayout::Rgb, image);
97 imageData = armnnUtils::QuantizedVector<uint8_t>(floatImageData, m_Scale, m_Offset);
99 catch (const InferenceTestImageException& e)
101 ARMNN_LOG(fatal) << "Failed to load image for test case " << testCaseId << ". Error: " << e.what();
105 std::vector<float> numDetections = { static_cast<float>(testCaseInput.second.size()) };
107 std::vector<float> detectionBoxes;
108 std::vector<float> detectionClasses;
109 std::vector<float> detectionScores;
111 for (DetectedObject expectedObject : testCaseInput.second)
113 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMin);
114 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMin);
115 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMax);
116 detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMax);
118 detectionClasses.push_back(expectedObject.m_Class);
120 detectionScores.push_back(expectedObject.m_Confidence);
123 // Prepare test case expected output
124 std::vector<std::vector<float>> expectedOutputs;
125 expectedOutputs.reserve(4);
126 expectedOutputs.push_back(detectionBoxes);
127 expectedOutputs.push_back(detectionClasses);
128 expectedOutputs.push_back(detectionScores);
129 expectedOutputs.push_back(numDetections);
131 return std::make_unique<MobileNetSsdTestCaseData>(imageData, testCaseInput.second, expectedOutputs);
134 } // anonymous namespace