2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "InferenceTestImage.hpp"
6 #include "MobileNetDatabase.hpp"
8 #include <boost/numeric/conversion/cast.hpp>
9 #include <boost/assert.hpp>
10 #include <boost/format.hpp>
19 inline float Lerp(float a, float b, float w)
21 return w * b + (1.f - w) * a;
24 inline void PutData(std::vector<float> & data,
25 const unsigned int width,
31 data[(3*((y*width)+x)) + c] = value;
35 ResizeBilinearAndNormalize(const InferenceTestImage & image,
36 const unsigned int outputWidth,
37 const unsigned int outputHeight)
39 std::vector<float> out;
40 out.resize(outputWidth * outputHeight * 3);
42 // We follow the definition of TensorFlow and AndroidNN: The top-left corner of a texel in the output
43 // image is projected into the input image to figure out the interpolants and weights. Note that this
44 // will yield different results than if projecting the centre of output texels.
46 const unsigned int inputWidth = image.GetWidth();
47 const unsigned int inputHeight = image.GetHeight();
49 // How much to scale pixel coordinates in the output image to get the corresponding pixel coordinates
51 const float scaleY = boost::numeric_cast<float>(inputHeight) / boost::numeric_cast<float>(outputHeight);
52 const float scaleX = boost::numeric_cast<float>(inputWidth) / boost::numeric_cast<float>(outputWidth);
59 for (unsigned int y = 0; y < outputHeight; ++y)
61 // Corresponding real-valued height coordinate in input image
62 const float iy = boost::numeric_cast<float>(y) * scaleY;
64 // Discrete height coordinate of top-left texel (in the 2x2 texel area used for interpolation)
65 const float fiy = floorf(iy);
66 const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy);
68 // Interpolation weight (range [0,1])
69 const float yw = iy - fiy;
71 for (unsigned int x = 0; x < outputWidth; ++x)
73 // Real-valued and discrete width coordinates in input image
74 const float ix = boost::numeric_cast<float>(x) * scaleX;
75 const float fix = floorf(ix);
76 const unsigned int x0 = boost::numeric_cast<unsigned int>(fix);
78 // Interpolation weight (range [0,1])
79 const float xw = ix - fix;
81 // Discrete width/height coordinates of texels below and to the right of (x0, y0)
82 const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u);
83 const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u);
85 std::tie(rgb_x0y0[0], rgb_x0y0[1], rgb_x0y0[2]) = image.GetPixelAs3Channels(x0, y0);
86 std::tie(rgb_x1y0[0], rgb_x1y0[1], rgb_x1y0[2]) = image.GetPixelAs3Channels(x1, y0);
87 std::tie(rgb_x0y1[0], rgb_x0y1[1], rgb_x0y1[2]) = image.GetPixelAs3Channels(x0, y1);
88 std::tie(rgb_x1y1[0], rgb_x1y1[1], rgb_x1y1[2]) = image.GetPixelAs3Channels(x1, y1);
90 for (unsigned c=0; c<3; ++c)
92 const float ly0 = Lerp(float(rgb_x0y0[c]), float(rgb_x1y0[c]), xw);
93 const float ly1 = Lerp(float(rgb_x0y1[c]), float(rgb_x1y1[c]), xw);
94 const float l = Lerp(ly0, ly1, yw);
95 PutData(out, outputWidth, x, y, c, l/255.0f);
103 } // end of anonymous namespace
106 MobileNetDatabase::MobileNetDatabase(const std::string& binaryFileDirectory,
109 const std::vector<ImageSet>& imageSet)
110 : m_BinaryDirectory(binaryFileDirectory)
113 , m_ImageSet(imageSet)
117 std::unique_ptr<MobileNetDatabase::TTestCaseData>
118 MobileNetDatabase::GetTestCaseData(unsigned int testCaseId)
120 testCaseId = testCaseId % boost::numeric_cast<unsigned int>(m_ImageSet.size());
121 const ImageSet& imageSet = m_ImageSet[testCaseId];
122 const std::string fullPath = m_BinaryDirectory + imageSet.first;
124 InferenceTestImage image(fullPath.c_str());
126 // this ResizeBilinear result is closer to the tensorflow one than STB.
127 // there is still some difference though, but the inference results are
128 // similar to tensorflow for MobileNet
129 std::vector<float> resized(ResizeBilinearAndNormalize(image, m_Width, m_Height));
131 const unsigned int label = imageSet.second;
132 return std::make_unique<TTestCaseData>(label, std::move(resized));