From da1fb9b01ce9545b375c61a3bf5a5337199834a7 Mon Sep 17 00:00:00 2001 From: Teresa Charlin Date: Tue, 2 Jul 2019 13:25:22 +0100 Subject: IVGCVSW-3161 Bug Fix in Nearest Neighbor implementation; consider 4 neighbors instead of 2. Signed-off-by: Teresa Charlin Change-Id: Ia724b35f723157e9f51b5345964200f126b98267 --- src/backends/reference/workloads/Resize.cpp | 51 ++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) (limited to 'src/backends/reference') diff --git a/src/backends/reference/workloads/Resize.cpp b/src/backends/reference/workloads/Resize.cpp index 0e0bdd7597..3050bae870 100644 --- a/src/backends/reference/workloads/Resize.cpp +++ b/src/backends/reference/workloads/Resize.cpp @@ -25,6 +25,11 @@ inline float Lerp(float a, float b, float w) return w * b + (1.f - w) * a; } +inline double EuclideanDistance(float Xa, float Ya, const unsigned int Xb, const unsigned int Yb) +{ + return std::sqrt(pow(Xa - boost::numeric_cast(Xb), 2) + pow(Ya - boost::numeric_cast(Yb), 2)); +} + }// anonymous namespace void Resize(Decoder& in, @@ -104,20 +109,50 @@ void Resize(Decoder& in, break; } case armnn::ResizeMethod::NearestNeighbor: - default: { - auto distance0 = std::sqrt(pow(fix - boost::numeric_cast(x0), 2) + - pow(fiy - boost::numeric_cast(y0), 2)); - auto distance1 = std::sqrt(pow(fix - boost::numeric_cast(x1), 2) + - pow(fiy - boost::numeric_cast(y1), 2)); - - unsigned int xNearest = distance0 <= distance1? x0 : x1; - unsigned int yNearest = distance0 <= distance1? y0 : y1; + // calculate euclidean distance to the 4 neighbours + auto distance00 = EuclideanDistance(fix, fiy, x0, y0); + auto distance01 = EuclideanDistance(fix, fiy, x0, y1); + auto distance10 = EuclideanDistance(fix, fiy, x1, y0); + auto distance11 = EuclideanDistance(fix, fiy, x1, y1); + + auto minimum = std::min( { distance00, distance01, distance10, distance11 } ); + + unsigned int xNearest = 0; + unsigned int yNearest = 0; + + if (minimum == distance00) + { + xNearest = x0; + yNearest = y0; + } + else if (minimum == distance01) + { + xNearest = x0; + yNearest = y1; + } + else if (minimum == distance10) + { + xNearest = x1; + yNearest = y0; + } + else if (minimum == distance11) + { + xNearest = x1; + yNearest = y1; + } + else + { + throw armnn::InvalidArgumentException("Resize Nearest Neighbor failure"); + } in[dataLayout.GetIndex(inputShape, n, c, yNearest, xNearest)]; interpolatedValue = in.Get(); break; } + default: + throw armnn::InvalidArgumentException("Unknown resize method: " + + std::to_string(static_cast(resizeMethod))); } out[dataLayout.GetIndex(outputShape, n, c, y, x)]; out.Set(interpolatedValue); -- cgit v1.2.1