diff options
author | Nina Drozd <nina.drozd@arm.com> | 2019-02-27 10:53:27 +0000 |
---|---|---|
committer | Nina Drozd <nina.drozd@arm.com> | 2019-03-01 10:52:26 +0000 |
commit | 57728788f65656e4fa08923d12bee0de34a72fc7 (patch) | |
tree | 4d84f00762483cb26f6c2e880a07eb8b7699b7da /src/armnnDeserializer | |
parent | 377351e5420304668da92da4ee00a012923619d1 (diff) | |
download | armnn-57728788f65656e4fa08923d12bee0de34a72fc7.tar.gz |
IVGCVSW-2700 Serialize/de-serialize the Normalization layer
Change-Id: Ib307ec6c28beb6c158d337678e67a2484c495a06
Signed-off-by: Nina Drozd <nina.drozd@arm.com>
Diffstat (limited to 'src/armnnDeserializer')
-rw-r--r-- | src/armnnDeserializer/Deserializer.cpp | 95 | ||||
-rw-r--r-- | src/armnnDeserializer/Deserializer.hpp | 8 | ||||
-rw-r--r-- | src/armnnDeserializer/DeserializerSupport.md | 1 | ||||
-rw-r--r-- | src/armnnDeserializer/test/DeserializeNormalization.cpp | 143 |
4 files changed, 245 insertions, 2 deletions
diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index 076b23e5a4..c7049f6a15 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -197,6 +197,7 @@ m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer) m_ParserFunctions[Layer_MinimumLayer] = &Deserializer::ParseMinimum; m_ParserFunctions[Layer_MaximumLayer] = &Deserializer::ParseMaximum; m_ParserFunctions[Layer_MultiplicationLayer] = &Deserializer::ParseMultiplication; + m_ParserFunctions[Layer_NormalizationLayer] = &Deserializer::ParseNormalization; m_ParserFunctions[Layer_PermuteLayer] = &Deserializer::ParsePermute; m_ParserFunctions[Layer_Pooling2dLayer] = &Deserializer::ParsePooling2d; m_ParserFunctions[Layer_ReshapeLayer] = &Deserializer::ParseReshape; @@ -236,6 +237,8 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt return graphPtr->layers()->Get(layerIndex)->layer_as_MaximumLayer()->base(); case Layer::Layer_MultiplicationLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base(); + case Layer::Layer_NormalizationLayer: + return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base(); case Layer::Layer_OutputLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base(); case Layer::Layer_PermuteLayer: @@ -1360,4 +1363,96 @@ void Deserializer::ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex) RegisterOutputSlots(graph, layerIndex, layer); } +armnn::NormalizationDescriptor Deserializer::GetNormalizationDescriptor( + Deserializer::NormalizationDescriptorPtr normalizationDescriptor, + unsigned int layerIndex) +{ + armnn::NormalizationDescriptor desc; + + switch (normalizationDescriptor->normChannelType()) + { + case NormalizationAlgorithmChannel_Across: + { + desc.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across; + break; + } + case NormalizationAlgorithmChannel_Within: + { + desc.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Within; + break; + } + default: + { + BOOST_ASSERT_MSG(false, "Unsupported normalization channel type"); + } + } + + switch (normalizationDescriptor->normMethodType()) + { + case NormalizationAlgorithmMethod_LocalBrightness: + { + desc.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness; + break; + } + case NormalizationAlgorithmMethod_LocalContrast: + { + desc.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalContrast; + break; + } + default: + { + BOOST_ASSERT_MSG(false, "Unsupported normalization method type"); + } + } + + switch (normalizationDescriptor->dataLayout()) + { + case DataLayout_NCHW: + { + desc.m_DataLayout = armnn::DataLayout::NCHW; + break; + } + case DataLayout_NHWC: + { + desc.m_DataLayout = armnn::DataLayout::NHWC; + break; + } + default: + { + BOOST_ASSERT_MSG(false, "Unsupported data layout"); + } + } + + desc.m_Alpha = normalizationDescriptor->alpha(); + desc.m_Beta = normalizationDescriptor->beta(); + desc.m_K = normalizationDescriptor->k(); + desc.m_NormSize = normalizationDescriptor->normSize(); + + return desc; +} + +void Deserializer::ParseNormalization(GraphPtr graph, unsigned int layerIndex) +{ + CHECK_LAYERS(graph, 0, layerIndex); + + auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor(); + + Deserializer::TensorRawPtrVector inputs = GetInputs(graph, layerIndex); + CHECK_VALID_SIZE(inputs.size(), 1); + + Deserializer::TensorRawPtrVector outputs = GetOutputs(graph, layerIndex); + CHECK_VALID_SIZE(outputs.size(), 1); + + auto outputInfo = ToTensorInfo(outputs[0]); + + auto normalizationDescriptor = GetNormalizationDescriptor(normalizationDes, layerIndex); + auto layerName = GetLayerName(graph, layerIndex); + + IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str()); + layer->GetOutputSlot(0).SetTensorInfo(outputInfo); + + RegisterInputSlots(graph, layerIndex, layer); + RegisterOutputSlots(graph, layerIndex, layer); +} + } // namespace armnnDeserializer diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp index 1dd7ec5284..fba8b88044 100644 --- a/src/armnnDeserializer/Deserializer.hpp +++ b/src/armnnDeserializer/Deserializer.hpp @@ -19,6 +19,7 @@ public: using GraphPtr = const armnnSerializer::SerializedGraph *; using TensorRawPtr = const armnnSerializer::TensorInfo *; using PoolingDescriptor = const armnnSerializer::Pooling2dDescriptor *; + using NormalizationDescriptorPtr = const armnnSerializer::NormalizationDescriptor *; using TensorRawPtrVector = std::vector<TensorRawPtr>; using LayerRawPtr = const armnnSerializer::LayerBase *; using LayerBaseRawPtr = const armnnSerializer::LayerBase *; @@ -51,8 +52,10 @@ public: static LayerBaseRawPtr GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex); static int32_t GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex); static std::string GetLayerName(const GraphPtr& graph, unsigned int index); - armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor, - unsigned int layerIndex); + static armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor, + unsigned int layerIndex); + static armnn::NormalizationDescriptor GetNormalizationDescriptor( + NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex); static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, const std::vector<uint32_t> & targetDimsIn); @@ -80,6 +83,7 @@ private: void ParseMinimum(GraphPtr graph, unsigned int layerIndex); void ParseMaximum(GraphPtr graph, unsigned int layerIndex); void ParseMultiplication(GraphPtr graph, unsigned int layerIndex); + void ParseNormalization(GraphPtr graph, unsigned int layerIndex); void ParsePermute(GraphPtr graph, unsigned int layerIndex); void ParsePooling2d(GraphPtr graph, unsigned int layerIndex); void ParseReshape(GraphPtr graph, unsigned int layerIndex); diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md index bb2f0637ac..cf8f6dea2d 100644 --- a/src/armnnDeserializer/DeserializerSupport.md +++ b/src/armnnDeserializer/DeserializerSupport.md @@ -18,6 +18,7 @@ The Arm NN SDK Deserialize parser currently supports the following layers: * Maximum * Minimum * Multiplication +* Normalization * Permute * Pooling2d * Reshape diff --git a/src/armnnDeserializer/test/DeserializeNormalization.cpp b/src/armnnDeserializer/test/DeserializeNormalization.cpp new file mode 100644 index 0000000000..eb7e958c5b --- /dev/null +++ b/src/armnnDeserializer/test/DeserializeNormalization.cpp @@ -0,0 +1,143 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <boost/test/unit_test.hpp> +#include "ParserFlatbuffersSerializeFixture.hpp" +#include "../Deserializer.hpp" + +#include <string> +#include <iostream> + +BOOST_AUTO_TEST_SUITE(Deserializer) + +struct NormalizationFixture : public ParserFlatbuffersSerializeFixture +{ + explicit NormalizationFixture(const std::string &inputShape, + const std::string & outputShape, + const std::string &dataType, + const std::string &normAlgorithmChannel, + const std::string &normAlgorithmMethod, + const std::string &dataLayout) + { + m_JsonString = R"( + { + inputIds: [0], + outputIds: [2], + layers: [{ + layer_type: "InputLayer", + layer: { + base: { + layerBindingId: 0, + base: { + index: 0, + layerName: "InputLayer", + layerType: "Input", + inputSlots: [{ + index: 0, + connection: {sourceLayerIndex:0, outputSlotIndex:0 }, + }], + outputSlots: [{ + index: 0, + tensorInfo: { + dimensions: )" + inputShape + R"(, + dataType: )" + dataType + R"(, + quantizationScale: 0.5, + quantizationOffset: 0 + }, + }] + }, + } + }, + }, + { + layer_type: "NormalizationLayer", + layer : { + base: { + index:1, + layerName: "NormalizationLayer", + layerType: "Normalization", + inputSlots: [{ + index: 0, + connection: {sourceLayerIndex:0, outputSlotIndex:0 }, + }], + outputSlots: [{ + index: 0, + tensorInfo: { + dimensions: )" + outputShape + R"(, + dataType: )" + dataType + R"( + }, + }], + }, + descriptor: { + normChannelType: )" + normAlgorithmChannel + R"(, + normMethodType: )" + normAlgorithmMethod + R"(, + normSize: 3, + alpha: 1, + beta: 1, + k: 1, + dataLayout: )" + dataLayout + R"( + } + }, + }, + { + layer_type: "OutputLayer", + layer: { + base:{ + layerBindingId: 0, + base: { + index: 2, + layerName: "OutputLayer", + layerType: "Output", + inputSlots: [{ + index: 0, + connection: {sourceLayerIndex:1, outputSlotIndex:0 }, + }], + outputSlots: [ { + index: 0, + tensorInfo: { + dimensions: )" + outputShape + R"(, + dataType: )" + dataType + R"( + }, + }], + } + }}, + }] + } + )"; + SetupSingleInputSingleOutput("InputLayer", "OutputLayer"); + } +}; + +struct FloatNhwcLocalBrightnessAcrossNormalizationFixture : NormalizationFixture +{ + FloatNhwcLocalBrightnessAcrossNormalizationFixture() : NormalizationFixture("[ 2, 2, 2, 1 ]", "[ 2, 2, 2, 1 ]", + "Float32", "0", "0", "NHWC") {} +}; + + +BOOST_FIXTURE_TEST_CASE(Float32NormalizationNhwcDataLayout, FloatNhwcLocalBrightnessAcrossNormalizationFixture) +{ + RunTest<4, armnn::DataType::Float32>(0, { 1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f }, + { 0.5f, 0.400000006f, 0.300000012f, 0.235294119f, + 0.192307696f, 0.16216217f, 0.140000001f, 0.123076923f }); +} + +struct FloatNchwLocalBrightnessWithinNormalizationFixture : NormalizationFixture +{ + FloatNchwLocalBrightnessWithinNormalizationFixture() : NormalizationFixture("[ 2, 1, 2, 2 ]", "[ 2, 1, 2, 2 ]", + "Float32", "1", "0", "NCHW") {} +}; + +BOOST_FIXTURE_TEST_CASE(Float32NormalizationNchwDataLayout, FloatNchwLocalBrightnessWithinNormalizationFixture) +{ + RunTest<4, armnn::DataType::Float32>(0, { 1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f }, + { 0.0322581f, 0.0645161f, 0.0967742f, 0.1290323f, + 0.0285714f, 0.0342857f, 0.04f, 0.0457143f }); +} + + +BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file |