blob: 3b09436114d5f6fdb8693438016fd92793b66c74 [file] [log] [blame]
Jonas Ohlsson45e653d2021-07-26 16:13:12 +02001# Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved.
Tim Hall79d07d22020-04-27 18:20:16 +01002#
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 Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Dispatcher for reading a neural network model.
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020018from . import tflite_model_semantic
Louis Verhaard7db78962020-05-25 15:05:26 +020019from . import tflite_reader
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020020from . import tosa_model_semantic
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020021from . import tosa_reader
Louis Verhaard7db78962020-05-25 15:05:26 +020022from .errors import InputFileError
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020023from .nn_graph import NetworkType
Tim Hall79d07d22020-04-27 18:20:16 +010024
25
26class 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 McGeagh6f725262020-12-03 15:21:36 +000036def read_model(fname, options, feed_dict=None, output_node_names=None, initialisation_nodes=None):
Tim Hall79d07d22020-04-27 18:20:16 +010037 if fname.endswith(".tflite"):
Michael McGeagh6f725262020-12-03 15:21:36 +000038 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 Ohlsson45e653d2021-07-26 16:13:12 +020044
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 Gustavsson8f1f9aa2021-06-28 07:41:58 +020051 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020052 nng = tflite_model_semantic.tflite_semantic_checker(nng)
53
54 return (nng, NetworkType.TFLite)
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020055 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 Ohlsson45e653d2021-07-26 16:13:12 +020063 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 Hallc8310b12020-06-17 14:53:11 +010069 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020070 nng = tosa_model_semantic.tosa_semantic_checker(nng)
71
72 return (nng, NetworkType.TOSA)
Tim Hall79d07d22020-04-27 18:20:16 +010073 else:
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020074 raise InputFileError(fname, "Unsupported file extension. Only .tflite and .tosa files are supported")