Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / caffe / binarization.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 import numpy as np
17
18 from extensions.ops.quantize import QuantizeOp
19 from mo.front.common.replacement import FrontReplacementOp
20 from mo.graph.graph import Node, Graph
21 from mo.ops.const import Const
22
23
24 class BinarizationToQuantize(FrontReplacementOp):
25     """
26     Replaces Binarization layer with Quantize.
27     """
28     op = "Binarization"
29     enabled = True
30
31     def replace_op(self, graph: Graph, node: Node):
32         in_node_0 = node.in_node(0)
33
34         broadcast = lambda x: np.array([x], dtype=np.float32)
35         threshold = Const(graph, {'name': node.id + "/Input_1", "value": broadcast(0)}).create_node()
36         in_1 = threshold
37         in_2 = threshold
38         in_3 = Const(graph, {'name': node.id + "/Input_3", "value": broadcast(-1)}).create_node()
39         in_4 = Const(graph, {'name': node.id + "/Input_4", "value": broadcast(+1)}).create_node()
40         quant = QuantizeOp(graph, {'name': node.id + "/Quantize_", "levels": 2}).create_node(
41             inputs=[in_node_0, in_1, in_2, in_3, in_4])
42
43         return [quant.id]