Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / DilatedConvolution.py
1 """
2  Copyright (c) 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 from mo.graph.graph import Graph
17 from mo.middle.replacement import MiddleReplacementPattern
18
19
20 class DilatedConvolutionConverter(MiddleReplacementPattern):
21     enabled = True
22     force_clean_up = True
23
24     def run_after(self):
25         from extensions.middle.pass_separator import PreMiddleStart
26         return [PreMiddleStart]
27
28     def run_before(self):
29         from extensions.middle.pass_separator import MiddleStart
30         return [MiddleStart]
31
32     def pattern(self):
33         return dict(
34             nodes=[
35                 ('conv', dict(kind='op', op=lambda value: value in ['Conv2D', 'DepthwiseConv2dNative', 'Conv3D'])),
36                 ('space_to_batch', dict(kind='op', op='SpaceToBatchND')),
37                 ('batch_to_space', dict(kind='op', op='BatchToSpaceND')),
38                 ('input', dict(kind='data')),
39                 ('output', dict(kind='data')),
40                 ('conv_output', dict(kind='data')),
41                 ('stb_output', dict(kind='data')),
42                 ('stb_bs', dict(kind='data')),
43                 ('stb_pad', dict(kind='data')),
44                 ('bts_bs', dict(kind='data')),
45                 ('bts_crop', dict(kind='data'))
46             ],
47             edges=[
48                 ('input', 'space_to_batch', {'in': 0}),
49                 ('stb_bs', 'space_to_batch', {'in': 1}),
50                 ('stb_pad', 'space_to_batch', {'in': 2}),
51                 ('space_to_batch', 'stb_output', {'out': 0}),
52                 ('stb_output', 'conv', {'in': 0}),
53                 ('conv', 'conv_output', {'out': 0}),
54                 ('conv_output', 'batch_to_space', {'in': 0}),
55                 ('bts_bs', 'batch_to_space', {'in': 1}),
56                 ('bts_crop', 'batch_to_space', {'in': 2}),
57                 ('batch_to_space', 'output', {'out': 0}),
58             ])
59
60     def replace_pattern(self, graph: Graph, match: dict):
61         conv = match['conv']
62         stb = match['space_to_batch']
63         bts = match['batch_to_space']
64
65         block_size = match['stb_bs']
66
67         input = match['input']
68         output = match['output']
69         stb_out = match['stb_output']
70         conv_out = match['conv_output']
71
72         in_edge_attrs = graph.get_edge_data(input.id, stb.id)[0]
73         out_edge_attrs = graph.get_edge_data(bts.id, output.id)[0]
74
75         graph.remove_edge(input.id, stb.id)
76         graph.remove_edge(stb_out.id, conv.id)
77         graph.remove_edge(conv.id, conv_out.id)
78         graph.remove_edge(bts.id, output.id)
79
80         conv.dilation[conv.spatial_dims] = block_size.value
81
82         pad = match['stb_pad'].value - match['bts_crop'].value
83         conv.pad[conv.spatial_dims] = [[pad[x][0], pad[x][1]] for x in range(len(pad))]
84         conv['auto_pad'] = None
85
86         graph.add_edges_from([
87             (input.id, conv.id, {'in': 0, **in_edge_attrs}),
88             (conv.id, output.id, {'out': 0, **out_edge_attrs}),
89         ])