From aa06714b9f49fe36716fd3c116aadd892fadd4ac Mon Sep 17 00:00:00 2001 From: Aron Virginas-Tar Date: Tue, 11 Jun 2019 16:01:44 +0100 Subject: [PATCH] IVGCVSW-3260 Add serialization support for SpaceToDepth Signed-off-by: Aron Virginas-Tar Change-Id: Ie0d9561437ff5da8fa4db81fee1e70bd18c47034 --- src/armnnDeserializer/Deserializer.cpp | 29 ++++++++++++++ src/armnnDeserializer/Deserializer.hpp | 1 + src/armnnDeserializer/DeserializerSupport.md | 1 + src/armnnSerializer/ArmnnSchema.fbs | 16 +++++++- src/armnnSerializer/Serializer.cpp | 12 +++++- src/armnnSerializer/SerializerSupport.md | 1 + src/armnnSerializer/test/SerializerTests.cpp | 57 ++++++++++++++++++++++++++++ 7 files changed, 114 insertions(+), 3 deletions(-) diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index b0e7bc2..68f3e8d 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -219,6 +219,7 @@ m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer) m_ParserFunctions[Layer_RsqrtLayer] = &Deserializer::ParseRsqrt; m_ParserFunctions[Layer_SoftmaxLayer] = &Deserializer::ParseSoftmax; m_ParserFunctions[Layer_SpaceToBatchNdLayer] = &Deserializer::ParseSpaceToBatchNd; + m_ParserFunctions[Layer_SpaceToDepthLayer] = &Deserializer::ParseSpaceToDepth; m_ParserFunctions[Layer_SplitterLayer] = &Deserializer::ParseSplitter; m_ParserFunctions[Layer_StridedSliceLayer] = &Deserializer::ParseStridedSlice; m_ParserFunctions[Layer_SubtractionLayer] = &Deserializer::ParseSubtraction; @@ -303,6 +304,8 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base(); case Layer::Layer_SpaceToBatchNdLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base(); + case Layer::Layer_SpaceToDepthLayer: + return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->base(); case Layer::Layer_SplitterLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_SplitterLayer()->base(); case Layer::Layer_StridedSliceLayer: @@ -1723,6 +1726,32 @@ void Deserializer::ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex) RegisterOutputSlots(graph, layerIndex, layer); } +void Deserializer::ParseSpaceToDepth(GraphPtr graph, unsigned int layerIndex) +{ + CHECK_LAYERS(graph, 0, layerIndex); + + 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 flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor(); + + armnn::SpaceToDepthDescriptor descriptor; + descriptor.m_BlockSize = flatBufferDescriptor->blockSize(); + descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); + + auto layerName = GetLayerName(graph, layerIndex); + IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str()); + + armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + RegisterInputSlots(graph, layerIndex, layer); + RegisterOutputSlots(graph, layerIndex, layer); +} + armnn::NormalizationDescriptor Deserializer::GetNormalizationDescriptor( Deserializer::NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex) diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp index 38a6b60..20ab8c2 100644 --- a/src/armnnDeserializer/Deserializer.hpp +++ b/src/armnnDeserializer/Deserializer.hpp @@ -109,6 +109,7 @@ private: void ParseRsqrt(GraphPtr graph, unsigned int layerIndex); void ParseSoftmax(GraphPtr graph, unsigned int layerIndex); void ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex); + void ParseSpaceToDepth(GraphPtr graph, unsigned int layerIndex); void ParseSplitter(GraphPtr graph, unsigned int layerIndex); void ParseStridedSlice(GraphPtr graph, unsigned int layerIndex); void ParseSubtraction(GraphPtr graph, unsigned int layerIndex); diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md index 5c61971..94c26fc 100644 --- a/src/armnnDeserializer/DeserializerSupport.md +++ b/src/armnnDeserializer/DeserializerSupport.md @@ -39,6 +39,7 @@ The Arm NN SDK Deserialize parser currently supports the following layers: * Rsqrt * Softmax * SpaceToBatchNd +* SpaceToDepth * Splitter * StridedSlice * Subtraction diff --git a/src/armnnSerializer/ArmnnSchema.fbs b/src/armnnSerializer/ArmnnSchema.fbs index 5a001de..1204ad8 100644 --- a/src/armnnSerializer/ArmnnSchema.fbs +++ b/src/armnnSerializer/ArmnnSchema.fbs @@ -121,7 +121,8 @@ enum LayerType : uint { Dequantize = 36, Merge = 37, Switch = 38, - Concat = 39 + Concat = 39, + SpaceToDepth = 40 } // Base layer table to be used as part of other layers @@ -338,6 +339,16 @@ table SpaceToBatchNdDescriptor { dataLayout:DataLayout; } +table SpaceToDepthLayer { + base:LayerBase; + descriptor:SpaceToDepthDescriptor; +} + +table SpaceToDepthDescriptor { + blockSize:uint; + dataLayout:DataLayout; +} + table SubtractionLayer { base:LayerBase; } @@ -584,7 +595,8 @@ union Layer { DequantizeLayer, MergeLayer, SwitchLayer, - ConcatLayer + ConcatLayer, + SpaceToDepthLayer } table AnyLayer { diff --git a/src/armnnSerializer/Serializer.cpp b/src/armnnSerializer/Serializer.cpp index 676d10c..131bb95 100644 --- a/src/armnnSerializer/Serializer.cpp +++ b/src/armnnSerializer/Serializer.cpp @@ -788,7 +788,17 @@ void SerializerVisitor::VisitSpaceToDepthLayer(const armnn::IConnectableLayer* l const armnn::SpaceToDepthDescriptor& spaceToDepthDescriptor, const char* name) { - throw armnn::Exception("SerializerVisitor::VisitSpaceToDepthLayer is not yet implemented"); + auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_SpaceToDepth); + auto flatBufferDescriptor = + CreateSpaceToDepthDescriptor(m_flatBufferBuilder, + spaceToDepthDescriptor.m_BlockSize, + GetFlatBufferDataLayout(spaceToDepthDescriptor.m_DataLayout)); + + auto flatBufferLayer = serializer::CreateSpaceToDepthLayer(m_flatBufferBuilder, + flatBufferBaseLayer, + flatBufferDescriptor); + + CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_SpaceToDepthLayer); } // Build FlatBuffer for Splitter Layer diff --git a/src/armnnSerializer/SerializerSupport.md b/src/armnnSerializer/SerializerSupport.md index 734ae18..865cdf0 100644 --- a/src/armnnSerializer/SerializerSupport.md +++ b/src/armnnSerializer/SerializerSupport.md @@ -39,6 +39,7 @@ The Arm NN SDK Serializer currently supports the following layers: * Rsqrt * Softmax * SpaceToBatchNd +* SpaceToDepth * Splitter * StridedSlice * Subtraction diff --git a/src/armnnSerializer/test/SerializerTests.cpp b/src/armnnSerializer/test/SerializerTests.cpp index 752cf0c..a757e16 100644 --- a/src/armnnSerializer/test/SerializerTests.cpp +++ b/src/armnnSerializer/test/SerializerTests.cpp @@ -2045,6 +2045,63 @@ BOOST_AUTO_TEST_CASE(SerializeSpaceToBatchNd) deserializedNetwork->Accept(verifier); } +BOOST_AUTO_TEST_CASE(SerializeSpaceToDepth) +{ + class SpaceToDepthLayerVerifier : public LayerVerifierBase + { + public: + SpaceToDepthLayerVerifier(const std::string& layerName, + const std::vector& inputInfos, + const std::vector& outputInfos, + const armnn::SpaceToDepthDescriptor& descriptor) + : LayerVerifierBase(layerName, inputInfos, outputInfos) + , m_Descriptor(descriptor) {} + + void VisitSpaceToDepthLayer(const armnn::IConnectableLayer* layer, + const armnn::SpaceToDepthDescriptor& descriptor, + const char* name) override + { + VerifyNameAndConnections(layer, name); + VerifyDescriptor(descriptor); + } + + private: + void VerifyDescriptor(const armnn::SpaceToDepthDescriptor& descriptor) + { + BOOST_TEST(descriptor.m_BlockSize == m_Descriptor.m_BlockSize); + BOOST_TEST(GetDataLayoutName(descriptor.m_DataLayout) == GetDataLayoutName(m_Descriptor.m_DataLayout)); + } + + armnn::SpaceToDepthDescriptor m_Descriptor; + }; + + const std::string layerName("spaceToDepth"); + + const armnn::TensorInfo inputInfo ({ 1, 16, 8, 3 }, armnn::DataType::Float32); + const armnn::TensorInfo outputInfo({ 1, 8, 4, 12 }, armnn::DataType::Float32); + + armnn::SpaceToDepthDescriptor desc; + desc.m_BlockSize = 2; + desc.m_DataLayout = armnn::DataLayout::NHWC; + + armnn::INetworkPtr network = armnn::INetwork::Create(); + armnn::IConnectableLayer* const inputLayer = network->AddInputLayer(0); + armnn::IConnectableLayer* const spaceToDepthLayer = network->AddSpaceToDepthLayer(desc, layerName.c_str()); + armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0); + + inputLayer->GetOutputSlot(0).Connect(spaceToDepthLayer->GetInputSlot(0)); + spaceToDepthLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); + + inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); + spaceToDepthLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); + + armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*network)); + BOOST_CHECK(deserializedNetwork); + + SpaceToDepthLayerVerifier verifier(layerName, {inputInfo}, {outputInfo}, desc); + deserializedNetwork->Accept(verifier); +} + BOOST_AUTO_TEST_CASE(SerializeSplitter) { class SplitterLayerVerifier : public LayerVerifierBase -- 2.7.4