telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 5 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 6 | #include "InferenceTestImage.hpp" |
| 7 | #include "ImagePreprocessor.hpp" |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 8 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include <armnn/TypesUtils.hpp> |
| 10 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 11 | #include <armnnUtils/Permute.hpp> |
| 12 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 13 | #include <boost/numeric/conversion/cast.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 14 | #include <boost/format.hpp> |
| 15 | |
| 16 | #include <iostream> |
| 17 | #include <fcntl.h> |
| 18 | #include <array> |
| 19 | |
| 20 | template <typename TDataType> |
| 21 | unsigned int ImagePreprocessor<TDataType>::GetLabelAndResizedImageAsFloat(unsigned int testCaseId, |
| 22 | std::vector<float> & result) |
| 23 | { |
| 24 | testCaseId = testCaseId % boost::numeric_cast<unsigned int>(m_ImageSet.size()); |
| 25 | 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, |
FinnWilliamsArm | af8b72d | 2019-05-22 14:50:55 +0100 | [diff] [blame] | 36 | m_Mean, m_Stddev, m_Scale); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | |
Nattapat Chaimanowong | d8eee59 | 2018-10-26 10:24:14 +0100 | [diff] [blame] | 38 | // duplicate data across the batch |
| 39 | for (unsigned int i = 1; i < m_BatchSize; i++) |
| 40 | { |
Matteo Martincigh | 19483db | 2018-10-29 13:45:34 +0000 | [diff] [blame] | 41 | result.insert(result.end(), result.begin(), result.begin() + boost::numeric_cast<int>(GetNumImageElements())); |
Nattapat Chaimanowong | d8eee59 | 2018-10-26 10:24:14 +0100 | [diff] [blame] | 42 | } |
| 43 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | if (m_DataFormat == DataFormat::NCHW) |
| 45 | { |
| 46 | const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 }; |
Nattapat Chaimanowong | d8eee59 | 2018-10-26 10:24:14 +0100 | [diff] [blame] | 47 | armnn::TensorShape dstShape({m_BatchSize, 3, m_Height, m_Width}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | std::vector<float> tempImage(result.size()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 49 | armnnUtils::Permute(dstShape, NHWCToArmNN, result.data(), tempImage.data(), sizeof(float)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | result.swap(tempImage); |
| 51 | } |
| 52 | |
| 53 | return imageSet.second; |
| 54 | } |
| 55 | |
| 56 | template <> |
| 57 | std::unique_ptr<ImagePreprocessor<float>::TTestCaseData> |
| 58 | ImagePreprocessor<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 | |
| 65 | template <> |
| 66 | std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData> |
| 67 | ImagePreprocessor<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 | { |
FinnWilliamsArm | af8b72d | 2019-05-22 14:50:55 +0100 | [diff] [blame] | 77 | quantized[i] = static_cast<uint8_t>(resized[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | } |
FinnWilliamsArm | af8b72d | 2019-05-22 14:50:55 +0100 | [diff] [blame] | 79 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | return std::make_unique<TTestCaseData>(label, std::move(quantized)); |
| 81 | } |