ArmNN
 20.02
CaffePreprocessor.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "InferenceTestImage.hpp"
6 #include "CaffePreprocessor.hpp"
7 
8 #include <boost/numeric/conversion/cast.hpp>
9 #include <boost/assert.hpp>
10 #include <boost/format.hpp>
11 
12 #include <iostream>
13 #include <fcntl.h>
14 #include <array>
15 
16 const std::vector<ImageSet> g_DefaultImageSet =
17 {
18  {"shark.jpg", 2}
19 };
20 
21 CaffePreprocessor::CaffePreprocessor(const std::string& binaryFileDirectory, unsigned int width, unsigned int height,
22  const std::vector<ImageSet>& imageSet)
23 : m_BinaryDirectory(binaryFileDirectory)
24 , m_Height(height)
25 , m_Width(width)
26 , m_ImageSet(imageSet.empty() ? g_DefaultImageSet : imageSet)
27 {
28 }
29 
30 std::unique_ptr<CaffePreprocessor::TTestCaseData> CaffePreprocessor::GetTestCaseData(unsigned int testCaseId)
31 {
32  testCaseId = testCaseId % boost::numeric_cast<unsigned int>(m_ImageSet.size());
33  const ImageSet& imageSet = m_ImageSet[testCaseId];
34  const std::string fullPath = m_BinaryDirectory + imageSet.first;
35 
36  InferenceTestImage image(fullPath.c_str());
37  image.Resize(m_Width, m_Height, CHECK_LOCATION());
38 
39  // The model expects image data in BGR format.
41  image, m_MeanBgr);
42 
43  // List of labels: https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a
44  const unsigned int label = imageSet.second;
45  return std::make_unique<TTestCaseData>(label, std::move(inputImageData));
46 }
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 std::vector< ImageSet > g_DefaultImageSet
std::vector< float > GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout, const InferenceTestImage &image, const std::array< float, 3 > &mean)
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
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)
CaffePreprocessor(const std::string &binaryFileDirectory, unsigned int width=227, unsigned int height=227, const std::vector< ImageSet > &imageSet=std::vector< ImageSet >())