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