Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / crop_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.front.common.partial_infer.crop import crop_infer
22 from mo.graph.graph import Node
23 from mo.utils.unittest.graph import build_graph
24
25 nodes_attributes = {'node_1': {'value': None, 'kind': 'data'},
26                     'node_2': {'value': None, 'kind': 'data'},
27                     'crop_1': {'type': 'Crop', 'kind': 'op'},
28                     'node_3': {'value': None, 'kind': 'data'},
29                     'op_output': { 'kind': 'op', 'op': 'OpOutput'}
30                     }
31
32
33 class TestCropInfer(unittest.TestCase):
34     def test_crop_infer_ideal(self):
35         graph = build_graph(nodes_attributes,
36                             [('node_1', 'crop_1'),
37                              ('node_2', 'crop_1'),
38                              ('crop_1', 'node_3'),
39                              ('node_3', 'op_output')
40                              ],
41                             {'node_3': {'shape': None},
42                              'node_1': {'shape': np.array([1, 2, 500, 500])},
43                              'node_2': {'shape': np.array([1, 2, 256, 256])},
44                              'crop_1': {'axis': 2, 'offset': [0, 0], 'dim': None}
45                              })
46
47         crop_node = Node(graph, 'crop_1')
48
49         crop_infer(crop_node)
50         exp_shape = np.array([1, 2, 256, 256])
51         res_shape = graph.node['node_3']['shape']
52         for i in range(0, len(exp_shape)):
53             self.assertEqual(exp_shape[i], res_shape[i])
54
55         self.assertEqual(crop_node.axis, [2, 3])
56         self.assertEqual(crop_node.offset, [0, 0])
57         self.assertEqual(crop_node.dim, [256, 256])
58
59     def test_crop_infer_negative_axis(self):
60         graph = build_graph(nodes_attributes,
61                             [('node_1', 'crop_1'),
62                              ('node_2', 'crop_1'),
63                              ('crop_1', 'node_3'),
64                              ('node_3', 'op_output')
65                              ],
66                             {'node_3': {'shape': None},
67                              'node_1': {'shape': np.array([1, 2, 500, 500])},
68                              'node_2': {'shape': np.array([1, 2, 256, 256])},
69                              'crop_1': {'axis': -1, 'offset': [0, 0], 'dim': None}
70                              })
71
72         crop_node = Node(graph, 'crop_1')
73
74         crop_infer(crop_node)
75         exp_shape = np.array([1, 2, 500, 256])
76         res_shape = graph.node['node_3']['shape']
77         for i in range(0, len(exp_shape)):
78             self.assertEqual(exp_shape[i], res_shape[i])
79
80         self.assertEqual(crop_node.axis, [3])
81         self.assertEqual(crop_node.offset, [0])
82         self.assertEqual(crop_node.dim, [256])
83
84     def test_crop_infer_no_shape(self):
85         graph = build_graph(nodes_attributes,
86                             [('node_1', 'crop_1'),
87                              ('node_2', 'crop_1'),
88                              ('crop_1', 'node_3'),
89                              ('node_3', 'op_output')
90                              ],
91                             {'node_3': {'shape': None},
92                              'node_1': {'shape': np.array([1, 2, 500, 500])},
93                              'node_2': {'shape': None},
94                              'crop_1': {'axis': 2, 'offset': [0, 0], 'dim': None}
95                              })
96
97         crop_node = Node(graph, 'crop_1')
98
99         crop_infer(crop_node)
100         self.assertIsNone(graph.node['node_3']['shape'])
101
102     def test_crop_infer_one_shape(self):
103         graph = build_graph(nodes_attributes,
104                             [('node_1', 'crop_1'),
105                              ('crop_1', 'node_3'),
106                              ('node_3', 'op_output')
107                              ],
108                             {'node_3': {'shape': None},
109                              'node_1': {'shape': np.array([1, 2, 500, 500])},
110                              'crop_1': {'axis': 2, 'offset': [0], 'dim': None}
111                              })
112
113         crop_node = Node(graph, 'crop_1')
114
115         crop_infer(crop_node)
116         self.assertIsNone(graph.node['node_3']['shape'])
117
118     def test_crop_infer_out_offset(self):
119         graph = build_graph(nodes_attributes,
120                             [('node_1', 'crop_1'),
121                              ('node_2', 'crop_1'),
122                              ('crop_1', 'node_3'),
123                              ('node_3', 'op_output')
124                              ],
125                             {'node_3': {'shape': None},
126                              'node_1': {'shape': np.array([1, 2, 500, 500])},
127                              'node_2': {'shape': np.array([1, 2, 256, 256])},
128                              'crop_1': {'axis': 2, 'offset': [300], 'dim': None}
129                              })
130
131         crop_node = Node(graph, 'crop_1')
132
133         crop_infer(crop_node)
134         self.assertIsNone(graph.node['node_3']['shape'])