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