blob: e6dbfb51c7302b0a2b3fef225cfb18a7348fa0bb [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 <iostream>
15#include <fcntl.h>
16#include <array>
17
18template <typename TDataType>
19unsigned int ImagePreprocessor<TDataType>::GetLabelAndResizedImageAsFloat(unsigned int testCaseId,
20 std::vector<float> & result)
21{
Matthew Sloyan80c6b142020-09-08 12:00:32 +010022 testCaseId = testCaseId % armnn::numeric_cast<unsigned int>(m_ImageSet.size());
telsoa01c577f2c2018-08-31 09:22:23 +010023 const ImageSet& imageSet = m_ImageSet[testCaseId];
24 const std::string fullPath = m_BinaryDirectory + imageSet.first;
25
26 InferenceTestImage image(fullPath.c_str());
27
28 // this ResizeBilinear result is closer to the tensorflow one than STB.
29 // there is still some difference though, but the inference results are
30 // similar to tensorflow for MobileNet
31
32 result = image.Resize(m_Width, m_Height, CHECK_LOCATION(),
33 InferenceTestImage::ResizingMethods::BilinearAndNormalized,
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010034 m_Mean, m_Stddev, m_Scale);
telsoa01c577f2c2018-08-31 09:22:23 +010035
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010036 // duplicate data across the batch
37 for (unsigned int i = 1; i < m_BatchSize; i++)
38 {
Matthew Sloyan80c6b142020-09-08 12:00:32 +010039 result.insert(result.end(), result.begin(), result.begin() + armnn::numeric_cast<int>(GetNumImageElements()));
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010040 }
41
telsoa01c577f2c2018-08-31 09:22:23 +010042 if (m_DataFormat == DataFormat::NCHW)
43 {
44 const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010045 armnn::TensorShape dstShape({m_BatchSize, 3, m_Height, m_Width});
telsoa01c577f2c2018-08-31 09:22:23 +010046 std::vector<float> tempImage(result.size());
Matteo Martincighd5b9e642019-01-04 18:01:21 +000047 armnnUtils::Permute(dstShape, NHWCToArmNN, result.data(), tempImage.data(), sizeof(float));
telsoa01c577f2c2018-08-31 09:22:23 +010048 result.swap(tempImage);
49 }
50
51 return imageSet.second;
52}
53
54template <>
55std::unique_ptr<ImagePreprocessor<float>::TTestCaseData>
56ImagePreprocessor<float>::GetTestCaseData(unsigned int testCaseId)
57{
58 std::vector<float> resized;
59 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
60 return std::make_unique<TTestCaseData>(label, std::move(resized));
61}
62
63template <>
64std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData>
65ImagePreprocessor<uint8_t>::GetTestCaseData(unsigned int testCaseId)
66{
67 std::vector<float> resized;
68 auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
69
70 size_t resizedSize = resized.size();
71 std::vector<uint8_t> quantized(resized.size());
72
73 for (size_t i=0; i<resizedSize; ++i)
74 {
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010075 quantized[i] = static_cast<uint8_t>(resized[i]);
telsoa01c577f2c2018-08-31 09:22:23 +010076 }
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010077
telsoa01c577f2c2018-08-31 09:22:23 +010078 return std::make_unique<TTestCaseData>(label, std::move(quantized));
79}