IVGCVSW-4661 Add include Assert to GatordMockService.cpp
[platform/upstream/armnn.git] / tests / YoloDatabase.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "YoloDatabase.hpp"
6
7 #include <armnn/Exceptions.hpp>
8 #include <armnn/Logging.hpp>
9
10 #include <array>
11 #include <cstdint>
12 #include <tuple>
13 #include <utility>
14
15 #include <boost/format.hpp>
16 #include <boost/numeric/conversion/cast.hpp>
17
18 #include "InferenceTestImage.hpp"
19
20 namespace
21 {
22 enum class YoloVocClass : unsigned int
23 {
24     Aeroplane,
25     Bicycle,
26     Bird,
27     Boat,
28     Bottle,
29     Bus,
30     Car,
31     Cat,
32     Chair,
33     Cow,
34     DiningTable,
35     Dog,
36     Horse,
37     Motorbike,
38     Person,
39     PottedPlant,
40     Sheep,
41     Sofa,
42     Train,
43     TvMonitor
44 };
45
46 template <typename E>
47 constexpr auto to_underlying(E e) noexcept
48 {
49     return static_cast<std::underlying_type_t<E>>(e);
50 }
51
52 class ImageNotFoundException : public armnn::Exception
53 {
54     using Exception::Exception;
55 };
56
57 using YoloInputOutput = std::pair<const char* const, YoloDetectedObject>;
58
59 const std::array<YoloInputOutput,1> g_PerTestCaseInputOutput =
60 {
61     YoloInputOutput{
62         "yolo_dog_448x448.png",
63         { to_underlying(YoloVocClass::Dog), YoloBoundingBox{ 233.0f, 256.0f, 299.0f, 462.0f }, 0.5088733434677124f }
64     },
65 };
66
67 } // namespace
68
69 YoloDatabase::YoloDatabase(const std::string& imageDir)
70     : m_ImageDir(imageDir)
71 {
72 }
73
74 std::unique_ptr<YoloDatabase::TTestCaseData> YoloDatabase::GetTestCaseData(unsigned int testCaseId)
75 {
76     testCaseId = testCaseId % boost::numeric_cast<unsigned int>(g_PerTestCaseInputOutput.size());
77     const auto& testCaseInputOutput = g_PerTestCaseInputOutput[testCaseId];
78     const std::string imagePath = m_ImageDir + testCaseInputOutput.first;
79
80     // Loads test case input image.
81     std::vector<float> imageData;
82     try
83     {
84         InferenceTestImage image(imagePath.c_str());
85         if (YoloImageWidth != image.GetWidth() || YoloImageHeight != image.GetHeight())
86         {
87             image.Resize(YoloImageWidth, YoloImageHeight, CHECK_LOCATION());
88         }
89         imageData = GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout::Rgb, image);
90     }
91     catch (const InferenceTestImageException& e)
92     {
93         ARMNN_LOG(fatal) << "Failed to load test case " << testCaseId << " with error: " << e.what();
94         return nullptr;
95     }
96
97     // Prepares test case output.
98     std::vector<YoloDetectedObject> topObjectDetections;
99     topObjectDetections.reserve(1);
100     topObjectDetections.push_back(testCaseInputOutput.second);
101
102     return std::make_unique<YoloTestCaseData>(std::move(imageData), std::move(topObjectDetections));
103 }