2 Copyright (c) 2017-2018 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 from extensions.front.mxnet.ssd_pattern_remove_flatten import SsdPatternRemoveFlatten
19 from extensions.front.mxnet.ssd_pattern_remove_reshape import SsdPatternRemoveReshape
20 from extensions.front.mxnet.ssd_pattern_flatten_softmax_activation import SsdPatternFlattenSoftmaxActivation
21 from mo.graph.graph import create_edge
22 from mo.front.common.replacement import FrontReplacementSubgraph
25 class SsdPatternRemoveTranspose(FrontReplacementSubgraph):
30 return [SsdPatternFlattenSoftmaxActivation, SsdPatternRemoveFlatten, SsdPatternRemoveReshape]
35 ('transpose', dict(op='transpose')),
36 ('softmax_activation', dict(op='SoftmaxActivation')),
37 ('multi_box_detection', dict(op='_contrib_MultiBoxDetection'))
40 ('transpose', 'softmax_activation', {'in': 0}),
41 ('softmax_activation', 'multi_box_detection', {'in': 1}),
46 def replace_sub_graph(self, graph: nx.MultiDiGraph, match: dict):
48 Need to find each occurrence of pattern:
49 transpose -> SoftmaxActivation -> _contrib_MultiBoxDetection
50 remove transpose layer to secure the order of weights in SoftMax to be the same as IE expects
51 IE expects weights to be in following order: class-wise values for each priorbox.
52 priorboxes change the quickest
56 graph : nx.MultiDiGraph
57 Graph with loaded model.
59 Patterns which were found in graph structure.
61 transpose_node = match['transpose']
62 softmax_activation = match['softmax_activation']
63 transpose_in_node = transpose_node.in_node(0)
65 graph.remove_edge(transpose_in_node.id, transpose_node.id)
66 graph.remove_edge(transpose_node.id, softmax_activation.id)
67 graph.remove_node(transpose_node.id)
68 create_edge(transpose_in_node, softmax_activation)