blob: 4557cadca538fb19909bf0cbcdf1962fd98ebbf2 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
Matteo Martincighe011d202019-11-28 11:35:47 +00005
telsoa01c577f2c2018-08-31 09:22:23 +01006#include "InferenceTestImage.hpp"
7#include "ImagePreprocessor.hpp"
Matteo Martincighe011d202019-11-28 11:35:47 +00008
telsoa01c577f2c2018-08-31 09:22:23 +01009#include <armnn/TypesUtils.hpp>
10
Matteo Martincighe011d202019-11-28 11:35:47 +000011#include <armnnUtils/Permute.hpp>
Matthew Sloyan80c6b142020-09-08 12:00:32 +010012#include <armnn/utility/NumericCast.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000013
telsoa01c577f2c2018-08-31 09:22:23 +010014#include <boost/format.hpp>
15
16#include <iostream>
17#include <fcntl.h>
18#include <array>
19
20template <typename TDataType>
21unsigned int ImagePreprocessor<TDataType>::GetLabelAndResizedImageAsFloat(unsigned int testCaseId,
22 std::vector<float> & result)
23{
Matthew Sloyan80c6b142020-09-08 12:00:32 +010024 testCaseId = testCaseId % armnn::numeric_cast<unsigned int>(m_ImageSet.size());
telsoa01c577f2c2018-08-31 09:22:23 +010025 const ImageSet& imageSet = m_ImageSet[testCaseId];
26 const std::string fullPath = m_BinaryDirectory + imageSet.first;
27
28 InferenceTestImage image(fullPath.c_str());
29
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
33
34 result = image.Resize(m_Width, m_Height, CHECK_LOCATION(),
35 InferenceTestImage::ResizingMethods::BilinearAndNormalized,
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010036 m_Mean, m_Stddev, m_Scale);
telsoa01c577f2c2018-08-31 09:22:23 +010037
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010038 // duplicate data across the batch
39 for (unsigned int i = 1; i < m_BatchSize; i++)
40 {
Matthew Sloyan80c6b142020-09-08 12:00:32 +010041 result.insert(result.end(), result.begin(), result.begin() + armnn::numeric_cast<int>(GetNumImageElements()));
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010042 }
43
telsoa01c577f2c2018-08-31 09:22:23 +010044 if (m_DataFormat == DataFormat::NCHW)
45 {
46 const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010047 armnn::TensorShape dstShape({m_BatchSize, 3, m_Height, m_Width});
telsoa01c577f2c2018-08-31 09:22:23 +010048 std::vector<float> tempImage(result.size());
Matteo Martincighd5b9e642019-01-04 18:01:21 +000049 armnnUtils::Permute(dstShape, NHWCToArmNN, result.data(), tempImage.data(), sizeof(float));
telsoa01c577f2c2018-08-31 09:22:23 +010050 result.swap(tempImage);
51 }
52
53 return imageSet.second;
54}
55
56template <>
57std::unique_ptr<ImagePreprocessor<float>::TTestCaseData>
58ImagePreprocessor<float>::GetTestCaseData(unsigned int testCaseId)
59{
60 std::vector<float> resized;
61 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
62 return std::make_unique<TTestCaseData>(label, std::move(resized));
63}
64
65template <>
66std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData>
67ImagePreprocessor<uint8_t>::GetTestCaseData(unsigned int testCaseId)
68{
69 std::vector<float> resized;
70 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
71
72 size_t resizedSize = resized.size();
73 std::vector<uint8_t> quantized(resized.size());
74
75 for (size_t i=0; i<resizedSize; ++i)
76 {
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010077 quantized[i] = static_cast<uint8_t>(resized[i]);
telsoa01c577f2c2018-08-31 09:22:23 +010078 }
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010079
telsoa01c577f2c2018-08-31 09:22:23 +010080 return std::make_unique<TTestCaseData>(label, std::move(quantized));
81}