ArmNN
 20.11
ImagePreprocessor.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "InferenceTestImage.hpp"
7 #include "ImagePreprocessor.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
10 
11 #include <armnnUtils/Permute.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 % armnn::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(),
34  m_Mean, m_Stddev, m_Scale);
35 
36  // duplicate data across the batch
37  for (unsigned int i = 1; i < m_BatchSize; i++)
38  {
39  result.insert(result.end(), result.begin(), result.begin() + armnn::numeric_cast<int>(GetNumImageElements()));
40  }
41 
42  if (m_DataFormat == DataFormat::NCHW)
43  {
44  const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
45  armnn::TensorShape dstShape({m_BatchSize, 3, m_Height, m_Width});
46  std::vector<float> tempImage(result.size());
47  armnnUtils::Permute(dstShape, NHWCToArmNN, result.data(), tempImage.data(), sizeof(float));
48  result.swap(tempImage);
49  }
50 
51  return imageSet.second;
52 }
53 
54 template <>
55 std::unique_ptr<ImagePreprocessor<float>::TTestCaseData>
57 {
58  std::vector<float> resized;
59  auto label = GetLabelAndResizedImageAsFloat(testCaseId, resized);
60  return std::make_unique<TTestCaseData>(label, std::move(resized));
61 }
62 
63 template <>
64 std::unique_ptr<ImagePreprocessor<uint8_t>::TTestCaseData>
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  {
75  quantized[i] = static_cast<uint8_t>(resized[i]);
76  }
77 
78  return std::make_unique<TTestCaseData>(label, std::move(quantized));
79 }
std::pair< const std::string, unsigned int > ImageSet
Caffe requires BGR images, not normalized, mean adjusted and resized using smooth resize of STB libra...
std::unique_ptr< TTestCaseData > GetTestCaseData(unsigned int testCaseId)
const armnn::PermutationVector NHWCToArmNN
void Permute(const armnn::TensorShape &dstShape, const armnn::PermutationVector &mappings, const void *src, void *dst, size_t dataTypeSize)
Definition: Permute.cpp:131
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
std::vector< float > Resize(unsigned int newWidth, unsigned int newHeight, const armnn::CheckLocation &location, const ResizingMethods meth=ResizingMethods::STB, const std::array< float, 3 > &mean={{0.0, 0.0, 0.0}}, const std::array< float, 3 > &stddev={{1.0, 1.0, 1.0}}, const float scale=255.0f)