2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include "YoloDatabase.hpp"
7 #include <armnn/Exceptions.hpp>
8 #include <armnn/Logging.hpp>
15 #include <boost/format.hpp>
16 #include <boost/numeric/conversion/cast.hpp>
18 #include "InferenceTestImage.hpp"
22 enum class YoloVocClass : unsigned int
47 constexpr auto to_underlying(E e) noexcept
49 return static_cast<std::underlying_type_t<E>>(e);
52 class ImageNotFoundException : public armnn::Exception
54 using Exception::Exception;
57 using YoloInputOutput = std::pair<const char* const, YoloDetectedObject>;
59 const std::array<YoloInputOutput,1> g_PerTestCaseInputOutput =
62 "yolo_dog_448x448.png",
63 { to_underlying(YoloVocClass::Dog), YoloBoundingBox{ 233.0f, 256.0f, 299.0f, 462.0f }, 0.5088733434677124f }
69 YoloDatabase::YoloDatabase(const std::string& imageDir)
70 : m_ImageDir(imageDir)
74 std::unique_ptr<YoloDatabase::TTestCaseData> YoloDatabase::GetTestCaseData(unsigned int testCaseId)
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;
80 // Loads test case input image.
81 std::vector<float> imageData;
84 InferenceTestImage image(imagePath.c_str());
85 if (YoloImageWidth != image.GetWidth() || YoloImageHeight != image.GetHeight())
87 image.Resize(YoloImageWidth, YoloImageHeight, CHECK_LOCATION());
89 imageData = GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout::Rgb, image);
91 catch (const InferenceTestImageException& e)
93 ARMNN_LOG(fatal) << "Failed to load test case " << testCaseId << " with error: " << e.what();
97 // Prepares test case output.
98 std::vector<YoloDetectedObject> topObjectDetections;
99 topObjectDetections.reserve(1);
100 topObjectDetections.push_back(testCaseInputOutput.second);
102 return std::make_unique<YoloTestCaseData>(std::move(imageData), std::move(topObjectDetections));