blob: 66f297c502b00dc6faacbfe094c9821721caf43f [file] [log] [blame]
surmeh01bceff2f2018-03-29 16:29:27 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// See LICENSE file in the project root for full license information.
4//
5#include "InferenceTestImage.hpp"
6#include "MobileNetDatabase.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
16namespace
17{
18
19inline float Lerp(float a, float b, float w)
20{
21 return w * b + (1.f - w) * a;
22}
23
24inline void PutData(std::vector<float> & data,
25 const unsigned int width,
26 const unsigned int x,
27 const unsigned int y,
28 const unsigned int c,
29 float value)
30{
31 data[(3*((y*width)+x)) + c] = value;
32}
33
34std::vector<float>
35ResizeBilinearAndNormalize(const InferenceTestImage & image,
36 const unsigned int outputWidth,
37 const unsigned int outputHeight)
38{
39 std::vector<float> out;
40 out.resize(outputWidth * outputHeight * 3);
41
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.
45
46 const unsigned int inputWidth = image.GetWidth();
47 const unsigned int inputHeight = image.GetHeight();
48
49 // How much to scale pixel coordinates in the output image to get the corresponding pixel coordinates
50 // in the input image
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);
53
54 uint8_t rgb_x0y0[3];
55 uint8_t rgb_x1y0[3];
56 uint8_t rgb_x0y1[3];
57 uint8_t rgb_x1y1[3];
58
59 for (unsigned int y = 0; y < outputHeight; ++y)
60 {
61 // Corresponding real-valued height coordinate in input image
62 const float iy = boost::numeric_cast<float>(y) * scaleY;
63
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);
67
68 // Interpolation weight (range [0,1])
69 const float yw = iy - fiy;
70
71 for (unsigned int x = 0; x < outputWidth; ++x)
72 {
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);
77
78 // Interpolation weight (range [0,1])
79 const float xw = ix - fix;
80
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);
84
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);
89
90 for (unsigned c=0; c<3; ++c)
91 {
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);
96 }
97 }
98 }
99
100 return out;
101}
102
103} // end of anonymous namespace
104
105
106MobileNetDatabase::MobileNetDatabase(const std::string& binaryFileDirectory,
107 unsigned int width,
108 unsigned int height,
109 const std::vector<ImageSet>& imageSet)
110: m_BinaryDirectory(binaryFileDirectory)
111, m_Height(height)
112, m_Width(width)
113, m_ImageSet(imageSet)
114{
115}
116
117std::unique_ptr<MobileNetDatabase::TTestCaseData>
118MobileNetDatabase::GetTestCaseData(unsigned int testCaseId)
119{
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;
123
124 InferenceTestImage image(fullPath.c_str());
125
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));
130
131 const unsigned int label = imageSet.second;
132 return std::make_unique<TTestCaseData>(label, std::move(resized));
133}