telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #include "YoloDatabase.hpp" |
| 6 | |
| 7 | #include <armnn/Exceptions.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 8 | #include <armnn/Logging.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 10 | #include <armnn/utility/NumericCast.hpp> |
| 11 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | #include <array> |
| 13 | #include <cstdint> |
| 14 | #include <tuple> |
| 15 | #include <utility> |
| 16 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | { |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 75 | testCaseId = testCaseId % armnn::numeric_cast<unsigned int>(g_PerTestCaseInputOutput.size()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 76 | const auto& testCaseInputOutput = g_PerTestCaseInputOutput[testCaseId]; |
| 77 | const std::string imagePath = m_ImageDir + testCaseInputOutput.first; |
| 78 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 79 | // Loads test case input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | std::vector<float> imageData; |
| 81 | try |
| 82 | { |
| 83 | InferenceTestImage image(imagePath.c_str()); |
James Conroy | e02d601 | 2018-11-26 09:48:30 +0000 | [diff] [blame] | 84 | if (YoloImageWidth != image.GetWidth() || YoloImageHeight != image.GetHeight()) |
| 85 | { |
| 86 | image.Resize(YoloImageWidth, YoloImageHeight, CHECK_LOCATION()); |
| 87 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 88 | imageData = GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout::Rgb, image); |
| 89 | } |
| 90 | catch (const InferenceTestImageException& e) |
| 91 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 92 | ARMNN_LOG(fatal) << "Failed to load test case " << testCaseId << " with error: " << e.what(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 93 | return nullptr; |
| 94 | } |
| 95 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 96 | // Prepares test case output. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 102 | } |