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 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | |
| 18 | namespace |
| 19 | { |
| 20 | |
| 21 | inline float Lerp(float a, float b, float w) |
| 22 | { |
| 23 | return w * b + (1.f - w) * a; |
| 24 | } |
| 25 | |
| 26 | } |
| 27 | |
| 28 | void ResizeBilinear(const float* in, const TensorInfo& inputInfo, float* out, const TensorInfo& outputInfo) |
| 29 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | // 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] | 31 | // 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | // How much to scale pixel coordinates in the output image, to get the corresponding pixel coordinates |
| 43 | // in the input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | 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 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | // Corresponding real-valued height coordinate in input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 57 | const float iy = boost::numeric_cast<float>(y) * scaleY; |
| 58 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | // 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] | 60 | const float fiy = floorf(iy); |
| 61 | const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy); |
| 62 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | // Interpolation weight (range [0,1]). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 64 | const float yw = iy - fiy; |
| 65 | |
| 66 | for (unsigned int x = 0; x < outputWidth; ++x) |
| 67 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | // Real-valued and discrete width coordinates in input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 69 | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | // Interpolation weight (range [0,1]). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 74 | const float xw = ix - fix; |
| 75 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 76 | // 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] | 77 | const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u); |
| 78 | const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u); |
| 79 | |
| 80 | // Interpolation |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | 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. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | 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 |