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