Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 5 | |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 6 | #pragma once |
| 7 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 8 | #include "InferenceTestImage.hpp" |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 9 | #include "ObjectDetectionCommon.hpp" |
| 10 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 11 | #include <QuantizeHelper.hpp> |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 12 | |
| 13 | #include <armnn/TypesUtils.hpp> |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 14 | #include <armnn/utility/NumericCast.hpp> |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 15 | |
| 16 | #include <array> |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 17 | #include <memory> |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 18 | #include <string> |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 19 | #include <vector> |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | struct MobileNetSsdTestCaseData |
| 25 | { |
| 26 | MobileNetSsdTestCaseData( |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 27 | const std::vector<uint8_t>& inputData, |
| 28 | const std::vector<DetectedObject>& expectedDetectedObject, |
| 29 | const std::vector<std::vector<float>>& expectedOutput) |
| 30 | : m_InputData(inputData) |
| 31 | , m_ExpectedDetectedObject(expectedDetectedObject) |
| 32 | , m_ExpectedOutput(expectedOutput) |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 33 | {} |
| 34 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 35 | std::vector<uint8_t> m_InputData; |
| 36 | std::vector<DetectedObject> m_ExpectedDetectedObject; |
| 37 | std::vector<std::vector<float>> m_ExpectedOutput; |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | class MobileNetSsdDatabase |
| 41 | { |
| 42 | public: |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 43 | explicit MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset); |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 44 | |
| 45 | std::unique_ptr<MobileNetSsdTestCaseData> GetTestCaseData(unsigned int testCaseId); |
| 46 | |
| 47 | private: |
| 48 | std::string m_ImageDir; |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 49 | float m_Scale; |
| 50 | int m_Offset; |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | constexpr unsigned int k_MobileNetSsdImageWidth = 300u; |
| 54 | constexpr unsigned int k_MobileNetSsdImageHeight = k_MobileNetSsdImageWidth; |
| 55 | |
| 56 | // Test cases |
| 57 | const std::array<ObjectDetectionInput, 1> g_PerTestCaseInput = |
| 58 | { |
| 59 | ObjectDetectionInput |
| 60 | { |
| 61 | "Cat.jpg", |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 62 | { |
Narumol Prangnawarat | 713e95c | 2019-06-20 17:08:03 +0100 | [diff] [blame] | 63 | DetectedObject(16.0f, BoundingBox(0.216785252f, 0.079726994f, 0.927124202f, 0.939067304f), 0.79296875f) |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 64 | } |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 65 | } |
| 66 | }; |
| 67 | |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 68 | MobileNetSsdDatabase::MobileNetSsdDatabase(const std::string& imageDir, float scale, int offset) |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 69 | : m_ImageDir(imageDir) |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 70 | , m_Scale(scale) |
| 71 | , m_Offset(offset) |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 72 | {} |
| 73 | |
| 74 | std::unique_ptr<MobileNetSsdTestCaseData> MobileNetSsdDatabase::GetTestCaseData(unsigned int testCaseId) |
| 75 | { |
| 76 | const unsigned int safeTestCaseId = |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 77 | testCaseId % armnn::numeric_cast<unsigned int>(g_PerTestCaseInput.size()); |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 78 | const ObjectDetectionInput& testCaseInput = g_PerTestCaseInput[safeTestCaseId]; |
| 79 | |
| 80 | // Load test case input |
| 81 | const std::string imagePath = m_ImageDir + testCaseInput.first; |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 82 | std::vector<uint8_t> imageData; |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 83 | try |
| 84 | { |
| 85 | InferenceTestImage image(imagePath.c_str()); |
| 86 | |
| 87 | // Resize image (if needed) |
| 88 | const unsigned int width = image.GetWidth(); |
| 89 | const unsigned int height = image.GetHeight(); |
| 90 | if (width != k_MobileNetSsdImageWidth || height != k_MobileNetSsdImageHeight) |
| 91 | { |
| 92 | image.Resize(k_MobileNetSsdImageWidth, k_MobileNetSsdImageHeight, CHECK_LOCATION()); |
| 93 | } |
| 94 | |
| 95 | // Get image data as a vector of floats |
Narumol Prangnawarat | c8bab1b | 2019-02-15 17:34:51 +0000 | [diff] [blame] | 96 | std::vector<float> floatImageData = GetImageDataAsNormalizedFloats(ImageChannelLayout::Rgb, image); |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 97 | imageData = armnnUtils::QuantizedVector<uint8_t>(floatImageData, m_Scale, m_Offset); |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 98 | } |
| 99 | catch (const InferenceTestImageException& e) |
| 100 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 101 | ARMNN_LOG(fatal) << "Failed to load image for test case " << testCaseId << ". Error: " << e.what(); |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 102 | return nullptr; |
| 103 | } |
| 104 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 105 | std::vector<float> numDetections = { static_cast<float>(testCaseInput.second.size()) }; |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 106 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 107 | std::vector<float> detectionBoxes; |
| 108 | std::vector<float> detectionClasses; |
| 109 | std::vector<float> detectionScores; |
| 110 | |
| 111 | for (DetectedObject expectedObject : testCaseInput.second) |
| 112 | { |
| 113 | detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMin); |
| 114 | detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMin); |
| 115 | detectionBoxes.push_back(expectedObject.m_BoundingBox.m_YMax); |
| 116 | detectionBoxes.push_back(expectedObject.m_BoundingBox.m_XMax); |
| 117 | |
| 118 | detectionClasses.push_back(expectedObject.m_Class); |
| 119 | |
| 120 | detectionScores.push_back(expectedObject.m_Confidence); |
| 121 | } |
| 122 | |
| 123 | // Prepare test case expected output |
| 124 | std::vector<std::vector<float>> expectedOutputs; |
| 125 | expectedOutputs.reserve(4); |
| 126 | expectedOutputs.push_back(detectionBoxes); |
| 127 | expectedOutputs.push_back(detectionClasses); |
| 128 | expectedOutputs.push_back(detectionScores); |
| 129 | expectedOutputs.push_back(numDetections); |
| 130 | |
| 131 | return std::make_unique<MobileNetSsdTestCaseData>(imageData, testCaseInput.second, expectedOutputs); |
Aron Virginas-Tar | d089b74 | 2019-01-29 11:09:51 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // anonymous namespace |