IVGCVSW-2436 Modify MobileNet SSD inference test
[platform/upstream/armnn.git] / tests / MobileNetSsdDatabase.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 #include "ObjectDetectionCommon.hpp"
8
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include <armnn/TypesUtils.hpp>
14 #include <backendsCommon/test/QuantizeHelper.hpp>
15
16 #include <boost/log/trivial.hpp>
17 #include <boost/numeric/conversion/cast.hpp>
18
19 #include <array>
20 #include <string>
21
22 #include "InferenceTestImage.hpp"
23
24 namespace
25 {
26
27 struct MobileNetSsdTestCaseData
28 {
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))
34     {}
35
36     std::vector<uint8_t>        m_InputData;
37     std::vector<DetectedObject> 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         DetectedObject(16, BoundingBox(0.208961248f, 0.0852333307f, 0.92757535f, 0.940263629f), 0.79296875f)
63     }
64 };
65
66 MobileNetSsdDatabase::MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset)
67     : m_ImageDir(imageDir)
68     , m_Scale(scale)
69     , m_Offset(offset)
70 {}
71
72 std::unique_ptr<MobileNetSsdTestCaseData> MobileNetSsdDatabase::GetTestCaseData(unsigned int testCaseId)
73 {
74     const unsigned int safeTestCaseId =
75         testCaseId % boost::numeric_cast<unsigned int>(g_PerTestCaseInput.size());
76     const ObjectDetectionInput& testCaseInput = g_PerTestCaseInput[safeTestCaseId];
77
78     // Load test case input
79     const std::string imagePath = m_ImageDir + testCaseInput.first;
80     std::vector<uint8_t> imageData;
81     try
82     {
83         InferenceTestImage image(imagePath.c_str());
84
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)
89         {
90             image.Resize(k_MobileNetSsdImageWidth, k_MobileNetSsdImageHeight, CHECK_LOCATION());
91         }
92
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);
96     }
97     catch (const InferenceTestImageException& e)
98     {
99         BOOST_LOG_TRIVIAL(fatal) << "Failed to load image for test case " << testCaseId << ". Error: " << e.what();
100         return nullptr;
101     }
102
103     // Prepare test case expected output
104     std::vector<DetectedObject> expectedOutput;
105     expectedOutput.reserve(1);
106     expectedOutput.push_back(testCaseInput.second);
107
108     return std::make_unique<MobileNetSsdTestCaseData>(std::move(imageData), std::move(expectedOutput));
109 }
110
111 } // anonymous namespace