Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / FusedBatchNormNonConstant.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 from mo.graph.graph import Graph
17 from mo.middle.replacement import MiddleReplacementPattern
18 from mo.ops.eltwise import Eltwise
19 from mo.ops.power import Power
20
21
22 class FusedBatchNormNonConstant(MiddleReplacementPattern):
23     """
24     Replaces FusedBatchNorm(input, beta, gamma, mean, variance) with non-constant mean and variance,
25     but with constant beta and gamma to a sub-expression consisting of a combinatin of Eltwise and Power
26     layers and ScaleShift.
27     """
28
29     enabled = True
30
31     def run_after(self):
32         from extensions.middle.pass_separator import MiddleStart
33         return [MiddleStart]
34
35     def run_before(self):
36         from extensions.middle.pass_separator import MiddleFinish
37         return [MiddleFinish]
38
39     def pattern(self):
40         return dict(
41             nodes=[
42                 ('op', dict(kind='op', op='FusedBatchNorm'))],
43             edges=[]
44         )
45
46     def replace_pattern(self, graph: Graph, match: dict):
47         node = match['op']
48         if (node.data_format != b'NHWC' or
49                 len(node.in_nodes()) != 5 or
50                 node.in_node(0).value is not None or  # input
51                 node.in_node(1).value is None or  # scale
52                 node.in_node(2).value is None or  # offset
53                 node.in_node(3).value is not None or  # mean
54                 node.in_node(4).value is not None or  # variance
55                 node.in_node(1).value.ndim != 1 or
56                 node.in_node(2).value.ndim != 1):
57             return
58
59         scale_mul = Eltwise(graph, dict(operation='mul', name=node.name + '/scale_mul_'))
60         shift_add = Eltwise(graph, dict(operation='sum', name=node.name + '/shift_add_'))
61         mean_add = Eltwise(graph, dict(operation='sum', name=node.name + '/mean_add_'))
62         variance_mul = Eltwise(graph, dict(operation='mul', name=node.name + '/variance_mul_'))
63
64         mean_negate = Power(graph, dict(scale=-1, name=node.name + '/mean_negate_'))
65         mean_arg = mean_add.create_node_with_data([
66             node.in_node(0),
67             mean_negate.create_node_with_data([node.in_node(3)])])
68
69         variance_square = Power(graph, dict(power=2, name=node.name + '/variance_square_'))
70         variance_denom = Power(graph, dict(shift=node.eps, power=-0.5, name=node.name + '/variance_denom_'))
71         variance_arg = variance_mul.create_node_with_data([
72             mean_arg,
73             variance_denom.create_node_with_data([node.in_node(4)])])
74
75         shift_add.create_node_with_data([
76             scale_mul.create_node_with_data([
77                 variance_arg,
78                 node.in_node(1)]),
79             node.in_node(2)],
80             data_nodes=node.out_node())
81
82         node.graph.remove_node(node.id)