Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / freeze_placeholder_value_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.freeze_placeholder_value import FreezePlaceholderValue
22 from mo.utils.unittest.graph import build_graph
23
24 nodes_bool = {
25     '0': {'name': 'input1', 'kind': 'op', 'op': 'Placeholder', 'data_type': bool, 'shape': np.array([])},
26     '1': {'name': 'input2', 'kind': 'op', 'op': 'Placeholder', 'data_type': bool, 'shape': np.array([])},
27     '2': {'name': 'node_1', 'kind': 'op', 'op': 'NotPlaceholder'},
28     '3': {'name': 'node_2', 'kind': 'op', 'op': 'NotPlaceholder'},
29     '4': {'name': 'node_3', 'kind': 'op', 'op': 'NotPlaceholder'},
30     '5': {'name': 'node_4', 'kind': 'op', 'op': 'NotPlaceholder'},
31     '6': {'name': 'output1', 'kind': 'op', 'op': 'OpOutput', 'type': 'OpOutput'},
32     '7': {'name': 'output2', 'kind': 'op', 'op': 'OpOutput', 'type': 'OpOutput'}
33
34 }
35 edges = {
36     ('0', '2'),
37     ('2', '3'),
38     ('4', '6'),
39     ('1', '5'),
40     ('5', '7')
41 }
42
43
44 class TestFreezePlaceholderValue(unittest.TestCase):
45     def test_freeze_true(self):
46         graph = build_graph(nodes_bool, edges)
47         graph.graph['fw'] = 'tf'
48         tested_class = FreezePlaceholderValue()
49         graph.graph['freeze_placeholder'] = {'input1': 'True'}
50         before_pattern = graph.nodes()
51         tested_class.find_and_replace_pattern(graph=graph)
52         after_pattern = graph.nodes()
53         # number of nodes in the grpaph didn't change
54         self.assertEqual(len(before_pattern), len(after_pattern))
55         # reach new placeholder
56         try:
57             new_ph_dict = graph.node[[u for u, v in graph.in_edges('2')][0]]
58         except Exception as e:
59             self.fail("Can't get frozen placeholder. Broken edge. Additional information: {}".format(e))
60         # check value
61         self.assertEqual('value' in new_ph_dict, True)
62         self.assertEqual(new_ph_dict['value'], True)
63
64     def test_freeze_false(self):
65         graph = build_graph(nodes_bool, edges)
66         graph.graph['fw'] = 'tf'
67         tested_class = FreezePlaceholderValue()
68         graph.graph['freeze_placeholder'] = {'input1': 'False'}
69         before_pattern = graph.nodes()
70         tested_class.find_and_replace_pattern(graph=graph)
71         after_pattern = graph.nodes()
72         # number of nodes in the grpaph didn't change
73         self.assertEqual(len(before_pattern), len(after_pattern))
74         # reach new placeholder
75         try:
76             new_ph_dict = graph.node[[u for u, v in graph.in_edges('2')][0]]
77         except Exception as e:
78             self.fail("Can't get frozen placeholder. Broken edge. Additional information: {}".format(e))
79         # check value
80         self.assertEqual('value' in new_ph_dict, True)
81         self.assertEqual(new_ph_dict['value'], False)
82
83     def test_freeze_both(self):
84         graph = build_graph(nodes_bool, edges)
85         graph.graph['fw'] = 'tf'
86         tested_class = FreezePlaceholderValue()
87         graph.graph['freeze_placeholder'] = {'input1': 'False', 'input2': 'True'}
88         before_pattern = graph.nodes()
89         tested_class.find_and_replace_pattern(graph=graph)
90         after_pattern = graph.nodes()
91         # number of nodes in the graph didn't change
92         self.assertEqual(len(before_pattern), len(after_pattern))
93         # reach new placeholder
94         try:
95             new_ph_dict_1 = graph.node[[u for u, v in graph.in_edges('2')][0]]
96             new_ph_dict_2 = graph.node[[u for u, v in graph.in_edges('5')][0]]
97         except Exception as e:
98             self.fail("Can't get frozen placeholder. Broken edge. Additional information: {}".format(e))
99         # check value
100         self.assertEqual('value' in new_ph_dict_1, True)
101         self.assertEqual('value' in new_ph_dict_2, True)
102         self.assertEqual(new_ph_dict_1['value'], False)
103         self.assertEqual(new_ph_dict_2['value'], True)