|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +""" |
| 5 | +tf2onnx.rewriter - rewrite tensorflow subgraph to onnx flatten op |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from tf2onnx import utils |
| 11 | +from tf2onnx.graph_matcher import OpTypePattern, GraphMatcher |
| 12 | + |
| 13 | + |
| 14 | +# pylint: disable=missing-docstring |
| 15 | + |
| 16 | + |
| 17 | +def rewrite_flatten(g, ops): |
| 18 | + pattern_fixed_shape_input = \ |
| 19 | + OpTypePattern('Reshape', name='reshape', inputs=[ |
| 20 | + OpTypePattern("*", name="input"), |
| 21 | + OpTypePattern('Pack', name="pack", inputs=[ |
| 22 | + OpTypePattern('StridedSlice', name="slice", inputs=[ |
| 23 | + "*", "*", "*", "*", |
| 24 | + ]), |
| 25 | + "*", |
| 26 | + ]), |
| 27 | + ]) |
| 28 | + pattern_non_fixed_shape_input = \ |
| 29 | + OpTypePattern('Reshape', name='reshape', inputs=[ |
| 30 | + OpTypePattern("*", name="input"), |
| 31 | + OpTypePattern('Pack', name="pack", inputs=[ |
| 32 | + OpTypePattern('StridedSlice', name="slice", inputs=[ |
| 33 | + OpTypePattern('Shape', inputs=[ |
| 34 | + OpTypePattern("*", name="input2") |
| 35 | + ]), |
| 36 | + "*", "*", "*", |
| 37 | + ]), |
| 38 | + "*", |
| 39 | + ]), |
| 40 | + ]) |
| 41 | + matcher = GraphMatcher(pattern_fixed_shape_input) |
| 42 | + match_results_1 = list(matcher.match_ops(ops)) |
| 43 | + |
| 44 | + matcher = GraphMatcher(pattern_non_fixed_shape_input) |
| 45 | + match_results_2 = list(matcher.match_ops(ops)) |
| 46 | + |
| 47 | + match_results = [(match_results_1, True), (match_results_2, False)] |
| 48 | + for match_results, check_fixed_input_shape in match_results: |
| 49 | + for match in match_results: |
| 50 | + input_node = match.get_op('input') |
| 51 | + reshape_node = match.get_op('reshape') |
| 52 | + pack_node = match.get_op('pack') |
| 53 | + slice_node = match.get_op('slice') |
| 54 | + need_rewrite = pack_node.inputs[1].is_const() and pack_node.inputs[1].get_tensor_value() == -1 |
| 55 | + if not need_rewrite: |
| 56 | + continue |
| 57 | + |
| 58 | + input_shape = g.get_shape(reshape_node.input[0]) |
| 59 | + need_rewrite = input_shape is not None |
| 60 | + if not need_rewrite: |
| 61 | + continue |
| 62 | + |
| 63 | + if check_fixed_input_shape: |
| 64 | + need_rewrite = slice_node.inputs[0].is_const() and \ |
| 65 | + np.array_equal(list(input_shape), list(slice_node.inputs[0].get_tensor_value())) |
| 66 | + if not need_rewrite: |
| 67 | + continue |
| 68 | + |
| 69 | + begin = slice_node.inputs[1].get_tensor_value(as_list=False) |
| 70 | + end = slice_node.inputs[2].get_tensor_value(as_list=False) |
| 71 | + strides = slice_node.inputs[3].get_tensor_value(as_list=False) |
| 72 | + need_rewrite = np.array_equal(begin, [0]) and len(end) == 1 and \ |
| 73 | + np.array_equal(strides, [1]) and end[0] - begin[0] == 1 |
| 74 | + if not need_rewrite: |
| 75 | + continue |
| 76 | + |
| 77 | + op_name = utils.make_name("Flatten") |
| 78 | + out_name = utils.port_name(op_name) |
| 79 | + g.make_node("Flatten", [reshape_node.input[0]], outputs=[out_name], name=op_name) |
| 80 | + |
| 81 | + last_dim = input_shape[-1] |
| 82 | + sec_last_dim = input_shape[-2] |
| 83 | + new_dim = None |
| 84 | + if last_dim > 0 and sec_last_dim > 0: |
| 85 | + new_dim = last_dim * sec_last_dim |
| 86 | + else: |
| 87 | + new_dim = -1 |
| 88 | + |
| 89 | + g.set_shape(out_name, input_shape[:-2] + [new_dim]) |
| 90 | + g.replace_all_inputs(ops, reshape_node.output[0], out_name) |
| 91 | + to_delete = [n for n in match.get_nodes() if n != input_node] |
| 92 | + g.safe_remove_nodes(to_delete) |
| 93 | + |
| 94 | + return ops |
0 commit comments