blob: e098c6c20d3f786036d473642af30fcef1467ebd [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
James Conroy59540822018-10-11 12:39:05 +010028void ResizeBilinear(const float* in,
29 const TensorInfo& inputInfo,
30 float* out,
31 const TensorInfo& outputInfo,
32 DataLayoutIndexed dataLayout)
telsoa014fcda012018-03-09 14:13:49 +000033{
telsoa01c577f2c2018-08-31 09:22:23 +010034 // We follow the definition of TensorFlow and AndroidNN: the top-left corner of a texel in the output
telsoa014fcda012018-03-09 14:13:49 +000035 // image is projected into the input image to figure out the interpolants and weights. Note that this
36 // will yield different results than if projecting the centre of output texels.
37
38 const unsigned int batchSize = inputInfo.GetShape()[0];
James Conroy59540822018-10-11 12:39:05 +010039 const unsigned int channelCount = inputInfo.GetShape()[dataLayout.GetChannelsIndex()];
telsoa014fcda012018-03-09 14:13:49 +000040
James Conroy59540822018-10-11 12:39:05 +010041 const unsigned int inputHeight = inputInfo.GetShape()[dataLayout.GetHeightIndex()];
42 const unsigned int inputWidth = inputInfo.GetShape()[dataLayout.GetWidthIndex()];
43 const unsigned int outputHeight = outputInfo.GetShape()[dataLayout.GetHeightIndex()];
44 const unsigned int outputWidth = outputInfo.GetShape()[dataLayout.GetWidthIndex()];
telsoa014fcda012018-03-09 14:13:49 +000045
telsoa01c577f2c2018-08-31 09:22:23 +010046 // How much to scale pixel coordinates in the output image, to get the corresponding pixel coordinates
47 // in the input image.
telsoa014fcda012018-03-09 14:13:49 +000048 const float scaleY = boost::numeric_cast<float>(inputHeight) / boost::numeric_cast<float>(outputHeight);
49 const float scaleX = boost::numeric_cast<float>(inputWidth) / boost::numeric_cast<float>(outputWidth);
50
James Conroy59540822018-10-11 12:39:05 +010051 TensorBufferArrayView<const float> input(inputInfo.GetShape(), in, dataLayout);
52 TensorBufferArrayView<float> output(outputInfo.GetShape(), out, dataLayout);
telsoa014fcda012018-03-09 14:13:49 +000053
54 for (unsigned int n = 0; n < batchSize; ++n)
55 {
56 for (unsigned int c = 0; c < channelCount; ++c)
57 {
58 for (unsigned int y = 0; y < outputHeight; ++y)
59 {
telsoa01c577f2c2018-08-31 09:22:23 +010060 // Corresponding real-valued height coordinate in input image.
telsoa014fcda012018-03-09 14:13:49 +000061 const float iy = boost::numeric_cast<float>(y) * scaleY;
62
telsoa01c577f2c2018-08-31 09:22:23 +010063 // Discrete height coordinate of top-left texel (in the 2x2 texel area used for interpolation).
telsoa014fcda012018-03-09 14:13:49 +000064 const float fiy = floorf(iy);
65 const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy);
66
telsoa01c577f2c2018-08-31 09:22:23 +010067 // Interpolation weight (range [0,1]).
telsoa014fcda012018-03-09 14:13:49 +000068 const float yw = iy - fiy;
69
70 for (unsigned int x = 0; x < outputWidth; ++x)
71 {
telsoa01c577f2c2018-08-31 09:22:23 +010072 // Real-valued and discrete width coordinates in input image.
telsoa014fcda012018-03-09 14:13:49 +000073 const float ix = boost::numeric_cast<float>(x) * scaleX;
74 const float fix = floorf(ix);
75 const unsigned int x0 = boost::numeric_cast<unsigned int>(fix);
76
telsoa01c577f2c2018-08-31 09:22:23 +010077 // Interpolation weight (range [0,1]).
telsoa014fcda012018-03-09 14:13:49 +000078 const float xw = ix - fix;
79
telsoa01c577f2c2018-08-31 09:22:23 +010080 // Discrete width/height coordinates of texels below and to the right of (x0, y0).
telsoa014fcda012018-03-09 14:13:49 +000081 const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u);
82 const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u);
83
84 // Interpolation
telsoa01c577f2c2018-08-31 09:22:23 +010085 const float ly0 = Lerp(input.Get(n, c, y0, x0), input.Get(n, c, y0, x1), xw); // lerp along row y0.
86 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 +000087 const float l = Lerp(ly0, ly1, yw);
88
89 output.Get(n, c, y, x) = l;
90 }
91 }
92 }
93 }
94}
95
96} //namespace armnn