IVGCVSW-3365 Add reference workload support for ResizeLayer

Signed-off-by: Teresa Charlin <teresa.charlinreyes@arm.com>
Change-Id: Id551690065dca0686ce597d1f0c14fd73163481e
diff --git a/src/backends/reference/workloads/CMakeLists.txt b/src/backends/reference/workloads/CMakeLists.txt
index 9be245b..3c0af01 100644
--- a/src/backends/reference/workloads/CMakeLists.txt
+++ b/src/backends/reference/workloads/CMakeLists.txt
@@ -96,6 +96,8 @@
     RefReshapeWorkload.hpp
     RefResizeBilinearWorkload.cpp
     RefResizeBilinearWorkload.hpp
+    RefResizeWorkload.cpp
+    RefResizeWorkload.hpp
     RefRsqrtWorkload.cpp
     RefRsqrtWorkload.hpp
     RefSoftmaxWorkload.cpp
@@ -112,8 +114,8 @@
     RefTransposeConvolution2dWorkload.hpp
     RefWorkloads.hpp
     RefWorkloadUtils.hpp
-    ResizeBilinear.cpp
-    ResizeBilinear.hpp
+    Resize.cpp
+    Resize.hpp
     Rsqrt.cpp
     Rsqrt.hpp
     Softmax.cpp
diff --git a/src/backends/reference/workloads/RefResizeBilinearWorkload.cpp b/src/backends/reference/workloads/RefResizeBilinearWorkload.cpp
index 03fcec2..fc27c0f 100644
--- a/src/backends/reference/workloads/RefResizeBilinearWorkload.cpp
+++ b/src/backends/reference/workloads/RefResizeBilinearWorkload.cpp
@@ -6,7 +6,7 @@
 #include "RefResizeBilinearWorkload.hpp"
 
 #include "RefWorkloadUtils.hpp"
-#include "ResizeBilinear.hpp"
+#include "Resize.hpp"
 #include "BaseIterator.hpp"
 #include "Profiling.hpp"
 
@@ -29,7 +29,7 @@
     std::unique_ptr<Encoder<float>> encoderPtr = MakeEncoder<float>(outputInfo, m_Data.m_Outputs[0]->Map());
     Encoder<float> &encoder = *encoderPtr;
 
-    ResizeBilinear(decoder, inputInfo, encoder, outputInfo, m_Data.m_Parameters.m_DataLayout);
+    Resize(decoder, inputInfo, encoder, outputInfo, m_Data.m_Parameters.m_DataLayout, armnn::ResizeMethod::Bilinear);
 }
 
 } //namespace armnn
diff --git a/src/backends/reference/workloads/RefResizeWorkload.cpp b/src/backends/reference/workloads/RefResizeWorkload.cpp
new file mode 100644
index 0000000..26225f8
--- /dev/null
+++ b/src/backends/reference/workloads/RefResizeWorkload.cpp
@@ -0,0 +1,35 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "RefResizeWorkload.hpp"
+
+#include "RefWorkloadUtils.hpp"
+#include "Resize.hpp"
+#include "BaseIterator.hpp"
+#include "Profiling.hpp"
+
+#include "BaseIterator.hpp"
+#include "Decoders.hpp"
+#include "Encoders.hpp"
+
+namespace armnn
+{
+
+void RefResizeWorkload::Execute() const
+{
+    ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefResizeWorkload_Execute");
+
+    const TensorInfo& inputInfo = GetTensorInfo(m_Data.m_Inputs[0]);
+    const TensorInfo& outputInfo = GetTensorInfo(m_Data.m_Outputs[0]);
+
+    std::unique_ptr<Decoder<float>> decoderPtr = MakeDecoder<float>(inputInfo, m_Data.m_Inputs[0]->Map());
+    Decoder<float> &decoder = *decoderPtr;
+    std::unique_ptr<Encoder<float>> encoderPtr = MakeEncoder<float>(outputInfo, m_Data.m_Outputs[0]->Map());
+    Encoder<float> &encoder = *encoderPtr;
+
+    Resize(decoder, inputInfo, encoder, outputInfo, m_Data.m_Parameters.m_DataLayout, m_Data.m_Parameters.m_Method);
+}
+
+} //namespace armnn
diff --git a/src/backends/reference/workloads/RefResizeWorkload.hpp b/src/backends/reference/workloads/RefResizeWorkload.hpp
new file mode 100644
index 0000000..1ddfcdf
--- /dev/null
+++ b/src/backends/reference/workloads/RefResizeWorkload.hpp
@@ -0,0 +1,21 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include <backendsCommon/Workload.hpp>
+#include <backendsCommon/WorkloadData.hpp>
+
+namespace armnn
+{
+
+class RefResizeWorkload : public BaseWorkload<ResizeQueueDescriptor>
+{
+public:
+    using BaseWorkload<ResizeQueueDescriptor>::BaseWorkload;
+    virtual void Execute() const override;
+};
+
+} //namespace armnn
diff --git a/src/backends/reference/workloads/RefWorkloads.hpp b/src/backends/reference/workloads/RefWorkloads.hpp
index 3a094c8..4bdf05d 100644
--- a/src/backends/reference/workloads/RefWorkloads.hpp
+++ b/src/backends/reference/workloads/RefWorkloads.hpp
@@ -40,6 +40,7 @@
 #include "RefPreluWorkload.hpp"
 #include "RefQuantizeWorkload.hpp"
 #include "RefResizeBilinearWorkload.hpp"
+#include "RefResizeWorkload.hpp"
 #include "RefRsqrtWorkload.hpp"
 #include "RefReshapeWorkload.hpp"
 #include "RefSplitterWorkload.hpp"
@@ -49,7 +50,7 @@
 #include "RefSpaceToDepthWorkload.hpp"
 #include "RefTransposeConvolution2dWorkload.hpp"
 #include "RefWorkloadUtils.hpp"
-#include "ResizeBilinear.hpp"
+#include "Resize.hpp"
 #include "Softmax.hpp"
 #include "Splitter.hpp"
 #include "TensorBufferArrayView.hpp"
\ No newline at end of file
diff --git a/src/backends/reference/workloads/Resize.cpp b/src/backends/reference/workloads/Resize.cpp
new file mode 100644
index 0000000..0e0bdd7
--- /dev/null
+++ b/src/backends/reference/workloads/Resize.cpp
@@ -0,0 +1,130 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "Resize.hpp"
+
+#include "TensorBufferArrayView.hpp"
+
+#include <boost/numeric/conversion/cast.hpp>
+
+#include <cmath>
+#include <algorithm>
+
+using namespace armnnUtils;
+
+namespace armnn
+{
+
+namespace
+{
+
+inline float Lerp(float a, float b, float w)
+{
+    return w * b + (1.f - w) * a;
+}
+
+}// anonymous namespace
+
+void Resize(Decoder<float>&   in,
+            const TensorInfo& inputInfo,
+            Encoder<float>&   out,
+            const TensorInfo& outputInfo,
+            DataLayoutIndexed dataLayout,
+            armnn::ResizeMethod resizeMethod)
+{
+    // We follow the definition of TensorFlow and AndroidNN: the top-left corner of a texel in the output
+    // image is projected into the input image to figure out the interpolants and weights. Note that this
+    // will yield different results than if projecting the centre of output texels.
+
+    const unsigned int batchSize = inputInfo.GetShape()[0];
+    const unsigned int channelCount = inputInfo.GetShape()[dataLayout.GetChannelsIndex()];
+
+    const unsigned int inputHeight = inputInfo.GetShape()[dataLayout.GetHeightIndex()];
+    const unsigned int inputWidth = inputInfo.GetShape()[dataLayout.GetWidthIndex()];
+    const unsigned int outputHeight = outputInfo.GetShape()[dataLayout.GetHeightIndex()];
+    const unsigned int outputWidth = outputInfo.GetShape()[dataLayout.GetWidthIndex()];
+
+    // How much to scale pixel coordinates in the output image, to get the corresponding pixel coordinates
+    // in the input image.
+    const float scaleY = boost::numeric_cast<float>(inputHeight) / boost::numeric_cast<float>(outputHeight);
+    const float scaleX = boost::numeric_cast<float>(inputWidth) / boost::numeric_cast<float>(outputWidth);
+
+    TensorShape inputShape =  inputInfo.GetShape();
+    TensorShape outputShape =  outputInfo.GetShape();
+
+    for (unsigned int n = 0; n < batchSize; ++n)
+    {
+        for (unsigned int c = 0; c < channelCount; ++c)
+        {
+            for (unsigned int y = 0; y < outputHeight; ++y)
+            {
+                // Corresponding real-valued height coordinate in input image.
+                const float iy = boost::numeric_cast<float>(y) * scaleY;
+
+                // Discrete height coordinate of top-left texel (in the 2x2 texel area used for interpolation).
+                const float fiy = floorf(iy);
+                const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy);
+
+                // Interpolation weight (range [0,1]).
+                const float yw = iy - fiy;
+
+                for (unsigned int x = 0; x < outputWidth; ++x)
+                {
+                    // Real-valued and discrete width coordinates in input image.
+                    const float ix = boost::numeric_cast<float>(x) * scaleX;
+                    const float fix = floorf(ix);
+                    const unsigned int x0 = boost::numeric_cast<unsigned int>(fix);
+
+                    // Interpolation weight (range [0,1]).
+                    const float xw = ix - fix;
+
+                    // Discrete width/height coordinates of texels below and to the right of (x0, y0).
+                    const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u);
+                    const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u);
+
+                    float interpolatedValue;
+                    switch (resizeMethod)
+                    {
+                        case armnn::ResizeMethod::Bilinear:
+                        {
+                            in[dataLayout.GetIndex(inputShape, n, c, y0, x0)];
+                            float input1 = in.Get();
+                            in[dataLayout.GetIndex(inputShape, n, c, y0, x1)];
+                            float input2 = in.Get();
+                            in[dataLayout.GetIndex(inputShape, n, c, y1, x0)];
+                            float input3 = in.Get();
+                            in[dataLayout.GetIndex(inputShape, n, c, y1, x1)];
+                            float input4 = in.Get();
+
+                            const float ly0 = Lerp(input1, input2, xw); // lerp along row y0.
+                            const float ly1 = Lerp(input3, input4, xw); // lerp along row y1.
+                            interpolatedValue = Lerp(ly0, ly1, yw);
+                            break;
+                        }
+                        case armnn::ResizeMethod::NearestNeighbor:
+                        default:
+                        {
+                            auto distance0 = std::sqrt(pow(fix - boost::numeric_cast<float>(x0), 2) + 
+                                                       pow(fiy - boost::numeric_cast<float>(y0), 2));
+                            auto distance1 = std::sqrt(pow(fix - boost::numeric_cast<float>(x1), 2) +
+                                                       pow(fiy - boost::numeric_cast<float>(y1), 2));
+
+                            unsigned int xNearest = distance0 <= distance1? x0 : x1;
+                            unsigned int yNearest = distance0 <= distance1? y0 : y1;
+
+                            in[dataLayout.GetIndex(inputShape, n, c, yNearest, xNearest)];
+                            interpolatedValue = in.Get();
+                            break;
+                        }
+                    }
+                    out[dataLayout.GetIndex(outputShape, n, c, y, x)];
+                    out.Set(interpolatedValue);
+                }
+            }
+        }
+    }
+}
+
+} //namespace armnn
diff --git a/src/backends/reference/workloads/Resize.hpp b/src/backends/reference/workloads/Resize.hpp
new file mode 100644
index 0000000..8bd8999
--- /dev/null
+++ b/src/backends/reference/workloads/Resize.hpp
@@ -0,0 +1,23 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include "BaseIterator.hpp"
+#include <armnn/Tensor.hpp>
+
+#include <DataLayoutIndexed.hpp>
+
+namespace armnn
+{
+
+void Resize(Decoder<float>&               in,
+            const TensorInfo&             inputInfo,
+            Encoder<float>&               out,
+            const TensorInfo&             outputInfo,
+            armnnUtils::DataLayoutIndexed dataLayout = DataLayout::NCHW,
+            ResizeMethod                  resizeMethod = ResizeMethod::NearestNeighbor);
+
+} //namespace armnn
diff --git a/src/backends/reference/workloads/ResizeBilinear.cpp b/src/backends/reference/workloads/ResizeBilinear.cpp
deleted file mode 100644
index 70a0514..0000000
--- a/src/backends/reference/workloads/ResizeBilinear.cpp
+++ /dev/null
@@ -1,108 +0,0 @@
-//
-// Copyright © 2017 Arm Ltd. All rights reserved.
-// SPDX-License-Identifier: MIT
-//
-
-#include "ResizeBilinear.hpp"
-
-#include "TensorBufferArrayView.hpp"
-
-#include <boost/numeric/conversion/cast.hpp>
-
-#include <cmath>
-#include <algorithm>
-
-using namespace armnnUtils;
-
-namespace armnn
-{
-
-namespace
-{
-
-inline float Lerp(float a, float b, float w)
-{
-    return w * b + (1.f - w) * a;
-}
-
-}
-
-void ResizeBilinear(Decoder<float>&   in,
-                    const TensorInfo& inputInfo,
-                    Encoder<float>&   out,
-                    const TensorInfo& outputInfo,
-                    DataLayoutIndexed dataLayout)
-{
-    // We follow the definition of TensorFlow and AndroidNN: the top-left corner of a texel in the output
-    // image is projected into the input image to figure out the interpolants and weights. Note that this
-    // will yield different results than if projecting the centre of output texels.
-
-    const unsigned int batchSize = inputInfo.GetShape()[0];
-    const unsigned int channelCount = inputInfo.GetShape()[dataLayout.GetChannelsIndex()];
-
-    const unsigned int inputHeight = inputInfo.GetShape()[dataLayout.GetHeightIndex()];
-    const unsigned int inputWidth = inputInfo.GetShape()[dataLayout.GetWidthIndex()];
-    const unsigned int outputHeight = outputInfo.GetShape()[dataLayout.GetHeightIndex()];
-    const unsigned int outputWidth = outputInfo.GetShape()[dataLayout.GetWidthIndex()];
-
-    // How much to scale pixel coordinates in the output image, to get the corresponding pixel coordinates
-    // in the input image.
-    const float scaleY = boost::numeric_cast<float>(inputHeight) / boost::numeric_cast<float>(outputHeight);
-    const float scaleX = boost::numeric_cast<float>(inputWidth) / boost::numeric_cast<float>(outputWidth);
-
-    TensorShape inputShape =  inputInfo.GetShape();
-    TensorShape outputShape =  outputInfo.GetShape();
-
-    for (unsigned int n = 0; n < batchSize; ++n)
-    {
-        for (unsigned int c = 0; c < channelCount; ++c)
-        {
-            for (unsigned int y = 0; y < outputHeight; ++y)
-            {
-                // Corresponding real-valued height coordinate in input image.
-                const float iy = boost::numeric_cast<float>(y) * scaleY;
-
-                // Discrete height coordinate of top-left texel (in the 2x2 texel area used for interpolation).
-                const float fiy = floorf(iy);
-                const unsigned int y0 = boost::numeric_cast<unsigned int>(fiy);
-
-                // Interpolation weight (range [0,1]).
-                const float yw = iy - fiy;
-
-                for (unsigned int x = 0; x < outputWidth; ++x)
-                {
-                    // Real-valued and discrete width coordinates in input image.
-                    const float ix = boost::numeric_cast<float>(x) * scaleX;
-                    const float fix = floorf(ix);
-                    const unsigned int x0 = boost::numeric_cast<unsigned int>(fix);
-
-                    // Interpolation weight (range [0,1]).
-                    const float xw = ix - fix;
-
-                    // Discrete width/height coordinates of texels below and to the right of (x0, y0).
-                    const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u);
-                    const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u);
-
-                    // Interpolation
-                    in[dataLayout.GetIndex(inputShape, n, c, y0, x0)];
-                    float input1 = in.Get();
-                    in[dataLayout.GetIndex(inputShape, n, c, y0, x1)];
-                    float input2 = in.Get();
-                    in[dataLayout.GetIndex(inputShape, n, c, y1, x0)];
-                    float input3 = in.Get();
-                    in[dataLayout.GetIndex(inputShape, n, c, y1, x1)];
-                    float input4 = in.Get();
-
-                    const float ly0 = Lerp(input1, input2, xw); // lerp along row y0.
-                    const float ly1 = Lerp(input3, input4, xw); // lerp along row y1.
-                    const float l = Lerp(ly0, ly1, yw);
-
-                    out[dataLayout.GetIndex(outputShape, n, c, y, x)];
-                    out.Set(l);
-                }
-            }
-        }
-    }
-}
-
-} //namespace armnn
diff --git a/src/backends/reference/workloads/ResizeBilinear.hpp b/src/backends/reference/workloads/ResizeBilinear.hpp
deleted file mode 100644
index ad2e487..0000000
--- a/src/backends/reference/workloads/ResizeBilinear.hpp
+++ /dev/null
@@ -1,22 +0,0 @@
-//
-// Copyright © 2017 Arm Ltd. All rights reserved.
-// SPDX-License-Identifier: MIT
-//
-
-#pragma once
-
-#include "BaseIterator.hpp"
-#include <armnn/Tensor.hpp>
-
-#include <DataLayoutIndexed.hpp>
-
-namespace armnn
-{
-
-void ResizeBilinear(Decoder<float>&               in,
-                    const TensorInfo&             inputInfo,
-                    Encoder<float>&               out,
-                    const TensorInfo&             outputInfo,
-                    armnnUtils::DataLayoutIndexed dataLayout = DataLayout::NCHW);
-
-} //namespace armnn