blob: 3b3e5a90d44e6e052ec709eb0acdb23748e88472 [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
10#include <array>
11#include <cstdint>
12#include <tuple>
13#include <utility>
14
telsoa014fcda012018-03-09 14:13:49 +000015#include <boost/format.hpp>
telsoa014fcda012018-03-09 14:13:49 +000016#include <boost/numeric/conversion/cast.hpp>
17
18#include "InferenceTestImage.hpp"
19
20namespace
21{
22enum 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
46template <typename E>
47constexpr auto to_underlying(E e) noexcept
48{
49 return static_cast<std::underlying_type_t<E>>(e);
50}
51
52class ImageNotFoundException : public armnn::Exception
53{
54 using Exception::Exception;
55};
56
57using YoloInputOutput = std::pair<const char* const, YoloDetectedObject>;
58
59const 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
69YoloDatabase::YoloDatabase(const std::string& imageDir)
70 : m_ImageDir(imageDir)
71{
72}
73
74std::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
telsoa01c577f2c2018-08-31 09:22:23 +010080 // Loads test case input image.
telsoa014fcda012018-03-09 14:13:49 +000081 std::vector<float> imageData;
82 try
83 {
84 InferenceTestImage image(imagePath.c_str());
James Conroye02d6012018-11-26 09:48:30 +000085 if (YoloImageWidth != image.GetWidth() || YoloImageHeight != image.GetHeight())
86 {
87 image.Resize(YoloImageWidth, YoloImageHeight, CHECK_LOCATION());
88 }
telsoa014fcda012018-03-09 14:13:49 +000089 imageData = GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout::Rgb, image);
90 }
91 catch (const InferenceTestImageException& e)
92 {
Derek Lamberti08446972019-11-26 16:38:31 +000093 ARMNN_LOG(fatal) << "Failed to load test case " << testCaseId << " with error: " << e.what();
telsoa014fcda012018-03-09 14:13:49 +000094 return nullptr;
95 }
96
telsoa01c577f2c2018-08-31 09:22:23 +010097 // Prepares test case output.
telsoa014fcda012018-03-09 14:13:49 +000098 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));
telsoa01c577f2c2018-08-31 09:22:23 +0100103}