From 0280785b00cd84f14249d3545a15b93627acf57b Mon Sep 17 00:00:00 2001 From: Narumol Prangnawarat Date: Wed, 11 Sep 2019 16:43:09 +0100 Subject: IVGCVSW-3663 Add utility function to expand tensor dimension Signed-off-by: Narumol Prangnawarat Change-Id: I54d2416be305db7d3fc15fb3123694ad6740b27d --- CMakeLists.txt | 1 + src/armnnUtils/TensorUtils.cpp | 32 ++++++++ src/armnnUtils/TensorUtils.hpp | 2 + src/armnnUtils/test/TensorUtilsTest.cpp | 137 ++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 src/armnnUtils/test/TensorUtilsTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f8ff91efcd..e92dc7e90c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -550,6 +550,7 @@ if(BUILD_UNIT_TESTS) src/armnn/test/UtilsTests.cpp src/armnnUtils/test/PrototxtConversionsTest.cpp src/armnnUtils/test/ParserHelperTest.cpp + src/armnnUtils/test/TensorUtilsTest.cpp src/profiling/test/ProfilingTests.cpp src/profiling/test/SendCounterPacketTests.cpp ) diff --git a/src/armnnUtils/TensorUtils.cpp b/src/armnnUtils/TensorUtils.cpp index c2fbbe0bcc..8baea78ab5 100644 --- a/src/armnnUtils/TensorUtils.cpp +++ b/src/armnnUtils/TensorUtils.cpp @@ -6,6 +6,10 @@ #include "TensorUtils.hpp" #include +#include +#include +#include + namespace armnnUtils { @@ -75,4 +79,32 @@ std::pair FindMinMax(armnn::ITensorHandle* tensorHandle) return std::make_pair(min, max); } +armnn::TensorShape ExpandDims(const armnn::TensorShape& tensorShape, int axis) +{ + unsigned int outputDim = tensorShape.GetNumDimensions() + 1; + + if (axis < -boost::numeric_cast(outputDim) || axis > boost::numeric_cast(tensorShape.GetNumDimensions())) + { + throw armnn::InvalidArgumentException( + boost::str(boost::format("Invalid expansion axis %1% for %2%D input tensor. %3%") % + axis % + tensorShape.GetNumDimensions() % + CHECK_LOCATION().AsString())); + } + + if (axis < 0) + { + axis = boost::numeric_cast(outputDim) + axis; + } + + std::vector outputShape; + for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); ++i) + { + outputShape.push_back(tensorShape[i]); + } + outputShape.insert(outputShape.begin() + axis, 1); + + return armnn::TensorShape(outputDim, outputShape.data()); +} + } diff --git a/src/armnnUtils/TensorUtils.hpp b/src/armnnUtils/TensorUtils.hpp index c273b497b3..03b1c8a2df 100644 --- a/src/armnnUtils/TensorUtils.hpp +++ b/src/armnnUtils/TensorUtils.hpp @@ -24,4 +24,6 @@ armnn::TensorInfo GetTensorInfo(unsigned int numberOfBatches, std::pair FindMinMax(armnn::ITensorHandle* tensorHandle); +armnn::TensorShape ExpandDims(const armnn::TensorShape& tensorShape, int axis); + } // namespace armnnUtils diff --git a/src/armnnUtils/test/TensorUtilsTest.cpp b/src/armnnUtils/test/TensorUtilsTest.cpp new file mode 100644 index 0000000000..a903b63f83 --- /dev/null +++ b/src/armnnUtils/test/TensorUtilsTest.cpp @@ -0,0 +1,137 @@ +// +// Copyright © 2019 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include + +#include + +#include + +using namespace armnn; +using namespace armnnUtils; + +BOOST_AUTO_TEST_SUITE(TensorUtilsSuite) + +BOOST_AUTO_TEST_CASE(ExpandDimsAxis0Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension 0 + armnn::TensorShape outputShape = ExpandDims(inputShape, 0); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 1); + BOOST_TEST(outputShape[1] == 2); + BOOST_TEST(outputShape[2] == 3); + BOOST_TEST(outputShape[3] == 4); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsAxis1Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension 1 + armnn::TensorShape outputShape = ExpandDims(inputShape, 1); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 2); + BOOST_TEST(outputShape[1] == 1); + BOOST_TEST(outputShape[2] == 3); + BOOST_TEST(outputShape[3] == 4); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsAxis2Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension 2 + armnn::TensorShape outputShape = ExpandDims(inputShape, 2); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 2); + BOOST_TEST(outputShape[1] == 3); + BOOST_TEST(outputShape[2] == 1); + BOOST_TEST(outputShape[3] == 4); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsAxis3Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension 3 + armnn::TensorShape outputShape = ExpandDims(inputShape, 3); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 2); + BOOST_TEST(outputShape[1] == 3); + BOOST_TEST(outputShape[2] == 4); + BOOST_TEST(outputShape[3] == 1); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsNegativeAxis1Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension -1 + armnn::TensorShape outputShape = ExpandDims(inputShape, -1); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 2); + BOOST_TEST(outputShape[1] == 3); + BOOST_TEST(outputShape[2] == 4); + BOOST_TEST(outputShape[3] == 1); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsNegativeAxis2Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension -2 + armnn::TensorShape outputShape = ExpandDims(inputShape, -2); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 2); + BOOST_TEST(outputShape[1] == 3); + BOOST_TEST(outputShape[2] == 1); + BOOST_TEST(outputShape[3] == 4); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsNegativeAxis3Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension -3 + armnn::TensorShape outputShape = ExpandDims(inputShape, -3); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 2); + BOOST_TEST(outputShape[1] == 1); + BOOST_TEST(outputShape[2] == 3); + BOOST_TEST(outputShape[3] == 4); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsNegativeAxis4Test) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Expand dimension -4 + armnn::TensorShape outputShape = ExpandDims(inputShape, -4); + BOOST_TEST(outputShape.GetNumDimensions() == 4); + BOOST_TEST(outputShape[0] == 1); + BOOST_TEST(outputShape[1] == 2); + BOOST_TEST(outputShape[2] == 3); + BOOST_TEST(outputShape[3] == 4); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsInvalidAxisTest) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Invalid expand dimension 4 + BOOST_CHECK_THROW(ExpandDims(inputShape, 4), armnn::InvalidArgumentException); +} + +BOOST_AUTO_TEST_CASE(ExpandDimsInvalidNegativeAxisTest) +{ + armnn::TensorShape inputShape({ 2, 3, 4 }); + + // Invalid expand dimension -5 + BOOST_CHECK_THROW(ExpandDims(inputShape, -5), armnn::InvalidArgumentException); +} + +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.1