blob: fe7410b8eba3bbf2406d99ede5d264efb0615b8d [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#include "YoloDatabase.hpp"
6
7#include <armnn/Exceptions.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +00008#include <armnn/Logging.hpp>
telsoa014fcda012018-03-09 14:13:49 +00009
Matthew Sloyan80c6b142020-09-08 12:00:32 +010010#include <armnn/utility/NumericCast.hpp>
11
telsoa014fcda012018-03-09 14:13:49 +000012#include <array>
13#include <cstdint>
14#include <tuple>
15#include <utility>
16
telsoa014fcda012018-03-09 14:13:49 +000017#include "InferenceTestImage.hpp"
18
19namespace
20{
21enum 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
45template <typename E>
46constexpr auto to_underlying(E e) noexcept
47{
48 return static_cast<std::underlying_type_t<E>>(e);
49}
50
51class ImageNotFoundException : public armnn::Exception
52{
53 using Exception::Exception;
54};
55
56using YoloInputOutput = std::pair<const char* const, YoloDetectedObject>;
57
58const 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
68YoloDatabase::YoloDatabase(const std::string& imageDir)
69 : m_ImageDir(imageDir)
70{
71}
72
73std::unique_ptr<YoloDatabase::TTestCaseData> YoloDatabase::GetTestCaseData(unsigned int testCaseId)
74{
Matthew Sloyan80c6b142020-09-08 12:00:32 +010075 testCaseId = testCaseId % armnn::numeric_cast<unsigned int>(g_PerTestCaseInputOutput.size());
telsoa014fcda012018-03-09 14:13:49 +000076 const auto& testCaseInputOutput = g_PerTestCaseInputOutput[testCaseId];
77 const std::string imagePath = m_ImageDir + testCaseInputOutput.first;
78
telsoa01c577f2c2018-08-31 09:22:23 +010079 // Loads test case input image.
telsoa014fcda012018-03-09 14:13:49 +000080 std::vector<float> imageData;
81 try
82 {
83 InferenceTestImage image(imagePath.c_str());
James Conroye02d6012018-11-26 09:48:30 +000084 if (YoloImageWidth != image.GetWidth() || YoloImageHeight != image.GetHeight())
85 {
86 image.Resize(YoloImageWidth, YoloImageHeight, CHECK_LOCATION());
87 }
telsoa014fcda012018-03-09 14:13:49 +000088 imageData = GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout::Rgb, image);
89 }
90 catch (const InferenceTestImageException& e)
91 {
Derek Lamberti08446972019-11-26 16:38:31 +000092 ARMNN_LOG(fatal) << "Failed to load test case " << testCaseId << " with error: " << e.what();
telsoa014fcda012018-03-09 14:13:49 +000093 return nullptr;
94 }
95
telsoa01c577f2c2018-08-31 09:22:23 +010096 // Prepares test case output.
telsoa014fcda012018-03-09 14:13:49 +000097 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));
telsoa01c577f2c2018-08-31 09:22:23 +0100102}