ArmNN
 21.02
YoloDatabase.cpp
Go to the documentation of this file.
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 
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>
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 
68 YoloDatabase::YoloDatabase(const std::string& imageDir)
69  : m_ImageDir(imageDir)
70 {
71 }
72 
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  {
87  }
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 }
constexpr unsigned int YoloImageHeight
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
std::vector< float > GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout, const InferenceTestImage &image)
YoloVocClass
YoloDatabase(const std::string &imageDir)
std::unique_ptr< TTestCaseData > GetTestCaseData(unsigned int testCaseId)
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
constexpr unsigned int YoloImageWidth
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
std::vector< float > Resize(unsigned int newWidth, unsigned int newHeight, const armnn::CheckLocation &location, const ResizingMethods meth=ResizingMethods::STB, const std::array< float, 3 > &mean={{0.0, 0.0, 0.0}}, const std::array< float, 3 > &stddev={{1.0, 1.0, 1.0}}, const float scale=255.0f)