ArmNN
 20.02
TransposeConvolution2dLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
10 
12 
15 
16 using namespace armnnUtils;
17 
18 namespace armnn
19 {
20 
22  const char* name)
24 {
25 }
26 
27 std::unique_ptr<IWorkload> TransposeConvolution2dLayer::CreateWorkload(const IWorkloadFactory& factory) const
28 {
29  BOOST_ASSERT_MSG(m_Weight != nullptr, "TransposeConvolution2dLayer: Weights data should not be null.");
30 
32  descriptor.m_Weight = m_Weight.get();
33 
35  {
36  BOOST_ASSERT_MSG(m_Bias != nullptr, "TransposeConvolution2dLayer: Bias data should not be null.");
37  descriptor.m_Bias = m_Bias.get();
38  }
39 
40  return factory.CreateTransposeConvolution2d(descriptor, PrepInfoAndDesc(descriptor));
41 }
42 
44 {
45  auto layer = CloneBase<TransposeConvolution2dLayer>(graph, m_Param, GetName());
46 
47  layer->m_Weight = m_Weight ? std::make_unique<ScopedCpuTensorHandle>(*m_Weight) : nullptr;
48 
49  if (layer->m_Param.m_BiasEnabled)
50  {
51  layer->m_Bias = m_Bias ? std::make_unique<ScopedCpuTensorHandle>(*m_Bias) : nullptr;
52  }
53 
54  return std::move(layer);
55 }
56 
58  const std::vector<TensorShape>& inputShapes) const
59 {
60  BOOST_ASSERT(inputShapes.size() == 2);
61  const TensorShape& inputShape = inputShapes[0];
62  const TensorShape& kernelShape = inputShapes[1];
63 
64  BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Transpose convolutions will always have 4D input");
65 
66  DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout);
67 
68  const unsigned int batches = inputShape[0];
69 
70  const unsigned int wInput = inputShape[dataLayoutIndex.GetWidthIndex()];
71  const unsigned int hInput = inputShape[dataLayoutIndex.GetHeightIndex()];
72 
73  const unsigned int wKernel = kernelShape[dataLayoutIndex.GetWidthIndex()];
74  const unsigned int hKernel = kernelShape[dataLayoutIndex.GetHeightIndex()];
75 
76  unsigned int wPadding = m_Param.m_PadLeft + m_Param.m_PadRight;
77  unsigned int hPadding = m_Param.m_PadTop + m_Param.m_PadBottom;
78 
79  unsigned int wOutput = (wInput - 1) * m_Param.m_StrideX + wKernel - wPadding;
80  unsigned int hOutput = (hInput - 1) * m_Param.m_StrideY + hKernel - hPadding;
81 
82  unsigned int kernelElements = kernelShape[0] * kernelShape[dataLayoutIndex.GetChannelsIndex()];
83  unsigned int inputElements = batches * inputShape[dataLayoutIndex.GetChannelsIndex()];
84 
85  BOOST_ASSERT_MSG(inputElements != 0, "Invalid number of input elements");
86  BOOST_ASSERT_MSG(kernelElements % inputElements == 0, "Invalid number of elements");
87 
88  unsigned int channels = kernelElements / inputElements;
89 
91  TensorShape( { batches, hOutput, wOutput, channels } ) :
92  TensorShape( { batches, channels, hOutput, wOutput });
93 
94  return std::vector<TensorShape>({ tensorShape });
95 }
96 
98 {
100 
101  BOOST_ASSERT_MSG(m_Weight != nullptr, "TransposeConvolution2dLayer: Weight data cannot be null.");
102 
103  auto inferredShapes = InferOutputShapes({
105  m_Weight->GetTensorInfo().GetShape() });
106 
107  BOOST_ASSERT(inferredShapes.size() == 1);
108 
109  ConditionalThrowIfNotEqual<LayerValidationException>(
110  "TransposeConvolution2dLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
112  inferredShapes[0]);
113 }
114 
116 {
117  return {m_Weight, m_Bias};
118 }
119 
121 {
122  ConstTensor weightsTensor(m_Weight->GetTensorInfo(), m_Weight->Map(true)) ;
123  Optional<ConstTensor> optionalBiasTensor = EmptyOptional();
124 
125  if (GetParameters().m_BiasEnabled)
126  {
127  ConstTensor biasTensor(m_Bias->GetTensorInfo(), m_Bias->Map(true));
128  optionalBiasTensor = Optional<ConstTensor>(biasTensor);
129  }
130 
131  visitor.VisitTransposeConvolution2dLayer(this, GetParameters(), weightsTensor, optionalBiasTensor, GetName());
132 }
133 
134 } // namespace armnn
ConstantTensors GetConstantTensorsByRef() override
Retrieve the handles to the constant values stored by the layer.
TransposeConvolution2dDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const TransposeConvolution2dDescriptor & GetParameters() const
This layer represents a 2D transpose convolution operation.
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
bool m_BiasEnabled
Enable/disable bias.
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
std::unique_ptr< ScopedCpuTensorHandle > m_Weight
A unique pointer to store weight values.
Copyright (c) 2020 ARM Limited.
uint32_t m_PadBottom
Padding bottom value in the height dimension.
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of TransposeConvolution2dLayer...
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:338
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:310
TransposeConvolution2dLayer(const TransposeConvolution2dDescriptor &param, const char *name)
Constructor to create a TransposeConvolution2dLayer.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the TransposeConvolution2d type.
Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout...
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199
uint32_t m_PadTop
Padding top value in the height dimension.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
uint32_t m_PadLeft
Padding left value in the width dimension.
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
virtual void VisitTransposeConvolution2dLayer(const IConnectableLayer *layer, const TransposeConvolution2dDescriptor &descriptor, const ConstTensor &weights, const Optional< ConstTensor > &biases, const char *name=nullptr)=0
Function that a 2D transpose convolution layer should call back to when its Accept(ILayerVisitor&) fu...
virtual std::unique_ptr< IWorkload > CreateTransposeConvolution2d(const TransposeConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info) const
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
uint32_t m_PadRight
Padding right value in the width dimension.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:312
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:305
std::vector< std::reference_wrapper< std::unique_ptr< ScopedCpuTensorHandle > >> ConstantTensors
Definition: Layer.hpp:363
TransposeConvolution2dLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
Infers the output shapes from given input shapes and layer properties.
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
std::unique_ptr< ScopedCpuTensorHandle > m_Bias
A unique pointer to store bias values.