Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / instance_normalization.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 networkx as nx
18
19 from mo.front.common.replacement import FrontReplacementOp
20 from mo.graph.graph import Node, Graph
21 from mo.ops.lin_op import Add, Mul
22 from extensions.ops.mvn import MVN
23
24
25 class InstanceNormalization(FrontReplacementOp):
26     ''' Decompose InstanceNormalization to scale*MVN(x) + B
27
28         There are should be also reshapes added for each scale and B.
29     '''
30     op = "InstanceNormalization"
31     enabled = True
32
33     def replace_op(self, graph: Graph, node: Node):
34         prefix = node.name + '/InstanceNormalization'
35         mvn = MVN(graph, dict(
36             name=prefix + '/MVN',
37             eps=node.epsilon
38         ))
39         mul = Mul(graph, dict(name=prefix + '/Mul', axis=1))
40         add = Add(graph, dict(name=prefix + '/Add', axis=1))
41
42
43         new_subgraph = add.create_node([
44             mul.create_node([
45                 mvn.create_node([node.in_node(0)]),
46                 node.in_node(1)
47             ]),
48             node.in_node(2)
49         ])
50
51         return [new_subgraph.id]