summaryrefslogtreecommitdiff
path: root/tools/tflitefile_tool/tests/test_tflite_parser.py
blob: dd1447a8a73bcc441e5e6873b95598361fe626f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python

# Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import tflite.Model
from parser.tflite.tflite_parser import TFLiteParser, TFLiteSubgraphParser
from .test_setup import TEST_MODEL_PATH


class TFLiteSubgraphParserTestCase(unittest.TestCase):
    def setUp(self):
        self.model_file = open(TEST_MODEL_PATH, 'rb')

    def tearDown(self):
        self.model_file.close()

    def test_Parse(self):
        buf = bytearray(self.model_file.read())
        tf_model = tflite.Model.Model.GetRootAsModel(buf, 0)
        for subgraph_index in range(tf_model.SubgraphsLength()):
            tf_subgraph = tf_model.Subgraphs(subgraph_index)
            subg_parser = TFLiteSubgraphParser(tf_model, subgraph_index)
            subg = subg_parser.Parse()
            self.assertEqual(subg.index, subgraph_index)
            self.assertEqual(len(subg.inputs), tf_subgraph.InputsLength())
            self.assertEqual(len(subg.outputs), tf_subgraph.OutputsLength())
            # if there is optional tensors, this assert could be wrong
            self.assertEqual(len(subg.tensors_map.keys()), tf_subgraph.TensorsLength())
            self.assertEqual(
                len(subg.operators_map.keys()), tf_subgraph.OperatorsLength())
            # because TEST_MODEL_PATH has an op(ADD)
            self.assertEqual(len(subg.optypes_map.keys()), tf_subgraph.OperatorsLength())


class TFLiteParserTestCase(unittest.TestCase):
    def setUp(self):
        self.model_file = open(TEST_MODEL_PATH, 'rb')
        self.parser = TFLiteParser(self.model_file)

    def tearDown(self):
        self.model_file.close()

    def test_Parse(self):
        subg_list = self.parser.Parse()
        self.assertIsNotNone(subg_list)
        self.assertEqual(len(subg_list), 1)


if __name__ == '__main__':
    unittest.main()