diff options
Diffstat (limited to 'src/armnnDeserializer')
-rw-r--r-- | src/armnnDeserializer/Deserializer.cpp | 46 | ||||
-rw-r--r-- | src/armnnDeserializer/Deserializer.hpp | 1 | ||||
-rw-r--r-- | src/armnnDeserializer/DeserializerSupport.md | 1 | ||||
-rw-r--r-- | src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp | 136 |
4 files changed, 184 insertions, 0 deletions
diff --git a/src/armnnDeserializer/Deserializer.cpp b/src/armnnDeserializer/Deserializer.cpp index 64f8e44429..30f7d05c93 100644 --- a/src/armnnDeserializer/Deserializer.cpp +++ b/src/armnnDeserializer/Deserializer.cpp @@ -187,6 +187,7 @@ m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer) // register supported layers m_ParserFunctions[Layer_ActivationLayer] = &Deserializer::ParseActivation; m_ParserFunctions[Layer_AdditionLayer] = &Deserializer::ParseAdd; + m_ParserFunctions[Layer_BatchToSpaceNdLayer] = &Deserializer::ParseBatchToSpaceNd; m_ParserFunctions[Layer_ConstantLayer] = &Deserializer::ParseConstant; m_ParserFunctions[Layer_Convolution2dLayer] = &Deserializer::ParseConvolution2d; m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d; @@ -209,6 +210,8 @@ Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPt return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base(); case Layer::Layer_AdditionLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base(); + case Layer::Layer_BatchToSpaceNdLayer: + return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base(); case Layer::Layer_ConstantLayer: return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base(); case Layer::Layer_Convolution2dLayer: @@ -778,6 +781,49 @@ void Deserializer::ParseAdd(GraphPtr graph, unsigned int layerIndex) RegisterOutputSlots(graph, layerIndex, layer); } +void Deserializer::ParseBatchToSpaceNd(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_BatchToSpaceNdLayer()->descriptor(); + auto flatBufferCrops = flatBufferDescriptor->crops(); + auto flatBufferBlockShape = flatBufferDescriptor->blockShape(); + + if (flatBufferCrops->Length() % 2 != 0) + { + throw ParseException(boost::str( + boost::format("The size of crops must be divisible by 2 %1%") % CHECK_LOCATION().AsString())); + } + + std::vector<std::pair<unsigned int, unsigned int>> crops; + crops.reserve(flatBufferCrops->Length() / 2); + for (unsigned int i = 0; i < flatBufferCrops->Length() - 1; i += 2) + { + crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1)); + } + + armnn::BatchToSpaceNdDescriptor descriptor; + descriptor.m_DataLayout = ToDataLayout(flatBufferDescriptor->dataLayout()); + descriptor.m_BlockShape = + std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end()); + descriptor.m_Crops = crops; + + auto layerName = GetLayerName(graph, layerIndex); + IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str()); + + armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + RegisterInputSlots(graph, layerIndex, layer); + RegisterOutputSlots(graph, layerIndex, layer); +} + void Deserializer::ParseConstant(GraphPtr graph, unsigned int layerIndex) { CHECK_LAYERS(graph, 0, layerIndex); diff --git a/src/armnnDeserializer/Deserializer.hpp b/src/armnnDeserializer/Deserializer.hpp index c88459d97a..0e0261f27b 100644 --- a/src/armnnDeserializer/Deserializer.hpp +++ b/src/armnnDeserializer/Deserializer.hpp @@ -70,6 +70,7 @@ private: void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex); void ParseActivation(GraphPtr graph, unsigned int layerIndex); void ParseAdd(GraphPtr graph, unsigned int layerIndex); + void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex); void ParseConstant(GraphPtr graph, unsigned int layerIndex); void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex); void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex); diff --git a/src/armnnDeserializer/DeserializerSupport.md b/src/armnnDeserializer/DeserializerSupport.md index 4ae9780bc7..31a53569ad 100644 --- a/src/armnnDeserializer/DeserializerSupport.md +++ b/src/armnnDeserializer/DeserializerSupport.md @@ -8,6 +8,7 @@ The Arm NN SDK Deserialize parser currently supports the following layers: * Activation * Addition +* BatchToSpaceNd * Constant * Convolution2d * DepthwiseConvolution2d diff --git a/src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp b/src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp new file mode 100644 index 0000000000..e85f4ada9d --- /dev/null +++ b/src/armnnDeserializer/test/DeserializeBatchToSpaceNd.cpp @@ -0,0 +1,136 @@ +// +// 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> + +BOOST_AUTO_TEST_SUITE(Deserializer) + +struct BatchToSpaceNdFixture : public ParserFlatbuffersSerializeFixture +{ + explicit BatchToSpaceNdFixture(const std::string &inputShape, + const std::string &blockShape, + const std::string &crops, + const std::string &dataLayout, + const std::string &outputShape, + const std::string &dataType) + { + 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"( + } + }] + } + } + } + }, + { + layer_type: "BatchToSpaceNdLayer", + layer: { + base: { + index: 1, + layerName: "BatchToSpaceNdLayer", + layerType: "BatchToSpaceNd", + inputSlots: [{ + index: 0, + connection: {sourceLayerIndex:0, outputSlotIndex:0 }, + }], + outputSlots: [{ + index: 0, + tensorInfo: { + dimensions: )" + outputShape + R"(, + dataType: )" + dataType + R"( + } + }] + }, + descriptor: { + blockShape: )" + blockShape + R"(, + crops: )" + crops + R"(, + dataLayout: )" + dataLayout + R"(, + } + } + }, + { + layer_type: "OutputLayer", + layer: { + base:{ + layerBindingId: 2, + 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 SimpleBatchToSpaceNdFixture : BatchToSpaceNdFixture +{ + SimpleBatchToSpaceNdFixture() : BatchToSpaceNdFixture("[ 4, 2, 2, 1 ]", + "[ 2, 2 ]", + "[ 0, 0, 0, 0 ]", + "NHWC", + "[ 1, 4, 4, 1 ]", + "Float32") {} +}; + +BOOST_FIXTURE_TEST_CASE(SimpleBatchToSpaceNdFloat32, SimpleBatchToSpaceNdFixture) +{ + RunTest<4, armnn::DataType::Float32>(0, + { + 1.0f, 3.0f, 9.0f, 11.0f, + 2.0f, 4.0f, 10.0f, 12.0f, + 5.0f, 7.0f, 13.0f, 15.0f, + 6.0f, 8.0f, 14.0f, 16.0f + }, + { + 1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f, + 13.0f, 14.0f, 15.0f, 16.0f + }); +} + +BOOST_AUTO_TEST_SUITE_END() |