telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 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 Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 15 | using namespace armnnUtils; |
| 16 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | inline float Lerp(float a, float b, float w) |
| 24 | { |
| 25 | return w * b + (1.f - w) * a; |
| 26 | } |
| 27 | |
| 28 | } |
| 29 | |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 30 | void ResizeBilinear(const float* in, |
| 31 | const TensorInfo& inputInfo, |
| 32 | float* out, |
| 33 | const TensorInfo& outputInfo, |
| 34 | DataLayoutIndexed dataLayout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | // We follow the definition of TensorFlow and AndroidNN: the top-left corner of a texel in the output |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | // 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 Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 41 | const unsigned int channelCount = inputInfo.GetShape()[dataLayout.GetChannelsIndex()]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 42 | |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 43 | 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()]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | // How much to scale pixel coordinates in the output image, to get the corresponding pixel coordinates |
| 49 | // in the input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | 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 Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 53 | TensorBufferArrayView<const float> input(inputInfo.GetShape(), in, dataLayout); |
| 54 | TensorBufferArrayView<float> output(outputInfo.GetShape(), out, dataLayout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 55 | |
| 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 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | // Corresponding real-valued height coordinate in input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | const float iy = boost::numeric_cast<float>(y) * scaleY; |
| 64 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | // Discrete height coordinate of top-left texel (in the 2x2 texel area used for interpolation). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 66 | const float fiy = floorf(iy); |
| 67 | const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy); |
| 68 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | // Interpolation weight (range [0,1]). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 70 | const float yw = iy - fiy; |
| 71 | |
| 72 | for (unsigned int x = 0; x < outputWidth; ++x) |
| 73 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | // Real-valued and discrete width coordinates in input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 75 | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 79 | // Interpolation weight (range [0,1]). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | const float xw = ix - fix; |
| 81 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 82 | // Discrete width/height coordinates of texels below and to the right of (x0, y0). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u); |
| 84 | const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u); |
| 85 | |
| 86 | // Interpolation |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 87 | 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. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 89 | 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 |