Johan Alfvén | 673683b | 2022-09-05 09:39:47 +0200 | [diff] [blame] | 1 | # Copyright (C) 2020-2022 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # Functions used to read from a TensorFlow Lite format file. |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 18 | import os.path |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 19 | import struct |
| 20 | import sys |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 21 | |
| 22 | import numpy as np |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 23 | |
Louis Verhaard | 678645b | 2020-06-15 15:22:47 +0200 | [diff] [blame] | 24 | from .errors import InputFileError |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 25 | from .nn_graph import Graph |
| 26 | from .nn_graph import Subgraph |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 27 | from .operation import create_activation_function |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 28 | from .operation import Op |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 29 | from .operation import Operation |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 30 | from .reader_util import align_tensor_indices_to_nng |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 31 | from .reader_util import clone_and_reshape_tensor |
| 32 | from .reader_util import decode_str |
| 33 | from .reader_util import fixup_tensors |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 34 | from .tensor import QuantizationParameters |
| 35 | from .tensor import Tensor |
| 36 | from .tflite.BuiltinOperator import BuiltinOperator |
| 37 | from .tflite.Model import Model |
| 38 | from .tflite_mapping import builtin_operator_map |
| 39 | from .tflite_mapping import DataType |
| 40 | from .tflite_mapping import datatype_map |
| 41 | from .tflite_mapping import datatype_map_numpy |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 42 | |
| 43 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 44 | class TFLiteSubgraph: |
| 45 | def __init__(self, graph, subgraph): |
| 46 | self.graph = graph |
| 47 | self.name = decode_str(subgraph.Name()) |
| 48 | |
| 49 | self.tensors = [] |
| 50 | for idx in range(subgraph.TensorsLength()): |
| 51 | self.tensors.append(self.parse_tensor(subgraph.Tensors(idx))) |
| 52 | |
| 53 | for idx in range(subgraph.OperatorsLength()): |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 54 | self.parse_operator(idx, subgraph.Operators(idx)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 55 | |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 56 | self.outputs = self.get_tensors_from_indices_remove_duplicates(subgraph.OutputsAsNumpy(), "output") |
| 57 | self.inputs = self.get_tensors_from_indices_remove_duplicates(subgraph.InputsAsNumpy(), "input") |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 58 | fixup_tensors(self.inputs, self.tensors) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 59 | |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 60 | def get_tensors_from_indices_remove_duplicates(self, indices, warning_str): |
| 61 | tensors = [] |
| 62 | for idx in indices: |
| 63 | tensor = self.tensors[idx] |
| 64 | if tensor not in tensors: |
| 65 | tensors.append(tensor) |
| 66 | else: |
| 67 | print( |
| 68 | "Warning: Subgraph {0} tensor ({1}) with idx = {2} already seen. Removing the duplicate.".format( |
| 69 | warning_str, tensor, idx |
| 70 | ) |
| 71 | ) |
| 72 | |
| 73 | return tensors |
| 74 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 75 | def parse_tensor(self, tens_data): |
| 76 | np_shape = tens_data.ShapeAsNumpy() |
| 77 | shape = list(np_shape) if type(np_shape) is np.ndarray else [] |
| 78 | name = decode_str(tens_data.Name()) |
Dwight Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 79 | tens_dtype = tens_data.Type() |
| 80 | dtype = datatype_map[tens_dtype] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 81 | tens = Tensor(shape, dtype, name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 82 | quant = tens_data.Quantization() |
Fredrik Svedberg | 8d0f489 | 2021-02-16 21:59:50 +0100 | [diff] [blame] | 83 | tens.is_variable = tens_data.IsVariable() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 84 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 85 | tens.quantization = QuantizationParameters() |
Tim Hall | e4e58e1 | 2020-05-08 09:50:21 +0100 | [diff] [blame] | 86 | if quant is not None: |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 87 | tens.quantization.min = self.len1_array_to_scalar(quant.MinAsNumpy()) |
| 88 | tens.quantization.max = self.len1_array_to_scalar(quant.MaxAsNumpy()) |
| 89 | tens.quantization.scale_f32 = self.len1_array_to_scalar(quant.ScaleAsNumpy()) |
| 90 | tens.quantization.zero_point = self.len1_array_to_scalar(quant.ZeroPointAsNumpy()) |
Fredrik Svedberg | cc8569f | 2021-11-01 14:25:29 +0100 | [diff] [blame] | 91 | tens.quantization.quant_dim = quant.QuantizedDimension() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 92 | |
| 93 | if dtype == DataType.uint8: |
| 94 | tens.quantization.quant_min = 0 |
| 95 | tens.quantization.quant_max = (1 << dtype.bits) - 1 |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 96 | elif dtype in (DataType.int8, DataType.int16, DataType.int32, DataType.int64): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 97 | tens.quantization.quant_min = -(1 << (dtype.bits - 1)) |
| 98 | tens.quantization.quant_max = (1 << (dtype.bits - 1)) - 1 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 99 | |
| 100 | if tens.quantization.scale_f32 is None and tens.quantization.zero_point is None: |
| 101 | tens.quantization = None |
| 102 | |
| 103 | tens.values = None |
| 104 | buf = self.graph.buffers[tens_data.Buffer()] |
Louis Verhaard | f4e12be | 2020-12-18 14:23:06 +0100 | [diff] [blame] | 105 | if buf is not None: |
| 106 | np_dtype = datatype_map_numpy[tens_dtype] |
| 107 | if dtype == DataType.string: |
| 108 | tens.values = np.array(buf.view(np_dtype)) |
| 109 | else: |
| 110 | tens.values = np.array(buf.view(np_dtype).reshape(shape)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 111 | return tens |
| 112 | |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 113 | def parse_operator(self, op_index, op_data): |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 114 | op_type, opt_serializer, custom_code, indices = self.graph.operator_codes[op_data.OpcodeIndex()] |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 115 | inputs = [self.tensors[idx] if idx != -1 else None for idx in op_data.InputsAsNumpy()] |
| 116 | outputs = [self.tensors[idx] if idx != -1 else None for idx in op_data.OutputsAsNumpy()] |
Fredrik Svedberg | 8d0f489 | 2021-02-16 21:59:50 +0100 | [diff] [blame] | 117 | intermediates = [] |
| 118 | if op_data.IntermediatesLength(): |
| 119 | intermediates = [self.tensors[idx] if idx != -1 else None for idx in op_data.IntermediatesAsNumpy()] |
| 120 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 121 | name = "unknown_op_name" |
| 122 | if len(outputs): |
| 123 | name = outputs[0].name |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 124 | inputs = align_tensor_indices_to_nng(op_type, indices, inputs) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 125 | op = Operation(op_type, name) |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 126 | op.op_index = op_index |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 127 | op.inputs = inputs |
| 128 | op.outputs = outputs |
Fredrik Svedberg | 8d0f489 | 2021-02-16 21:59:50 +0100 | [diff] [blame] | 129 | op.intermediates = intermediates |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 130 | for out in op.outputs: |
| 131 | out.ops = [op] |
| 132 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 133 | if op.type.is_depthwise_conv2d_op() or op.type.is_conv2d_op() or op.type == Op.FullyConnected: |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 134 | if inputs[1].values is not None: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 135 | if op.type == Op.FullyConnected: |
Patrik Gustavsson | 6ae0e42 | 2020-11-04 12:43:50 +0100 | [diff] [blame] | 136 | inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 0), False) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 137 | else: |
Patrik Gustavsson | 6ae0e42 | 2020-11-04 12:43:50 +0100 | [diff] [blame] | 138 | inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 2, 3, 0), False) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 139 | if op.type.needs_bias() and len(inputs) <= op_type.info.indices.biases[0]: |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 140 | # No Bias tensor |
| 141 | inputs.append(None) |
Patrik Gustavsson | e2dbed2 | 2020-10-06 10:14:36 +0200 | [diff] [blame] | 142 | if inputs[-1] and inputs[-1].values is not None: |
Patrik Gustavsson | 3435958 | 2020-11-03 10:24:08 +0100 | [diff] [blame] | 143 | # Since bias tensor is used for both bias and scale, |
Johan Alfvén | 53605be | 2022-10-26 12:52:17 +0200 | [diff] [blame^] | 144 | # a clone with a unique equivalence_id is needed. |
| 145 | inputs[-1] = clone_and_reshape_tensor(inputs[-1], None, True) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 146 | |
| 147 | if opt_serializer is not None: |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 148 | op.attrs = opt_serializer.deserialize(op_data) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 149 | |
Johan Alfvén | 673683b | 2022-09-05 09:39:47 +0200 | [diff] [blame] | 150 | if op_type == Op.While: |
| 151 | # Attach the actual nng subgraphs to the op |
| 152 | cond_subgraph_index = op.attrs["cond_subgraph_index"] |
| 153 | body_subgraph_index = op.attrs["body_subgraph_index"] |
| 154 | op.attrs["subgraph"] = ( |
| 155 | self.graph.nng.subgraphs[cond_subgraph_index], |
| 156 | self.graph.nng.subgraphs[body_subgraph_index], |
| 157 | ) |
| 158 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 159 | if op_type == Op.Reshape and "new_shape" not in op.attrs: |
Michael McGeagh | 7b245fd | 2020-07-31 12:50:57 +0100 | [diff] [blame] | 160 | # Reshape should have an attrib "new_shape" but if it is missing, add it based on the output shape |
| 161 | op.attrs["new_shape"] = outputs[0].shape |
| 162 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 163 | if op_type == Op.Cast: |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 164 | # Cast op should have "in/out_data_type" attribs add if missing |
| 165 | if "in_data_type" not in op.attrs: |
| 166 | op.attrs["in_data_type"] = inputs[0].dtype |
| 167 | if "out_data_type" not in op.attrs: |
| 168 | op.attrs["out_data_type"] = outputs[0].dtype |
| 169 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 170 | if "stride_w" in op.attrs: |
| 171 | op.attrs["strides"] = (1, op.attrs["stride_h"], op.attrs["stride_w"], 1) |
| 172 | if "filter_width" in op.attrs: |
| 173 | op.attrs["ksize"] = (1, op.attrs["filter_height"], op.attrs["filter_width"], 1) |
| 174 | if "dilation_w_factor" in op.attrs: |
| 175 | op.attrs["dilation"] = (1, op.attrs["dilation_h_factor"], op.attrs["dilation_w_factor"], 1) |
| 176 | if "depth_multiplier" in op.attrs: |
| 177 | op.attrs["channel_multiplier"] = op.attrs["depth_multiplier"] |
| 178 | |
Fredrik Svedberg | bdf09f9 | 2020-11-18 11:30:21 +0100 | [diff] [blame] | 179 | if op_type == Op.DepthwiseConv2DBias and op.attrs["depth_multiplier"] == 0: |
| 180 | # The depth multiplier is implicit and is calculated as weight channels / ifm channels |
| 181 | # Note however that the weights have been reshaped above. |
| 182 | # The original value is cached above in channel_multiplier |
| 183 | op.attrs["depth_multiplier"] = op.weights.shape[2] // op.ifm.shape[-1] |
| 184 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 185 | faf = op.attrs.pop("fused_activation_function", None) |
| 186 | if faf is not None: |
| 187 | op.activation = create_activation_function(faf) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 188 | if custom_code is not None: |
| 189 | op.attrs["custom_code"] = custom_code |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 190 | |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 191 | @staticmethod |
| 192 | def len1_array_to_scalar(arr): |
| 193 | # The following flatbuffer quantisation fields all return a scalar value of 0 if they are not definied in |
| 194 | # the input buffer. This is represented in Vela by using None. |
| 195 | # Otherwise, the fields returned are a single or multi-element array. In which case, single element arrays |
| 196 | # are converted to scalars |
| 197 | if isinstance(arr, int) and arr == 0: |
| 198 | return None |
| 199 | if len(arr) == 1: |
| 200 | return arr[0] |
| 201 | return arr |
| 202 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 203 | |
| 204 | class TFLiteGraph: |
Michael McGeagh | 6f72526 | 2020-12-03 15:21:36 +0000 | [diff] [blame] | 205 | def __init__(self, filename, batch_size, feed_dict, output_node_names, initialisation_nodes): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 206 | |
| 207 | self.op_times = {} |
| 208 | if batch_size is None: |
| 209 | batch_size = 1 |
| 210 | self.batch_size = batch_size |
| 211 | self.name = os.path.splitext(os.path.basename(filename))[0] |
| 212 | self.initialisation_nodes = initialisation_nodes |
| 213 | |
| 214 | with open(filename, "rb") as f: |
| 215 | buf = bytearray(f.read()) |
| 216 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 217 | try: |
| 218 | parsing_step = "parsing root" |
| 219 | model = Model.GetRootAsModel(buf, 0) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 220 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 221 | parsing_step = "parsing buffers length" |
| 222 | self.buffers = [] |
| 223 | for idx in range(model.BuffersLength()): |
| 224 | parsing_step = f"parsing buffer {idx}" |
| 225 | self.buffers.append(self.parse_buffer(model.Buffers(idx))) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 226 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 227 | parsing_step = "parsing operator codes length" |
| 228 | self.operator_codes = [] |
| 229 | for idx in range(model.OperatorCodesLength()): |
| 230 | parsing_step = f"parsing operator code {idx}" |
| 231 | self.operator_codes.append(self.parse_operator_code(model.OperatorCodes(idx))) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 232 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 233 | parsing_step = "parsing subgraphs length" |
| 234 | self.subgraphs = [] |
Johan Alfvén | 673683b | 2022-09-05 09:39:47 +0200 | [diff] [blame] | 235 | |
| 236 | # Pre-allocate nng subgraphs - needed when parsing an operator and the operator |
| 237 | # has subgraph attributes. |
| 238 | self.nng = Graph(self.name, self.batch_size) |
| 239 | for idx in range(model.SubgraphsLength()): |
| 240 | sg = Subgraph() |
| 241 | self.nng.subgraphs.append(sg) |
| 242 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 243 | for idx in range(model.SubgraphsLength()): |
| 244 | parsing_step = f"parsing subgraph {idx}" |
| 245 | self.subgraphs.append(TFLiteSubgraph(self, model.Subgraphs(idx))) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 246 | |
Johan Alfvén | 673683b | 2022-09-05 09:39:47 +0200 | [diff] [blame] | 247 | for idx, tflite_sg in enumerate(self.subgraphs): |
| 248 | sg = self.nng.subgraphs[idx] |
| 249 | sg.name = tflite_sg.name |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 250 | sg.original_inputs = tflite_sg.inputs # Preserve the original input order |
| 251 | sg.output_tensors = tflite_sg.outputs |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 252 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 253 | parsing_step = "parsing metadata length" |
| 254 | # Preserve the original metadata |
| 255 | for idx in range(model.MetadataLength()): |
| 256 | parsing_step = f"parsing metadata {idx}" |
| 257 | meta = model.Metadata(idx) |
| 258 | parsing_step = f"parsing metadata name of metadata {idx}" |
| 259 | name = meta.Name() |
| 260 | if name is not None: |
| 261 | parsing_step = f"parsing metadata {idx} ({name})" |
| 262 | buf_data = self.buffers[meta.Buffer()] |
| 263 | self.nng.metadata.append((name, buf_data)) |
| 264 | except (struct.error, TypeError, RuntimeError) as e: |
| 265 | print(f'Error: Invalid tflite file. Got "{e}" while {parsing_step}.') |
| 266 | sys.exit(1) |
Michael McGeagh | 22f74e1 | 2020-08-07 16:21:03 +0100 | [diff] [blame] | 267 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 268 | def parse_buffer(self, buf_data): |
| 269 | if buf_data.DataLength() == 0: |
| 270 | return None |
| 271 | data = buf_data.DataAsNumpy() |
| 272 | return data |
| 273 | |
| 274 | def parse_operator_code(self, code): |
| 275 | c = code.BuiltinCode() |
Tim Hall | 42abec1 | 2021-02-04 21:31:57 +0000 | [diff] [blame] | 276 | if c == 0: |
| 277 | c = code.DeprecatedBuiltinCode() |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 278 | if c not in builtin_operator_map: |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 279 | raise InputFileError( |
| 280 | self.name, f"The input file contains operator code '{c}' which is currently not supported" |
| 281 | ) |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 282 | op_type, ser, indices = builtin_operator_map[c] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 283 | custom_code = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 284 | if c == BuiltinOperator.CUSTOM: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 285 | custom_code = decode_str(code.CustomCode()) |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 286 | return op_type, ser, custom_code, indices |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 287 | |
| 288 | |
Michael McGeagh | 6f72526 | 2020-12-03 15:21:36 +0000 | [diff] [blame] | 289 | def read_tflite(filename, batch_size, feed_dict, output_node_names, initialisation_nodes): |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 290 | tflite_graph = TFLiteGraph(filename, batch_size, feed_dict, output_node_names, initialisation_nodes) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 291 | nng = tflite_graph.nng |
| 292 | nng.refresh_after_modification() |
| 293 | return nng |