2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "InferenceTestImage.hpp"
7 #include "ImagePreprocessor.hpp"
9 #include <armnn/TypesUtils.hpp>
11 #include <armnnUtils/Permute.hpp>
12 #include <armnn/utility/NumericCast.hpp>
14 #include <boost/format.hpp>
20 template <typename TDataType>
21 unsigned int ImagePreprocessor<TDataType>::GetLabelAndResizedImageAsFloat(unsigned int testCaseId,
22 std::vector<float> & result)
24 testCaseId = testCaseId % armnn::numeric_cast<unsigned int>(m_ImageSet.size());
25 const ImageSet& imageSet = m_ImageSet[testCaseId];
26 const std::string fullPath = m_BinaryDirectory + imageSet.first;
28 InferenceTestImage image(fullPath.c_str());
30 // this ResizeBilinear result is closer to the tensorflow one than STB.
31 // there is still some difference though, but the inference results are
32 // similar to tensorflow for MobileNet
34 result = image.Resize(m_Width, m_Height, CHECK_LOCATION(),
35 InferenceTestImage::ResizingMethods::BilinearAndNormalized,
36 m_Mean, m_Stddev, m_Scale);
38 // duplicate data across the batch
39 for (unsigned int i = 1; i < m_BatchSize; i++)
41 result.insert(result.end(), result.begin(), result.begin() + armnn::numeric_cast<int>(GetNumImageElements()));
44 if (m_DataFormat == DataFormat::NCHW)
46 const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
47 armnn::TensorShape dstShape({m_BatchSize, 3, m_Height, m_Width});
48 std::vector<float> tempImage(result.size());
49 armnnUtils::Permute(dstShape, NHWCToArmNN, result.data(), tempImage.data(), sizeof(float));
50 result.swap(tempImage);
53 return imageSet.second;
57 std::unique_ptr<ImagePreprocessor<float>::TTestCaseData>
58 ImagePreprocessor<float>::GetTestCaseData(unsigned int testCaseId)
60 std::vector<float> resized;
61 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
62 return std::make_unique<TTestCaseData>(label, std::move(resized));
66 std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData>
67 ImagePreprocessor<uint8_t>::GetTestCaseData(unsigned int testCaseId)
69 std::vector<float> resized;
70 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
72 size_t resizedSize = resized.size();
73 std::vector<uint8_t> quantized(resized.size());
75 for (size_t i=0; i<resizedSize; ++i)
77 quantized[i] = static_cast<uint8_t>(resized[i]);
80 return std::make_unique<TTestCaseData>(label, std::move(quantized));