blob: 0bce3c7ed89a6dcde9610ab3325081bd68b5664e [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
6#include "ResizeBilinear.hpp"
7
8#include "TensorBufferArrayView.hpp"
9
10#include <boost/numeric/conversion/cast.hpp>
11
12#include <cmath>
13#include <algorithm>
14
15namespace armnn
16{
17
18namespace
19{
20
21inline float Lerp(float a, float b, float w)
22{
23 return w * b + (1.f - w) * a;
24}
25
26}
27
28void ResizeBilinear(const float* in, const TensorInfo& inputInfo, float* out, const TensorInfo& outputInfo)
29{
telsoa01c577f2c2018-08-31 09:22:23 +010030 // We follow the definition of TensorFlow and AndroidNN: the top-left corner of a texel in the output
telsoa014fcda012018-03-09 14:13:49 +000031 // image is projected into the input image to figure out the interpolants and weights. Note that this
32 // will yield different results than if projecting the centre of output texels.
33
34 const unsigned int batchSize = inputInfo.GetShape()[0];
35 const unsigned int channelCount = inputInfo.GetShape()[1];
36
37 const unsigned int inputHeight = inputInfo.GetShape()[2];
38 const unsigned int inputWidth = inputInfo.GetShape()[3];
39 const unsigned int outputHeight = outputInfo.GetShape()[2];
40 const unsigned int outputWidth = outputInfo.GetShape()[3];
41
telsoa01c577f2c2018-08-31 09:22:23 +010042 // How much to scale pixel coordinates in the output image, to get the corresponding pixel coordinates
43 // in the input image.
telsoa014fcda012018-03-09 14:13:49 +000044 const float scaleY = boost::numeric_cast<float>(inputHeight) / boost::numeric_cast<float>(outputHeight);
45 const float scaleX = boost::numeric_cast<float>(inputWidth) / boost::numeric_cast<float>(outputWidth);
46
47 TensorBufferArrayView<const float> input(inputInfo.GetShape(), in);
48 TensorBufferArrayView<float> output(outputInfo.GetShape(), out);
49
50 for (unsigned int n = 0; n < batchSize; ++n)
51 {
52 for (unsigned int c = 0; c < channelCount; ++c)
53 {
54 for (unsigned int y = 0; y < outputHeight; ++y)
55 {
telsoa01c577f2c2018-08-31 09:22:23 +010056 // Corresponding real-valued height coordinate in input image.
telsoa014fcda012018-03-09 14:13:49 +000057 const float iy = boost::numeric_cast<float>(y) * scaleY;
58
telsoa01c577f2c2018-08-31 09:22:23 +010059 // Discrete height coordinate of top-left texel (in the 2x2 texel area used for interpolation).
telsoa014fcda012018-03-09 14:13:49 +000060 const float fiy = floorf(iy);
61 const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy);
62
telsoa01c577f2c2018-08-31 09:22:23 +010063 // Interpolation weight (range [0,1]).
telsoa014fcda012018-03-09 14:13:49 +000064 const float yw = iy - fiy;
65
66 for (unsigned int x = 0; x < outputWidth; ++x)
67 {
telsoa01c577f2c2018-08-31 09:22:23 +010068 // Real-valued and discrete width coordinates in input image.
telsoa014fcda012018-03-09 14:13:49 +000069 const float ix = boost::numeric_cast<float>(x) * scaleX;
70 const float fix = floorf(ix);
71 const unsigned int x0 = boost::numeric_cast<unsigned int>(fix);
72
telsoa01c577f2c2018-08-31 09:22:23 +010073 // Interpolation weight (range [0,1]).
telsoa014fcda012018-03-09 14:13:49 +000074 const float xw = ix - fix;
75
telsoa01c577f2c2018-08-31 09:22:23 +010076 // Discrete width/height coordinates of texels below and to the right of (x0, y0).
telsoa014fcda012018-03-09 14:13:49 +000077 const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u);
78 const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u);
79
80 // Interpolation
telsoa01c577f2c2018-08-31 09:22:23 +010081 const float ly0 = Lerp(input.Get(n, c, y0, x0), input.Get(n, c, y0, x1), xw); // lerp along row y0.
82 const float ly1 = Lerp(input.Get(n, c, y1, x0), input.Get(n, c, y1, x1), xw); // lerp along row y1.
telsoa014fcda012018-03-09 14:13:49 +000083 const float l = Lerp(ly0, ly1, yw);
84
85 output.Get(n, c, y, x) = l;
86 }
87 }
88 }
89 }
90}
91
92} //namespace armnn