conv_test.py 15.1 KB
Newer Older
1
"""
2
 Copyright (c) 2018-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import unittest

import numpy as np

21
from mo.graph.graph import Node
22
from mo.middle.passes.conv import convert_muladd_to_scaleshift, convert_add_or_mul_to_scaleshift
23 24 25 26
from mo.middle.passes.eliminate import graph_clean_up
from mo.utils.unittest.graph import build_graph, compare_graphs

nodes_attributes = {
27
    'placeholder_1': {'shape': None, 'type': 'Parameter', 'kind': 'op', 'op': 'Parameter'},
28 29 30
    'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
    # ScaleShift layer
    'scaleshift_1': {'type': 'ScaleShift', 'value': None, 'kind': 'op', 'op': 'ScaleShift'},
31
    'const_scaleshift_1_w': {'value': None, 'shape': None, 'kind': 'op'},
32
    'scaleshift_1_w': {'value': None, 'shape': None, 'kind': 'data'},
33
    'const_scaleshift_1_b': {'value': None, 'shape': None, 'kind': 'op'},
34 35 36 37
    'scaleshift_1_b': {'value': None, 'shape': None, 'kind': 'data'},
    'scaleshift_1_data': {'value': None, 'shape': None, 'kind': 'data'},
    # Mul and Add operations
    'mul_1': {'value': None, 'kind': 'op', 'op': 'Mul'},
38
    'const_mul_1_w': {'value': None, 'shape': None, 'kind': 'op'},
39 40 41
    'mul_1_w': {'value': None, 'shape': None, 'kind': 'data'},
    'mul_1_data': {'value': None, 'shape': None, 'kind': 'data'},
    'add_1': {'value': None, 'kind': 'op', 'op': 'Add'},
42
    'const_add_1_w': {'value': None, 'shape': None, 'kind': 'op'},
43 44
    'add_1_w': {'value': None, 'shape': None, 'kind': 'data'},
    'add_1_data': {'value': None, 'shape': None, 'kind': 'data'},
45
    'op_output': {'kind': 'op', 'op': 'Result'},
46 47 48
}


49
class MulAddToScaleShift(unittest.TestCase):
50 51 52 53
    def _create_graph_with_mul_add(self, mul_w, add_w):
        graph = build_graph(nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1_data', 'mul_1'),
54
                             ('const_mul_1_w', 'mul_1_w'),
55 56 57
                             ('mul_1_w', 'mul_1'),
                             ('mul_1', 'mul_1_data'),
                             ('mul_1_data', 'add_1'),
58
                             ('const_add_1_w', 'add_1_w'),
59 60
                             ('add_1_w', 'add_1'),
                             ('add_1', 'add_1_data'),
61
                             ('add_1_data', 'op_output')
62 63 64
                             ],
                            {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'mul_1_data': {'shape': np.array([1, 227, 227, 3])},
65 66 67
                             'add_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'const_mul_1_w': {'shape': np.array(mul_w.shape) if mul_w is not None else None,
                                               'value': np.array(mul_w) if mul_w is not None else None},
68 69
                             'mul_1_w': {'shape': np.array(mul_w.shape) if mul_w is not None else None,
                                         'value': np.array(mul_w) if mul_w is not None else None},
70 71
                             'const_add_1_w': {'shape': np.array(add_w.shape) if add_w is not None else None,
                                               'value': np.array(add_w) if add_w is not None else None},
72 73 74 75 76 77 78
                             'add_1_w': {'shape': np.array(add_w.shape) if add_w is not None else None,
                                         'value': np.array(add_w) if add_w is not None else None},
                             })
        del graph['mul_1']['mul_1_data'][0]['in']
        del graph['add_1']['add_1_data'][0]['in']
        return graph

79
    @unittest.skip("ScaleShift is not supported")
80 81 82 83 84 85
    def test_mul_add_to_scaleshift_1(self):
        graph = self._create_graph_with_mul_add(np.array([1, 2, 3]), np.array([1, 2, 3]))

        graph_ref = build_graph(nodes_attributes,
                                [('placeholder_1', 'placeholder_1_data'),
                                 ('placeholder_1_data', 'scaleshift_1'),
86
                                 ('const_scaleshift_1_w', 'scaleshift_1_w'),
87
                                 ('scaleshift_1_w', 'scaleshift_1'),
88
                                 ('const_scaleshift_1_b', 'scaleshift_1_b'),
89 90
                                 ('scaleshift_1_b', 'scaleshift_1'),
                                 ('scaleshift_1', 'scaleshift_1_data'),
91
                                 ('scaleshift_1_data', 'op_output'),
92
                                 ],
93 94 95
                                {'const_scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
                                 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
                                 'const_scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
96
                                 'scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
97
                                 'scaleshift_1_data': {}
98 99
                                 })

100
        convert_muladd_to_scaleshift(graph)
101 102 103 104
        graph_clean_up(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'scaleshift_1_data')
        self.assertTrue(flag, resp)

105
    @unittest.skip("Power is not supported")
106 107 108 109
    def test_mul_add_neg_1(self):
        graph = self._create_graph_with_mul_add(None, np.array([2]))
        graph_ref = self._create_graph_with_mul_add(None, np.array([2]))

110
        convert_muladd_to_scaleshift(graph)
111 112 113 114 115 116 117 118
        graph_clean_up(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'add_1_data', check_op_attrs=True)
        self.assertTrue(flag, resp)

    def test_mul_add_neg_2(self):
        graph = self._create_graph_with_mul_add(np.array([2]), None)
        graph_ref = self._create_graph_with_mul_add(np.array([2]), None)

119
        convert_muladd_to_scaleshift(graph)
120 121 122 123 124 125 126 127
        graph_clean_up(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'add_1_data', check_op_attrs=True)
        self.assertTrue(flag, resp)

    def test_mul_add_neg_3(self):
        graph = self._create_graph_with_mul_add(None, None)
        graph_ref = self._create_graph_with_mul_add(None, None)

128
        convert_muladd_to_scaleshift(graph)
129 130 131 132
        graph_clean_up(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'add_1_data', check_op_attrs=True)
        self.assertTrue(flag, resp)

133
    @unittest.skip("TODO investigate why this test is not passing")
134 135
    def test_mul_add_neg_4(self):
        graph = self._create_graph_with_mul_add(np.array([1, 2, 3]), np.array([3]))
136
        graph_ref = self._create_graph_with_mul_add(np.array([1, 2, 3]), np.array([3]))
137

138
        convert_muladd_to_scaleshift(graph)
139 140 141 142
        graph_clean_up(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'add_1_data', check_op_attrs=True)
        self.assertTrue(flag, resp)

143
    @unittest.skip("ScaleShift is not supported")
144 145 146 147 148
    def test_mul_add_neg_5(self):
        graph = self._create_graph_with_mul_add(np.array([3]), np.array([3, 2, 1]))
        graph_ref = build_graph(nodes_attributes,
                                [('placeholder_1', 'placeholder_1_data'),
                                 ('placeholder_1_data', 'scaleshift_1'),
149
                                 ('const_scaleshift_1_w', 'scaleshift_1_w'),
150
                                 ('scaleshift_1_w', 'scaleshift_1'),
151
                                 ('const_scaleshift_1_b', 'scaleshift_1_b'),
152 153
                                 ('scaleshift_1_b', 'scaleshift_1'),
                                 ('scaleshift_1', 'add_1_data'),
154
                                 ('add_1_data', 'op_output'),
155
                                 ],
156 157 158
                                {'const_scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([3, 3, 3])},
                                 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([3, 3, 3])},
                                 'const_scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([3, 2, 1])},
159 160 161
                                 'scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([3, 2, 1])},
                                 })

162
        convert_muladd_to_scaleshift(graph)
163 164 165 166 167
        graph_clean_up(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'add_1_data', check_op_attrs=True)
        self.assertTrue(flag, resp)


168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
class AddToScaleShift(unittest.TestCase):
    @staticmethod
    def _create_graph_with_add(add_w: np.ndarray):
        graph = build_graph(nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1_data', 'add_1'),
                             ('const_add_1_w', 'add_1_w'),
                             ('add_1_w', 'add_1'),
                             ('add_1', 'add_1_data'),
                             ('add_1_data', 'op_output')
                             ],
                            {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'add_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'const_add_1_w': {'shape': np.array(add_w.shape) if add_w is not None else None,
                                               'value': np.array(add_w) if add_w is not None else None},
                             'add_1_w': {'shape': np.array(add_w.shape) if add_w is not None else None,
                                         'value': np.array(add_w) if add_w is not None else None},
                             }, nodes_with_edges_only=True)
        del graph['add_1']['add_1_data'][0]['in']
        return graph

    @staticmethod
    def _create_graph_with_mul(mul_w: np.ndarray):
        graph = build_graph(nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1_data', 'mul_1'),
                             ('const_mul_1_w', 'mul_1_w'),
                             ('mul_1_w', 'mul_1'),
                             ('mul_1', 'mul_1_data'),
                             ('mul_1_data', 'op_output')
                             ],
                            {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'mul_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'const_mul_1_w': {'shape': np.array(mul_w.shape) if mul_w is not None else None,
                                               'value': np.array(mul_w) if mul_w is not None else None},
                             'mul_1_w': {'shape': np.array(mul_w.shape) if mul_w is not None else None,
                                         'value': np.array(mul_w) if mul_w is not None else None},
                             }, nodes_with_edges_only=True)
        del graph['mul_1']['mul_1_data'][0]['in']
        return graph

209
    @unittest.skip("ScaleShift is not supported")
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    def test_add_to_scaleshift_1(self):
        graph = AddToScaleShift._create_graph_with_add(np.array([1, 2, 3], dtype=np.float32))
        graph.stage = 'middle'

        graph_ref = build_graph(nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1_data', 'scaleshift_1'),
                             ('const_scaleshift_1_w', 'scaleshift_1_w'),
                             ('const_scaleshift_1_b', 'scaleshift_1_b'),
                             ('scaleshift_1_w', 'scaleshift_1'),
                             ('scaleshift_1_b', 'scaleshift_1'),
                             ('scaleshift_1', 'scaleshift_1_data'),
                             ('scaleshift_1_data', 'op_output')
                             ],
                            {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'scaleshift_1_data': {'shape': np.array([1, 227, 227, 3])},

                             'const_scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 1, 1])},
                             'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 1, 1])},

                             'const_scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
                             'scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
                             }, nodes_with_edges_only=True)

        convert_add_or_mul_to_scaleshift(graph)
        graph_clean_up(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'op_output')
        self.assertTrue(flag, resp)

        scsh_node = Node(graph, 'op_output').in_port(0).get_source().node

        self.assertTrue(graph.get_edge_data(scsh_node.in_node(1).id, scsh_node.id)[0]['bin'] == 'weights')
        self.assertTrue(graph.get_edge_data(scsh_node.in_node(2).id, scsh_node.id)[0]['bin'] == 'biases')

245
    @unittest.skip("ScaleShift is not supported")
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    def test_mul_to_scaleshift_1(self):
        graph = AddToScaleShift._create_graph_with_mul(np.array([1, 2, 3], dtype=np.float32))
        graph.stage = 'middle'

        graph_ref = build_graph(nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1_data', 'scaleshift_1'),
                             ('const_scaleshift_1_w', 'scaleshift_1_w'),
                             ('const_scaleshift_1_b', 'scaleshift_1_b'),
                             ('scaleshift_1_w', 'scaleshift_1'),
                             ('scaleshift_1_b', 'scaleshift_1'),
                             ('scaleshift_1', 'scaleshift_1_data'),
                             ('scaleshift_1_data', 'op_output')
                             ],
                            {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
                             'scaleshift_1_data': {'shape': np.array([1, 227, 227, 3])},

                             'const_scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
                             'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},

                             'const_scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([0, 0, 0])},
                             'scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([0, 0, 0])},
                             }, nodes_with_edges_only=True)

        convert_add_or_mul_to_scaleshift(graph)
        graph_clean_up(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'op_output')
        self.assertTrue(flag, resp)

        scsh_node = Node(graph, 'op_output').in_port(0).get_source().node

        self.assertTrue(graph.get_edge_data(scsh_node.in_node(1).id, scsh_node.id)[0]['bin'] == 'weights')
        self.assertTrue(graph.get_edge_data(scsh_node.in_node(2).id, scsh_node.id)[0]['bin'] == 'biases')



283 284
if __name__ == '__main__':
    unittest.main()