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 | // |
| 5 | #include "InferenceTestImage.hpp" |
| 6 | #include "ImagePreprocessor.hpp" |
| 7 | #include "Permute.hpp" |
| 8 | #include <armnn/TypesUtils.hpp> |
| 9 | |
| 10 | #include <boost/numeric/conversion/cast.hpp> |
| 11 | #include <boost/assert.hpp> |
| 12 | #include <boost/format.hpp> |
| 13 | |
| 14 | #include <iostream> |
| 15 | #include <fcntl.h> |
| 16 | #include <array> |
| 17 | |
| 18 | template <typename TDataType> |
| 19 | unsigned int ImagePreprocessor<TDataType>::GetLabelAndResizedImageAsFloat(unsigned int testCaseId, |
| 20 | std::vector<float> & result) |
| 21 | { |
| 22 | testCaseId = testCaseId % boost::numeric_cast<unsigned int>(m_ImageSet.size()); |
| 23 | 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, |
| 34 | m_Mean, m_Stddev); |
| 35 | |
| 36 | if (m_DataFormat == DataFormat::NCHW) |
| 37 | { |
| 38 | const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 }; |
| 39 | armnn::TensorShape dstShape({1, 3, m_Height, m_Width}); |
| 40 | std::vector<float> tempImage(result.size()); |
| 41 | armnnUtils::Permute<float>(dstShape, NHWCToArmNN, result.data(), tempImage.data()); |
| 42 | result.swap(tempImage); |
| 43 | } |
| 44 | |
| 45 | return imageSet.second; |
| 46 | } |
| 47 | |
| 48 | template <> |
| 49 | std::unique_ptr<ImagePreprocessor<float>::TTestCaseData> |
| 50 | ImagePreprocessor<float>::GetTestCaseData(unsigned int testCaseId) |
| 51 | { |
| 52 | std::vector<float> resized; |
| 53 | auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized); |
| 54 | return std::make_unique<TTestCaseData>(label, std::move(resized)); |
| 55 | } |
| 56 | |
| 57 | template <> |
| 58 | std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData> |
| 59 | ImagePreprocessor<uint8_t>::GetTestCaseData(unsigned int testCaseId) |
| 60 | { |
| 61 | std::vector<float> resized; |
| 62 | auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized); |
| 63 | |
| 64 | size_t resizedSize = resized.size(); |
| 65 | std::vector<uint8_t> quantized(resized.size()); |
| 66 | |
| 67 | for (size_t i=0; i<resizedSize; ++i) |
| 68 | { |
| 69 | quantized[i] = armnn::Quantize<uint8_t>(resized[i], |
| 70 | m_Scale, |
| 71 | m_Offset); |
| 72 | } |
| 73 | return std::make_unique<TTestCaseData>(label, std::move(quantized)); |
| 74 | } |