Added unit tests and readme for model optimizer (#79)
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / lstm_sequence_normalize_test.py
1
2 """
3  Copyright (c) 2018 Intel Corporation
4
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8
9       http://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,
13  WITHOUT 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.
16 """
17
18 import unittest
19 import numpy as np
20
21 from extensions.middle.lstm_sequence_normalize import LSTMSequenceNormalize
22 from mo.utils.unittest.graph import compare_graphs, build_graph_with_attrs
23 from mo.graph.graph import Node
24
25
26 class LSTMSequenceNormalizeTest(unittest.TestCase):
27
28     def test_squeeze_num_directions(self):
29         tested_obj = LSTMSequenceNormalize()
30         pattern = tested_obj.pattern()
31         orig_shape = np.array([10, 1, 20, 128], dtype=np.int64)  # seq_length, num_dims, batch_size, data_size
32         new_shape = np.array([10, 20, 128], dtype=np.int64)
33         graph = build_graph_with_attrs(
34             nodes_with_attrs=pattern['nodes'],
35             edges_with_attrs=pattern['edges'],
36             update_edge_attrs={
37                 ('W', 'lstm', 0): {'in': 1},
38                 ('R', 'lstm', 0): {'in': 2},
39             },
40             new_nodes_with_attrs=[
41                 ('output', {'shape': orig_shape}),
42             ],
43             new_edges_with_attrs=[
44                 ('lstm', 'output', {'out': 0}),
45             ],
46         )
47
48         lstm = Node(graph, 'lstm')
49         match = {'lstm': lstm}
50         tested_obj.squeeze_num_directions(graph, match)
51         self.assertTrue(np.array_equal(lstm.out_node(0).shape, new_shape))
52         reshape_node = lstm.out_node(0).out_node(0)
53         self.assertTrue(reshape_node.op == 'Reshape')
54         self.assertTrue(np.array_equal(reshape_node.dim, orig_shape))
55         self.assertTrue(reshape_node.out_node(0).id == 'output')