2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 bool is_valid(const tflite::OperatorCode *opcode)
27 tflite::BuiltinOperator code = opcode->builtin_code();
28 return (tflite::BuiltinOperator_MIN <= code && code <= tflite::BuiltinOperator_MAX);
31 bool is_custom(const tflite::OperatorCode *opcode)
33 tflite::BuiltinOperator code = opcode->builtin_code();
34 return (code == tflite::BuiltinOperator_CUSTOM);
37 std::string opcode_name(const tflite::OperatorCode *opcode)
41 if (!is_valid(opcode))
43 std::ostringstream oss;
48 if (is_custom(opcode))
50 if (!opcode->custom_code())
51 return "(invalid custom)";
53 std::string custom_op = "CUSTOM(";
54 custom_op += opcode->custom_code()->c_str();
59 tflite::BuiltinOperator code = opcode->builtin_code();
60 return tflite::EnumNameBuiltinOperator(code);
63 const char *tensor_type(const tflite::Tensor *tensor)
65 return tflite::EnumNameTensorType(tensor->type());
68 const char *tensor_name(const tflite::Tensor *tensor)
70 static const char *kEmptyTensorName = "(noname)";
72 auto name = tensor->name();
76 return kEmptyTensorName;
79 Reader::Reader(const tflite::Model *model)
81 _version = model->version();
82 _subgraphs = model->subgraphs();
83 _buffers = model->buffers();
84 _metadata = model->metadata();
86 auto opcodes = model->operator_codes();
87 for (const ::tflite::OperatorCode *opcode : *opcodes)
89 _op_codes.push_back(opcode);
93 size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
100 if (auto *buffer = (*_buffers)[buf_idx])
102 if (auto *array = buffer->data())
104 if (size_t size = array->size())
106 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
115 tflite::BuiltinOperator Reader::builtin_code(const tflite::Operator *op) const
117 uint32_t index = op->opcode_index();
118 assert(index < _op_codes.size());
119 const tflite::OperatorCode *opcode = _op_codes.at(index);
121 return opcode->builtin_code();
124 std::string Reader::opcode_name(const tflite::Operator *op) const
126 uint32_t index = op->opcode_index();
127 assert(index < _op_codes.size());
128 const tflite::OperatorCode *opcode = _op_codes.at(index);
130 if (!is_valid(opcode))
132 std::ostringstream oss;
133 oss << "(invalid: " << index << ")";
137 return tflread::opcode_name(opcode);
140 bool Reader::select_subgraph(uint32_t sgindex)
142 _subgraph_index = sgindex;
144 _operators = nullptr;
149 if (_subgraphs->Length() <= sgindex)
155 const tflite::SubGraph *subgraph = (*_subgraphs)[sgindex];
157 auto name = subgraph->name();
158 _subgraph_name = name ? name->c_str() : "(noname)";
160 _tensors = subgraph->tensors();
161 _operators = subgraph->operators();
163 _inputs = as_index_vector(subgraph->inputs());
164 _outputs = as_index_vector(subgraph->outputs());
169 } // namespace tflread