ArmNN
 21.08
TransposeConvolution2dEndToEndTestImpl.hpp File Reference
#include "QuantizeHelper.hpp"
#include <armnnUtils/Permute.hpp>
#include <ResolveType.hpp>
#include <backendsCommon/test/CommonTestUtils.hpp>
#include <map>
#include <vector>

Go to the source code of this file.

Functions

template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType>
void TransposeConvolution2dEndToEnd (const std::vector< armnn::BackendId > &backends, armnn::DataLayout dataLayout)
 

Function Documentation

◆ TransposeConvolution2dEndToEnd()

void TransposeConvolution2dEndToEnd ( const std::vector< armnn::BackendId > &  backends,
armnn::DataLayout  dataLayout 
)

Definition at line 46 of file TransposeConvolution2dEndToEndTestImpl.hpp.

References TensorInfo::GetShape(), TransposeConvolution2dDescriptor::m_BiasEnabled, TransposeConvolution2dDescriptor::m_DataLayout, TransposeConvolution2dDescriptor::m_PadBottom, TransposeConvolution2dDescriptor::m_PadLeft, TransposeConvolution2dDescriptor::m_PadRight, TransposeConvolution2dDescriptor::m_PadTop, TransposeConvolution2dDescriptor::m_StrideX, TransposeConvolution2dDescriptor::m_StrideY, MakeTensorShape(), armnn::NHWC, and armnn::Permute.

48 {
49  using namespace armnn;
50  using T = ResolveType<ArmnnType>;
51 
52  constexpr unsigned int batches = 1u;
53  constexpr unsigned int channels = 1u;
54 
55  constexpr unsigned int wInput = 3u;
56  constexpr unsigned int hInput = wInput;
57 
58  constexpr unsigned int wOutput = 5u;
59  constexpr unsigned int hOutput = wOutput;
60 
61  constexpr unsigned int wWeights = 3u;
62  constexpr unsigned int hWeights = wWeights;
63 
64  TensorShape inputShape = MakeTensorShape(batches, channels, hInput, wInput, dataLayout);
65  TensorShape outputShape = MakeTensorShape(batches, channels, hOutput, wOutput, dataLayout);
66  TensorShape weightsShape = MakeTensorShape(batches, channels, hWeights, wWeights, dataLayout);
67 
68  const float qScale = IsQuantizedType<T>() ? 0.25f : 1.0f;
69  const int32_t qOffset = IsQuantizedType<T>() ? 50 : 0;
70 
71  TensorInfo inputInfo(inputShape, ArmnnType, qScale, qOffset);
72  TensorInfo outputInfo(outputShape, ArmnnType, qScale, qOffset);
73  TensorInfo weightsInfo(weightsShape, ArmnnType, qScale, qOffset);
74  TensorInfo biasesInfo({ channels }, ArmnnBType, qScale * qScale, 0);
75 
76  std::vector<float> inputData =
77  {
78  1.f, 1.f, 1.f,
79  1.f, 1.f, 1.f,
80  1.f, 1.f, 1.f
81  };
82 
83  std::vector<float> weightsData =
84  {
85  1.f, 2.f, 3.f,
86  4.f, 5.f, 6.f,
87  7.f, 8.f, 9.f
88  };
89 
90  std::vector<float> biasesData = { 1.f };
91 
92  std::vector<float> expectedOutputData =
93  {
94  6.f, 11.f, 6.f, 11.f, 6.f,
95  11.f, 21.f, 11.f, 21.f, 11.f,
96  6.f, 11.f, 6.f, 11.f, 6.f,
97  11.f, 21.f, 11.f, 21.f, 11.f,
98  6.f, 11.f, 6.f, 11.f, 6.f
99  };
100 
102  descriptor.m_PadLeft = 1;
103  descriptor.m_PadRight = 1;
104  descriptor.m_PadTop = 1;
105  descriptor.m_PadBottom = 1;
106  descriptor.m_StrideX = 2;
107  descriptor.m_StrideY = 2;
108  descriptor.m_BiasEnabled = true;
109  descriptor.m_DataLayout = dataLayout;
110 
111  // swizzle data if needed
112  if (dataLayout == armnn::DataLayout::NHWC)
113  {
114  constexpr size_t dataTypeSize = sizeof(float);
115  const armnn::PermutationVector nchwToNhwc = { 0, 3, 1, 2 };
116 
117  std::vector<float> tmp(inputData.size());
118  armnnUtils::Permute(inputInfo.GetShape(), nchwToNhwc, inputData.data(), tmp.data(), dataTypeSize);
119  inputData = tmp;
120 
121  tmp.resize(weightsData.size());
122  armnnUtils::Permute(weightsInfo.GetShape(), nchwToNhwc, weightsData.data(), tmp.data(), dataTypeSize);
123  weightsData = tmp;
124 
125  tmp.resize(expectedOutputData.size());
126  armnnUtils::Permute(outputInfo.GetShape(), nchwToNhwc, expectedOutputData.data(), tmp.data(), dataTypeSize);
127  expectedOutputData = tmp;
128  }
129 
130  // quantize data
131  std::vector<T> qInputData = armnnUtils::QuantizedVector<T>(inputData, qScale, qOffset);
132  std::vector<T> qWeightsData = armnnUtils::QuantizedVector<T>(weightsData, qScale, qOffset);
133  std::vector<T> qExpectedOutputData = armnnUtils::QuantizedVector<T>(expectedOutputData, qScale, qOffset);
134 
135  using BT = ResolveType<ArmnnBType>;
136  std::vector<BT> qBiasesData = armnnUtils::QuantizedVector<BT>(biasesData, qScale * qScale, 0);
137 
138  ConstTensor weights(weightsInfo, qWeightsData);
139  ConstTensor biases(biasesInfo, qBiasesData);
140 
141  INetworkPtr network = CreateTransposeConvolution2dNetwork(descriptor,
142  inputInfo,
143  outputInfo,
144  weights,
145  Optional<ConstTensor>(biases));
146 
147 
148  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network),
149  { { 0, qInputData } },
150  { { 0, qExpectedOutputData } },
151  backends);
152 }
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
bool m_BiasEnabled
Enable/disable bias.
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:79
Copyright (c) 2021 ARM Limited and Contributors.
uint32_t m_PadBottom
Padding bottom value in the height dimension.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
uint32_t m_PadTop
Padding top value in the height dimension.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
uint32_t m_PadLeft
Padding left value in the width dimension.
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.
uint32_t m_PadRight
Padding right value in the width dimension.
armnn::TensorShape MakeTensorShape(unsigned int batches, unsigned int channels, unsigned int height, unsigned int width, armnn::DataLayout layout)
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:172