2 Copyright (c) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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
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'}
33 class TestCropInfer(unittest.TestCase):
34 def test_crop_infer_ideal(self):
35 graph = build_graph(nodes_attributes,
36 [('node_1', 'crop_1'),
39 ('node_3', 'op_output')
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}
47 crop_node = Node(graph, 'crop_1')
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])
55 self.assertEqual(crop_node.axis, [2, 3])
56 self.assertEqual(crop_node.offset, [0, 0])
57 self.assertEqual(crop_node.dim, [256, 256])
59 def test_crop_infer_negative_axis(self):
60 graph = build_graph(nodes_attributes,
61 [('node_1', 'crop_1'),
64 ('node_3', 'op_output')
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}
72 crop_node = Node(graph, 'crop_1')
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])
80 self.assertEqual(crop_node.axis, [3])
81 self.assertEqual(crop_node.offset, [0])
82 self.assertEqual(crop_node.dim, [256])
84 def test_crop_infer_no_shape(self):
85 graph = build_graph(nodes_attributes,
86 [('node_1', 'crop_1'),
89 ('node_3', 'op_output')
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}
97 crop_node = Node(graph, 'crop_1')
100 self.assertIsNone(graph.node['node_3']['shape'])
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')
108 {'node_3': {'shape': None},
109 'node_1': {'shape': np.array([1, 2, 500, 500])},
110 'crop_1': {'axis': 2, 'offset': [0], 'dim': None}
113 crop_node = Node(graph, 'crop_1')
115 crop_infer(crop_node)
116 self.assertIsNone(graph.node['node_3']['shape'])
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')
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}
131 crop_node = Node(graph, 'crop_1')
133 crop_infer(crop_node)
134 self.assertIsNone(graph.node['node_3']['shape'])