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>
10 #include <armnn/utility/NumericCast.hpp>
17 #include "InferenceTestImage.hpp"
21 enum class YoloVocClass : unsigned int
46 constexpr auto to_underlying(E e) noexcept
48 return static_cast<std::underlying_type_t<E>>(e);
51 class ImageNotFoundException : public armnn::Exception
53 using Exception::Exception;
56 using YoloInputOutput = std::pair<const char* const, YoloDetectedObject>;
58 const std::array<YoloInputOutput,1> g_PerTestCaseInputOutput =
61 "yolo_dog_448x448.png",
62 { to_underlying(YoloVocClass::Dog), YoloBoundingBox{ 233.0f, 256.0f, 299.0f, 462.0f }, 0.5088733434677124f }
68 YoloDatabase::YoloDatabase(const std::string& imageDir)
69 : m_ImageDir(imageDir)
73 std::unique_ptr<YoloDatabase::TTestCaseData> YoloDatabase::GetTestCaseData(unsigned int testCaseId)
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;
79 // Loads test case input image.
80 std::vector<float> imageData;
83 InferenceTestImage image(imagePath.c_str());
84 if (YoloImageWidth != image.GetWidth() || YoloImageHeight != image.GetHeight())
86 image.Resize(YoloImageWidth, YoloImageHeight, CHECK_LOCATION());
88 imageData = GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout::Rgb, image);
90 catch (const InferenceTestImageException& e)
92 ARMNN_LOG(fatal) << "Failed to load test case " << testCaseId << " with error: " << e.what();
96 // Prepares test case output.
97 std::vector<YoloDetectedObject> topObjectDetections;
98 topObjectDetections.reserve(1);
99 topObjectDetections.push_back(testCaseInputOutput.second);
101 return std::make_unique<YoloTestCaseData>(std::move(imageData), std::move(topObjectDetections));