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.
17 #ifndef __TFLREAD_READ_H__
18 #define __TFLREAD_READ_H__
20 #include <mio/tflite/schema_generated.h>
29 template <typename T> std::vector<T> as_index_vector(const flatbuffers::Vector<T> *flat_array)
31 std::vector<T> ret(flat_array->Length());
32 for (uint32_t i = 0; i < flat_array->Length(); i++)
34 ret[i] = flat_array->Get(i);
39 bool is_valid(const tflite::OperatorCode *opcode);
40 bool is_custom(const tflite::OperatorCode *opcode);
41 std::string opcode_name(const tflite::OperatorCode *opcode);
42 const char *tensor_type(const tflite::Tensor *tensor);
43 const char *tensor_name(const tflite::Tensor *tensor);
46 * @brief Loads TF lite file and provides helpers to access attributes
51 using TFliteSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<tflite::SubGraph>>;
52 using TFliteBuffers_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Buffer>>;
53 using TFliteTensors_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Tensor>>;
54 using TFliteOperators_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Operator>>;
57 Reader(const tflite::Model *model);
62 uint32_t version() const { return _version; }
64 const std::vector<const tflite::OperatorCode *> &opcodes() { return _op_codes; }
65 const TFliteBuffers_t *buffers() { return _buffers; }
66 const TFliteTensors_t *tensors() { return _tensors; }
67 const TFliteOperators_t *operators() { return _operators; }
68 const std::vector<int32_t> &inputs() const { return _inputs; }
69 const std::vector<int32_t> &outputs() const { return _outputs; }
71 uint32_t num_subgraph() const { return _subgraphs->Length(); }
73 size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data);
74 tflite::BuiltinOperator builtin_code(const tflite::Operator *op) const;
75 std::string opcode_name(const tflite::Operator *op) const;
78 bool select_subgraph(uint32_t subgraph);
79 const std::string &subgraph_name(void) const { return _subgraph_name; }
80 uint32_t subgraph_index(void) const { return _subgraph_index; }
85 const TFliteSubGraphs_t *_subgraphs{nullptr};
86 const TFliteBuffers_t *_buffers{nullptr};
87 const TFliteTensors_t *_tensors{nullptr};
88 const TFliteOperators_t *_operators{nullptr};
90 uint32_t _subgraph_index;
91 std::string _subgraph_name;
92 std::vector<const tflite::OperatorCode *> _op_codes;
93 std::vector<int32_t> _inputs;
94 std::vector<int32_t> _outputs;
97 } // namespace tflread
99 #endif // __TFLREAD_READ_H__