Files
openvino/model-optimizer/extensions/front/global_pooling_to_reduce.py
T
Evgeny Lazarev 970b1301b5 Cleanup IR v7 from the MO (#1008)
* Removed back phase transformations related to IRv7

* Fixed setting value for the input port using the 'set_value' method

* Removed front and middle phase transformations related to IRv7

* Cleanup the rest of the Model Optimizer transformations from IRv7 specific transformations

* Final cleanup of the deprecated IR v7 related code

* Removed 'blobs_as_input' usage in the Model Optimizer.

* Removed function '_fuse_add' from the Model Optimizer since it is not used anymore.

* Removed 'keep_in_IR' node attribute for FakeQuantize ops in the MO

* Disabled failing gpu_engine.user_context test
2020-06-22 11:52:00 +03:00

78 lines
3.0 KiB
Python

"""
Copyright (C) 2018-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import logging as log
from extensions.front.rank_decomposer import RankDecomposer
from extensions.ops.ReduceOps import ReduceMax, ReduceMean
from extensions.ops.range import Range
from extensions.ops.rank import Rank
from mo.front.common.partial_infer.utils import int64_array
from mo.front.common.replacement import FrontReplacementPattern
from mo.graph.graph import Graph
from mo.ops.const import Const
class GlobalPoolingToReduce(FrontReplacementPattern):
op = "Pooling"
enabled = True
pool_method_to_reduce_type = {
'max': ReduceMax,
'avg': ReduceMean,
}
def run_before(self):
return [RankDecomposer]
def find_and_replace_pattern(self, graph: Graph):
global_poolings = graph.get_op_nodes(type='Pooling', global_pool=True)
if len(global_poolings) == 0:
return
layout = graph.graph['layout']
assert layout != 'NHWC', 'Global pooling transformation depends on layout (NHWC not enabled)'
for pooling in global_poolings:
name = pooling.soft_get('name', pooling.id)
assert pooling.has_valid('pool_method'), 'Global Pooling {} has no `pool_method` attribute'.format(name)
method = pooling['pool_method']
assert method in self.pool_method_to_reduce_type, \
'Unexpected Global Pooling method `{}` for node `{}`'.format(method, name)
reduce_op_class = self.pool_method_to_reduce_type[method]
reduce = reduce_op_class(graph, {'name': name + '/reduce', 'keep_dims': True}).create_node()
pooling.out_port(0).get_connection().set_source(reduce.out_port(0))
src = pooling.in_port(0).get_connection().get_source()
pooling.in_port(0).disconnect()
src.connect(reduce.in_port(0))
start = Const(graph, {'value': int64_array(2)}).create_node()
end = Rank(graph, {'name': name + '/input_rank'}).create_node()
delta = Const(graph, {'value': int64_array(1)}).create_node()
axis = Range(graph, {'name': name + '/global_pooling_reduce_axis'}).create_node()
axis.in_port(0).connect(start.out_port(0))
src.connect(end.in_port(0))
axis.in_port(1).connect(end.out_port(0))
axis.in_port(2).connect(delta.out_port(0))
axis.out_port(0).connect(reduce.in_port(1))
log.debug('Global {} pooling was converted to reduce: `{}`'.format(method, name))