telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 5 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 6 | #include "TfLiteParser.hpp" |
| 7 | |
Matthew Bentham | 39ef3e5 | 2020-01-20 10:09:09 +0000 | [diff] [blame] | 8 | #include <armnn/Descriptors.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include <armnn/Exceptions.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 10 | #include <armnn/Logging.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | #include <armnn/TypesUtils.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 12 | #include <armnn/utility/IgnoreUnused.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 13 | |
| 14 | // armnnUtils: |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 15 | #include <armnnUtils/Permute.hpp> |
| 16 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 17 | #include <ParserHelper.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | #include <VerificationHelpers.hpp> |
| 19 | |
| 20 | // The generated code based on the Tf Lite schema: |
| 21 | #include <schema_generated.h> |
| 22 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 23 | #include <flatbuffers/flexbuffers.h> |
| 24 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 25 | #include <boost/assert.hpp> |
| 26 | #include <boost/format.hpp> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 27 | #include <boost/numeric/conversion/cast.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | |
| 30 | #include <fstream> |
| 31 | #include <algorithm> |
| 32 | #include <limits> |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 33 | #include <numeric> |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 34 | #include <sstream> |
| 35 | |
| 36 | #define ARMNN_THROW_PARSE_EXCEPTION(msg) \ |
| 37 | { \ |
| 38 | throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \ |
| 39 | << ": " \ |
| 40 | << CHECK_LOCATION().AsString()).str()); \ |
| 41 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | |
| 43 | using namespace armnn; |
| 44 | using armnn::CheckLocation; |
| 45 | namespace armnnTfLiteParser |
| 46 | { |
| 47 | namespace |
| 48 | { |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 49 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max(); |
| 51 | |
| 52 | void CheckSubgraph(const TfLiteParser::ModelPtr & model, |
| 53 | size_t subgraphIndex, |
| 54 | const CheckLocation & location) |
| 55 | { |
| 56 | if (model.get() == nullptr) |
| 57 | { |
| 58 | throw ParseException( |
| 59 | boost::str( |
| 60 | boost::format("%1% was called with invalid (null) model. " |
| 61 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 62 | "subgraph:%2% at %3%") % |
| 63 | location.m_Function % |
| 64 | subgraphIndex % |
| 65 | location.FileLine())); |
| 66 | } |
| 67 | else if (subgraphIndex >= model->subgraphs.size()) |
| 68 | { |
| 69 | throw ParseException( |
| 70 | boost::str( |
| 71 | boost::format("%1% was called with an invalid subgraph index. " |
| 72 | "subgraph:%2% at %3%") % |
| 73 | location.m_Function % |
| 74 | subgraphIndex % |
| 75 | location.FileLine())); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \ |
| 80 | CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION()) |
| 81 | |
| 82 | void CheckModel(const TfLiteParser::ModelPtr & model, |
| 83 | size_t subgraphIndex, |
| 84 | size_t operatorIndex, |
| 85 | const CheckLocation & location) |
| 86 | { |
| 87 | if (model.get() == nullptr) |
| 88 | { |
| 89 | throw ParseException( |
| 90 | boost::str( |
| 91 | boost::format("%1% was called with invalid (null) model. " |
| 92 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 93 | "subgraph:%2% operator:%3% at %4%") % |
| 94 | location.m_Function % |
| 95 | subgraphIndex % |
| 96 | operatorIndex % |
| 97 | location.FileLine())); |
| 98 | } |
| 99 | else if (subgraphIndex >= model->subgraphs.size()) |
| 100 | { |
| 101 | throw ParseException( |
| 102 | boost::str( |
| 103 | boost::format("%1% was called with an invalid subgraph index. " |
| 104 | "subgraph:%2% operator:%3% at %4%") % |
| 105 | location.m_Function % |
| 106 | subgraphIndex % |
| 107 | operatorIndex % |
| 108 | location.FileLine())); |
| 109 | } |
| 110 | else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() && |
| 111 | operatorIndex != VIRTUAL_OPERATOR_ID) |
| 112 | { |
| 113 | throw ParseException( |
| 114 | boost::str( |
| 115 | boost::format("%1% was called with an invalid operator index. " |
| 116 | "subgraph:%2% operator:%3% at %4%") % |
| 117 | location.m_Function % |
| 118 | subgraphIndex % |
| 119 | operatorIndex % |
| 120 | location.FileLine())); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \ |
| 125 | CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION()) |
| 126 | |
| 127 | void CheckTensor(const TfLiteParser::ModelPtr & model, |
| 128 | size_t subgraphIndex, |
| 129 | size_t tensorIndex, |
| 130 | const CheckLocation & location) |
| 131 | { |
| 132 | // not checking model, because I assume CHECK_MODEL already run |
| 133 | // and checked that. An assert would do. |
| 134 | BOOST_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function"); |
| 135 | |
| 136 | // also subgraph index should be checked by CHECK_MODEL so |
| 137 | // I only add an assert here |
| 138 | BOOST_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index"); |
| 139 | |
| 140 | // the tensor index is the only one to check here |
| 141 | if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size()) |
| 142 | { |
| 143 | throw ParseException( |
| 144 | boost::str( |
| 145 | boost::format("%1% was called with an invalid tensor index. " |
| 146 | "subgraph:%2% tensor:%3% at %4%") % |
| 147 | location.m_Function % |
| 148 | subgraphIndex % |
| 149 | tensorIndex % |
| 150 | location.FileLine())); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \ |
| 155 | CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION()) |
| 156 | |
| 157 | void CheckTensorPtr(TfLiteParser::TensorRawPtr rawPtr, |
| 158 | const CheckLocation & location) |
| 159 | { |
| 160 | if (rawPtr == nullptr) |
| 161 | { |
| 162 | throw ParseException( |
| 163 | boost::str( |
| 164 | boost::format("%1% was called with a null tensor pointer. " |
| 165 | "at %2%") % |
| 166 | location.m_Function % |
| 167 | location.FileLine())); |
| 168 | |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #define CHECK_TENSOR_PTR(TENSOR_PTR) \ |
| 173 | CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) |
| 174 | |
| 175 | void CheckBuffer(const TfLiteParser::ModelPtr & model, |
| 176 | size_t bufferIndex, |
| 177 | const CheckLocation & location) |
| 178 | { |
| 179 | if (model.get() == nullptr) |
| 180 | { |
| 181 | throw ParseException( |
| 182 | boost::str( |
| 183 | boost::format("%1% was called with invalid (null) model. " |
| 184 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 185 | "buffer:%2% at %3%") % |
| 186 | location.m_Function % |
| 187 | bufferIndex % |
| 188 | location.FileLine())); |
| 189 | } |
| 190 | else if (bufferIndex >= model->buffers.size()) |
| 191 | { |
| 192 | throw ParseException( |
| 193 | boost::str( |
| 194 | boost::format("%1% was called with an invalid buffer index. " |
| 195 | "buffer index:%2% at %3%") % |
| 196 | location.m_Function % |
| 197 | bufferIndex % |
| 198 | location.FileLine())); |
| 199 | } |
| 200 | else if (model->buffers[bufferIndex].get() == nullptr) |
| 201 | { |
| 202 | throw ParseException( |
| 203 | boost::str( |
| 204 | boost::format("The buffer #%1% is null. %3%") % |
| 205 | bufferIndex % |
| 206 | location.AsString())); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \ |
| 211 | CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION()) |
| 212 | |
| 213 | void CheckBufferSize(TfLiteParser::BufferRawPtr bufferPtr, |
| 214 | const armnn::TensorInfo & tensorInfo, |
| 215 | uint32_t bufferId, |
| 216 | const CheckLocation & location) |
| 217 | { |
| 218 | if (bufferPtr == nullptr) |
| 219 | { |
| 220 | throw ParseException( |
| 221 | boost::str( |
| 222 | boost::format("BufferPtr is null for buffer:%1%. %2%") % |
| 223 | bufferId % |
| 224 | location.AsString())); |
| 225 | } |
| 226 | else if(tensorInfo.GetNumElements() > bufferPtr->data.size() || |
| 227 | tensorInfo.GetNumBytes() > bufferPtr->data.size()) |
| 228 | { |
| 229 | std::stringstream ss; |
| 230 | ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. " |
| 231 | << "For tensor: " << tensorInfo.GetShape() |
| 232 | << " expecting: " << tensorInfo.GetNumBytes() << " bytes and " |
| 233 | << tensorInfo.GetNumElements() << " elements. " << location.AsString(); |
| 234 | throw ParseException(ss.str()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \ |
| 239 | CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION()) |
| 240 | |
| 241 | bool IsActivationSupported(tflite::ActivationFunctionType activationType) |
| 242 | { |
| 243 | switch(activationType) |
| 244 | { |
| 245 | case tflite::ActivationFunctionType_NONE: |
| 246 | case tflite::ActivationFunctionType_RELU: |
| 247 | case tflite::ActivationFunctionType_RELU6: |
| 248 | case tflite::ActivationFunctionType_TANH: |
| 249 | { |
| 250 | return true; |
| 251 | } |
| 252 | default: |
| 253 | { |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \ |
| 260 | do { \ |
| 261 | if (IsActivationSupported(OPTION->fused_activation_function) == false) \ |
| 262 | { \ |
| 263 | throw ParseException( \ |
| 264 | boost::str( \ |
| 265 | boost::format("TfLite parser doesn't suppport fused activation: " \ |
| 266 | "%1%/%2% in %3% subgraph:%4% operator:%5% at %6%") % \ |
| 267 | OPTION->fused_activation_function % \ |
| 268 | tflite::EnumNameActivationFunctionType(\ |
| 269 | OPTION->fused_activation_function) % \ |
| 270 | __func__ % \ |
| 271 | SUBGRAPH_INDEX % \ |
| 272 | OPERATOR_INDEX % \ |
| 273 | CHECK_LOCATION().FileLine())); \ |
| 274 | } \ |
| 275 | } while(false) |
| 276 | |
| 277 | |
| 278 | std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t> & in) |
| 279 | { |
| 280 | std::vector<unsigned int> result; |
| 281 | result.reserve(in.size()); |
| 282 | for (auto & i : in) |
| 283 | { |
| 284 | result.push_back(CHECKED_NON_NEGATIVE(i)); |
| 285 | } |
| 286 | return result; |
| 287 | } |
| 288 | |
| 289 | void CalcPadding(uint32_t inputSize, |
| 290 | uint32_t filterSize, |
| 291 | uint32_t stride, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 292 | uint32_t dilation, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 293 | uint32_t& paddingFront, |
| 294 | uint32_t& paddingBack, |
| 295 | tflite::Padding padding) |
| 296 | { |
| 297 | paddingFront = 0; |
| 298 | paddingBack = 0; |
| 299 | if (padding == tflite::Padding_SAME) |
| 300 | { |
| 301 | uint32_t outputSize = (inputSize + stride - 1) / stride; |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 302 | uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1); |
| 303 | uint32_t temp = (outputSize - 1) * stride + dilatedSize; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 304 | if (temp > inputSize) |
| 305 | { |
| 306 | paddingFront = (temp - inputSize) / 2; |
| 307 | paddingBack = (temp - inputSize) - paddingFront; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 312 | armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr, const std::vector<unsigned int>& shapes, |
| 313 | const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3}) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 314 | { |
| 315 | armnn::DataType type; |
| 316 | CHECK_TENSOR_PTR(tensorPtr); |
| 317 | |
| 318 | switch (tensorPtr->type) |
| 319 | { |
| 320 | case tflite::TensorType_UINT8: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 321 | type = armnn::DataType::QAsymmU8; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 322 | break; |
| 323 | case tflite::TensorType_FLOAT32: |
| 324 | type = armnn::DataType::Float32; |
| 325 | break; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 326 | case tflite::TensorType_INT8: |
Keith Davis | 67e6c54 | 2020-02-19 10:08:33 +0000 | [diff] [blame] | 327 | if (tensorPtr->quantization->zero_point.size() == 1) |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 328 | { |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 329 | // Per-tensor |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 330 | type = armnn::DataType::QAsymmS8; |
| 331 | } |
| 332 | else |
| 333 | { |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 334 | // Per-channel |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 335 | type = armnn::DataType::QSymmS8; |
| 336 | } |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 337 | break; |
| 338 | case tflite::TensorType_INT16: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 339 | type = armnn::DataType::QSymmS16; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 340 | break; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 341 | case tflite::TensorType_INT32: |
| 342 | type = armnn::DataType::Signed32; |
| 343 | break; |
| 344 | |
| 345 | default: |
| 346 | { |
| 347 | CheckLocation location = CHECK_LOCATION(); |
| 348 | throw ParseException( |
| 349 | boost::str( |
| 350 | boost::format("Unsupported data type %1% = %2% for tensor: %3%. %4%") % |
| 351 | tensorPtr->type % |
| 352 | tflite::EnumNameTensorType(tensorPtr->type) % |
| 353 | tensorPtr->name % |
| 354 | location.AsString())); |
| 355 | } |
| 356 | } |
Narumol Prangnawarat | 4818d46 | 2019-04-17 11:22:38 +0100 | [diff] [blame] | 357 | std::vector<unsigned int> safeShape = shapes; |
| 358 | if (safeShape.size() == 0) |
| 359 | { |
| 360 | safeShape.push_back(1); |
| 361 | } |
| 362 | |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 363 | float quantizationScale = 0.0f; |
| 364 | int32_t quantizationOffset = 0; |
| 365 | |
| 366 | if (tensorPtr->quantization.get()) |
| 367 | { |
| 368 | if (tensorPtr->quantization->scale.size() <= 1) |
| 369 | { |
| 370 | CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1); |
| 371 | CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1); |
| 372 | |
| 373 | if (tensorPtr->quantization->scale.size() == 1) |
| 374 | { |
| 375 | quantizationScale = tensorPtr->quantization->scale[0]; |
| 376 | } |
| 377 | if (tensorPtr->quantization->zero_point.size() == 1) |
| 378 | { |
| 379 | // NOTE: we lose precision here when converting from 64 bit to 32 |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 380 | // but this is what we support at the moment in ArmNN |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 381 | quantizationOffset = boost::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]); |
| 382 | } |
| 383 | |
| 384 | armnn::TensorInfo result(boost::numeric_cast<unsigned int>(safeShape.size()), |
| 385 | safeShape.data(), |
| 386 | type, |
| 387 | quantizationScale, |
| 388 | quantizationOffset); |
| 389 | |
| 390 | return result; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | std::vector<float> quantizationScales; |
| 395 | std::vector<int32_t> quantizationOffsets; |
| 396 | |
| 397 | // Scale |
| 398 | std::copy(tensorPtr->quantization->scale.begin(), |
| 399 | tensorPtr->quantization->scale.end(), |
| 400 | std::back_inserter(quantizationScales)); |
| 401 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 402 | // QSymmS8 Per-axis |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 403 | armnn::TensorInfo result(boost::numeric_cast<unsigned int>(safeShape.size()), |
| 404 | safeShape.data(), |
| 405 | type, |
| 406 | quantizationScales, |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 407 | dimensionMappings[boost::numeric_cast<unsigned int>( |
| 408 | tensorPtr->quantization->quantized_dimension)]); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 409 | return result; |
| 410 | } |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | armnn::TensorInfo result(boost::numeric_cast<unsigned int>(safeShape.size()), |
| 415 | safeShape.data(), |
| 416 | type, |
| 417 | quantizationScale, |
| 418 | quantizationOffset); |
| 419 | return result; |
| 420 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 423 | armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr, |
| 424 | const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3}) |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 425 | { |
| 426 | auto const & dimensions = AsUnsignedVector(tensorPtr->shape); |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 427 | return ToTensorInfo(tensorPtr, dimensions, dimensionMappings); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 428 | } |
| 429 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 430 | template<typename T> |
| 431 | std::pair<armnn::ConstTensor, std::unique_ptr<T[]>> |
| 432 | CreateConstTensorImpl(TfLiteParser::BufferRawPtr bufferPtr, |
| 433 | TfLiteParser::TensorRawPtr tensorPtr, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 434 | armnn::TensorInfo& tensorInfo, |
| 435 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 436 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 437 | IgnoreUnused(tensorPtr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 438 | BOOST_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null"); |
| 439 | BOOST_ASSERT_MSG(bufferPtr != nullptr, |
| 440 | boost::str( |
| 441 | boost::format("Buffer for buffer:%1% is null") % tensorPtr->buffer).c_str()); |
| 442 | |
| 443 | std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 444 | |
| 445 | if (permutationVector.has_value() && permutationVector.value().GetSize() > 0) |
| 446 | { |
| 447 | tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 448 | armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(), |
| 449 | reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T)); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 450 | } |
| 451 | else |
| 452 | { |
| 453 | ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes()); |
| 454 | } |
| 455 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 456 | return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data)); |
| 457 | } |
| 458 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 459 | armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex) |
| 460 | { |
| 461 | // generate the binding id by shifting the tensor id by 8 bit |
| 462 | // and add the subgraph id, which allows 256 subgraphs |
| 463 | return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex); |
| 464 | } |
| 465 | |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 466 | bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected) |
| 467 | { |
| 468 | const unsigned int actualSize = actual.GetNumDimensions(); |
| 469 | if (actualSize != expected.size()) |
| 470 | { |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | for (unsigned int i = 0u; i < actualSize; i++) |
| 475 | { |
| 476 | if (expected[i] < 0 || |
| 477 | actual[i] != static_cast<unsigned int>(expected[i])) |
| 478 | { |
| 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return true; |
| 484 | } |
| 485 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 486 | } // <anonymous> |
| 487 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 488 | TfLiteParser::TfLiteParser(const Optional<ITfLiteParser::TfLiteParserOptions>& options) |
| 489 | : m_Options(options) |
| 490 | , m_Network(nullptr, nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 491 | , m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParser::ParseUnsupportedOperator) |
| 492 | { |
| 493 | // register supported operators |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 494 | m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParser::ParseAdd; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 495 | m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParser::ParseAveragePool2D; |
| 496 | m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParser::ParseBatchToSpaceND; |
| 497 | m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParser::ParseConcatenation; |
| 498 | m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParser::ParseConv2D; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 499 | m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParser::ParseCustomOperator; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 500 | m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParser::ParseDepthwiseConv2D; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 501 | m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParser::ParseDequantize; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 502 | m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParser::ParseFullyConnected; |
| 503 | m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParser::ParseLogistic; |
| 504 | m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParser::ParseL2Normalization; |
| 505 | m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParser::ParseMaxPool2D; |
| 506 | m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParser::ParseMaximum; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 507 | m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParser::ParseMean; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 508 | m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParser::ParseMinimum; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 509 | m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParser::ParseMul; |
| 510 | m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParser::ParsePack; |
| 511 | m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParser::ParsePad; |
| 512 | m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParser::ParseQuantize; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 513 | m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParser::ParseRelu; |
| 514 | m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParser::ParseRelu6; |
| 515 | m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParser::ParseReshape; |
| 516 | m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParser::ParseResizeBilinear; |
| 517 | m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParser::ParseResizeNearestNeighbor; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 518 | m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParser::ParseSlice; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 519 | m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParser::ParseSoftmax; |
| 520 | m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParser::ParseSpaceToBatchND; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 521 | m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParser::ParseSplit; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 522 | m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParser::ParseSqueeze; |
| 523 | m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParser::ParseStridedSlice; |
| 524 | m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParser::ParseSub; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 525 | m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParser::ParseTanH; |
| 526 | m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParser::ParseTranspose; |
| 527 | m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParser::ParseTransposeConv; |
| 528 | m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParser::ParseUnpack; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 529 | |
| 530 | // register supported custom operators |
| 531 | m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParser::ParseDetectionPostProcess; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void TfLiteParser::ResetParser() |
| 535 | { |
| 536 | m_Network = armnn::INetworkPtr(nullptr, nullptr); |
| 537 | m_Model = nullptr; |
| 538 | m_SubgraphConnections.clear(); |
| 539 | } |
| 540 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 541 | void TfLiteParser::AddBroadcastReshapeLayer(size_t subgraphIndex, |
| 542 | size_t operatorIndex, |
| 543 | IConnectableLayer *layer) |
| 544 | { |
| 545 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 546 | BOOST_ASSERT(layer != nullptr); |
| 547 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 548 | const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex]; |
| 549 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 550 | |
| 551 | BOOST_ASSERT(operatorPtr->inputs.size() > 1); |
| 552 | |
| 553 | uint32_t reshapedInputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[0]); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 554 | TensorRawPtr tensorPtr = subgraphPtr->tensors[reshapedInputId].get(); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 555 | uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[1]); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 556 | TensorRawPtr tensorPtr1 = subgraphPtr->tensors[inputId].get(); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 557 | |
| 558 | armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(tensorPtr); |
| 559 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(tensorPtr1); |
| 560 | |
| 561 | if (inputTensorInfo.GetNumDimensions() < reshapedTensorInfo.GetNumDimensions()) |
| 562 | { |
| 563 | uint32_t id = reshapedInputId; |
| 564 | reshapedInputId = inputId; |
| 565 | inputId = id; |
| 566 | |
| 567 | reshapedTensorInfo = ToTensorInfo(tensorPtr1); |
| 568 | inputTensorInfo = ToTensorInfo(tensorPtr); |
| 569 | } |
| 570 | |
| 571 | uint32_t numDimensions = inputTensorInfo.GetNumDimensions(); |
| 572 | |
| 573 | std::vector<unsigned> reshapedDim; |
| 574 | for (unsigned int i = 0; i < reshapedTensorInfo.GetNumDimensions(); ++i) |
| 575 | { |
| 576 | reshapedDim.push_back(reshapedTensorInfo.GetShape()[i]); |
| 577 | } |
| 578 | |
| 579 | std::vector<unsigned int> reshapedDimensions(numDimensions, 1); |
| 580 | std::copy_backward (reshapedDim.begin(), reshapedDim.end(), reshapedDimensions.end()); |
| 581 | |
| 582 | reshapedTensorInfo.SetShape(armnn::TensorShape{ numDimensions, reshapedDimensions.data() }); |
| 583 | |
| 584 | std::string layerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName()); |
| 585 | armnn::ReshapeDescriptor desc; |
| 586 | desc.m_TargetShape = reshapedTensorInfo.GetShape(); |
| 587 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str()); |
| 588 | |
| 589 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo); |
| 590 | reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 591 | |
| 592 | RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {reshapedInputId}); |
| 593 | |
| 594 | armnn::IInputSlot* input1Slot = &(layer->GetInputSlot(1)); |
| 595 | RegisterConsumerOfTensor(subgraphIndex, inputId, input1Slot); |
| 596 | } |
| 597 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 598 | INetworkPtr TfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile) |
| 599 | { |
| 600 | ResetParser(); |
| 601 | m_Model = LoadModelFromFile(graphFile); |
| 602 | return CreateNetworkFromModel(); |
| 603 | } |
| 604 | |
| 605 | INetworkPtr TfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent) |
| 606 | { |
| 607 | ResetParser(); |
| 608 | m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size()); |
| 609 | return CreateNetworkFromModel(); |
| 610 | } |
| 611 | |
| 612 | INetworkPtr TfLiteParser::CreateNetworkFromModel() |
| 613 | { |
| 614 | m_Network = INetwork::Create(); |
| 615 | BOOST_ASSERT(m_Model.get() != nullptr); |
| 616 | |
| 617 | bool failedToCreate = false; |
| 618 | std::stringstream errors; |
| 619 | |
| 620 | if (m_Model->subgraphs.size() != 1) |
| 621 | { |
| 622 | throw ParseException( |
| 623 | boost::str( |
| 624 | boost::format("Current TfLite parser only supports 1 subgraph. Current one has: %1% %2%") % |
| 625 | m_Model->subgraphs.size() % |
| 626 | CHECK_LOCATION().AsString())); |
| 627 | } |
| 628 | |
| 629 | size_t subgraphIndex = 0; |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 630 | for (SubgraphPtr const & subgraph : m_Model->subgraphs) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 631 | { |
| 632 | m_SubgraphConnections.emplace_back(subgraph->tensors.size()); |
| 633 | |
| 634 | size_t operatorIndex = 0; |
| 635 | for (OperatorPtr const & op : subgraph->operators) |
| 636 | { |
| 637 | try |
| 638 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 639 | auto const & opCodePtr = m_Model->operator_codes[op->opcode_index]; |
| 640 | auto builtinCode = opCodePtr->builtin_code; |
| 641 | |
| 642 | if (builtinCode > tflite::BuiltinOperator_MAX) |
| 643 | { |
| 644 | throw ParseException( |
| 645 | boost::str( |
| 646 | boost::format("Operator code %1% is out of range 0-%2%. " |
| 647 | "subgraph:%3% operator idx:%4%. %5%") % |
| 648 | builtinCode % |
| 649 | tflite::BuiltinOperator_MAX % |
| 650 | subgraphIndex % |
| 651 | operatorIndex % |
| 652 | CHECK_LOCATION().AsString())); |
| 653 | } |
| 654 | |
| 655 | // lookup and call the parser function |
| 656 | auto & parserFunction = m_ParserFunctions[builtinCode]; |
| 657 | (this->*parserFunction)(subgraphIndex, operatorIndex); |
| 658 | } |
| 659 | catch (const ParseException& e) |
| 660 | { |
| 661 | failedToCreate = true; |
| 662 | std::stringstream errorString; |
| 663 | |
| 664 | errorString << "Failed to parse operator #" << operatorIndex |
| 665 | << " within subgraph #" << subgraphIndex |
| 666 | << " error: " << e.what(); |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 667 | ARMNN_LOG(error) << errorString.str(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 668 | |
| 669 | errors << errorString.str() << "\n"; |
| 670 | } |
| 671 | ++operatorIndex; |
| 672 | } |
| 673 | |
| 674 | SetupInputLayers(subgraphIndex); |
| 675 | SetupOutputLayers(subgraphIndex); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 676 | SetupConstantLayers(subgraphIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 677 | |
| 678 | ++subgraphIndex; |
| 679 | } |
| 680 | |
| 681 | if (failedToCreate) |
| 682 | { |
| 683 | // we can skip everything and let the outer exception handler deal with the error |
| 684 | throw ParseException(errors.str()); |
| 685 | } |
| 686 | |
| 687 | // establish the connections from the layer outputs to the inputs of the subsequent layers |
| 688 | for (size_t subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex) |
| 689 | { |
| 690 | for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex) |
| 691 | { |
| 692 | if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr) |
| 693 | { |
| 694 | for (size_t inputSlotIdx = 0; |
| 695 | inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size(); |
| 696 | ++inputSlotIdx) |
| 697 | { |
| 698 | m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect( |
| 699 | *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx])); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return std::move(m_Network); |
| 706 | } |
| 707 | |
| 708 | void TfLiteParser::RegisterProducerOfTensor(size_t subgraphIndex, |
| 709 | size_t tensorIndex, |
| 710 | armnn::IOutputSlot* slot) |
| 711 | { |
| 712 | CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex); |
| 713 | BOOST_ASSERT(m_SubgraphConnections.size() > subgraphIndex); |
| 714 | BOOST_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex); |
| 715 | |
| 716 | TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex]; |
| 717 | |
| 718 | // assuming there is only one producer for that tensor |
| 719 | if (tensorSlots.outputSlot != nullptr) |
| 720 | { |
| 721 | throw ParseException(boost::str( |
| 722 | boost::format("Another layer has already registered itself as the producer of " |
| 723 | "subgraph:%1% tensor:%2% %3%") % |
| 724 | subgraphIndex % |
| 725 | tensorIndex % |
| 726 | CHECK_LOCATION().AsString())); |
| 727 | } |
| 728 | |
| 729 | tensorSlots.outputSlot = slot; |
| 730 | } |
| 731 | |
| 732 | void TfLiteParser::RegisterConsumerOfTensor(size_t subgraphIndex, |
| 733 | size_t tensorIndex, |
| 734 | armnn::IInputSlot* slot) |
| 735 | { |
| 736 | CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex); |
| 737 | BOOST_ASSERT(m_SubgraphConnections.size() > subgraphIndex); |
| 738 | BOOST_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex); |
| 739 | |
| 740 | TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex]; |
| 741 | tensorSlots.inputSlots.push_back(slot); |
| 742 | } |
| 743 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 744 | void TfLiteParser::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex) |
| 745 | { |
| 746 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 747 | |
| 748 | // NOTE: By default we presume the custom operator is not supported |
| 749 | auto customParserFunction = &TfLiteParser::ParseUnsupportedOperator; |
| 750 | |
| 751 | // Identify custom code defined for custom operator |
| 752 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 753 | const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code; |
| 754 | |
| 755 | // Find parser function that correspondes to custom code (if any) |
| 756 | auto iterator = m_CustomParserFunctions.find(customCode); |
| 757 | if (iterator != m_CustomParserFunctions.end()) |
| 758 | { |
| 759 | customParserFunction = iterator->second; |
| 760 | } |
| 761 | |
| 762 | // Run parser function |
| 763 | (this->*customParserFunction)(subgraphIndex, operatorIndex); |
| 764 | } |
| 765 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 766 | void TfLiteParser::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex) |
| 767 | { |
| 768 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 769 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 770 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 771 | |
| 772 | auto opcodeIndex = operatorPtr->opcode_index; |
| 773 | auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code; |
| 774 | |
| 775 | if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported) |
| 776 | { |
| 777 | // Do not add StandInLayer, throw ParseException instead |
| 778 | throw ParseException( |
| 779 | boost::str( |
| 780 | boost::format("Operator not supported. " |
| 781 | "subgraph:%1% operator:%2% " |
| 782 | "opcode_index:%3% opcode:%4% / %5% %6%") % |
| 783 | subgraphIndex % |
| 784 | operatorIndex % |
| 785 | opcodeIndex % |
| 786 | opcode % |
| 787 | tflite::EnumNameBuiltinOperator(opcode) % |
| 788 | CHECK_LOCATION().AsString())); |
| 789 | } |
| 790 | |
| 791 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 792 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 793 | |
| 794 | const unsigned int numInputs = boost::numeric_cast<unsigned int>(inputs.size()); |
| 795 | const unsigned int numOutputs = boost::numeric_cast<unsigned int>(outputs.size()); |
| 796 | |
| 797 | StandInDescriptor descriptor(numInputs, numOutputs); |
| 798 | auto layerName = boost::str(boost::format("StandIn:%1%:%2%:%3%") % subgraphIndex % operatorIndex % opcode); |
| 799 | |
| 800 | // Add a non-executable StandInLayer as a placeholder for any unsupported operator |
| 801 | IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str()); |
| 802 | for (unsigned int i = 0u; i < numOutputs; ++i) |
| 803 | { |
| 804 | layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i])); |
| 805 | } |
| 806 | |
| 807 | auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 808 | auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 809 | |
| 810 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds); |
| 811 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 812 | } |
| 813 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 814 | void TfLiteParser::ParseConv2D(size_t subgraphIndex, size_t operatorIndex) |
| 815 | { |
| 816 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 817 | |
| 818 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 819 | const auto * options = operatorPtr->builtin_options.AsConv2DOptions(); |
| 820 | |
| 821 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 822 | |
| 823 | Convolution2dDescriptor desc; |
| 824 | desc.m_BiasEnabled = false; |
| 825 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 826 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 827 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 828 | desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor); |
| 829 | desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor); |
Kevin May | 83add21 | 2019-03-26 11:39:19 +0000 | [diff] [blame] | 830 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 831 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 832 | CHECK_VALID_SIZE(inputs.size(), 2, 3); |
| 833 | |
| 834 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 835 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 836 | |
| 837 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 838 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 839 | |
| 840 | // assuming input is NHWC |
| 841 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 842 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 843 | |
| 844 | // assuming the filter is OHWI : Output, H, W, Input |
| 845 | // which is essentially the same as NHWC |
| 846 | unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 847 | unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 848 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 849 | CalcPadding(inputHeight, filterHeight, desc.m_StrideY, |
| 850 | desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 851 | CalcPadding(inputWidth, filterWidth, desc.m_StrideX, |
| 852 | desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 853 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 854 | auto filterTensorAndData = CreateConstTensor(inputs[1], |
| 855 | filterTensorInfo, |
| 856 | armnn::Optional<armnn::PermutationVector&>()); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 857 | armnn::IConnectableLayer* layer = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 858 | |
| 859 | auto layerName = boost::str(boost::format("Conv2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 860 | |
| 861 | if (inputs.size() == 3) |
| 862 | { |
| 863 | desc.m_BiasEnabled = true; |
| 864 | armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 865 | auto biasTensorAndData = CreateConstTensor(inputs[2], |
| 866 | biasTensorInfo, |
| 867 | armnn::Optional<armnn::PermutationVector&>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 868 | layer = m_Network->AddConvolution2dLayer(desc, |
| 869 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 870 | Optional<ConstTensor>(biasTensorAndData.first), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 871 | layerName.c_str()); |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | layer = m_Network->AddConvolution2dLayer(desc, |
| 876 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 877 | EmptyOptional(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 878 | layerName.c_str()); |
| 879 | } |
| 880 | |
| 881 | BOOST_ASSERT(layer != nullptr); |
| 882 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 883 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 884 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 885 | |
| 886 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 887 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 888 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 889 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 890 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 891 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 892 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 893 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 894 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 895 | } |
| 896 | |
| 897 | void TfLiteParser::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex) |
| 898 | { |
| 899 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 900 | |
| 901 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 902 | const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions(); |
| 903 | |
| 904 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 905 | |
| 906 | DepthwiseConvolution2dDescriptor desc; |
| 907 | desc.m_BiasEnabled = false; |
| 908 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 909 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 910 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Matthew Jackson | d6a9dee | 2019-07-22 13:53:24 +0100 | [diff] [blame] | 911 | CHECKED_NON_NEGATIVE(options->depth_multiplier); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 912 | |
| 913 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 914 | CHECK_VALID_SIZE(inputs.size(), 2, 3); |
| 915 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 916 | CHECK_VALID_SIZE(outputs.size(), 1); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 917 | desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor); |
| 918 | desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor); |
Kevin May | 83add21 | 2019-03-26 11:39:19 +0000 | [diff] [blame] | 919 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 920 | // Mappings from TensorflowLite filter tensors to the ArmNN filter tensors (ArmNN weights have to be [M, I, H, W]) |
| 921 | PermutationVector permutationVector{ 2, 3, 1, 0 }; // [H, W, I, M] -> [M, I, H, W] |
| 922 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 923 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 924 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1], permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 925 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 926 | // Assuming input is NHWC |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 927 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 928 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 929 | |
| 930 | // TensorflowLite weights come in the format [1, H, W, I * M] |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 931 | unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 932 | unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 933 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 934 | // Reshape weights as [ H, W, I, M ] |
| 935 | filterTensorInfo.SetShape({ filterHeight, |
| 936 | filterWidth, |
| 937 | inputTensorInfo.GetShape()[3], |
| 938 | filterTensorInfo.GetShape()[3] / inputTensorInfo.GetShape()[3] }); |
| 939 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 940 | CalcPadding(inputHeight, filterHeight, desc.m_StrideY, |
| 941 | desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 942 | CalcPadding(inputWidth, filterWidth, desc.m_StrideX, |
| 943 | desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 944 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 945 | auto filterTensorAndData = CreateConstTensor(inputs[1], filterTensorInfo, permutationVector); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 946 | armnn::IConnectableLayer* layer = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 947 | auto layerName = boost::str(boost::format("DepthwiseConv2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 948 | |
| 949 | if (inputs.size() == 3) |
| 950 | { |
| 951 | desc.m_BiasEnabled = true; |
| 952 | TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 953 | auto biasTensorAndData = CreateConstTensor(inputs[2], |
| 954 | biasTensorInfo, |
| 955 | armnn::Optional<armnn::PermutationVector&>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 956 | layer = m_Network->AddDepthwiseConvolution2dLayer(desc, |
| 957 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 958 | Optional<ConstTensor>(biasTensorAndData.first), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 959 | layerName.c_str()); |
| 960 | } |
| 961 | else |
| 962 | { |
| 963 | layer = m_Network->AddDepthwiseConvolution2dLayer(desc, |
| 964 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 965 | EmptyOptional(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 966 | layerName.c_str()); |
| 967 | } |
| 968 | BOOST_ASSERT(layer != nullptr); |
| 969 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 970 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 971 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 972 | |
| 973 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 974 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 975 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 976 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 977 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 978 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 979 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 980 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 981 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 982 | } |
| 983 | |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 984 | void TfLiteParser::ParseDequantize(size_t subgraphIndex, size_t operatorIndex) |
| 985 | { |
| 986 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 987 | |
| 988 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 989 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 990 | |
| 991 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 992 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 993 | |
| 994 | auto layerName = boost::str(boost::format("Dequantize:%1%:%2%") % subgraphIndex % operatorIndex); |
| 995 | |
| 996 | IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str()); |
| 997 | BOOST_ASSERT(layer != nullptr); |
| 998 | |
| 999 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1000 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1001 | |
| 1002 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1003 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1004 | |
| 1005 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1006 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 1007 | } |
| 1008 | |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1009 | void TfLiteParser::ParseTranspose(size_t subgraphIndex, size_t operatorIndex) |
| 1010 | { |
| 1011 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1012 | |
| 1013 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1014 | CHECK_VALID_SIZE(inputs.size(), 1, 2); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1015 | |
| 1016 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1017 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1018 | |
| 1019 | armnn::IConnectableLayer* layer = nullptr; |
| 1020 | auto layerName = boost::str(boost::format("Transpose:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1021 | |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1022 | TransposeDescriptor desc; |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1023 | |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1024 | if (inputs.size() == 2) |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1025 | { |
| 1026 | armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]); |
| 1027 | BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1028 | auto numPermVecElements = permuteTensorInfo.GetNumElements(); |
| 1029 | std::vector<unsigned int> permuteShape(numPermVecElements); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1030 | ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes()); |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1031 | PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements()); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1032 | |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1033 | desc = TransposeDescriptor(permutationVector); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1036 | layer = m_Network->AddTransposeLayer(desc, layerName.c_str()); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1037 | |
| 1038 | BOOST_ASSERT(layer != nullptr); |
| 1039 | |
| 1040 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1041 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1042 | |
| 1043 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1044 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1045 | |
| 1046 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1047 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1048 | } |
| 1049 | |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1050 | void TfLiteParser::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex) |
| 1051 | { |
| 1052 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1053 | |
| 1054 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1055 | const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions(); |
| 1056 | |
| 1057 | TransposeConvolution2dDescriptor desc; |
| 1058 | desc.m_BiasEnabled = false; |
| 1059 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1060 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
| 1061 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1062 | |
| 1063 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1064 | CHECK_VALID_SIZE(inputs.size(), 3); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1065 | |
| 1066 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1067 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1068 | |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1069 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1070 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 1071 | |
| 1072 | // TfLite uses NHWC tensors |
| 1073 | const unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1074 | const unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 1075 | |
| 1076 | const unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 1077 | const unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 1078 | |
| 1079 | CalcPadding(inputHeight, |
| 1080 | filterHeight, |
| 1081 | desc.m_StrideY, |
| 1082 | 1, // DilationY |
| 1083 | desc.m_PadTop, |
| 1084 | desc.m_PadBottom, |
| 1085 | options->padding); |
| 1086 | |
| 1087 | CalcPadding(inputWidth, |
| 1088 | filterWidth, |
| 1089 | desc.m_StrideX, |
| 1090 | 1, // DilationX |
| 1091 | desc.m_PadLeft, |
| 1092 | desc.m_PadRight, |
| 1093 | options->padding); |
| 1094 | |
| 1095 | auto filterTensorAndData = CreateConstTensor(inputs[1], |
| 1096 | filterTensorInfo, |
| 1097 | armnn::Optional<armnn::PermutationVector&>()); |
| 1098 | |
| 1099 | armnn::IConnectableLayer* layer = nullptr; |
| 1100 | auto layerName = boost::str(boost::format("TransposeConv:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1101 | |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1102 | layer = m_Network->AddTransposeConvolution2dLayer(desc, |
| 1103 | filterTensorAndData.first, |
| 1104 | EmptyOptional(), |
| 1105 | layerName.c_str()); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1106 | |
| 1107 | BOOST_ASSERT(layer != nullptr); |
| 1108 | |
| 1109 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1110 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1111 | |
| 1112 | // only the tensors for the inputs are relevant, exclude the const (filter) tensor |
| 1113 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1114 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]}); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1115 | |
| 1116 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1117 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1118 | } |
| 1119 | |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1120 | void TfLiteParser::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex) |
| 1121 | { |
| 1122 | ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average); |
| 1123 | } |
| 1124 | |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1125 | void TfLiteParser::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex) |
| 1126 | { |
| 1127 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1128 | |
| 1129 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1130 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1131 | |
| 1132 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1133 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1134 | |
| 1135 | armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]); |
| 1136 | BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1137 | |
| 1138 | armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]); |
| 1139 | BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1140 | |
| 1141 | std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements()); |
| 1142 | ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes()); |
| 1143 | |
| 1144 | std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements()); |
| 1145 | ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes()); |
| 1146 | |
| 1147 | size_t step = 2; |
| 1148 | std::vector<std::pair<unsigned int, unsigned int>> crops; |
| 1149 | for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i) |
| 1150 | { |
| 1151 | crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]); |
| 1152 | } |
| 1153 | |
| 1154 | armnn::BatchToSpaceNdDescriptor desc; |
| 1155 | desc.m_BlockShape = blockShape; |
| 1156 | desc.m_Crops = crops; |
| 1157 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1158 | |
| 1159 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1160 | |
| 1161 | auto layerName = boost::str(boost::format("BatchToSpaceND:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1162 | IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str()); |
| 1163 | |
| 1164 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1165 | |
| 1166 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1167 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1168 | |
| 1169 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1170 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1171 | } |
| 1172 | |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1173 | void TfLiteParser::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex) |
| 1174 | { |
| 1175 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1176 | |
| 1177 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1178 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1179 | |
| 1180 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1181 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1182 | |
| 1183 | L2NormalizationDescriptor desc; |
| 1184 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1185 | auto layerName = boost::str(boost::format("L2Normalization:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1186 | IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str()); |
| 1187 | |
| 1188 | BOOST_ASSERT(layer != nullptr); |
| 1189 | |
| 1190 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1191 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1192 | |
| 1193 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1194 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1195 | |
| 1196 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1197 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1198 | } |
| 1199 | |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1200 | void TfLiteParser::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex) |
| 1201 | { |
| 1202 | ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max); |
| 1203 | } |
| 1204 | |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1205 | void TfLiteParser::ParseMaximum(size_t subgraphIndex, size_t operatorIndex) |
| 1206 | { |
| 1207 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1208 | |
| 1209 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1210 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1211 | |
| 1212 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1213 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1214 | |
| 1215 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1216 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1217 | |
| 1218 | auto layerName = boost::str(boost::format("Maximum:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1219 | IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str()); |
| 1220 | |
| 1221 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1222 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1223 | |
| 1224 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1225 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1226 | { |
| 1227 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1232 | } |
| 1233 | |
| 1234 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1235 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1236 | } |
| 1237 | |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1238 | void TfLiteParser::ParseMinimum(size_t subgraphIndex, size_t operatorIndex) |
| 1239 | { |
| 1240 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1241 | |
| 1242 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1243 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1244 | |
| 1245 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1246 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1247 | |
| 1248 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1249 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1250 | |
| 1251 | auto layerName = boost::str(boost::format("Minimum:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1252 | IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str()); |
| 1253 | |
| 1254 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1255 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1256 | |
| 1257 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1258 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1259 | { |
| 1260 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1261 | } |
| 1262 | else |
| 1263 | { |
| 1264 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1265 | } |
| 1266 | |
| 1267 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1268 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1269 | } |
| 1270 | |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1271 | void TfLiteParser::ParsePool(size_t subgraphIndex, |
| 1272 | size_t operatorIndex, |
| 1273 | PoolingAlgorithm algorithm) |
| 1274 | { |
| 1275 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1276 | |
| 1277 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1278 | const auto * options = operatorPtr->builtin_options.AsPool2DOptions(); |
| 1279 | |
| 1280 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 1281 | |
| 1282 | std::string layerName; |
| 1283 | |
| 1284 | switch (algorithm) |
| 1285 | { |
| 1286 | case PoolingAlgorithm::Average: |
| 1287 | layerName = |
| 1288 | boost::str(boost::format("AveragePool2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1289 | break; |
| 1290 | case PoolingAlgorithm::Max: |
| 1291 | layerName = |
| 1292 | boost::str(boost::format("MaxPool2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1293 | break; |
| 1294 | default: |
| 1295 | BOOST_ASSERT_MSG(false, "Unsupported Pooling Algorithm"); |
| 1296 | } |
| 1297 | |
| 1298 | Pooling2dDescriptor desc; |
| 1299 | |
| 1300 | desc.m_PoolType = algorithm; |
| 1301 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1302 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
| 1303 | desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width); |
| 1304 | desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height); |
| 1305 | desc.m_PaddingMethod = PaddingMethod::Exclude; |
| 1306 | desc.m_OutputShapeRounding = OutputShapeRounding::Floor; |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1307 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1308 | |
| 1309 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1310 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1311 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1312 | |
| 1313 | // assuming input is NHWC |
| 1314 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1315 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 1316 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 1317 | CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u, |
| 1318 | desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 1319 | CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u, |
| 1320 | desc.m_PadLeft, desc.m_PadRight, options->padding); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1321 | |
| 1322 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1323 | CHECK_VALID_SIZE(outputs.size(), 1); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1324 | |
| 1325 | IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str()); |
| 1326 | |
| 1327 | BOOST_ASSERT(layer != nullptr); |
| 1328 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1329 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1330 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1331 | |
| 1332 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1333 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1334 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1335 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1336 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1337 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1338 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1339 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1340 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1341 | } |
| 1342 | |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1343 | void TfLiteParser::ParseSlice(size_t subgraphIndex, size_t operatorIndex) |
| 1344 | { |
| 1345 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1346 | |
| 1347 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1348 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1349 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1350 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1351 | |
| 1352 | SliceDescriptor desc; |
| 1353 | |
| 1354 | // set begin tensor info for slice descriptor |
| 1355 | armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]); |
| 1356 | BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1357 | |
| 1358 | std::vector<unsigned int> begin(beginTensorInfo.GetNumElements()); |
| 1359 | ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes()); |
| 1360 | |
| 1361 | // set size tensor info for slice descriptor |
| 1362 | armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]); |
| 1363 | BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1364 | |
| 1365 | std::vector<unsigned int> size(sizeTensorInfo.GetNumElements()); |
| 1366 | ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes()); |
| 1367 | desc = SliceDescriptor(begin, size); |
| 1368 | |
| 1369 | auto layerName = boost::str(boost::format("Slice:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1370 | IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str()); |
| 1371 | |
| 1372 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1373 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1374 | |
| 1375 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1376 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1377 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1378 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1379 | |
| 1380 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1381 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1382 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1383 | } |
| 1384 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1385 | void TfLiteParser::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex) |
| 1386 | { |
| 1387 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1388 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1389 | const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions(); |
| 1390 | |
| 1391 | SoftmaxDescriptor desc; |
| 1392 | desc.m_Beta = options->beta; |
| 1393 | |
| 1394 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1395 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1396 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1397 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1398 | |
| 1399 | auto layerName = boost::str(boost::format("Softmax:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1400 | IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str()); |
| 1401 | |
| 1402 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1403 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1404 | |
| 1405 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1406 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1407 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1408 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1409 | |
| 1410 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1411 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1412 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1413 | } |
| 1414 | |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1415 | void TfLiteParser::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex) |
| 1416 | { |
| 1417 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1418 | |
| 1419 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1420 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1421 | |
| 1422 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1423 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1424 | |
| 1425 | armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]); |
| 1426 | BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1427 | |
| 1428 | armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]); |
| 1429 | BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1430 | |
| 1431 | std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements()); |
| 1432 | ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes()); |
| 1433 | |
| 1434 | std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements()); |
| 1435 | ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes()); |
| 1436 | |
| 1437 | size_t step = 2; |
| 1438 | std::vector<std::pair<unsigned int, unsigned int>> padList; |
| 1439 | for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i) |
| 1440 | { |
| 1441 | padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]); |
| 1442 | } |
| 1443 | |
| 1444 | armnn::SpaceToBatchNdDescriptor desc; |
| 1445 | desc.m_BlockShape = blockShape; |
| 1446 | desc.m_PadList = padList; |
| 1447 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1448 | |
| 1449 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1450 | |
| 1451 | auto layerName = boost::str(boost::format("SpaceToBatchND:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1452 | IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str()); |
| 1453 | |
| 1454 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1455 | |
| 1456 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1457 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1458 | |
| 1459 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1460 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1461 | } |
| 1462 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1463 | armnn::TensorInfo TfLiteParser::OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDimsIn, |
| 1464 | const armnn::TensorInfo & inputTensorInfo) |
| 1465 | { |
| 1466 | CHECK_VALID_SIZE(squeezeDimsIn.size(), 0, 1, 2, 3, 4); |
| 1467 | std::vector<uint32_t> squeezeDims = squeezeDimsIn; |
| 1468 | static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; |
| 1469 | |
| 1470 | if (inputTensorInfo.GetNumDimensions() > 4) |
| 1471 | { |
| 1472 | std::stringstream ss; |
| 1473 | ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions() |
| 1474 | << " shape:" << inputTensorInfo.GetShape() << " " |
| 1475 | << CHECK_LOCATION().AsString(); |
| 1476 | throw ParseException(ss.str()); |
| 1477 | } |
| 1478 | |
| 1479 | if (squeezeDims.empty()) |
| 1480 | { |
| 1481 | squeezeDims.assign(dimensionSequence, |
| 1482 | dimensionSequence+inputTensorInfo.GetNumDimensions()); |
| 1483 | } |
| 1484 | |
| 1485 | std::vector<uint32_t> outputDims; |
| 1486 | for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++) |
| 1487 | { |
| 1488 | bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end()); |
| 1489 | auto currentDimension = inputTensorInfo.GetShape()[i]; |
| 1490 | if (skipSqueeze || currentDimension != 1) |
| 1491 | { |
| 1492 | outputDims.push_back(currentDimension); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | if (outputDims.size() > 4) |
| 1497 | { |
| 1498 | std::stringstream ss; |
| 1499 | ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions() |
| 1500 | << " shape:" << inputTensorInfo.GetShape() << " " |
| 1501 | << CHECK_LOCATION().AsString(); |
| 1502 | throw ParseException(ss.str()); |
| 1503 | } |
| 1504 | |
| 1505 | TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()), |
| 1506 | outputDims.data()); |
| 1507 | |
| 1508 | // we need to preserve the tensor type and the quantization data as well |
| 1509 | TensorInfo outTensorInfo = inputTensorInfo; |
| 1510 | outTensorInfo.SetShape(outShape); |
| 1511 | |
| 1512 | return outTensorInfo; |
| 1513 | } |
| 1514 | |
| 1515 | void TfLiteParser::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex) |
| 1516 | { |
| 1517 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1518 | |
| 1519 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1520 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1521 | |
| 1522 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1523 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1524 | |
| 1525 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1526 | const auto * options = operatorPtr->builtin_options.AsSqueezeOptions(); |
| 1527 | |
| 1528 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1529 | armnn::TensorInfo outputTensorInfo = |
| 1530 | TfLiteParser::OutputShapeOfSqueeze(AsUnsignedVector(options->squeeze_dims), |
| 1531 | inputTensorInfo); |
| 1532 | |
| 1533 | ReshapeDescriptor reshapeDesc; |
| 1534 | reshapeDesc.m_TargetShape = outputTensorInfo.GetShape(); |
| 1535 | |
| 1536 | auto layerName = boost::str(boost::format("Squeeze:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1537 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
| 1538 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1539 | |
| 1540 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1541 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1542 | |
| 1543 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1544 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1545 | } |
| 1546 | |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 1547 | void TfLiteParser::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex) |
| 1548 | { |
| 1549 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1550 | |
| 1551 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1552 | CHECK_VALID_SIZE(inputs.size(), 4); |
| 1553 | |
| 1554 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1555 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1556 | |
| 1557 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1558 | const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions(); |
| 1559 | |
| 1560 | StridedSliceDescriptor desc; |
| 1561 | desc.m_BeginMask = options->begin_mask; |
| 1562 | desc.m_EllipsisMask = options->ellipsis_mask; |
| 1563 | desc.m_EndMask = options->end_mask; |
| 1564 | desc.m_NewAxisMask = options->new_axis_mask; |
| 1565 | desc.m_ShrinkAxisMask = options->shrink_axis_mask; |
| 1566 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1567 | |
| 1568 | armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]); |
| 1569 | BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1570 | |
| 1571 | std::vector<int> begin(beginTensorInfo.GetNumElements()); |
| 1572 | ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes()); |
| 1573 | |
| 1574 | armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]); |
| 1575 | BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1576 | |
| 1577 | std::vector<int> end(endTensorInfo.GetNumElements()); |
| 1578 | ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes()); |
| 1579 | |
| 1580 | armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]); |
| 1581 | BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer); |
| 1582 | |
| 1583 | std::vector<int> stride(strideTensorInfo.GetNumElements()); |
| 1584 | ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes()); |
| 1585 | |
| 1586 | desc.m_Begin = begin; |
| 1587 | desc.m_End = end; |
| 1588 | desc.m_Stride = stride; |
| 1589 | |
| 1590 | auto layerName = boost::str(boost::format("StridedSlice:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1591 | IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str()); |
| 1592 | |
| 1593 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1594 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1595 | |
| 1596 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1597 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1598 | |
| 1599 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1600 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1601 | } |
| 1602 | |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 1603 | void TfLiteParser::ParseSub(size_t subgraphIndex, size_t operatorIndex) |
| 1604 | { |
| 1605 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1606 | |
| 1607 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1608 | const auto * options = operatorPtr->builtin_options.AsSubOptions(); |
| 1609 | |
| 1610 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1611 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1612 | |
| 1613 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1614 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1615 | |
| 1616 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1617 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1618 | |
| 1619 | auto layerName = boost::str(boost::format("Sub:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1620 | IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str()); |
| 1621 | |
| 1622 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1623 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1624 | |
| 1625 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1626 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1627 | { |
| 1628 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1629 | } |
| 1630 | else |
| 1631 | { |
| 1632 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1633 | } |
| 1634 | |
| 1635 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1636 | |
| 1637 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1638 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1639 | } |
| 1640 | |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1641 | void TfLiteParser::ParseAdd(size_t subgraphIndex, size_t operatorIndex) |
| 1642 | { |
| 1643 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1644 | |
| 1645 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1646 | const auto * options = operatorPtr->builtin_options.AsAddOptions(); |
| 1647 | |
| 1648 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1649 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1650 | |
| 1651 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1652 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1653 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1654 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1655 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1656 | |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1657 | auto layerName = boost::str(boost::format("Add:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1658 | IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str()); |
| 1659 | |
| 1660 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1661 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1662 | |
| 1663 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1664 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1665 | { |
| 1666 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1667 | } |
| 1668 | else |
| 1669 | { |
| 1670 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1671 | } |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1672 | |
| 1673 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1674 | |
| 1675 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1676 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1677 | } |
| 1678 | |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1679 | void TfLiteParser::ParseMul(size_t subgraphIndex, size_t operatorIndex) |
| 1680 | { |
| 1681 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1682 | |
| 1683 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1684 | const auto * options = operatorPtr->builtin_options.AsMulOptions(); |
| 1685 | |
| 1686 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1687 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1688 | |
| 1689 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1690 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1691 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1692 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1693 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1694 | |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1695 | auto layerName = boost::str(boost::format("Mul:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1696 | IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str()); |
| 1697 | |
| 1698 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1699 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1700 | |
| 1701 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1702 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1703 | { |
| 1704 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1705 | } |
| 1706 | else |
| 1707 | { |
| 1708 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1709 | } |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1710 | |
| 1711 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1712 | |
| 1713 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1714 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1715 | } |
| 1716 | |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 1717 | void TfLiteParser::ParseMean(size_t subgraphIndex, size_t operatorIndex) |
| 1718 | { |
| 1719 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1720 | |
| 1721 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1722 | |
| 1723 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1724 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1725 | |
| 1726 | armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]); |
| 1727 | BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1728 | |
| 1729 | armnn::MeanDescriptor desc; |
| 1730 | std::vector<unsigned int> axis(dimTensorInfo.GetNumElements()); |
| 1731 | ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes()); |
| 1732 | desc.m_Axis = axis; |
| 1733 | |
| 1734 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1735 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1736 | |
| 1737 | desc.m_KeepDims = |
| 1738 | inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? |
| 1739 | true : false; |
| 1740 | |
| 1741 | auto layerName = boost::str(boost::format("Mean:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1742 | IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str()); |
| 1743 | |
| 1744 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1745 | |
| 1746 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1747 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1748 | |
| 1749 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1750 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1751 | } |
| 1752 | |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 1753 | void TfLiteParser::ParsePad(size_t subgraphIndex, size_t operatorIndex) |
| 1754 | { |
| 1755 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1756 | |
| 1757 | TfLiteParser::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1758 | |
| 1759 | TfLiteParser::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1760 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1761 | |
| 1762 | armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]); |
| 1763 | BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1764 | |
| 1765 | std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements()); |
| 1766 | ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes()); |
| 1767 | |
| 1768 | size_t step = 2; |
| 1769 | armnn::PadDescriptor desc; |
| 1770 | for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i) |
| 1771 | { |
| 1772 | desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]); |
| 1773 | } |
| 1774 | |
| 1775 | auto layerName = boost::str(boost::format("Pad:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1776 | IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str()); |
| 1777 | |
| 1778 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1779 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1780 | |
| 1781 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1782 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1783 | |
| 1784 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1785 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1786 | } |
| 1787 | |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 1788 | void TfLiteParser::ParseQuantize(size_t subgraphIndex, size_t operatorIndex) |
| 1789 | { |
| 1790 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1791 | |
| 1792 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1793 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1794 | |
| 1795 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1796 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1797 | |
| 1798 | auto layerName = boost::str(boost::format("Quantize:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1799 | |
| 1800 | IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str()); |
| 1801 | BOOST_ASSERT(layer != nullptr); |
| 1802 | |
| 1803 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1804 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1805 | |
| 1806 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1807 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1808 | |
| 1809 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1810 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 1811 | } |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1812 | |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 1813 | void TfLiteParser::ParseRelu(size_t subgraphIndex, size_t operatorIndex) |
| 1814 | { |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1815 | ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | void TfLiteParser::ParseRelu6(size_t subgraphIndex, size_t operatorIndex) |
| 1819 | { |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1820 | ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu); |
| 1821 | } |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 1822 | |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1823 | void TfLiteParser::ParseLogistic(size_t subgraphIndex, size_t operatorIndex) |
| 1824 | { |
| 1825 | ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid); |
| 1826 | } |
| 1827 | |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 1828 | void TfLiteParser::ParseTanH(size_t subgraphIndex, size_t operatorIndex) |
| 1829 | { |
| 1830 | ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH); |
| 1831 | } |
| 1832 | |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1833 | |
| 1834 | void TfLiteParser::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType) |
| 1835 | { |
| 1836 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 1837 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 1838 | IgnoreUnused(operatorPtr); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 1839 | |
| 1840 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1841 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1842 | |
| 1843 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1844 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1845 | |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1846 | auto layerName = str(boost::format("Activation:")); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 1847 | ActivationDescriptor activationDesc; |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1848 | activationDesc.m_Function = activationType; |
| 1849 | |
| 1850 | switch (activationType) |
| 1851 | { |
| 1852 | case ActivationFunction::ReLu: |
| 1853 | { |
| 1854 | layerName += str(boost::format("RELU:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1855 | break; |
| 1856 | } |
| 1857 | case ActivationFunction::BoundedReLu: |
| 1858 | { |
| 1859 | layerName += str(boost::format("RELU6:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1860 | activationDesc.m_A = 6.0f; |
| 1861 | activationDesc.m_B = 0.0f; |
| 1862 | break; |
| 1863 | } |
| 1864 | case ActivationFunction::Sigmoid: |
| 1865 | { |
| 1866 | layerName += str(boost::format("SIGMOID:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1867 | break; |
| 1868 | } |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 1869 | case ActivationFunction::TanH: |
| 1870 | { |
| 1871 | layerName += str(boost::format("TANH:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1872 | activationDesc.m_A = 1.0f; |
| 1873 | activationDesc.m_B = 1.0f; |
| 1874 | break; |
| 1875 | } |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 1876 | default: |
| 1877 | { |
| 1878 | throw ParseException( |
| 1879 | boost::str(boost::format("Unexpected ActivationFunction[%1%] when creating layerName " |
| 1880 | " %2% ") %static_cast<int>(activationType)% CHECK_LOCATION().AsString())); |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str()); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 1885 | |
| 1886 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 1887 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1888 | |
| 1889 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1890 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1891 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1892 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1893 | |
| 1894 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1895 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1896 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1897 | } |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 1898 | armnn::TensorInfo TfLiteParser::OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, |
| 1899 | const std::vector<int32_t> & targetDimsIn) |
| 1900 | { |
| 1901 | std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end()); |
| 1902 | const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1); |
| 1903 | |
| 1904 | if (stretchDim != targetDimsIn.end()) |
| 1905 | { |
| 1906 | if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end()) |
| 1907 | { |
| 1908 | throw ParseException( |
| 1909 | boost::str( |
| 1910 | boost::format("At most one component of shape can be -1 %1%") % CHECK_LOCATION().AsString())); |
| 1911 | } |
| 1912 | |
| 1913 | auto targetNumElements = |
| 1914 | boost::numeric_cast<unsigned int>( |
| 1915 | std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>())); |
| 1916 | |
| 1917 | auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim)); |
| 1918 | outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements; |
| 1919 | } |
| 1920 | |
| 1921 | TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data()); |
| 1922 | |
| 1923 | TensorInfo reshapeInfo = inputTensorInfo; |
| 1924 | reshapeInfo.SetShape(outputShape); |
| 1925 | |
| 1926 | return reshapeInfo; |
| 1927 | } |
| 1928 | |
| 1929 | void TfLiteParser::ParseReshape(size_t subgraphIndex, size_t operatorIndex) |
| 1930 | { |
| 1931 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1932 | |
| 1933 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 1934 | |
| 1935 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1936 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1937 | |
| 1938 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1939 | const auto * options = operatorPtr->builtin_options.AsReshapeOptions(); |
| 1940 | |
| 1941 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 1942 | armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]); |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 1943 | |
| 1944 | std::vector<int32_t> targetShape; |
| 1945 | if (inputs.size() > 1 && inputs[1] != nullptr) |
| 1946 | { |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 1947 | if (inputs[1]->is_variable) |
| 1948 | { |
| 1949 | ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported"); |
| 1950 | } |
| 1951 | |
| 1952 | if (inputs[1]->shape.size() != 1) |
| 1953 | { |
| 1954 | ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor"); |
| 1955 | } |
| 1956 | |
| 1957 | if (inputs[1]->type != tflite::TensorType_INT32) |
| 1958 | { |
| 1959 | ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type"); |
| 1960 | } |
| 1961 | |
| 1962 | auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1963 | auto vals = reinterpret_cast<const int32_t*>(bufferPtr->data.data()); |
| 1964 | for (int i=0; i < inputs[1]->shape[0]; i++) |
| 1965 | { |
| 1966 | targetShape.push_back(vals[i]); |
| 1967 | } |
Derek Lamberti | f4a953f | 2020-03-17 14:25:57 +0000 | [diff] [blame] | 1968 | |
| 1969 | if (options != nullptr && |
| 1970 | options->new_shape.empty() == false && |
| 1971 | options->new_shape != targetShape) |
| 1972 | { |
| 1973 | ARMNN_THROW_PARSE_EXCEPTION("Target shape defined in reshape parameters and as input tensor but " |
| 1974 | "the values do not match"); |
| 1975 | } |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 1976 | } |
| 1977 | else |
| 1978 | { |
| 1979 | if (options == nullptr) |
| 1980 | { |
| 1981 | ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. " |
| 1982 | "At least one method required"); |
| 1983 | } |
| 1984 | |
| 1985 | targetShape = options->new_shape; |
| 1986 | } |
| 1987 | |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 1988 | armnn::TensorInfo reshapeOutputTensorInfo = |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 1989 | TfLiteParser::OutputShapeOfReshape(inputTensorInfo, targetShape); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 1990 | |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 1991 | // Check for valid input size and that reshape parameters equal output shape |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 1992 | const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape(); |
| 1993 | if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape)) |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 1994 | { |
| 1995 | std::stringstream ss; |
| 1996 | ss << "New shape defined in reshape parameters " |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 1997 | << reshapeOutputTensorShape |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 1998 | << " does not equal output shape " |
| 1999 | << actualOutputTensorInfo.GetShape() |
| 2000 | << ": " |
| 2001 | << CHECK_LOCATION().AsString(); |
| 2002 | throw ParseException(ss.str()); |
| 2003 | } |
| 2004 | |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2005 | ReshapeDescriptor reshapeDesc; |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2006 | reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape(); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2007 | |
| 2008 | auto layerName = boost::str(boost::format("Reshape:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2009 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2010 | layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2011 | |
| 2012 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2013 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2014 | |
| 2015 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2016 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2017 | } |
| 2018 | |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2019 | void TfLiteParser::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex) |
| 2020 | { |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2021 | ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear); |
| 2022 | } |
| 2023 | |
| 2024 | void TfLiteParser::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex) |
| 2025 | { |
| 2026 | ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor); |
| 2027 | } |
| 2028 | |
| 2029 | void TfLiteParser::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod) |
| 2030 | { |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2031 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2032 | |
| 2033 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2034 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2035 | |
| 2036 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2037 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2038 | |
| 2039 | armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]); |
| 2040 | |
| 2041 | // Data for the parsed tensor args (size) must be stored locally. |
| 2042 | std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements()); |
| 2043 | |
| 2044 | BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2045 | ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes()); |
| 2046 | |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2047 | ResizeDescriptor desc; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2048 | desc.m_Method = resizeMethod; |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2049 | desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]); |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2050 | desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]); |
| 2051 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2052 | |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2053 | auto layerName = str(boost::format("Resize:")); |
| 2054 | |
| 2055 | switch (resizeMethod) |
| 2056 | { |
| 2057 | case ResizeMethod::Bilinear: |
| 2058 | { |
| 2059 | layerName += str(boost::format("BILINEAR:%1%:%2%") % subgraphIndex % operatorIndex); |
Sang-Hoon Park | 820eb14 | 2020-01-08 10:25:24 +0000 | [diff] [blame] | 2060 | |
| 2061 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2062 | const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions(); |
| 2063 | |
| 2064 | desc.m_BilinearAlignCorners = options->align_corners; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2065 | break; |
| 2066 | } |
| 2067 | case ResizeMethod::NearestNeighbor: |
| 2068 | { |
| 2069 | layerName += str(boost::format("NEARESTNEIGHBOR:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2070 | break; |
| 2071 | } |
| 2072 | default: |
| 2073 | { |
| 2074 | throw ParseException( |
| 2075 | boost::str(boost::format("Unexpected ResizeMethod[%1%] when creating layerName " |
| 2076 | " %2% ") %static_cast<int>(resizeMethod)% CHECK_LOCATION().AsString())); |
| 2077 | } |
| 2078 | } |
| 2079 | |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2080 | IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str()); |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2081 | |
| 2082 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2083 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2084 | |
| 2085 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2086 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2087 | |
| 2088 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2089 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2090 | } |
| 2091 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2092 | void TfLiteParser::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex) |
| 2093 | { |
| 2094 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2095 | |
| 2096 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2097 | const auto * options = operatorPtr->builtin_options.AsConcatenationOptions(); |
| 2098 | |
| 2099 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 2100 | |
| 2101 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2102 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2103 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2104 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2105 | unsigned int numConcatView = static_cast<unsigned int>(inputs.size()); |
| 2106 | uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions(); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2107 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2108 | const unsigned int concatDimInput = static_cast<unsigned int>( |
| 2109 | (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank)); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2110 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2111 | OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank); |
| 2112 | concatDescriptor.SetConcatAxis(concatDimInput); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2113 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2114 | unsigned int mergeDimOrigin = 0; |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2115 | |
| 2116 | for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex) |
| 2117 | { |
| 2118 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]); |
| 2119 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2120 | // This set up concatDescriptor view origin |
| 2121 | armnnUtils::ProcessConcatInputTensorInfo( |
| 2122 | inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | auto layerName = boost::str(boost::format("Concatenation:%1%:%2%") % subgraphIndex % operatorIndex); |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 2126 | IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str()); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2127 | |
| 2128 | BOOST_ASSERT(layer != nullptr); |
| 2129 | |
| 2130 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2131 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2132 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2133 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2134 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2135 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes}); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2136 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2137 | // add fused activation layer |
| 2138 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2139 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2140 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2141 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2142 | } |
| 2143 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2144 | void TfLiteParser::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex) |
| 2145 | { |
| 2146 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2147 | |
| 2148 | const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2149 | const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions(); |
| 2150 | |
| 2151 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 2152 | |
| 2153 | FullyConnectedDescriptor desc; |
| 2154 | desc.m_BiasEnabled = false; |
Nattapat Chaimanowong | d8eee59 | 2018-10-26 10:24:14 +0100 | [diff] [blame] | 2155 | desc.m_TransposeWeightMatrix = true; |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2156 | |
| 2157 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2158 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2159 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2160 | |
| 2161 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 2162 | |
| 2163 | // Fully Connected Layer accepts two dimensional weights input |
| 2164 | int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions()); |
| 2165 | if (weightsDimension != 2) |
| 2166 | { |
| 2167 | throw ParseException( |
| 2168 | boost::str( |
| 2169 | boost::format( |
| 2170 | "Dimension %1% for Fully Connected weights is not supported by Armnn. " |
| 2171 | "Node %2%") |
| 2172 | % weightsDimension |
| 2173 | % CHECK_LOCATION().AsString())); |
| 2174 | } |
| 2175 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2176 | auto filterTensorAndData = CreateConstTensor(inputs[1], |
| 2177 | filterTensorInfo, |
| 2178 | armnn::Optional<armnn::PermutationVector&>()); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 2179 | armnn::IConnectableLayer* layer = nullptr; |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2180 | auto layerName = boost::str(boost::format("FullyConnected:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2181 | |
| 2182 | if (inputs.size() == 3) |
| 2183 | { |
| 2184 | desc.m_BiasEnabled = true; |
| 2185 | TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2186 | auto biasTensorAndData = CreateConstTensor(inputs[2], |
| 2187 | biasTensorInfo, |
| 2188 | armnn::Optional<armnn::PermutationVector&>()); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2189 | layer = m_Network->AddFullyConnectedLayer(desc, |
| 2190 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 2191 | Optional<ConstTensor>(biasTensorAndData.first), |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2192 | layerName.c_str()); |
| 2193 | } |
| 2194 | else |
| 2195 | { |
| 2196 | layer = m_Network->AddFullyConnectedLayer(desc, |
| 2197 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 2198 | EmptyOptional(), |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2199 | layerName.c_str()); |
| 2200 | } |
| 2201 | BOOST_ASSERT(layer != nullptr); |
| 2202 | |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2203 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2204 | |
| 2205 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2206 | |
| 2207 | if (inputTensorInfo.GetNumDimensions() > 2) |
| 2208 | { |
| 2209 | // Add reshape to flatten to 2D [batch_size, input_size], |
| 2210 | // where "input_size" corresponds to the number of inputs to the layer, |
| 2211 | // matching the second dimension of weights, |
| 2212 | // and "batch_size" is calculated by dividing the number of elements by "input_size". |
| 2213 | std::vector<unsigned int> reshapedDimensions(2); |
| 2214 | reshapedDimensions[1] = filterTensorInfo.GetShape()[1]; |
| 2215 | reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1]; |
| 2216 | |
| 2217 | if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0) |
| 2218 | { |
| 2219 | throw ParseException( |
| 2220 | boost::str( |
| 2221 | boost::format( |
| 2222 | "Failed to deduce input tensor shape from filter size %1%") |
| 2223 | % reshapedDimensions[1] |
| 2224 | % CHECK_LOCATION().AsString())); |
| 2225 | } |
| 2226 | |
| 2227 | armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]); |
| 2228 | reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() }); |
| 2229 | |
| 2230 | std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName()); |
| 2231 | armnn::ReshapeDescriptor desc; |
| 2232 | desc.m_TargetShape = reshapedTensorInfo.GetShape(); |
| 2233 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str()); |
| 2234 | |
| 2235 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo); |
| 2236 | reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 2237 | |
| 2238 | RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]}); |
| 2239 | } |
| 2240 | else |
| 2241 | { |
| 2242 | // register the input connection slot for the layer |
| 2243 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 2244 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2245 | } |
| 2246 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2247 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2248 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2249 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2250 | // we need to add the activation layer and fortunately we don't need to care about the data layout |
| 2251 | armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0, |
| 2252 | options->fused_activation_function); |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2253 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2254 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 2255 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2256 | RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]}); |
| 2257 | } |
| 2258 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2259 | void TfLiteParser::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex) |
| 2260 | { |
| 2261 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2262 | |
| 2263 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2264 | |
| 2265 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2266 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2267 | CHECK_VALID_SIZE(outputs.size(), 4); |
| 2268 | |
| 2269 | // Obtain custom options from flexbuffers |
| 2270 | auto custom_options = operatorPtr->custom_options; |
| 2271 | const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap(); |
| 2272 | |
| 2273 | // Obtain descriptor information from tf lite |
| 2274 | DetectionPostProcessDescriptor desc; |
| 2275 | desc.m_MaxDetections = m["max_detections"].AsUInt32(); |
| 2276 | desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32(); |
| 2277 | desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat(); |
| 2278 | desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat(); |
| 2279 | desc.m_NumClasses = m["num_classes"].AsUInt32(); |
| 2280 | desc.m_ScaleH = m["h_scale"].AsFloat(); |
| 2281 | desc.m_ScaleW = m["w_scale"].AsFloat(); |
| 2282 | desc.m_ScaleX = m["x_scale"].AsFloat(); |
| 2283 | desc.m_ScaleY = m["y_scale"].AsFloat(); |
| 2284 | |
keidav01 | 07d58c7 | 2019-02-26 11:57:39 +0000 | [diff] [blame] | 2285 | if (!(m["use_regular_nms"].IsNull())) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2286 | { |
keidav01 | 07d58c7 | 2019-02-26 11:57:39 +0000 | [diff] [blame] | 2287 | desc.m_UseRegularNms = m["use_regular_nms"].AsBool(); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2288 | } |
| 2289 | if (!(m["detections_per_class"].IsNull())) |
| 2290 | { |
| 2291 | desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32(); |
| 2292 | } |
| 2293 | |
| 2294 | if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f) |
| 2295 | { |
| 2296 | throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold " |
| 2297 | "must be positive and less than or equal to 1."); |
| 2298 | } |
| 2299 | |
| 2300 | armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]); |
| 2301 | auto anchorTensorAndData = CreateConstTensor(inputs[2], anchorTensorInfo, |
| 2302 | armnn::Optional<armnn::PermutationVector&>()); |
| 2303 | |
| 2304 | auto layerName = boost::str(boost::format("DetectionPostProcess:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2305 | IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData.first, |
| 2306 | layerName.c_str()); |
| 2307 | |
| 2308 | BOOST_ASSERT(layer != nullptr); |
| 2309 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2310 | // The model does not specify the output shapes. |
| 2311 | // The output shapes are calculated from the max_detection and max_classes_per_detection. |
| 2312 | unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection; |
| 2313 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 }); |
| 2314 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox }); |
| 2315 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox }); |
| 2316 | m_OverridenOutputShapes.push_back({ 1 }); |
| 2317 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2318 | for (unsigned int i = 0 ; i < outputs.size() ; ++i) |
| 2319 | { |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2320 | armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2321 | layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo); |
| 2322 | } |
| 2323 | |
| 2324 | // Register the input connection slots for the layer, connections are made after all layers have been created |
| 2325 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 2326 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2327 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 2328 | |
| 2329 | // Register the output connection slots for the layer, connections are made after all layers have been created |
| 2330 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2331 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0], |
| 2332 | outputTensorIndexes[1], |
| 2333 | outputTensorIndexes[2], |
| 2334 | outputTensorIndexes[3]}); |
| 2335 | } |
| 2336 | |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2337 | /// The TfLite Pack operator is equivalent to the ArmNN Stack operator |
| 2338 | void TfLiteParser::ParsePack(size_t subgraphIndex, size_t operatorIndex) |
| 2339 | { |
| 2340 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2341 | |
| 2342 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2343 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2344 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2345 | |
| 2346 | if (inputs.size() < 1) |
| 2347 | { |
| 2348 | throw ParseException("Pack must have at least one input."); |
| 2349 | } |
| 2350 | |
| 2351 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2352 | const auto* options = operatorPtr->builtin_options.AsPackOptions(); |
| 2353 | |
| 2354 | StackDescriptor desc; |
| 2355 | desc.m_Axis = static_cast<uint32_t>(options->axis); |
| 2356 | desc.m_NumInputs = static_cast<uint32_t>(inputs.size()); |
| 2357 | |
| 2358 | // Use the tensor shape of the first input as the "correct" input shape in the descriptor |
| 2359 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2360 | desc.m_InputShape = inputTensorInfo.GetShape(); |
| 2361 | |
| 2362 | auto layerName = boost::str(boost::format("Pack:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2363 | IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str()); |
| 2364 | |
| 2365 | BOOST_ASSERT(layer != nullptr); |
| 2366 | |
| 2367 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 2368 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2369 | |
| 2370 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2371 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes}); |
| 2372 | |
| 2373 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2374 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2375 | } |
| 2376 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2377 | void TfLiteParser::ParseUnpack(size_t subgraphIndex, size_t operatorIndex) |
| 2378 | { |
| 2379 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2380 | |
| 2381 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2382 | const auto * options = operatorPtr->builtin_options.AsUnpackOptions(); |
| 2383 | |
| 2384 | // This unpackAxis indicates the axis to unpack |
| 2385 | const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis); |
| 2386 | |
| 2387 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2388 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2389 | |
| 2390 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2391 | |
| 2392 | if (unpackAxis >= inputTensorInfo.GetNumDimensions()) |
| 2393 | { |
| 2394 | throw ParseException( |
| 2395 | boost::str( |
| 2396 | boost::format( |
| 2397 | "The unpack axis: %1% cannot be greater than or equal to " |
| 2398 | "the number of input dimension %2% %3%") |
| 2399 | % unpackAxis |
| 2400 | % inputTensorInfo.GetNumDimensions() |
| 2401 | % CHECK_LOCATION().AsString())); |
| 2402 | } |
| 2403 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2404 | unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num); |
| 2405 | // If num is not defined, automatically infer from the length of the dimension axis. |
| 2406 | if(unpackNum == 0) |
| 2407 | { |
| 2408 | unpackNum = inputTensorInfo.GetShape()[unpackAxis]; |
| 2409 | } |
| 2410 | |
| 2411 | // If unpack number cannot be inferred and is still zero, throw ParseException. |
| 2412 | if(unpackNum == 0) |
| 2413 | { |
| 2414 | throw ParseException("Number to unpack must greater than zero."); |
| 2415 | } |
| 2416 | |
| 2417 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2418 | CHECK_VALID_SIZE(outputs.size(), unpackNum); |
| 2419 | |
| 2420 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
| 2421 | std::vector<unsigned int> unpackDimSizes(inputDimSize); |
| 2422 | |
| 2423 | // Add current input shape to unpackDimSizes |
| 2424 | for (unsigned int i = 0; i < inputDimSize; ++i) |
| 2425 | { |
| 2426 | unpackDimSizes[i] = inputTensorInfo.GetShape()[i]; |
| 2427 | } |
| 2428 | |
| 2429 | if (unpackDimSizes[unpackAxis] != unpackNum) |
| 2430 | { |
| 2431 | throw ParseException("Number to unpack must be the same as length of the dimension to " |
| 2432 | "unpack along."); |
| 2433 | } |
| 2434 | |
| 2435 | unpackDimSizes[unpackAxis] /= unpackNum; |
| 2436 | |
| 2437 | SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size())); |
| 2438 | for (unsigned int j = 0; j < unpackNum; ++j) |
| 2439 | { |
| 2440 | // Set the size of the views. |
| 2441 | for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx) |
| 2442 | { |
| 2443 | splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]); |
| 2444 | } |
| 2445 | splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j); |
| 2446 | } |
| 2447 | |
| 2448 | auto layerName = boost::str(boost::format("Unpack:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2449 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
| 2450 | |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2451 | TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()), |
| 2452 | unpackDimSizes.data()); |
| 2453 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2454 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2455 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2456 | |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2457 | // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter. |
| 2458 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 2459 | { |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 2460 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k]); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2461 | std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName()); |
| 2462 | armnn::ReshapeDescriptor desc; |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 2463 | desc.m_TargetShape = outputTensorInfo.GetShape(); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2464 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str()); |
| 2465 | |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 2466 | layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape, |
| 2467 | outputTensorInfo.GetDataType(), |
| 2468 | outputTensorInfo.GetQuantizationScale(), |
| 2469 | outputTensorInfo.GetQuantizationOffset())); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2470 | layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0)); |
| 2471 | |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 2472 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2473 | |
| 2474 | uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]); |
| 2475 | armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0)); |
| 2476 | RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot); |
| 2477 | } |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2480 | void TfLiteParser::ParseSplit(size_t subgraphIndex, size_t operatorIndex) |
| 2481 | { |
| 2482 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2483 | |
| 2484 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2485 | const auto * options = operatorPtr->builtin_options.AsSplitOptions(); |
| 2486 | |
| 2487 | const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits); |
| 2488 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2489 | // If number of splits cannot be inferred and is zero, throw ParseException. |
| 2490 | if(numSplits == 0) |
| 2491 | { |
| 2492 | throw ParseException("Number to splits must greater than zero."); |
| 2493 | } |
| 2494 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2495 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2496 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2497 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2498 | CHECK_VALID_SIZE(outputs.size(), numSplits); |
| 2499 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2500 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]); |
| 2501 | armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2502 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2503 | BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer); |
| 2504 | std::vector<unsigned int> axisData(axisTensorInfo.GetNumElements()); |
| 2505 | ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes()); |
| 2506 | |
| 2507 | BOOST_ASSERT(axisTensorInfo.GetNumElements() == 1); |
| 2508 | const unsigned int splitDim = axisData[0]; |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2509 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2510 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2511 | if (inputDimSize > MaxNumOfTensorDimensions) |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2512 | { |
| 2513 | throw ParseException( |
| 2514 | boost::str( |
| 2515 | boost::format( |
| 2516 | "The number of dimensions: %1% for input tensors of the " |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2517 | "split op cannot be greater than %2% %3%") |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2518 | % inputTensorInfo.GetNumDimensions() |
| 2519 | % MaxNumOfTensorDimensions |
| 2520 | % CHECK_LOCATION().AsString())); |
| 2521 | } |
| 2522 | |
| 2523 | std::vector<unsigned int> splitterDimSizes(inputDimSize); |
| 2524 | |
| 2525 | // Add current input shape to splitterDimSizes |
| 2526 | for (unsigned int i = 0; i < inputDimSize; ++i) |
| 2527 | { |
| 2528 | splitterDimSizes[i] = inputTensorInfo.GetShape()[i]; |
| 2529 | } |
| 2530 | |
| 2531 | if (splitterDimSizes[splitDim] % numSplits != 0) |
| 2532 | { |
| 2533 | throw ParseException("Number of splits must evenly divide the dimension"); |
| 2534 | } |
| 2535 | splitterDimSizes[splitDim] /= numSplits; |
| 2536 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2537 | SplitterDescriptor splitDesc(numSplits, inputDimSize); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2538 | for (unsigned int j = 0; j < numSplits; ++j) |
| 2539 | { |
| 2540 | // Set the size of the views. |
| 2541 | for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx) |
| 2542 | { |
| 2543 | splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]); |
| 2544 | } |
| 2545 | splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j); |
| 2546 | } |
| 2547 | |
| 2548 | auto layerName = boost::str(boost::format("Split:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2549 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
| 2550 | |
| 2551 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2552 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]}); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2553 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2554 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 2555 | { |
Francis Murtagh | 98d6b3d | 2019-10-21 10:52:54 +0100 | [diff] [blame] | 2556 | armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k]); |
| 2557 | layer->GetOutputSlot(k).SetTensorInfo(tensorInfo); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2561 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2562 | } |
| 2563 | |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2564 | armnn::IConnectableLayer* TfLiteParser::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer, |
| 2565 | unsigned int outputSlot, |
| 2566 | tflite::ActivationFunctionType activationType) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2567 | { |
| 2568 | ActivationDescriptor activationDesc; |
| 2569 | std::string layerName = prevLayer->GetName(); |
| 2570 | |
| 2571 | switch(activationType) |
| 2572 | { |
| 2573 | case tflite::ActivationFunctionType_NONE: |
| 2574 | { |
| 2575 | // this is a no-op: return previous layer |
| 2576 | return prevLayer; |
| 2577 | } |
| 2578 | case tflite::ActivationFunctionType_RELU: |
| 2579 | { |
| 2580 | activationDesc.m_Function = ActivationFunction::ReLu; |
| 2581 | layerName += ":RELU"; |
| 2582 | break; |
| 2583 | } |
| 2584 | case tflite::ActivationFunctionType_RELU6: |
| 2585 | { |
| 2586 | activationDesc.m_Function = ActivationFunction::BoundedReLu; |
| 2587 | activationDesc.m_A = 6.0f; |
| 2588 | activationDesc.m_B = 0.0f; |
| 2589 | layerName += ":RELU6"; |
| 2590 | break; |
| 2591 | } |
| 2592 | case tflite::ActivationFunctionType_TANH: |
| 2593 | { |
| 2594 | activationDesc.m_Function = ActivationFunction::TanH; |
| 2595 | activationDesc.m_A = 1.0f; |
| 2596 | activationDesc.m_B = 1.0f; |
| 2597 | layerName += ":TANH"; |
| 2598 | break; |
| 2599 | } |
| 2600 | |
| 2601 | // I only put these here as a reminder what others we could support |
| 2602 | case tflite::ActivationFunctionType_RELU_N1_TO_1: |
| 2603 | case tflite::ActivationFunctionType_SIGN_BIT: |
| 2604 | default: |
| 2605 | { |
| 2606 | throw ParseException( |
| 2607 | boost::str( |
| 2608 | boost::format("TfLite parser doesn't suppport fused activation: " |
| 2609 | "%1%/%2% %3% ") % |
| 2610 | activationType % |
| 2611 | tflite::EnumNameActivationFunctionType(activationType) % |
| 2612 | CHECK_LOCATION().AsString())); |
| 2613 | |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | IConnectableLayer* activationLayer = |
| 2618 | m_Network->AddActivationLayer(activationDesc, layerName.c_str()); |
| 2619 | |
| 2620 | auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot); |
| 2621 | prevOutputSlot.Connect(activationLayer->GetInputSlot(0)); |
| 2622 | activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo()); |
| 2623 | return activationLayer; |
| 2624 | } |
| 2625 | |
| 2626 | TfLiteParser::ModelPtr TfLiteParser::LoadModelFromFile(const char * fileName) |
| 2627 | { |
| 2628 | if (fileName == nullptr) |
| 2629 | { |
| 2630 | throw InvalidArgumentException(boost::str(boost::format("Invalid (null) file name %1%") % |
| 2631 | CHECK_LOCATION().AsString())); |
| 2632 | } |
| 2633 | boost::system::error_code errorCode; |
| 2634 | boost::filesystem::path pathToFile(fileName); |
| 2635 | if (!boost::filesystem::exists(pathToFile, errorCode)) |
| 2636 | { |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2637 | std::string locationString = CHECK_LOCATION().AsString(); |
| 2638 | std::string msg = boost::str(boost::format("Cannot find the file (%1%) errorCode: %2% %3%") % |
| 2639 | fileName % |
| 2640 | errorCode % |
| 2641 | locationString); |
| 2642 | throw FileNotFoundException(msg); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2643 | } |
| 2644 | std::ifstream file(fileName, std::ios::binary); |
| 2645 | std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); |
| 2646 | return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()), |
| 2647 | fileContent.size()); |
| 2648 | } |
| 2649 | |
| 2650 | TfLiteParser::ModelPtr TfLiteParser::LoadModelFromBinary(const uint8_t * binaryContent, size_t len) |
| 2651 | { |
| 2652 | if (binaryContent == nullptr) |
| 2653 | { |
| 2654 | throw InvalidArgumentException(boost::str(boost::format("Invalid (null) binary content %1%") % |
| 2655 | CHECK_LOCATION().AsString())); |
| 2656 | } |
| 2657 | flatbuffers::Verifier verifier(binaryContent, len); |
| 2658 | if (verifier.VerifyBuffer<tflite::Model>() == false) |
| 2659 | { |
| 2660 | throw ParseException( |
| 2661 | boost::str(boost::format("Buffer doesn't conform to the expected Tensorflow Lite " |
| 2662 | "flatbuffers format. size:%1% %2%") % |
| 2663 | len % |
| 2664 | CHECK_LOCATION().AsString())); |
| 2665 | } |
| 2666 | return tflite::UnPackModel(binaryContent); |
| 2667 | } |
| 2668 | |
| 2669 | TfLiteParser::TensorRawPtrVector TfLiteParser::GetInputs(const ModelPtr & model, |
| 2670 | size_t subgraphIndex, |
| 2671 | size_t operatorIndex) |
| 2672 | { |
| 2673 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
| 2674 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2675 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 2676 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2677 | |
| 2678 | size_t inputCount = operatorPtr->inputs.size(); |
| 2679 | TensorRawPtrVector result(inputCount); |
| 2680 | for (size_t i=0; i<inputCount; ++i) |
| 2681 | { |
| 2682 | uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2683 | result[i] = subgraphPtr->tensors[inputId].get(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2684 | } |
| 2685 | return result; |
| 2686 | } |
| 2687 | |
| 2688 | TfLiteParser::TensorRawPtrVector TfLiteParser::GetOutputs(const ModelPtr & model, |
| 2689 | size_t subgraphIndex, |
| 2690 | size_t operatorIndex) |
| 2691 | { |
| 2692 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
| 2693 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2694 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 2695 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2696 | |
| 2697 | size_t outputCount = operatorPtr->outputs.size(); |
| 2698 | TensorRawPtrVector result(outputCount); |
| 2699 | for (size_t i=0; i<outputCount; ++i) |
| 2700 | { |
| 2701 | uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]); |
| 2702 | CHECK_TENSOR(model, subgraphIndex, outputId); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2703 | result[i] = subgraphPtr->tensors[outputId].get(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2704 | } |
| 2705 | return result; |
| 2706 | } |
| 2707 | |
| 2708 | TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphInputs(const ModelPtr & model, |
| 2709 | size_t subgraphIndex) |
| 2710 | { |
| 2711 | CHECK_SUBGRAPH(model, subgraphIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2712 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2713 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2714 | size_t inputCount = subgraphPtr->inputs.size(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2715 | TensorIdRawPtrVector result(inputCount); |
| 2716 | for (size_t i=0; i<inputCount; ++i) |
| 2717 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2718 | uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2719 | CHECK_TENSOR(model, subgraphIndex, inputId); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2720 | result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2721 | } |
| 2722 | return result; |
| 2723 | } |
| 2724 | |
| 2725 | TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphOutputs(const ModelPtr & model, |
| 2726 | size_t subgraphIndex) |
| 2727 | { |
| 2728 | CHECK_SUBGRAPH(model, subgraphIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2729 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2730 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2731 | size_t outputCount = subgraphPtr->outputs.size(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2732 | TensorIdRawPtrVector result(outputCount); |
| 2733 | for (size_t i=0; i<outputCount; ++i) |
| 2734 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2735 | uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]); |
| 2736 | result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2737 | } |
| 2738 | return result; |
| 2739 | } |
| 2740 | |
| 2741 | std::vector<int32_t>& TfLiteParser::GetInputTensorIds(const ModelPtr& model, |
| 2742 | size_t subgraphIndex, |
| 2743 | size_t operatorIndex) |
| 2744 | { |
| 2745 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2746 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 2747 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2748 | return operatorPtr->inputs; |
| 2749 | } |
| 2750 | |
| 2751 | std::vector<int32_t>& TfLiteParser::GetOutputTensorIds(const ModelPtr& model, |
| 2752 | size_t subgraphIndex, |
| 2753 | size_t operatorIndex) |
| 2754 | { |
| 2755 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2756 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 2757 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2758 | return operatorPtr->outputs; |
| 2759 | } |
| 2760 | |
| 2761 | void TfLiteParser::RegisterInputSlots(size_t subgraphIndex, |
| 2762 | size_t operatorIndex, |
| 2763 | IConnectableLayer* layer, |
| 2764 | const std::vector<unsigned int>& tensorIndexes) |
| 2765 | { |
| 2766 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2767 | BOOST_ASSERT(layer != nullptr); |
| 2768 | if (tensorIndexes.size() != layer->GetNumInputSlots()) |
| 2769 | { |
| 2770 | throw ParseException( |
| 2771 | boost::str(boost::format("The number of tensor inputs (%1%) does not match the number expected (%2%)" |
| 2772 | " for subgraph:%3% operator index:%4% %5%") % |
| 2773 | tensorIndexes.size() % |
| 2774 | layer->GetNumInputSlots() % |
| 2775 | subgraphIndex % |
| 2776 | operatorIndex % |
| 2777 | CHECK_LOCATION().AsString())); |
| 2778 | } |
| 2779 | |
| 2780 | for (unsigned int slotIndex = 0; slotIndex < layer->GetNumInputSlots(); ++slotIndex) |
| 2781 | { |
| 2782 | unsigned int tensorIndex = tensorIndexes[slotIndex]; |
| 2783 | armnn::IInputSlot* slot = &(layer->GetInputSlot(slotIndex)); |
| 2784 | RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | void TfLiteParser::RegisterOutputSlots(size_t subgraphIndex, |
| 2789 | size_t operatorIndex, |
| 2790 | IConnectableLayer* layer, |
| 2791 | const std::vector<unsigned int>& tensorIndexes) |
| 2792 | { |
| 2793 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2794 | BOOST_ASSERT(layer != nullptr); |
| 2795 | if (tensorIndexes.size() != layer->GetNumOutputSlots()) |
| 2796 | { |
| 2797 | throw ParseException( |
| 2798 | boost::str(boost::format("The number of tensor outputs (%1%) does not match the number expected (%2%)" |
| 2799 | " for subgraph:%3% operator index:%4% %5%") % |
| 2800 | tensorIndexes.size() % |
| 2801 | layer->GetNumOutputSlots() % |
| 2802 | subgraphIndex % |
| 2803 | operatorIndex % |
| 2804 | CHECK_LOCATION().AsString())); |
| 2805 | } |
| 2806 | |
| 2807 | for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex) |
| 2808 | { |
| 2809 | unsigned int tensorIndex = tensorIndexes[slotIndex]; |
| 2810 | armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex)); |
| 2811 | RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | void TfLiteParser::SetupInputLayers(size_t subgraphIndex) |
| 2816 | { |
| 2817 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 2818 | |
| 2819 | auto inputs = GetSubgraphInputs(m_Model, subgraphIndex); |
| 2820 | for (auto const & tensorIdAndPtr : inputs) |
| 2821 | { |
| 2822 | auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first); |
| 2823 | IConnectableLayer* layer = |
| 2824 | m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str()); |
| 2825 | |
| 2826 | auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second); |
| 2827 | layer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 2828 | |
| 2829 | RegisterOutputSlots(subgraphIndex, |
| 2830 | VIRTUAL_OPERATOR_ID, |
| 2831 | layer, |
| 2832 | { static_cast<uint32_t>(tensorIdAndPtr.first) }); |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | void TfLiteParser::SetupOutputLayers(size_t subgraphIndex) |
| 2837 | { |
| 2838 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 2839 | |
| 2840 | auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex); |
| 2841 | for (auto const & tensorIdAndPtr : outputs) |
| 2842 | { |
| 2843 | auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first); |
| 2844 | IConnectableLayer* layer = |
| 2845 | m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str()); |
| 2846 | |
| 2847 | RegisterInputSlots(subgraphIndex, |
| 2848 | VIRTUAL_OPERATOR_ID, |
| 2849 | layer, |
| 2850 | { static_cast<uint32_t>(tensorIdAndPtr.first) }); |
| 2851 | } |
| 2852 | } |
| 2853 | |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 2854 | void TfLiteParser::SetupConstantLayers(size_t subgraphIndex) |
| 2855 | { |
| 2856 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 2857 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2858 | const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex]; |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 2859 | for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex) |
| 2860 | { |
| 2861 | for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex) |
| 2862 | { |
| 2863 | if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr && |
| 2864 | m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0) |
| 2865 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 2866 | TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get(); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 2867 | armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr); |
| 2868 | auto tensorAndData = CreateConstTensor(tensorPtr, |
| 2869 | tensorInfo, |
| 2870 | armnn::Optional<armnn::PermutationVector&>()); |
| 2871 | |
| 2872 | std::string layerName = boost::str(boost::format("Constant:%1%") % tensorPtr->name); |
| 2873 | IConnectableLayer *layer = |
| 2874 | m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str()); |
| 2875 | |
| 2876 | layer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 2877 | RegisterOutputSlots(subgraphIndex, |
| 2878 | VIRTUAL_OPERATOR_ID, |
| 2879 | layer, |
| 2880 | { tensorIndex }); |
| 2881 | |
| 2882 | } |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2887 | // example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer); |
| 2888 | TfLiteParser::BufferRawPtr TfLiteParser::GetBuffer(const ModelPtr& model, size_t bufferIndex) |
| 2889 | { |
| 2890 | CHECK_BUFFER(model, bufferIndex); |
| 2891 | return model->buffers[bufferIndex].get(); |
| 2892 | } |
| 2893 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2894 | template<typename T> |
| 2895 | std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage> |
| 2896 | TfLiteParser::CreateConstTensorAndStoreData(TfLiteParser::BufferRawPtr bufferPtr, |
| 2897 | TfLiteParser::TensorRawPtr tensorPtr, |
| 2898 | armnn::TensorInfo& tensorInfo, |
| 2899 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
| 2900 | { |
| 2901 | auto constData = CreateConstTensorImpl<T>(bufferPtr, |
| 2902 | tensorPtr, |
| 2903 | tensorInfo, |
| 2904 | permutationVector); |
| 2905 | TfLiteParser::SupportedDataStorage storage(std::move(constData.second)); |
| 2906 | return std::make_pair(constData.first, std::move(storage)); |
| 2907 | } |
| 2908 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2909 | std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage> |
| 2910 | TfLiteParser::CreateConstTensor(TensorRawPtr tensorPtr, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2911 | armnn::TensorInfo& tensorInfo, |
| 2912 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2913 | { |
| 2914 | CHECK_TENSOR_PTR(tensorPtr); |
| 2915 | auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer); |
| 2916 | CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer); |
| 2917 | |
| 2918 | switch (tensorInfo.GetDataType()) |
| 2919 | { |
| 2920 | case armnn::DataType::Float32: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2921 | return CreateConstTensorAndStoreData<float>(bufferPtr, |
| 2922 | tensorPtr, |
| 2923 | tensorInfo, |
| 2924 | permutationVector); |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 2925 | case armnn::DataType::QAsymmU8: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2926 | return CreateConstTensorAndStoreData<uint8_t>(bufferPtr, |
| 2927 | tensorPtr, |
| 2928 | tensorInfo, |
| 2929 | permutationVector); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 2930 | case armnn::DataType::QSymmS8: |
| 2931 | return CreateConstTensorAndStoreData<int8_t>(bufferPtr, |
| 2932 | tensorPtr, |
| 2933 | tensorInfo, |
| 2934 | permutationVector); |
Keith Davis | 67e6c54 | 2020-02-19 10:08:33 +0000 | [diff] [blame] | 2935 | case armnn::DataType::QAsymmS8: |
| 2936 | return CreateConstTensorAndStoreData<int8_t>(bufferPtr, |
| 2937 | tensorPtr, |
| 2938 | tensorInfo, |
| 2939 | permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2940 | case armnn::DataType::Signed32: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2941 | return CreateConstTensorAndStoreData<int32_t>(bufferPtr, |
| 2942 | tensorPtr, |
| 2943 | tensorInfo, |
| 2944 | permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2945 | default: |
| 2946 | { |
| 2947 | std::stringstream errString; |
| 2948 | errString << "Unexpected datatype when creating const tensor: " |
| 2949 | << armnn::GetDataTypeName(tensorInfo.GetDataType()) |
| 2950 | << " shape:" << tensorInfo.GetShape() |
| 2951 | << CHECK_LOCATION().AsString(); |
| 2952 | throw ParseException(errString.str()); |
| 2953 | } |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | BindingPointInfo TfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId, |
| 2958 | const std::string& name) const |
| 2959 | { |
| 2960 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 2961 | auto inputs = GetSubgraphInputs(m_Model, subgraphId); |
| 2962 | for (auto const & input : inputs) |
| 2963 | { |
| 2964 | if (input.second->name == name) |
| 2965 | { |
| 2966 | auto bindingId = GenerateLayerBindingId(subgraphId, input.first); |
| 2967 | return std::make_pair(bindingId, ToTensorInfo(input.second)); |
| 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | std::stringstream bindings; |
| 2972 | for (auto const & input : inputs) |
| 2973 | { |
| 2974 | bindings << "'" << input.second->name << "' "; |
| 2975 | } |
| 2976 | |
| 2977 | throw ParseException( |
| 2978 | boost::str( |
| 2979 | boost::format("No input binding found for subgraph:%1% and name:%2%. " |
| 2980 | "Possible inputs are: [%3%] %4%") % |
| 2981 | subgraphId % |
| 2982 | name % |
| 2983 | bindings.str() % |
| 2984 | CHECK_LOCATION().AsString())); |
| 2985 | } |
| 2986 | |
| 2987 | BindingPointInfo TfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId, |
| 2988 | const std::string& name) const |
| 2989 | { |
| 2990 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 2991 | auto outputs = GetSubgraphOutputs(m_Model, subgraphId); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2992 | for (unsigned int i = 0; i < outputs.size(); ++i) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2993 | { |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2994 | auto const output = outputs[i]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2995 | if (output.second->name == name) |
| 2996 | { |
| 2997 | auto bindingId = GenerateLayerBindingId(subgraphId, output.first); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2998 | std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ? |
| 2999 | m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape); |
| 3000 | return std::make_pair(bindingId, ToTensorInfo(output.second, shape)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3001 | } |
| 3002 | } |
| 3003 | |
| 3004 | std::stringstream bindings; |
| 3005 | for (auto const & output : outputs) |
| 3006 | { |
| 3007 | bindings << "'" << output.second->name << "' "; |
| 3008 | } |
| 3009 | |
| 3010 | throw ParseException( |
| 3011 | boost::str( |
| 3012 | boost::format("No output binding found for subgraph:%1% and name:%2%. " |
| 3013 | "Possible outputs are: [%3%] %4%") % |
| 3014 | subgraphId % |
| 3015 | name % |
| 3016 | bindings.str() % |
| 3017 | CHECK_LOCATION().AsString())); |
| 3018 | } |
| 3019 | |
| 3020 | size_t TfLiteParser::GetSubgraphCount() const |
| 3021 | { |
| 3022 | return m_Model->subgraphs.size(); |
| 3023 | } |
| 3024 | |
| 3025 | std::vector<std::string> TfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const |
| 3026 | { |
| 3027 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 3028 | auto inputs = GetSubgraphInputs(m_Model, subgraphId); |
| 3029 | std::vector<std::string> result; |
| 3030 | result.reserve(inputs.size()); |
| 3031 | for (auto const & input : inputs) |
| 3032 | { |
| 3033 | result.push_back(input.second->name); |
| 3034 | } |
| 3035 | return result; |
| 3036 | } |
| 3037 | |
| 3038 | std::vector<std::string> TfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const |
| 3039 | { |
| 3040 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 3041 | auto outputs = GetSubgraphOutputs(m_Model, subgraphId); |
| 3042 | std::vector<std::string> result; |
| 3043 | result.reserve(outputs.size()); |
| 3044 | for (auto const & output : outputs) |
| 3045 | { |
| 3046 | result.push_back(output.second->name); |
| 3047 | } |
| 3048 | return result; |
| 3049 | } |
| 3050 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3051 | ITfLiteParser* ITfLiteParser::CreateRaw(const Optional<ITfLiteParser::TfLiteParserOptions>& options) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3052 | { |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3053 | return new TfLiteParser(options); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3054 | } |
| 3055 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3056 | ITfLiteParserPtr ITfLiteParser::Create(const Optional<ITfLiteParser::TfLiteParserOptions>& options) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3057 | { |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3058 | return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | void ITfLiteParser::Destroy(ITfLiteParser* parser) |
| 3062 | { |
| 3063 | delete parser; |
| 3064 | } |
| 3065 | |
| 3066 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data) |
| 3067 | : m_FloatData(std::move(data)) |
| 3068 | , m_Uint8Data(nullptr) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 3069 | , m_Int8Data(nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3070 | , m_Int32Data(nullptr) |
| 3071 | { |
| 3072 | } |
| 3073 | |
| 3074 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data) |
| 3075 | : m_FloatData(nullptr) |
| 3076 | , m_Uint8Data(std::move(data)) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 3077 | , m_Int8Data(nullptr) |
| 3078 | , m_Int32Data(nullptr) |
| 3079 | { |
| 3080 | } |
| 3081 | |
| 3082 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data) |
| 3083 | : m_FloatData(nullptr) |
| 3084 | , m_Uint8Data(nullptr) |
| 3085 | , m_Int8Data(std::move(data)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3086 | , m_Int32Data(nullptr) |
| 3087 | { |
| 3088 | } |
| 3089 | |
| 3090 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data) |
| 3091 | : m_FloatData(nullptr) |
| 3092 | , m_Uint8Data(nullptr) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 3093 | , m_Int8Data(nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3094 | , m_Int32Data(std::move(data)) |
| 3095 | { |
| 3096 | } |
| 3097 | |
| 3098 | } // armnnTfLiteParser |