blob: f0184e466e818d5fe0afb73ec80282acc94777da [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>
12
telsoa01c577f2c2018-08-31 09:22:23 +010013#include <boost/numeric/conversion/cast.hpp>
14#include <boost/assert.hpp>
15#include <boost/format.hpp>
16
17#include <iostream>
18#include <fcntl.h>
19#include <array>
20
21template <typename TDataType>
22unsigned int ImagePreprocessor<TDataType>::GetLabelAndResizedImageAsFloat(unsigned int testCaseId,
23 std::vector<float> & result)
24{
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;
28
29 InferenceTestImage image(fullPath.c_str());
30
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
34
35 result = image.Resize(m_Width, m_Height, CHECK_LOCATION(),
36 InferenceTestImage::ResizingMethods::BilinearAndNormalized,
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010037 m_Mean, m_Stddev, m_Scale);
telsoa01c577f2c2018-08-31 09:22:23 +010038
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010039 // duplicate data across the batch
40 for (unsigned int i = 1; i < m_BatchSize; i++)
41 {
Matteo Martincigh19483db2018-10-29 13:45:34 +000042 result.insert(result.end(), result.begin(), result.begin() + boost::numeric_cast<int>(GetNumImageElements()));
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010043 }
44
telsoa01c577f2c2018-08-31 09:22:23 +010045 if (m_DataFormat == DataFormat::NCHW)
46 {
47 const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010048 armnn::TensorShape dstShape({m_BatchSize, 3, m_Height, m_Width});
telsoa01c577f2c2018-08-31 09:22:23 +010049 std::vector<float> tempImage(result.size());
Matteo Martincighd5b9e642019-01-04 18:01:21 +000050 armnnUtils::Permute(dstShape, NHWCToArmNN, result.data(), tempImage.data(), sizeof(float));
telsoa01c577f2c2018-08-31 09:22:23 +010051 result.swap(tempImage);
52 }
53
54 return imageSet.second;
55}
56
57template <>
58std::unique_ptr<ImagePreprocessor<float>::TTestCaseData>
59ImagePreprocessor<float>::GetTestCaseData(unsigned int testCaseId)
60{
61 std::vector<float> resized;
62 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
63 return std::make_unique<TTestCaseData>(label, std::move(resized));
64}
65
66template <>
67std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData>
68ImagePreprocessor<uint8_t>::GetTestCaseData(unsigned int testCaseId)
69{
70 std::vector<float> resized;
71 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
72
73 size_t resizedSize = resized.size();
74 std::vector<uint8_t> quantized(resized.size());
75
76 for (size_t i=0; i<resizedSize; ++i)
77 {
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010078 quantized[i] = static_cast<uint8_t>(resized[i]);
telsoa01c577f2c2018-08-31 09:22:23 +010079 }
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010080
telsoa01c577f2c2018-08-31 09:22:23 +010081 return std::make_unique<TTestCaseData>(label, std::move(quantized));
82}