blob: 2d1087c9a0736da92fb0fbe8c1af804c99625c72 [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
Matteo Martincigh21350152018-11-28 16:22:22 +000015using namespace armnnUtils;
16
telsoa014fcda012018-03-09 14:13:49 +000017namespace armnn
18{
19
20namespace
21{
22
23inline float Lerp(float a, float b, float w)
24{
25 return w * b + (1.f - w) * a;
26}
27
28}
29
James Conroy59540822018-10-11 12:39:05 +010030void ResizeBilinear(const float* in,
31 const TensorInfo& inputInfo,
32 float* out,
33 const TensorInfo& outputInfo,
34 DataLayoutIndexed dataLayout)
telsoa014fcda012018-03-09 14:13:49 +000035{
telsoa01c577f2c2018-08-31 09:22:23 +010036 // We follow the definition of TensorFlow and AndroidNN: the top-left corner of a texel in the output
telsoa014fcda012018-03-09 14:13:49 +000037 // image is projected into the input image to figure out the interpolants and weights. Note that this
38 // will yield different results than if projecting the centre of output texels.
39
40 const unsigned int batchSize = inputInfo.GetShape()[0];
James Conroy59540822018-10-11 12:39:05 +010041 const unsigned int channelCount = inputInfo.GetShape()[dataLayout.GetChannelsIndex()];
telsoa014fcda012018-03-09 14:13:49 +000042
James Conroy59540822018-10-11 12:39:05 +010043 const unsigned int inputHeight = inputInfo.GetShape()[dataLayout.GetHeightIndex()];
44 const unsigned int inputWidth = inputInfo.GetShape()[dataLayout.GetWidthIndex()];
45 const unsigned int outputHeight = outputInfo.GetShape()[dataLayout.GetHeightIndex()];
46 const unsigned int outputWidth = outputInfo.GetShape()[dataLayout.GetWidthIndex()];
telsoa014fcda012018-03-09 14:13:49 +000047
telsoa01c577f2c2018-08-31 09:22:23 +010048 // How much to scale pixel coordinates in the output image, to get the corresponding pixel coordinates
49 // in the input image.
telsoa014fcda012018-03-09 14:13:49 +000050 const float scaleY = boost::numeric_cast<float>(inputHeight) / boost::numeric_cast<float>(outputHeight);
51 const float scaleX = boost::numeric_cast<float>(inputWidth) / boost::numeric_cast<float>(outputWidth);
52
James Conroy59540822018-10-11 12:39:05 +010053 TensorBufferArrayView<const float> input(inputInfo.GetShape(), in, dataLayout);
54 TensorBufferArrayView<float> output(outputInfo.GetShape(), out, dataLayout);
telsoa014fcda012018-03-09 14:13:49 +000055
56 for (unsigned int n = 0; n < batchSize; ++n)
57 {
58 for (unsigned int c = 0; c < channelCount; ++c)
59 {
60 for (unsigned int y = 0; y < outputHeight; ++y)
61 {
telsoa01c577f2c2018-08-31 09:22:23 +010062 // Corresponding real-valued height coordinate in input image.
telsoa014fcda012018-03-09 14:13:49 +000063 const float iy = boost::numeric_cast<float>(y) * scaleY;
64
telsoa01c577f2c2018-08-31 09:22:23 +010065 // Discrete height coordinate of top-left texel (in the 2x2 texel area used for interpolation).
telsoa014fcda012018-03-09 14:13:49 +000066 const float fiy = floorf(iy);
67 const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy);
68
telsoa01c577f2c2018-08-31 09:22:23 +010069 // Interpolation weight (range [0,1]).
telsoa014fcda012018-03-09 14:13:49 +000070 const float yw = iy - fiy;
71
72 for (unsigned int x = 0; x < outputWidth; ++x)
73 {
telsoa01c577f2c2018-08-31 09:22:23 +010074 // Real-valued and discrete width coordinates in input image.
telsoa014fcda012018-03-09 14:13:49 +000075 const float ix = boost::numeric_cast<float>(x) * scaleX;
76 const float fix = floorf(ix);
77 const unsigned int x0 = boost::numeric_cast<unsigned int>(fix);
78
telsoa01c577f2c2018-08-31 09:22:23 +010079 // Interpolation weight (range [0,1]).
telsoa014fcda012018-03-09 14:13:49 +000080 const float xw = ix - fix;
81
telsoa01c577f2c2018-08-31 09:22:23 +010082 // Discrete width/height coordinates of texels below and to the right of (x0, y0).
telsoa014fcda012018-03-09 14:13:49 +000083 const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u);
84 const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u);
85
86 // Interpolation
telsoa01c577f2c2018-08-31 09:22:23 +010087 const float ly0 = Lerp(input.Get(n, c, y0, x0), input.Get(n, c, y0, x1), xw); // lerp along row y0.
88 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 +000089 const float l = Lerp(ly0, ly1, yw);
90
91 output.Get(n, c, y, x) = l;
92 }
93 }
94 }
95 }
96}
97
98} //namespace armnn