Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / resample_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.ops.resample import ResampleOp
22 from mo.graph.graph import Node
23 from mo.utils.unittest.graph import build_graph
24
25 nodes_attributes = {'node_1': {'type': 'Identity', 'kind': 'op'},
26                     'resample': {'type': 'Resample', 'kind': 'op'},
27                     'node_3': {'type': 'Identity', 'kind': 'op'},
28                     'op_output': {'kind': 'op', 'op': 'OpOutput'},
29                     }
30
31
32 class TestResampleOp(unittest.TestCase):
33     def test_tf_resample_infer(self):
34         graph = build_graph(nodes_attributes,
35                             [('node_1', 'resample'),
36                              ('resample', 'node_3'),
37                              ('node_3', 'op_output')
38                              ],
39                             {'node_3': {'shape': None},
40                              'node_1': {'shape': np.array([1, 3, 227, 227])},
41                              'resample': {'antialias': 1,
42                                           'height': 384,
43                                           'width': 512,
44                                           'resample_type': 'LINEAR',
45                                           'factor': 1.0}
46                              })
47
48         graph.graph['layout'] = 'NCHW'
49         resample_node = Node(graph, 'resample')
50         ResampleOp.resample_infer(resample_node)
51         exp_shape = np.array([1, 3, 384, 512])
52         res_shape = graph.node['node_3']['shape']
53         for i in range(0, len(exp_shape)):
54             self.assertEqual(exp_shape[i], res_shape[i])
55
56     def test_caffe_factor_infer(self):
57         factor = 3.0
58         graph = build_graph(nodes_attributes,
59                             [('node_1', 'resample'),
60                              ('resample', 'node_3'),
61                              ('node_3', 'op_output')
62                              ],
63                             {'node_3': {'shape': None},
64                              'node_1': {'shape': np.array([1, 3, 224, 227])},
65                              'resample': {'antialias': 1,
66                                           'resample_type': 'LINEAR',
67                                           'factor': factor}
68                              })
69         graph.graph['layout'] = 'NCHW'
70         resample_node = Node(graph, 'resample')
71         ResampleOp.resample_infer(resample_node)
72         exp_shape = np.array([1, 3, 224 * factor, 227 * factor])
73         res_shape = graph.node['node_3']['shape']
74         for i in range(0, len(exp_shape)):
75             self.assertEqual(exp_shape[i], res_shape[i])
76
77     def test_tf_infer(self):
78         new_width = 100
79         new_height = 125
80         new_attrs = nodes_attributes.copy()
81         new_attrs.update({'new_shape': {'value': np.array([new_height, new_width]), 'type': 'Const', 'kind': 'op'}})
82         graph = build_graph(new_attrs,
83                             [('node_1', 'resample'),
84                              ('new_shape', 'resample'),
85                              ('resample', 'node_3'),
86                              ('node_3', 'op_output')
87                              ],
88                             {'node_3': {'shape': None},
89                              'node_1': {'shape': np.array([1, 224, 227, 3])},
90                              'resample': {'antialias': 1,
91                                           'resample_type': 'LINEAR',
92                                           'factor': 1.0,
93                                           'fw': 'tf'}
94                              })
95         graph.graph['layout'] = 'NHWC'
96         resample_node = Node(graph, 'resample')
97         ResampleOp.resample_infer(resample_node)
98         exp_shape = np.array([1, new_height, new_width, 3])
99         res_shape = graph.node['node_3']['shape']
100         for i in range(0, len(exp_shape)):
101             self.assertEqual(exp_shape[i], res_shape[i])