Sadik Armagan | 062e0e9 | 2019-10-14 10:31:43 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "InstanceNormalizationEndToEndTestImpl.hpp" |
| 7 | |
| 8 | #include "DataLayoutIndexed.hpp" |
| 9 | #include "EndToEndTestImpl.hpp" |
| 10 | #include "ResolveType.hpp" |
| 11 | |
| 12 | #include <Permute.hpp> |
| 13 | |
| 14 | #include <armnn/INetwork.hpp> |
| 15 | |
| 16 | #include <backendsCommon/test/DataLayoutUtils.hpp> |
| 17 | |
| 18 | #include <test/TestUtils.hpp> |
| 19 | |
| 20 | #include <boost/test/unit_test.hpp> |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | template<typename armnn::DataType DataType> |
| 26 | armnn::INetworkPtr CreateInstanceNormalizationNetwork(const armnn::TensorShape& inputShape, |
| 27 | const armnn::TensorShape& outputShape, |
| 28 | const armnn::DataLayout dataLayout, |
| 29 | const float gamma, |
| 30 | const float beta, |
| 31 | const float eps, |
| 32 | const float qScale = 1.0f, |
| 33 | const int32_t qOffset = 0) |
| 34 | { |
| 35 | using namespace armnn; |
| 36 | |
| 37 | // Builds up the structure of the network. |
| 38 | INetworkPtr net(INetwork::Create()); |
| 39 | |
| 40 | TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset); |
| 41 | |
| 42 | InstanceNormalizationDescriptor instanceNormalizationDesc; |
| 43 | instanceNormalizationDesc.m_Gamma = gamma; |
| 44 | instanceNormalizationDesc.m_Beta = beta; |
| 45 | instanceNormalizationDesc.m_Eps = eps; |
| 46 | instanceNormalizationDesc.m_DataLayout = dataLayout; |
| 47 | |
| 48 | IConnectableLayer* instanceNormalization = net->AddInstanceNormalizationLayer(instanceNormalizationDesc, |
| 49 | "InstanceNormalization"); |
| 50 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 51 | Connect(input, instanceNormalization, inputTensorInfo, 0, 0); |
| 52 | |
| 53 | TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset); |
| 54 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 55 | Connect(instanceNormalization, output, outputTensorInfo, 0, 0); |
| 56 | |
| 57 | return net; |
| 58 | } |
| 59 | |
| 60 | void InstanceNormalizationEndToEnd(const std::vector<armnn::BackendId>& backends, |
| 61 | const armnn::DataLayout& dataLayout, |
| 62 | armnn::TensorInfo& inputTensorInfo, |
| 63 | armnn::TensorInfo& outputTensorInfo, |
| 64 | std::vector<float>& inputData, |
| 65 | std::vector<float>& expectedOutputData, |
| 66 | const float gamma, |
| 67 | const float beta, |
| 68 | const float eps) |
| 69 | { |
| 70 | using namespace armnn; |
| 71 | |
| 72 | if (dataLayout == DataLayout::NCHW) |
| 73 | { |
| 74 | PermuteTensorNhwcToNchw<float>(inputTensorInfo, inputData); |
| 75 | PermuteTensorNhwcToNchw<float>(outputTensorInfo, expectedOutputData); |
| 76 | } |
| 77 | |
| 78 | // Builds up the structure of the network |
| 79 | INetworkPtr net = CreateInstanceNormalizationNetwork<DataType::Float32>(inputTensorInfo.GetShape(), |
| 80 | outputTensorInfo.GetShape(), |
| 81 | dataLayout, |
| 82 | gamma, |
| 83 | beta, |
| 84 | eps); |
| 85 | |
| 86 | BOOST_TEST_CHECKPOINT("Create a network"); |
| 87 | |
| 88 | std::map<int, std::vector<float>> inputTensorData = { { 0, inputData } }; |
| 89 | std::map<int, std::vector<float>> expectedOutputTensorData = { { 0, expectedOutputData } }; |
| 90 | |
| 91 | EndToEndLayerTestImpl<DataType::Float32, DataType::Float32>(move(net), |
| 92 | inputTensorData, |
| 93 | expectedOutputTensorData, |
| 94 | backends); |
| 95 | } |
| 96 | |
| 97 | } // anonymous namespace |
| 98 | |
| 99 | void InstanceNormalizationNhwcEndToEndTest1(const std::vector<armnn::BackendId>& defaultBackends) |
| 100 | { |
| 101 | using namespace armnn; |
| 102 | |
| 103 | const float eps = 0.0001f; |
| 104 | const float beta = 0.0f; |
| 105 | const float gamma = 1.0f; |
| 106 | |
| 107 | TensorShape inputShape{2, 2, 2, 2}; |
| 108 | TensorInfo inputTensorInfo(inputShape, DataType::Float32); |
| 109 | |
| 110 | TensorShape outputShape{2, 2, 2, 2}; |
| 111 | TensorInfo outputTensorInfo(outputShape, DataType::Float32); |
| 112 | |
| 113 | std::vector<float> inputData = std::vector<float>( |
| 114 | { |
| 115 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 116 | 0.f, 1.f, |
| 117 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 118 | 0.f, 2.f, |
| 119 | |
| 120 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 121 | 0.f, 2.f, |
| 122 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 123 | 0.f, 4.f, |
| 124 | |
| 125 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 126 | 1.f, -1.f, |
| 127 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 128 | -1.f, 2.f, |
| 129 | |
| 130 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 131 | -1.f, -2.f, |
| 132 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 133 | 1.f, 4.f |
| 134 | }); |
| 135 | |
| 136 | std::vector<float> expectedOutputData = std::vector<float>( |
| 137 | { |
| 138 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 139 | 0.f, -1.1470304f, |
| 140 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 141 | 0.f, -0.22940612f, |
| 142 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 143 | 0.f, -0.22940612f, |
| 144 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 145 | 0.f, 1.6058424f, |
| 146 | |
| 147 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 148 | 0.99995005f, -0.7337929f, |
| 149 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 150 | -0.99995005f, 0.52413774f, |
| 151 | |
| 152 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 153 | -0.99995005f, -1.1531031f, |
| 154 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 155 | 0.99995005f, 1.3627582f |
| 156 | }); |
| 157 | |
| 158 | InstanceNormalizationEndToEnd(defaultBackends, |
| 159 | DataLayout::NHWC, |
| 160 | inputTensorInfo, |
| 161 | outputTensorInfo, |
| 162 | inputData, |
| 163 | expectedOutputData, |
| 164 | gamma, |
| 165 | beta, |
| 166 | eps); |
| 167 | } |
| 168 | |
| 169 | void InstanceNormalizationNchwEndToEndTest1(const std::vector<armnn::BackendId>& defaultBackends) |
| 170 | { |
| 171 | using namespace armnn; |
| 172 | |
| 173 | const float eps = 0.0001f; |
| 174 | const float beta = 0.0f; |
| 175 | const float gamma = 1.0f; |
| 176 | |
| 177 | TensorShape inputShape{2, 2, 2, 2}; |
| 178 | TensorInfo inputTensorInfo(inputShape, DataType::Float32); |
| 179 | |
| 180 | TensorShape outputShape{2, 2, 2, 2}; |
| 181 | TensorInfo outputTensorInfo(outputShape, DataType::Float32); |
| 182 | |
| 183 | std::vector<float> inputData = std::vector<float>( |
| 184 | { |
| 185 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 186 | 0.f, 1.f, |
| 187 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 188 | 0.f, 2.f, |
| 189 | |
| 190 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 191 | 0.f, 2.f, |
| 192 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 193 | 0.f, 4.f, |
| 194 | |
| 195 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 196 | 1.f, -1.f, |
| 197 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 198 | -1.f, 2.f, |
| 199 | |
| 200 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 201 | -1.f, -2.f, |
| 202 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 203 | 1.f, 4.f |
| 204 | }); |
| 205 | |
| 206 | std::vector<float> expectedOutputData = std::vector<float>( |
| 207 | { |
| 208 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 209 | 0.f, -1.1470304f, |
| 210 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 211 | 0.f, -0.22940612f, |
| 212 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 213 | 0.f, -0.22940612f, |
| 214 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 215 | 0.f, 1.6058424f, |
| 216 | |
| 217 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 218 | 0.99995005f, -0.7337929f, |
| 219 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 220 | -0.99995005f, 0.52413774f, |
| 221 | |
| 222 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 223 | -0.99995005f, -1.1531031f, |
| 224 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 225 | 0.99995005f, 1.3627582f |
| 226 | }); |
| 227 | |
| 228 | |
| 229 | InstanceNormalizationEndToEnd(defaultBackends, |
| 230 | DataLayout::NCHW, |
| 231 | inputTensorInfo, |
| 232 | outputTensorInfo, |
| 233 | inputData, |
| 234 | expectedOutputData, |
| 235 | gamma, |
| 236 | beta, |
| 237 | eps); |
| 238 | } |
| 239 | |
| 240 | void InstanceNormalizationNhwcEndToEndTest2(const std::vector<armnn::BackendId>& defaultBackends) |
| 241 | { |
| 242 | using namespace armnn; |
| 243 | |
| 244 | const float eps = 0.0001f; |
| 245 | const float beta = 10.0f; |
| 246 | const float gamma = 2.0f; |
| 247 | |
| 248 | TensorShape inputShape{2, 2, 2, 2}; |
| 249 | TensorShape outputShape{2, 2, 2, 2}; |
| 250 | |
| 251 | TensorInfo outputTensorInfo(outputShape, DataType::Float32); |
| 252 | TensorInfo inputTensorInfo(inputShape, DataType::Float32); |
| 253 | |
| 254 | std::vector<float> inputData = std::vector<float>( |
| 255 | { |
| 256 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 257 | 0.f, 1.f, |
| 258 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 259 | 0.f, 2.f, |
| 260 | |
| 261 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 262 | 0.f, 2.f, |
| 263 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 264 | 0.f, 4.f, |
| 265 | |
| 266 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 267 | 1.f, -1.f, |
| 268 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 269 | -1.f, 2.f, |
| 270 | |
| 271 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 272 | -1.f, -2.f, |
| 273 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 274 | 1.f, 4.f |
| 275 | }); |
| 276 | |
| 277 | std::vector<float> expectedOutputData = std::vector<float>( |
| 278 | { |
| 279 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 280 | 10.f, 7.7059393f, |
| 281 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 282 | 10.f, 9.541187f, |
| 283 | |
| 284 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 285 | 10.f, 9.541187f, |
| 286 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 287 | 10.f, 13.211685f, |
| 288 | |
| 289 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 290 | 11.9999f, 8.532414f, |
| 291 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 292 | 8.0001f, 11.048275f, |
| 293 | |
| 294 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 295 | 8.0001f, 7.693794f, |
| 296 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 297 | 11.9999f, 12.725516f |
| 298 | }); |
| 299 | |
| 300 | InstanceNormalizationEndToEnd(defaultBackends, |
| 301 | DataLayout::NHWC, |
| 302 | inputTensorInfo, |
| 303 | outputTensorInfo, |
| 304 | inputData, |
| 305 | expectedOutputData, |
| 306 | gamma, |
| 307 | beta, |
| 308 | eps); |
| 309 | } |
| 310 | |
| 311 | void InstanceNormalizationNchwEndToEndTest2(const std::vector<armnn::BackendId>& defaultBackends) |
| 312 | { |
| 313 | using namespace armnn; |
| 314 | |
| 315 | const float eps = 0.0001f; |
| 316 | const float beta = 10.0f; |
| 317 | const float gamma = 2.0f; |
| 318 | |
| 319 | TensorShape inputShape{2, 2, 2, 2}; |
| 320 | TensorShape outputShape{2, 2, 2, 2}; |
| 321 | |
| 322 | TensorInfo outputTensorInfo(outputShape, DataType::Float32); |
| 323 | TensorInfo inputTensorInfo(inputShape, DataType::Float32); |
| 324 | |
| 325 | std::vector<float> inputData = std::vector<float>( |
| 326 | { |
| 327 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 328 | 0.f, 1.f, |
| 329 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 330 | 0.f, 2.f, |
| 331 | |
| 332 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 333 | 0.f, 2.f, |
| 334 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 335 | 0.f, 4.f, |
| 336 | |
| 337 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 338 | 1.f, -1.f, |
| 339 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 340 | -1.f, 2.f, |
| 341 | |
| 342 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 343 | -1.f, -2.f, |
| 344 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 345 | 1.f, 4.f |
| 346 | }); |
| 347 | |
| 348 | std::vector<float> expectedOutputData = std::vector<float>( |
| 349 | { |
| 350 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 351 | 10.f, 7.7059393f, |
| 352 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 353 | 10.f, 9.541187f, |
| 354 | |
| 355 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 356 | 10.f, 9.541187f, |
| 357 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 358 | 10.f, 13.211685f, |
| 359 | |
| 360 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 361 | 11.9999f, 8.532414f, |
| 362 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 363 | 8.0001f, 11.048275f, |
| 364 | |
| 365 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 366 | 8.0001f, 7.693794f, |
| 367 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 368 | 11.9999f, 12.725516f |
| 369 | }); |
| 370 | |
| 371 | InstanceNormalizationEndToEnd(defaultBackends, |
| 372 | DataLayout::NCHW, |
| 373 | inputTensorInfo, |
| 374 | outputTensorInfo, |
| 375 | inputData, |
| 376 | expectedOutputData, |
| 377 | gamma, |
| 378 | beta, |
| 379 | eps); |
| 380 | } |