Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 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 | # Dispatcher for reading a neural network model. |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 18 | from . import tflite_model_semantic |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 19 | from . import tflite_reader |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 20 | from . import tosa_model_semantic |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 21 | from . import tosa_reader |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 22 | from .errors import InputFileError |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 23 | from .nn_graph import NetworkType |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class ModelReaderOptions: |
| 27 | def __init__(self, batch_size=1): |
| 28 | self.batch_size = batch_size |
| 29 | |
| 30 | def __str__(self): |
| 31 | return type(self).__name__ + ": " + str(self.__dict__) |
| 32 | |
| 33 | __repr__ = __str__ |
| 34 | |
| 35 | |
Michael McGeagh | 6f72526 | 2020-12-03 15:21:36 +0000 | [diff] [blame] | 36 | def read_model(fname, options, feed_dict=None, output_node_names=None, initialisation_nodes=None): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 37 | if fname.endswith(".tflite"): |
Michael McGeagh | 6f72526 | 2020-12-03 15:21:36 +0000 | [diff] [blame] | 38 | if feed_dict is None: |
| 39 | feed_dict = {} |
| 40 | if output_node_names is None: |
| 41 | output_node_names = [] |
| 42 | if initialisation_nodes is None: |
| 43 | initialisation_nodes = [] |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 44 | |
| 45 | nng = tflite_reader.read_tflite( |
| 46 | fname, |
| 47 | options.batch_size, |
| 48 | feed_dict=feed_dict, |
| 49 | output_node_names=output_node_names, |
| 50 | initialisation_nodes=initialisation_nodes, |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 51 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 52 | nng = tflite_model_semantic.tflite_semantic_checker(nng) |
| 53 | |
| 54 | return (nng, NetworkType.TFLite) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 55 | elif fname.endswith(".tosa"): |
| 56 | if feed_dict is None: |
| 57 | feed_dict = {} |
| 58 | if output_node_names is None: |
| 59 | output_node_names = [] |
| 60 | if initialisation_nodes is None: |
| 61 | initialisation_nodes = [] |
| 62 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 63 | nng = tosa_reader.read_tosa( |
| 64 | fname, |
| 65 | options.batch_size, |
| 66 | feed_dict=feed_dict, |
| 67 | output_node_names=output_node_names, |
| 68 | initialisation_nodes=initialisation_nodes, |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 69 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 70 | nng = tosa_model_semantic.tosa_semantic_checker(nng) |
| 71 | |
| 72 | return (nng, NetworkType.TOSA) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 73 | else: |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 74 | raise InputFileError(fname, "Unsupported file extension. Only .tflite and .tosa files are supported") |