From 781ced9d1472486f86314e320a00d62329dcd363 Mon Sep 17 00:00:00 2001 From: Aron Virginas-Tar Date: Thu, 3 Oct 2019 11:15:39 +0100 Subject: IVGCVSW-3934 Add serialization support for INSTANCE_NORMALIZATION Signed-off-by: Aron Virginas-Tar Change-Id: If9b4d30cbd625206ec1c7d37dd8b449983442147 --- src/armnnDeserializer/Deserializer.cpp | 32 +++++++++++++++ src/armnnDeserializer/Deserializer.hpp | 1 + src/armnnDeserializer/DeserializerSupport.md | 1 + src/armnnSerializer/ArmnnSchema.fbs | 18 ++++++++- src/armnnSerializer/Serializer.cpp | 12 +++++- src/armnnSerializer/SerializerSupport.md | 1 + src/armnnSerializer/test/SerializerTests.cpp | 60 ++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index c85282f418..960d300fa5 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -204,6 +204,7 @@ m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer) m_ParserFunctions[Layer_FloorLayer] = &Deserializer::ParseFloor; m_ParserFunctions[Layer_GatherLayer] = &Deserializer::ParseGather; m_ParserFunctions[Layer_GreaterLayer] = &Deserializer::ParseGreater; + m_ParserFunctions[Layer_InstanceNormalizationLayer] = &Deserializer::ParseInstanceNormalization; m_ParserFunctions[Layer_L2NormalizationLayer] = &Deserializer::ParseL2Normalization; m_ParserFunctions[Layer_LstmLayer] = &Deserializer::ParseLstm; m_ParserFunctions[Layer_MaximumLayer] = &Deserializer::ParseMaximum; @@ -281,6 +282,8 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt return graphPtr->layers()->Get(layerIndex)->layer_as_GreaterLayer()->base(); case Layer::Layer_InputLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base(); + case Layer::Layer_InstanceNormalizationLayer: + return graphPtr->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer()->base(); case Layer::Layer_L2NormalizationLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer()->base(); case Layer::Layer_LstmLayer: @@ -1293,6 +1296,35 @@ void Deserializer::ParseGreater(GraphPtr graph, unsigned int layerIndex) RegisterOutputSlots(graph, layerIndex, layer); } +void Deserializer::ParseInstanceNormalization(GraphPtr graph, unsigned int layerIndex) +{ + CHECK_LAYERS(graph, 0, layerIndex); + + auto inputs = GetInputs(graph, layerIndex); + CHECK_VALID_SIZE(inputs.size(), 1); + + auto outputs = GetOutputs(graph, layerIndex); + CHECK_VALID_SIZE(outputs.size(), 1); + + auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer(); + auto fbDescriptor = fbLayer->descriptor(); + + armnn::InstanceNormalizationDescriptor descriptor; + descriptor.m_Gamma = fbDescriptor->gamma(); + descriptor.m_Beta = fbDescriptor->beta(); + descriptor.m_Eps = fbDescriptor->eps(); + descriptor.m_DataLayout = ToDataLayout(fbDescriptor->dataLayout()); + + const std::string layerName = GetLayerName(graph, layerIndex); + const armnn::TensorInfo outputInfo = ToTensorInfo(outputs[0]); + + IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str()); + layer->GetOutputSlot(0).SetTensorInfo(outputInfo); + + RegisterInputSlots(graph, layerIndex, layer); + RegisterOutputSlots(graph, layerIndex, layer); +} + void Deserializer::ParseL2Normalization(GraphPtr graph, unsigned int layerIndex) { CHECK_LAYERS(graph, 0, layerIndex); diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp index 0e2a34fc80..b5de6a76d0 100644 --- a/src/armnnDeserializer/Deserializer.hpp +++ b/src/armnnDeserializer/Deserializer.hpp @@ -96,6 +96,7 @@ private: void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex); void ParseGather(GraphPtr graph, unsigned int layerIndex); void ParseGreater(GraphPtr graph, unsigned int layerIndex); + void ParseInstanceNormalization(GraphPtr graph, unsigned int layerIndex); void ParseL2Normalization(GraphPtr graph, unsigned int layerIndex); void ParseMaximum(GraphPtr graph, unsigned int layerIndex); void ParseMean(GraphPtr graph, unsigned int layerIndex); diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md index cbc3eb97fd..31b975f458 100644 --- a/src/armnnDeserializer/DeserializerSupport.md +++ b/src/armnnDeserializer/DeserializerSupport.md @@ -26,6 +26,7 @@ The Arm NN SDK Deserialize parser currently supports the following layers: * Gather * Greater * Input +* InstanceNormalization * L2Normalization * Lstm * Maximum diff --git a/src/armnnSerializer/ArmnnSchema.fbs b/src/armnnSerializer/ArmnnSchema.fbs index 3f3e8fcb0b..7588b9d11e 100644 --- a/src/armnnSerializer/ArmnnSchema.fbs +++ b/src/armnnSerializer/ArmnnSchema.fbs @@ -141,7 +141,8 @@ enum LayerType : uint { Abs = 46, ArgMinMax = 47, Slice = 48, - DepthToSpace = 49 + DepthToSpace = 49, + InstanceNormalization = 50 } // Base layer table to be used as part of other layers @@ -260,6 +261,18 @@ table InputLayer { base:BindableLayerBase; } +table InstanceNormalizationLayer { + base:LayerBase; + descriptor:InstanceNormalizationDescriptor; +} + +table InstanceNormalizationDescriptor { + gamma:float; + beta:float; + eps:float; + dataLayout:DataLayout; +} + table L2NormalizationLayer { base:LayerBase; descriptor:L2NormalizationDescriptor; @@ -734,7 +747,8 @@ union Layer { AbsLayer, ArgMinMaxLayer, SliceLayer, - DepthToSpaceLayer + DepthToSpaceLayer, + InstanceNormalizationLayer } table AnyLayer { diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp index 84a1b6b18e..5949d1d9fa 100644 --- a/src/armnnSerializer/Serializer.cpp +++ b/src/armnnSerializer/Serializer.cpp @@ -431,7 +431,17 @@ void SerializerVisitor::VisitInstanceNormalizationLayer( const armnn::InstanceNormalizationDescriptor& instanceNormalizationDescriptor, const char* name) { - throw UnimplementedException("SerializerVisitor::InstanceNormalizationLayer is not implemented"); + auto fbDescriptor = serializer::CreateInstanceNormalizationDescriptor( + m_flatBufferBuilder, + instanceNormalizationDescriptor.m_Gamma, + instanceNormalizationDescriptor.m_Beta, + instanceNormalizationDescriptor.m_Eps, + GetFlatBufferDataLayout(instanceNormalizationDescriptor.m_DataLayout)); + + auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_InstanceNormalization); + auto fbLayer = serializer::CreateInstanceNormalizationLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor); + + CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_InstanceNormalizationLayer); } void SerializerVisitor::VisitL2NormalizationLayer(const armnn::IConnectableLayer* layer, diff --git a/src/armnnSerializer/SerializerSupport.md b/src/armnnSerializer/SerializerSupport.md index 2e6263b390..e628253f19 100644 --- a/src/armnnSerializer/SerializerSupport.md +++ b/src/armnnSerializer/SerializerSupport.md @@ -26,6 +26,7 @@ The Arm NN SDK Serializer currently supports the following layers: * Gather * Greater * Input +* InstanceNormalization * L2Normalization * Lstm * Maximum diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp index dfa98acbcd..98893a59cf 100644 --- a/src/armnnSerializer/test/SerializerTests.cpp +++ b/src/armnnSerializer/test/SerializerTests.cpp @@ -1267,6 +1267,66 @@ BOOST_AUTO_TEST_CASE(SerializeGreater) deserializedNetwork->Accept(verifier); } +BOOST_AUTO_TEST_CASE(SerializeInstanceNormalization) +{ + class InstanceNormalizationLayerVerifier : public LayerVerifierBase + { + public: + InstanceNormalizationLayerVerifier(const std::string& layerName, + const std::vector& inputInfos, + const std::vector& outputInfos, + const armnn::InstanceNormalizationDescriptor& descriptor) + : LayerVerifierBase(layerName, inputInfos, outputInfos) + , m_Descriptor(descriptor) {} + + void VisitInstanceNormalizationLayer(const armnn::IConnectableLayer* layer, + const armnn::InstanceNormalizationDescriptor& descriptor, + const char* name) override + { + VerifyNameAndConnections(layer, name); + VerifyDescriptor(descriptor); + } + + private: + void VerifyDescriptor(const armnn::InstanceNormalizationDescriptor& descriptor) + { + BOOST_CHECK(descriptor.m_Gamma == m_Descriptor.m_Gamma); + BOOST_CHECK(descriptor.m_Beta == m_Descriptor.m_Beta); + BOOST_CHECK(descriptor.m_Eps == m_Descriptor.m_Eps); + BOOST_CHECK(descriptor.m_DataLayout == m_Descriptor.m_DataLayout); + } + + armnn::InstanceNormalizationDescriptor m_Descriptor; + }; + + const std::string layerName("instanceNormalization"); + const armnn::TensorInfo info({ 1, 2, 1, 5 }, armnn::DataType::Float32); + + armnn::InstanceNormalizationDescriptor descriptor; + descriptor.m_Gamma = 1.1f; + descriptor.m_Beta = 0.1f; + descriptor.m_Eps = 0.0001f; + descriptor.m_DataLayout = armnn::DataLayout::NHWC; + + armnn::INetworkPtr network = armnn::INetwork::Create(); + armnn::IConnectableLayer* const inputLayer = network->AddInputLayer(0); + armnn::IConnectableLayer* const instanceNormLayer = + network->AddInstanceNormalizationLayer(descriptor, layerName.c_str()); + armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0); + + inputLayer->GetOutputSlot(0).Connect(instanceNormLayer->GetInputSlot(0)); + instanceNormLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); + + inputLayer->GetOutputSlot(0).SetTensorInfo(info); + instanceNormLayer->GetOutputSlot(0).SetTensorInfo(info); + + armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*network)); + BOOST_CHECK(deserializedNetwork); + + InstanceNormalizationLayerVerifier verifier(layerName, {info}, {info}, descriptor); + deserializedNetwork->Accept(verifier); +} + class L2NormalizationLayerVerifier : public LayerVerifierBase { public: -- cgit v1.2.1