ArmNN
 21.08
ResizeEndToEndTestImpl.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "QuantizeHelper.hpp"
8 
9 
10 #include <armnnUtils/Permute.hpp>
11 
12 #include <QuantizeHelper.hpp>
13 #include <ResolveType.hpp>
14 
16 
17 #include <map>
18 #include <vector>
19 
20 namespace
21 {
22 
23 armnn::INetworkPtr CreateResizeNetwork(const armnn::ResizeDescriptor& descriptor,
24  const armnn::TensorInfo& inputInfo,
25  const armnn::TensorInfo& outputInfo)
26 {
27  using namespace armnn;
28 
29  INetworkPtr network(INetwork::Create());
30  IConnectableLayer* input = network->AddInputLayer(0, "input");
31  IConnectableLayer* resize = network->AddResizeLayer(descriptor, "resize");
32  IConnectableLayer* output = network->AddOutputLayer(0, "output");
33 
34  Connect(input, resize, inputInfo, 0, 0);
35  Connect(resize, output, outputInfo, 0, 0);
36 
37  return network;
38 }
39 
40 template<armnn::DataType ArmnnType>
41 void ResizeEndToEnd(const std::vector<armnn::BackendId>& backends,
42  armnn::DataLayout dataLayout,
43  armnn::ResizeMethod resizeMethod)
44 {
45  using namespace armnn;
46  using T = ResolveType<ArmnnType>;
47 
48  constexpr unsigned int inputWidth = 3u;
49  constexpr unsigned int inputHeight = inputWidth;
50 
51  constexpr unsigned int outputWidth = 5u;
52  constexpr unsigned int outputHeight = outputWidth;
53 
54  TensorShape inputShape = MakeTensorShape(1, 1, inputHeight, inputWidth, dataLayout);
55  TensorShape outputShape = MakeTensorShape(1, 1, outputHeight, outputWidth, dataLayout);
56 
57  const float qScale = IsQuantizedType<T>() ? 0.25f : 1.0f;
58  const int32_t qOffset = IsQuantizedType<T>() ? 50 : 0;
59 
60  TensorInfo inputInfo(inputShape, ArmnnType, qScale, qOffset);
61  TensorInfo outputInfo(outputShape, ArmnnType, qScale, qOffset);
62 
63  std::vector<float> inputData =
64  {
65  1.f, 2.f, 3.f,
66  4.f, 5.f, 6.f,
67  7.f, 8.f, 9.f
68  };
69 
70  std::vector<float> expectedOutputData;
71  switch(resizeMethod)
72  {
74  {
75  expectedOutputData =
76  {
77  1.0f, 1.6f, 2.2f, 2.8f, 3.0f,
78  2.8f, 3.4f, 4.0f, 4.6f, 4.8f,
79  4.6f, 5.2f, 5.8f, 6.4f, 6.6f,
80  6.4f, 7.0f, 7.6f, 8.2f, 8.4f,
81  7.0f, 7.6f, 8.2f, 8.8f, 9.0f
82  };
83  break;
84  }
86  {
87  expectedOutputData =
88  {
89  1.f, 1.f, 2.f, 2.f, 3.f,
90  1.f, 1.f, 2.f, 2.f, 3.f,
91  4.f, 4.f, 5.f, 5.f, 6.f,
92  4.f, 4.f, 5.f, 5.f, 6.f,
93  7.f, 7.f, 8.f, 8.f, 9.f
94  };
95  break;
96  }
97  default:
98  {
99  throw InvalidArgumentException("Unrecognized resize method");
100  }
101  }
102 
103  ResizeDescriptor descriptor;
104  descriptor.m_TargetWidth = outputWidth;
105  descriptor.m_TargetHeight = outputHeight;
106  descriptor.m_Method = resizeMethod;
107  descriptor.m_DataLayout = dataLayout;
108 
109  // swizzle data if needed
110  if (dataLayout == armnn::DataLayout::NHWC)
111  {
112  constexpr size_t dataTypeSize = sizeof(float);
113  const armnn::PermutationVector nchwToNhwc = { 0, 3, 1, 2 };
114 
115  std::vector<float> tmp(inputData.size());
116  armnnUtils::Permute(inputInfo.GetShape(), nchwToNhwc, inputData.data(), tmp.data(), dataTypeSize);
117  inputData = tmp;
118  }
119 
120  // quantize data
121  std::vector<T> qInputData = armnnUtils::QuantizedVector<T>(inputData, qScale, qOffset);
122  std::vector<T> qExpectedOutputData = armnnUtils::QuantizedVector<T>(expectedOutputData, qScale, qOffset);
123 
124  INetworkPtr network = CreateResizeNetwork(descriptor, inputInfo, outputInfo);
125 
126  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network),
127  { { 0, qInputData } },
128  { { 0, qExpectedOutputData } },
129  backends);
130 }
131 
132 } // anonymous namespace
133 
134 template<armnn::DataType ArmnnType>
135 void ResizeBilinearEndToEnd(const std::vector<armnn::BackendId>& backends,
136  armnn::DataLayout dataLayout)
137 {
138  ResizeEndToEnd<ArmnnType>(backends, dataLayout, armnn::ResizeMethod::Bilinear);
139 }
140 
141 template<armnn::DataType ArmnnType>
142 void ResizeNearestNeighborEndToEnd(const std::vector<armnn::BackendId>& backends,
143  armnn::DataLayout dataLayout)
144 {
145  ResizeEndToEnd<ArmnnType>(backends, dataLayout, armnn::ResizeMethod::NearestNeighbor);
146 }
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
DataLayout
Definition: Types.hpp:53
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
void ResizeBilinearEndToEnd(const std::vector< armnn::BackendId > &backends, armnn::DataLayout dataLayout)
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
void ResizeNearestNeighborEndToEnd(const std::vector< armnn::BackendId > &backends, armnn::DataLayout dataLayout)
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:79
Copyright (c) 2021 ARM Limited and Contributors.
A ResizeDescriptor for the ResizeLayer.
uint32_t m_TargetWidth
Target width value.
uint32_t m_TargetHeight
Target height value.
ResizeMethod
Definition: Types.hpp:131
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
Definition: TestUtils.cpp:12
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
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:530