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