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 std::vector<uint8_t> inputData,
31 std::vector<DetectedObject> expectedOutput)
32 : m_InputData(std::move(inputData))
33 , m_ExpectedOutput(std::move(expectedOutput))
36 std::vector<uint8_t> m_InputData;
37 std::vector<DetectedObject> 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 =
62 DetectedObject(16, BoundingBox(0.208961248f, 0.0852333307f, 0.92757535f, 0.940263629f), 0.79296875f)
66 MobileNetSsdDatabase::MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset)
67 : m_ImageDir(imageDir)
72 std::unique_ptr<MobileNetSsdTestCaseData> MobileNetSsdDatabase::GetTestCaseData(unsigned int testCaseId)
74 const unsigned int safeTestCaseId =
75 testCaseId % boost::numeric_cast<unsigned int>(g_PerTestCaseInput.size());
76 const ObjectDetectionInput& testCaseInput = g_PerTestCaseInput[safeTestCaseId];
78 // Load test case input
79 const std::string imagePath = m_ImageDir + testCaseInput.first;
80 std::vector<uint8_t> imageData;
83 InferenceTestImage image(imagePath.c_str());
85 // Resize image (if needed)
86 const unsigned int width = image.GetWidth();
87 const unsigned int height = image.GetHeight();
88 if (width != k_MobileNetSsdImageWidth || height != k_MobileNetSsdImageHeight)
90 image.Resize(k_MobileNetSsdImageWidth, k_MobileNetSsdImageHeight, CHECK_LOCATION());
93 // Get image data as a vector of floats
94 std::vector<float> floatImageData = GetImageDataAsNormalizedFloats(ImageChannelLayout::Rgb, image);
95 imageData = QuantizedVector<uint8_t>(m_Scale, m_Offset, floatImageData);
97 catch (const InferenceTestImageException& e)
99 BOOST_LOG_TRIVIAL(fatal) << "Failed to load image for test case " << testCaseId << ". Error: " << e.what();
103 // Prepare test case expected output
104 std::vector<DetectedObject> expectedOutput;
105 expectedOutput.reserve(1);
106 expectedOutput.push_back(testCaseInput.second);
108 return std::make_unique<MobileNetSsdTestCaseData>(std::move(imageData), std::move(expectedOutput));
111 } // anonymous namespace