Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / ops / pad_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 mo.graph.graph import Node
22 from mo.ops.pad import Pad
23 from mo.utils.unittest.graph import build_graph
24
25
26 class TestPadONNXOp(unittest.TestCase):
27     # There are tests for InnerProduct.infer in mo/front/common/partial_infer/inner_product_test.py
28     node_attrs = {
29         'data_in': {
30             'kind': 'data',
31             'shape': np.array([1, 3, 100, 200])
32         },
33         # optional input for one of the two flavors of pad op
34         'data_pads': {
35             'kind': 'data',
36             'value': np.array([[0, 0], [0, 0], [1, 3], [2, 4]], dtype=np.int64),
37             'shape': np.array([2, 4], dtype=np.int64)
38         },
39         'pad': {
40             'op': 'Pad',
41             'kind': 'op',
42             'pads': None,
43         },
44         'data_out': {
45             'kind': 'data',
46             'shape': None,
47         }
48     }
49
50     edge_attrs = [
51         ('data_in', 'pad'),
52         ('pad', 'data_out')
53     ]
54
55     def test_one_input(self):
56         graph = build_graph(
57             self.node_attrs,
58             self.edge_attrs,
59             {'pad': {'pads': np.array([[0, 0], [0, 0], [1, 3], [2, 4]], dtype=np.int64)}},
60             nodes_with_edges_only=True,
61         )
62         pad_node = Node(graph, 'pad')
63         Pad.infer(pad_node)
64         self.assertTrue(np.array_equal(Node(graph, 'data_out').shape, np.array([1, 3, 100 + 1 + 3, 200 + 2 + 4])))
65
66     def test_two_inputs(self):
67         graph = build_graph(
68             self.node_attrs,
69             self.edge_attrs + [('data_pads', 'pad')],
70             nodes_with_edges_only=True,
71         )
72         pad_node = Node(graph, 'pad')
73         Pad.infer(pad_node)
74         self.assertTrue(np.array_equal(Node(graph, 'data_out').shape, np.array([1, 3, 100 + 1 + 3, 200 + 2 + 4])))
75
76     def test_one_input_and_no_pads(self):
77         graph = build_graph(
78             self.node_attrs,
79             self.edge_attrs,
80             nodes_with_edges_only=True,
81         )
82         pad_node = Node(graph, 'pad')
83         with self.assertRaisesRegex(AssertionError, ".*pads attribute is missing.*"):
84             Pad.infer(pad_node)
85
86     def test_two_inputs_and_pads(self):
87         graph = build_graph(
88             self.node_attrs,
89             self.edge_attrs + [('data_pads', 'pad')],
90             {'pad': {'pads': np.array([[0, 0], [0, 0], [1, 3], [2, 4]], dtype=np.int64)}},
91             nodes_with_edges_only=True,
92         )
93         pad_node = Node(graph, 'pad')
94         with self.assertRaisesRegex(AssertionError, ".*unexpected additional input argument.*"):
95             Pad.infer(pad_node)