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>
13 #include <boost/numeric/conversion/cast.hpp>
14 #include <boost/assert.hpp>
15 #include <boost/format.hpp>
21 template <typename TDataType>
22 unsigned int ImagePreprocessor<TDataType>::GetLabelAndResizedImageAsFloat(unsigned int testCaseId,
23 std::vector<float> & result)
25 testCaseId = testCaseId % boost::numeric_cast<unsigned int>(m_ImageSet.size());
26 const ImageSet& imageSet = m_ImageSet[testCaseId];
27 const std::string fullPath = m_BinaryDirectory + imageSet.first;
29 InferenceTestImage image(fullPath.c_str());
31 // this ResizeBilinear result is closer to the tensorflow one than STB.
32 // there is still some difference though, but the inference results are
33 // similar to tensorflow for MobileNet
35 result = image.Resize(m_Width, m_Height, CHECK_LOCATION(),
36 InferenceTestImage::ResizingMethods::BilinearAndNormalized,
37 m_Mean, m_Stddev, m_Scale);
39 // duplicate data across the batch
40 for (unsigned int i = 1; i < m_BatchSize; i++)
42 result.insert(result.end(), result.begin(), result.begin() + boost::numeric_cast<int>(GetNumImageElements()));
45 if (m_DataFormat == DataFormat::NCHW)
47 const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
48 armnn::TensorShape dstShape({m_BatchSize, 3, m_Height, m_Width});
49 std::vector<float> tempImage(result.size());
50 armnnUtils::Permute(dstShape, NHWCToArmNN, result.data(), tempImage.data(), sizeof(float));
51 result.swap(tempImage);
54 return imageSet.second;
58 std::unique_ptr<ImagePreprocessor<float>::TTestCaseData>
59 ImagePreprocessor<float>::GetTestCaseData(unsigned int testCaseId)
61 std::vector<float> resized;
62 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
63 return std::make_unique<TTestCaseData>(label, std::move(resized));
67 std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData>
68 ImagePreprocessor<uint8_t>::GetTestCaseData(unsigned int testCaseId)
70 std::vector<float> resized;
71 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
73 size_t resizedSize = resized.size();
74 std::vector<uint8_t> quantized(resized.size());
76 for (size_t i=0; i<resizedSize; ++i)
78 quantized[i] = static_cast<uint8_t>(resized[i]);
81 return std::make_unique<TTestCaseData>(label, std::move(quantized));