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