1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
8 #include "InferenceTestImage.hpp"
9 #include "ObjectDetectionCommon.hpp"
10
11 #include <armnnUtils/QuantizeHelper.hpp>
12
13 #include <armnn/TypesUtils.hpp>
14 #include <armnn/utility/NumericCast.hpp>
15
16 #include <array>
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 namespace
22 {
23
24 struct MobileNetSsdTestCaseData
25 {
MobileNetSsdTestCaseData__anon3c500c880111::MobileNetSsdTestCaseData26 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)
33 {}
34
35 std::vector<uint8_t> m_InputData;
36 std::vector<DetectedObject> m_ExpectedDetectedObject;
37 std::vector<std::vector<float>> m_ExpectedOutput;
38 };
39
40 class MobileNetSsdDatabase
41 {
42 public:
43 explicit MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset);
44
45 std::unique_ptr<MobileNetSsdTestCaseData> GetTestCaseData(unsigned int testCaseId);
46
47 private:
48 std::string m_ImageDir;
49 float m_Scale;
50 int m_Offset;
51 };
52
53 constexpr unsigned int k_MobileNetSsdImageWidth = 300u;
54 constexpr unsigned int k_MobileNetSsdImageHeight = k_MobileNetSsdImageWidth;
55
56 // Test cases
57 const std::array<ObjectDetectionInput, 1> g_PerTestCaseInput =
58 {
59 ObjectDetectionInput
60 {
61 "Cat.jpg",
62 {
63 DetectedObject(16.0f, BoundingBox(0.216785252f, 0.079726994f, 0.927124202f, 0.939067304f), 0.79296875f)
64 }
65 }
66 };
67
MobileNetSsdDatabase(const std::string & imageDir,float scale,int offset)68 MobileNetSsdDatabase::MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset)
69 : m_ImageDir(imageDir)
70 , m_Scale(scale)
71 , m_Offset(offset)
72 {}
73
GetTestCaseData(unsigned int testCaseId)74 std::unique_ptr<MobileNetSsdTestCaseData> MobileNetSsdDatabase::GetTestCaseData(unsigned int testCaseId)
75 {
76 const unsigned int safeTestCaseId =
77 testCaseId % armnn::numeric_cast<unsigned int>(g_PerTestCaseInput.size());
78 const ObjectDetectionInput& testCaseInput = g_PerTestCaseInput[safeTestCaseId];
79
80 // Load test case input
81 const std::string imagePath = m_ImageDir + testCaseInput.first;
82 std::vector<uint8_t> imageData;
83 try
84 {
85 InferenceTestImage image(imagePath.c_str());
86
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)
91 {
92 image.Resize(k_MobileNetSsdImageWidth, k_MobileNetSsdImageHeight, CHECK_LOCATION());
93 }
94
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);
98 }
99 catch (const InferenceTestImageException& e)
100 {
101 ARMNN_LOG(fatal) << "Failed to load image for test case " << testCaseId << ". Error: " << e.what();
102 return nullptr;
103 }
104
105 std::vector<float> numDetections = { static_cast<float>(testCaseInput.second.size()) };
106
107 std::vector<float> detectionBoxes;
108 std::vector<float> detectionClasses;
109 std::vector<float> detectionScores;
110
111 for (DetectedObject expectedObject : testCaseInput.second)
112 {
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);
117
118 detectionClasses.push_back(expectedObject.m_Class);
119
120 detectionScores.push_back(expectedObject.m_Confidence);
121 }
122
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);
130
131 return std::make_unique<MobileNetSsdTestCaseData>(imageData, testCaseInput.second, expectedOutputs);
132 }
133
134 } // anonymous namespace
135