Narumol Prangnawarat | d1f5773 | 2019-10-31 14:24:02 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "CommonTestUtils.hpp" |
| 9 | |
| 10 | #include <QuantizeHelper.hpp> |
| 11 | #include <ResolveType.hpp> |
| 12 | |
| 13 | #include <armnn/ArmNN.hpp> |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | armnn::INetworkPtr CreateArgMinMaxNetwork(const armnn::TensorInfo& inputTensorInfo, |
| 19 | const armnn::TensorInfo& outputTensorInfo, |
| 20 | armnn::ArgMinMaxFunction function, |
| 21 | int axis) |
| 22 | { |
| 23 | armnn::INetworkPtr network(armnn::INetwork::Create()); |
| 24 | |
| 25 | armnn::ArgMinMaxDescriptor descriptor; |
| 26 | descriptor.m_Function = function; |
| 27 | descriptor.m_Axis = axis; |
| 28 | |
| 29 | armnn::IConnectableLayer* inputLayer = network->AddInputLayer(0, "Input"); |
| 30 | armnn::IConnectableLayer* argMinMaxLayer = network->AddArgMinMaxLayer(descriptor, "ArgMinMax"); |
| 31 | armnn::IConnectableLayer* outputLayer = network->AddOutputLayer(0, "Output"); |
| 32 | |
| 33 | Connect(inputLayer, argMinMaxLayer, inputTensorInfo, 0, 0); |
| 34 | Connect(argMinMaxLayer, outputLayer, outputTensorInfo, 0, 0); |
| 35 | |
| 36 | return network; |
| 37 | } |
| 38 | |
| 39 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 40 | void ArgMinMaxEndToEndImpl(const armnn::TensorShape& inputShape, |
| 41 | const armnn::TensorShape& outputShape, |
| 42 | const std::vector<float>& inputData, |
| 43 | const std::vector<int32_t>& expectedOutputData, |
| 44 | armnn::ArgMinMaxFunction function, |
| 45 | int axis, |
| 46 | const std::vector<armnn::BackendId>& backends) |
| 47 | { |
| 48 | const float qScale = armnn::IsQuantizedType<T>() ? 2.0f : 1.0f; |
| 49 | const int32_t qOffset = armnn::IsQuantizedType<T>() ? 2 : 0; |
| 50 | |
| 51 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType, qScale, qOffset); |
| 52 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Signed32); |
| 53 | |
| 54 | // quantize data |
| 55 | std::vector<T> qInputData = armnnUtils::QuantizedVector<T>(inputData, qScale, qOffset); |
| 56 | |
| 57 | armnn::INetworkPtr network = CreateArgMinMaxNetwork(inputTensorInfo, |
| 58 | outputTensorInfo, |
| 59 | function, |
| 60 | axis); |
| 61 | |
| 62 | EndToEndLayerTestImpl<ArmnnType, armnn::DataType::Signed32>(std::move(network), |
| 63 | { { 0, qInputData } }, |
| 64 | { { 0, expectedOutputData } }, |
| 65 | backends); |
| 66 | } |
| 67 | |
| 68 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 69 | void ArgMaxEndToEndSimple(const std::vector<armnn::BackendId>& backends) |
| 70 | { |
| 71 | const armnn::TensorShape inputShape{ 1, 1, 1, 5 }; |
| 72 | const armnn::TensorShape outputShape{ 1, 1, 1 }; |
| 73 | |
| 74 | std::vector<float> inputData({ 5.0f, 2.0f, 8.0f, 10.0f, 9.0f }); |
| 75 | std::vector<int32_t> expectedOutputData({ 3 }); |
| 76 | |
| 77 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 78 | outputShape, |
| 79 | inputData, |
| 80 | expectedOutputData, |
| 81 | armnn::ArgMinMaxFunction::Max, |
| 82 | -1, |
| 83 | backends); |
| 84 | } |
| 85 | |
| 86 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 87 | void ArgMinEndToEndSimple(const std::vector<armnn::BackendId>& backends) |
| 88 | { |
| 89 | const armnn::TensorShape inputShape{ 1, 1, 1, 5 }; |
| 90 | const armnn::TensorShape outputShape{ 1, 1, 1 }; |
| 91 | |
| 92 | std::vector<float> inputData({ 5.0f, 2.0f, 8.0f, 10.0f, 9.0f }); |
| 93 | std::vector<int32_t> expectedOutputData({ 1 }); |
| 94 | |
| 95 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 96 | outputShape, |
| 97 | inputData, |
| 98 | expectedOutputData, |
| 99 | armnn::ArgMinMaxFunction::Min, |
| 100 | 3, |
| 101 | backends); |
| 102 | } |
| 103 | |
| 104 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 105 | void ArgMaxAxis0EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 106 | { |
| 107 | const armnn::TensorShape inputShape{ 3, 2, 1, 4 }; |
| 108 | const armnn::TensorShape outputShape{ 2, 1, 4 }; |
| 109 | |
| 110 | std::vector<float> inputData({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 111 | 8.0f, 7.0f, 6.0f, 5.0f, |
| 112 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 113 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 114 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 115 | 10.0f, 200.0f, 30.0f, 400.0f }); |
| 116 | |
| 117 | std::vector<int32_t> expectedOutputData({ 1, 2, 1, 2, |
| 118 | 1, 1, 1, 1 }); |
| 119 | |
| 120 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 121 | outputShape, |
| 122 | inputData, |
| 123 | expectedOutputData, |
| 124 | armnn::ArgMinMaxFunction::Max, |
| 125 | 0, |
| 126 | backends); |
| 127 | } |
| 128 | |
| 129 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 130 | void ArgMinAxis0EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 131 | { |
| 132 | const armnn::TensorShape inputShape{ 3, 2, 1, 4 }; |
| 133 | const armnn::TensorShape outputShape{ 2, 1, 4 }; |
| 134 | |
| 135 | std::vector<float> inputData({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 136 | 8.0f, 7.0f, 6.0f, 5.0f, |
| 137 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 138 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 139 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 140 | 10.0f, 200.0f, 30.0f, 400.0f }); |
| 141 | |
| 142 | std::vector<int32_t> expectedOutputData({ 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0 }); |
| 144 | |
| 145 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 146 | outputShape, |
| 147 | inputData, |
| 148 | expectedOutputData, |
| 149 | armnn::ArgMinMaxFunction::Min, |
| 150 | 0, |
| 151 | backends); |
| 152 | } |
| 153 | |
| 154 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 155 | void ArgMaxAxis1EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 156 | { |
| 157 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 158 | const armnn::TensorShape outputShape{ 1, 2, 4 }; |
| 159 | |
| 160 | std::vector<float> inputData({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 161 | 8.0f, 7.0f, 6.0f, 5.0f, |
| 162 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 163 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 164 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 165 | 10.0f, 200.0f, 30.0f, 400.0f }); |
| 166 | |
| 167 | std::vector<int32_t> expectedOutputData({ 1, 2, 1, 2, |
| 168 | 1, 1, 1, 1 }); |
| 169 | |
| 170 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 171 | outputShape, |
| 172 | inputData, |
| 173 | expectedOutputData, |
| 174 | armnn::ArgMinMaxFunction::Max, |
| 175 | 1, |
| 176 | backends); |
| 177 | } |
| 178 | |
| 179 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 180 | void ArgMinAxis1EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 181 | { |
| 182 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 183 | const armnn::TensorShape outputShape{ 1, 2, 4 }; |
| 184 | |
| 185 | std::vector<float> inputData({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 186 | 8.0f, 7.0f, 6.0f, 5.0f, |
| 187 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 188 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 189 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 190 | 10.0f, 200.0f, 30.0f, 400.0f }); |
| 191 | |
| 192 | std::vector<int32_t> expectedOutputData({ 0, 0, 0, 0, |
| 193 | 0, 0, 0, 0 }); |
| 194 | |
| 195 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 196 | outputShape, |
| 197 | inputData, |
| 198 | expectedOutputData, |
| 199 | armnn::ArgMinMaxFunction::Min, |
| 200 | 1, |
| 201 | backends); |
| 202 | } |
| 203 | |
| 204 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 205 | void ArgMaxAxis2EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 206 | { |
| 207 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 208 | const armnn::TensorShape outputShape{ 1, 3, 4 }; |
| 209 | |
| 210 | std::vector<float> inputData({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 211 | 8.0f, 7.0f, 6.0f, 5.0f, |
| 212 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 213 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 214 | 10.0f, 200.0f, 30.0f, 400.0f, |
| 215 | 50.0f, 60.0f, 70.0f, 80.0f }); |
| 216 | |
| 217 | std::vector<int32_t> expectedOutputData({ 1, 1, 1, 1, |
| 218 | 1, 1, 1, 1, |
| 219 | 1, 0, 1, 0}); |
| 220 | |
| 221 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 222 | outputShape, |
| 223 | inputData, |
| 224 | expectedOutputData, |
| 225 | armnn::ArgMinMaxFunction::Max, |
| 226 | 2, |
| 227 | backends); |
| 228 | } |
| 229 | |
| 230 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 231 | void ArgMinAxis2EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 232 | { |
| 233 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 234 | const armnn::TensorShape outputShape{ 1, 3, 4 }; |
| 235 | |
| 236 | std::vector<float> inputData({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 237 | 8.0f, 7.0f, 6.0f, 5.0f, |
| 238 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 239 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 240 | 10.0f, 200.0f, 30.0f, 400.0f, |
| 241 | 50.0f, 60.0f, 70.0f, 80.0f }); |
| 242 | |
| 243 | std::vector<int32_t> expectedOutputData({ 0, 0, 0, 0, |
| 244 | 0, 0, 0, 0, |
| 245 | 0, 1, 0, 1 }); |
| 246 | |
| 247 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 248 | outputShape, |
| 249 | inputData, |
| 250 | expectedOutputData, |
| 251 | armnn::ArgMinMaxFunction::Min, |
| 252 | 2, |
| 253 | backends); |
| 254 | } |
| 255 | |
| 256 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 257 | void ArgMaxAxis3EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 258 | { |
| 259 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 260 | const armnn::TensorShape outputShape{ 1, 3, 2 }; |
| 261 | |
| 262 | std::vector<float> inputData({ 1.0f, 3.0f, 5.0f, 7.0f, |
| 263 | 8.0f, 7.0f, 6.0f, 5.0f, |
| 264 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 265 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 266 | 10.0f, 200.0f, 30.0f, 400.0f, |
| 267 | 50.0f, 60.0f, 70.0f, 80.0f }); |
| 268 | |
| 269 | std::vector<int32_t> expectedOutputData({ 3, 0, |
| 270 | 2, 0, |
| 271 | 3, 3}); |
| 272 | |
| 273 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 274 | outputShape, |
| 275 | inputData, |
| 276 | expectedOutputData, |
| 277 | armnn::ArgMinMaxFunction::Max, |
| 278 | 3, |
| 279 | backends); |
| 280 | } |
| 281 | |
| 282 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 283 | void ArgMinAxis3EndToEnd(const std::vector<armnn::BackendId>& backends) |
| 284 | { |
| 285 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 286 | const armnn::TensorShape outputShape{ 1, 3, 2 }; |
| 287 | |
| 288 | std::vector<float> inputData({ 1.0f, 3.0f, 5.0f, 7.0f, |
| 289 | 18.0f, 16.0f, 14.0f, 12.0f, |
| 290 | 100.0f, 20.0f, 300.0f, 40.0f, |
| 291 | 500.0f, 475.0f, 450.0f, 425.0f, |
| 292 | 10.0f, 200.0f, 30.0f, 400.0f, |
| 293 | 50.0f, 60.0f, 70.0f, 80.0f }); |
| 294 | |
| 295 | std::vector<int32_t> expectedOutputData({ 0, 3, |
| 296 | 1, 3, |
| 297 | 0, 0 }); |
| 298 | |
| 299 | ArgMinMaxEndToEndImpl<ArmnnType>(inputShape, |
| 300 | outputShape, |
| 301 | inputData, |
| 302 | expectedOutputData, |
| 303 | armnn::ArgMinMaxFunction::Min, |
| 304 | 3, |
| 305 | backends); |
| 306 | } |
| 307 | |
| 308 | } // anonymous namespace |