From 9a5f9f2c3b6709003dfb935ecf65150145b9c73b Mon Sep 17 00:00:00 2001 From: Matteo Martincigh Date: Thu, 31 Oct 2019 11:02:47 +0000 Subject: GitHub #292 Build the ML-examples using only ArmNN's public frontend API * Refactoring of some of the public API headers Signed-off-by: Matteo Martincigh Change-Id: I9006fe49945b10a6b83908b05aa4a6556639b491 --- include/armnn/Utils.hpp | 2 -- src/armnn/NetworkQuantizer.cpp | 24 +++++++------- src/armnnUtils/TensorIOUtils.hpp | 67 ++++++++++++++++++++-------------------- src/armnnUtils/TensorUtils.cpp | 45 ++++++++++++++------------- tests/InferenceTestImage.hpp | 6 ++-- 5 files changed, 72 insertions(+), 72 deletions(-) diff --git a/include/armnn/Utils.hpp b/include/armnn/Utils.hpp index d91cc79fc9..26a27f4100 100644 --- a/include/armnn/Utils.hpp +++ b/include/armnn/Utils.hpp @@ -6,8 +6,6 @@ #include "armnn/TypesUtils.hpp" -#include - namespace armnn { diff --git a/src/armnn/NetworkQuantizer.cpp b/src/armnn/NetworkQuantizer.cpp index f308d54d49..4a8bae52f2 100644 --- a/src/armnn/NetworkQuantizer.cpp +++ b/src/armnn/NetworkQuantizer.cpp @@ -3,36 +3,34 @@ // SPDX-License-Identifier: MIT // -#include -#include -#include -#include -#include -#include - +#include "NetworkQuantizer.hpp" +#include "NetworkQuantizerUtils.hpp" #include "Graph.hpp" #include "Layer.hpp" #include "Network.hpp" -#include "NetworkQuantizer.hpp" -#include "NetworkQuantizerUtils.hpp" - #include "DynamicQuantizationVisitor.hpp" #include "StaticRangeVisitor.hpp" #include "QuantizerVisitor.hpp" #include "OverrideInputRangeVisitor.hpp" -#include -#include +#include +#include +#include +#include + +#include +#include #include +#include +#include namespace armnn { using TContainer = boost::variant, std::vector, std::vector>; - INetworkQuantizer* INetworkQuantizer::CreateRaw(INetwork* inputNetwork, const QuantizerOptions& options) { return new NetworkQuantizer(inputNetwork, options); diff --git a/src/armnnUtils/TensorIOUtils.hpp b/src/armnnUtils/TensorIOUtils.hpp index 07f3723279..1dc7f21857 100644 --- a/src/armnnUtils/TensorIOUtils.hpp +++ b/src/armnnUtils/TensorIOUtils.hpp @@ -6,7 +6,6 @@ #pragma once #include -#include #include #include @@ -15,17 +14,18 @@ namespace armnnUtils { template -inline armnn::InputTensors MakeInputTensors( - const std::vector& inputBindings, - const std::vector& inputDataContainers) +inline armnn::InputTensors MakeInputTensors(const std::vector& inputBindings, + const std::vector& inputDataContainers) { armnn::InputTensors inputTensors; const size_t numInputs = inputBindings.size(); if (numInputs != inputDataContainers.size()) { - throw armnn::Exception(boost::str(boost::format("Number of inputs does not match number of " - "tensor data containers: %1% != %2%") % numInputs % inputDataContainers.size())); + throw armnn::Exception(boost::str(boost::format("The number of inputs does not match number of " + "tensor data containers: %1% != %2%") + % numInputs + % inputDataContainers.size())); } for (size_t i = 0; i < numInputs; i++) @@ -34,28 +34,27 @@ inline armnn::InputTensors MakeInputTensors( const TContainer& inputData = inputDataContainers[i]; boost::apply_visitor([&](auto&& value) - { - if (value.size() != inputBinding.second.GetNumElements()) - { - throw armnn::Exception(boost::str(boost::format("Input tensor has incorrect size " - "(expected %1% got %2%)") - % inputBinding.second.GetNumElements() - % value.size())); - } - - armnn::ConstTensor inputTensor(inputBinding.second, value.data()); - inputTensors.push_back(std::make_pair(inputBinding.first, inputTensor)); - }, - inputData); + { + if (value.size() != inputBinding.second.GetNumElements()) + { + throw armnn::Exception(boost::str(boost::format("The input tensor has incorrect size " + "(expected %1% got %2%)") + % inputBinding.second.GetNumElements() + % value.size())); + } + + armnn::ConstTensor inputTensor(inputBinding.second, value.data()); + inputTensors.push_back(std::make_pair(inputBinding.first, inputTensor)); + }, + inputData); } return inputTensors; } template -inline armnn::OutputTensors MakeOutputTensors( - const std::vector& outputBindings, - std::vector& outputDataContainers) +inline armnn::OutputTensors MakeOutputTensors(const std::vector& outputBindings, + std::vector& outputDataContainers) { armnn::OutputTensors outputTensors; @@ -63,7 +62,9 @@ inline armnn::OutputTensors MakeOutputTensors( if (numOutputs != outputDataContainers.size()) { throw armnn::Exception(boost::str(boost::format("Number of outputs does not match number of " - "tensor data containers: %1% != %2%") % numOutputs % outputDataContainers.size())); + "tensor data containers: %1% != %2%") + % numOutputs + % outputDataContainers.size())); } for (size_t i = 0; i < numOutputs; i++) @@ -72,16 +73,16 @@ inline armnn::OutputTensors MakeOutputTensors( TContainer& outputData = outputDataContainers[i]; boost::apply_visitor([&](auto&& value) - { - if (value.size() != outputBinding.second.GetNumElements()) - { - throw armnn::Exception("Output tensor has incorrect size"); - } - - armnn::Tensor outputTensor(outputBinding.second, value.data()); - outputTensors.push_back(std::make_pair(outputBinding.first, outputTensor)); - }, - outputData); + { + if (value.size() != outputBinding.second.GetNumElements()) + { + throw armnn::Exception("Output tensor has incorrect size"); + } + + armnn::Tensor outputTensor(outputBinding.second, value.data()); + outputTensors.push_back(std::make_pair(outputBinding.first, outputTensor)); + }, + outputData); } return outputTensors; diff --git a/src/armnnUtils/TensorUtils.cpp b/src/armnnUtils/TensorUtils.cpp index b4e8d5acda..0dbb75c33a 100644 --- a/src/armnnUtils/TensorUtils.cpp +++ b/src/armnnUtils/TensorUtils.cpp @@ -4,55 +4,58 @@ // #include "TensorUtils.hpp" + #include #include #include #include +using namespace armnn; + namespace armnnUtils { -armnn::TensorShape GetTensorShape(unsigned int numberOfBatches, +TensorShape GetTensorShape(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, - const armnn::DataLayout dataLayout) + const DataLayout dataLayout) { switch (dataLayout) { - case armnn::DataLayout::NCHW: - return armnn::TensorShape({numberOfBatches, numberOfChannels, height, width}); - case armnn::DataLayout::NHWC: - return armnn::TensorShape({numberOfBatches, height, width, numberOfChannels}); + case DataLayout::NCHW: + return TensorShape({numberOfBatches, numberOfChannels, height, width}); + case DataLayout::NHWC: + return TensorShape({numberOfBatches, height, width, numberOfChannels}); default: - throw armnn::InvalidArgumentException("Unknown data layout [" + throw InvalidArgumentException("Unknown data layout [" + std::to_string(static_cast(dataLayout)) + "]", CHECK_LOCATION()); } } -armnn::TensorInfo GetTensorInfo(unsigned int numberOfBatches, +TensorInfo GetTensorInfo(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, - const armnn::DataLayout dataLayout, - const armnn::DataType dataType) + const DataLayout dataLayout, + const DataType dataType) { switch (dataLayout) { - case armnn::DataLayout::NCHW: - return armnn::TensorInfo({numberOfBatches, numberOfChannels, height, width}, dataType); - case armnn::DataLayout::NHWC: - return armnn::TensorInfo({numberOfBatches, height, width, numberOfChannels}, dataType); + case DataLayout::NCHW: + return TensorInfo({numberOfBatches, numberOfChannels, height, width}, dataType); + case DataLayout::NHWC: + return TensorInfo({numberOfBatches, height, width, numberOfChannels}, dataType); default: - throw armnn::InvalidArgumentException("Unknown data layout [" + throw InvalidArgumentException("Unknown data layout [" + std::to_string(static_cast(dataLayout)) + "]", CHECK_LOCATION()); } } -std::pair FindMinMax(armnn::ITensorHandle* tensorHandle) +std::pair FindMinMax(ITensorHandle* tensorHandle) { auto tensor_data = static_cast(tensorHandle->Map(true)); auto tensor_size = tensorHandle->GetShape().GetNumElements(); @@ -79,13 +82,13 @@ std::pair FindMinMax(armnn::ITensorHandle* tensorHandle) return std::make_pair(min, max); } -armnn::TensorShape ExpandDims(const armnn::TensorShape& tensorShape, int axis) +TensorShape ExpandDims(const 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( + throw InvalidArgumentException( boost::str(boost::format("Invalid expansion axis %1% for %2%D input tensor. %3%") % axis % tensorShape.GetNumDimensions() % @@ -104,10 +107,10 @@ armnn::TensorShape ExpandDims(const armnn::TensorShape& tensorShape, int axis) } outputShape.insert(outputShape.begin() + axis, 1); - return armnn::TensorShape(outputDim, outputShape.data()); + return TensorShape(outputDim, outputShape.data()); } -unsigned int GetNumElementsBetween(const armnn::TensorShape& shape, +unsigned int GetNumElementsBetween(const TensorShape& shape, const unsigned int firstAxisInclusive, const unsigned int lastAxisExclusive) { @@ -135,4 +138,4 @@ unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis) return uAxis; } -} +} // namespace armnnUtils diff --git a/tests/InferenceTestImage.hpp b/tests/InferenceTestImage.hpp index 643d060919..d15f263609 100644 --- a/tests/InferenceTestImage.hpp +++ b/tests/InferenceTestImage.hpp @@ -128,15 +128,15 @@ enum class ImageChannelLayout // and now lie in the range [0,1]. Channel data is stored according to the ArmNN layout (CHW). The order in which // channels appear in the resulting vector is defined by the provided layout. std::vector GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout, - const InferenceTestImage& image); + const InferenceTestImage& image); // Reads the contents of an inference test image as 3-channel pixels, whose value is the result of subtracting the mean // from the values in the original image. Channel data is stored according to the ArmNN layout (CHW). The order in // which channels appear in the resulting vector is defined by the provided layout. The order of the channels of the // provided mean should also match the given layout. std::vector GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout, - const InferenceTestImage& image, - const std::array& mean); + const InferenceTestImage& image, + const std::array& mean); // Reads the contents of an inference test image as 3-channel pixels and returns the image data as normalized float // values. The returned image stay in the original order (HWC) order. The C order may be changed according to the -- cgit v1.2.1