blob: 70a051492a7299719648b97cfada2f4037c37667 [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
Ellen Norris-Thompson719d2a92019-06-12 10:23:57 +010030void ResizeBilinear(Decoder<float>& in,
James Conroy59540822018-10-11 12:39:05 +010031 const TensorInfo& inputInfo,
Ellen Norris-Thompson719d2a92019-06-12 10:23:57 +010032 Encoder<float>& out,
James Conroy59540822018-10-11 12:39:05 +010033 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
Ellen Norris-Thompson719d2a92019-06-12 10:23:57 +010053 TensorShape inputShape = inputInfo.GetShape();
54 TensorShape outputShape = outputInfo.GetShape();
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
Ellen Norris-Thompson719d2a92019-06-12 10:23:57 +010087 in[dataLayout.GetIndex(inputShape, n, c, y0, x0)];
88 float input1 = in.Get();
89 in[dataLayout.GetIndex(inputShape, n, c, y0, x1)];
90 float input2 = in.Get();
91 in[dataLayout.GetIndex(inputShape, n, c, y1, x0)];
92 float input3 = in.Get();
93 in[dataLayout.GetIndex(inputShape, n, c, y1, x1)];
94 float input4 = in.Get();
95
96 const float ly0 = Lerp(input1, input2, xw); // lerp along row y0.
97 const float ly1 = Lerp(input3, input4, xw); // lerp along row y1.
telsoa014fcda012018-03-09 14:13:49 +000098 const float l = Lerp(ly0, ly1, yw);
99
Ellen Norris-Thompson719d2a92019-06-12 10:23:57 +0100100 out[dataLayout.GetIndex(outputShape, n, c, y, x)];
101 out.Set(l);
telsoa014fcda012018-03-09 14:13:49 +0000102 }
103 }
104 }
105 }
106}
107
108} //namespace armnn