Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / fifo_replacer_test.py
1 """
2  Copyright (c) 2018-2019 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 import unittest
18
19 import numpy as np
20
21 from extensions.front.tf.fifo_replacer import FIFOQueue
22 from mo.utils.unittest.graph import build_graph_with_edge_attrs
23
24
25 class TestFIFOQueueReplacement(unittest.TestCase):
26     def test_fifo_with_label_batch(self):
27         nodes = {
28             'placeholder': {'op': 'Placeholder', 'data_type': np.int32, 'kind': 'op', 'shape': np.array(1)},
29             'batch_join/fifo_queue': {'op': 'FIFOQueueV2', 'name': 'batch_join/fifo_queue',
30                                       'shapes': np.array([[1, 2, 3]]), 'kind': 'op'},
31             'batch_join': {'op': 'QueueDequeueUpToV2', 'kind': 'op'},
32             'image_batch': {'op': 'Identity', 'data_type': np.float32, 'kind': 'op'},
33             'label_batch': {'op': 'Identity', 'kind': 'op'},
34             'label_batch_op_output': {'op': 'OpOutput', 'kind': 'op'},
35         }
36         edges = [
37             ('placeholder', 'batch_join', {'out': 0, 'in': 0}),
38             ('batch_join/fifo_queue', 'batch_join', {'out': 0, 'in': 1}),
39             ('batch_join', 'image_batch', {'out': 0, 'in': 0}),
40             ('batch_join', 'label_batch', {'out': 1, 'in': 0}),
41             ('label_batch', 'label_batch_op_output', {'out': 0, 'in': 0})
42         ]
43         graph = build_graph_with_edge_attrs(nodes, edges)
44         tested_class = FIFOQueue()
45         tested_class.find_and_replace_pattern(graph=graph)
46         after_pattern = graph.nodes()
47         self.assertEqual(2, len(after_pattern))
48         try:
49             new_ph_dict = graph.node[[u for u, v in graph.in_edges('image_batch')][0]]
50         except Exception as e:
51             self.fail("Can't get new placeholder. Broken edge. Additional information: {}".format(e))
52         self.assertEqual(new_ph_dict['name'], 'batch_join/fifo_queue')
53         self.assertTrue(np.array_equal(new_ph_dict['shape'], [1, 2, 3]))
54
55     def test_fifo_with_out_label_batch(self):
56         nodes_no_label = {
57             'placeholder': {'op': 'Placeholder', 'data_type': np.int32, 'kind': 'op', 'shape': np.array(0)},
58             'batch_join/fifo_queue': {'op': 'FIFOQueueV2', 'name': 'batch_join/fifo_queue',
59                                       'shapes': np.array([[1, 2, 3]]), 'kind': 'op'},
60             'batch_join': {'op': 'QueueDequeueUpToV2', 'kind': 'op'},
61             'image_batch': {'op': 'Identity', 'data_type': np.float32, 'kind': 'op'},
62         }
63         edges_no_label = [
64             ('placeholder', 'batch_join', {'out': 0}),
65             ('batch_join/fifo_queue', 'batch_join', {'out': 0}),
66             ('batch_join', 'image_batch', {'out': 0})
67         ]
68
69         graph = build_graph_with_edge_attrs(nodes_no_label, edges_no_label)
70         tested_class = FIFOQueue()
71         tested_class.find_and_replace_pattern(graph=graph)
72         after_pattern = graph.nodes()
73         self.assertEqual(2, len(after_pattern))
74         try:
75             new_ph_dict = graph.node[[u for u, v in graph.in_edges('image_batch')][0]]
76         except Exception as e:
77             self.fail("Can't get new placeholder. Broken edge. Additional information: {}".format(e))
78         self.assertEqual(new_ph_dict['name'], 'batch_join/fifo_queue')
79         self.assertTrue(np.array_equal(new_ph_dict['shape'], np.array([1, 2, 3])))