|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | + |
| 4 | +""" |
| 5 | +tf2onnx.rewriter.extract_image_patches_rewriter - Rewrites ExtractImagePatches into supported operations. |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from tf2onnx import utils |
| 10 | +from tf2onnx.graph_matcher import OpTypePattern, GraphMatcher |
| 11 | + |
| 12 | + |
| 13 | +def rewrite_extract_image_patches(g, ops): |
| 14 | + pattern = OpTypePattern("ExtractImagePatches", name="extract_image_patches") |
| 15 | + matcher = GraphMatcher(pattern) |
| 16 | + match_results = list(matcher.match_ops(ops)) |
| 17 | + for match_result in match_results: |
| 18 | + operation = match_result.get_op("extract_image_patches") |
| 19 | + input_shape = g.get_shape(operation.input[0]) |
| 20 | + output_shape = operation.output_shapes[0] |
| 21 | + |
| 22 | + sizes = operation.get_attr_value("ksizes") |
| 23 | + strides = operation.get_attr_value("strides") |
| 24 | + rates = operation.get_attr_value("rates") |
| 25 | + padding = operation.get_attr_str("padding") |
| 26 | + |
| 27 | + # Our constraints. |
| 28 | + utils.make_sure(0 not in output_shape, "Empty ExtractImagePatches output is unsupported.") |
| 29 | + [_, size_rows, size_cols, _] = sizes |
| 30 | + |
| 31 | + # Transform input into [N * C, H, W, 1]. |
| 32 | + transformed_input = g.make_node("Reshape", inputs=[ |
| 33 | + g.make_node("Transpose", inputs=operation.input, attr=dict(perm=[0, 3, 1, 2])).output[0], |
| 34 | + g.make_const(utils.make_name("new_shape"), np.int64([ |
| 35 | + input_shape[0] * input_shape[3], |
| 36 | + input_shape[1], |
| 37 | + input_shape[2], |
| 38 | + 1, |
| 39 | + ])).output[0], |
| 40 | + ]) |
| 41 | + |
| 42 | + # Create identity kernel. |
| 43 | + k = size_rows * size_cols |
| 44 | + identity_kernel = g.make_node("Reshape", inputs=[ |
| 45 | + g.make_node("EyeLike", inputs=[ |
| 46 | + g.make_node("ConstantOfShape", inputs=[ |
| 47 | + g.make_const(utils.make_name("eye_size"), np.array([k, k], dtype=np.int64)).output[0], |
| 48 | + ]).output[0], |
| 49 | + ]).output[0], |
| 50 | + g.make_const(utils.make_name("new_shape"), np.array([ |
| 51 | + size_rows, |
| 52 | + size_cols, |
| 53 | + 1, |
| 54 | + k, |
| 55 | + ], dtype=np.int64)).output[0], |
| 56 | + ]) |
| 57 | + |
| 58 | + # Convolve into [N * C, ?H, ?W, K]. |
| 59 | + convolution = g.make_node("Conv2D", inputs=[transformed_input.output[0], identity_kernel.output[0]], |
| 60 | + attr=dict(strides=strides, dilations=rates, padding=padding, data_format="NHWC"), |
| 61 | + shapes=[[input_shape[0] * input_shape[3], output_shape[1], output_shape[2], k]], |
| 62 | + dtypes=operation.output_dtypes, skip_conversion=False) |
| 63 | + |
| 64 | + # Transform into [N, ?H, ?W, C * K]. |
| 65 | + output_node = g.make_node("Reshape", inputs=[ |
| 66 | + g.make_node("Transpose", inputs=[ |
| 67 | + g.make_node("Reshape", inputs=[ |
| 68 | + convolution.output[0], |
| 69 | + g.make_const(utils.make_name("new_shape"), np.array([ |
| 70 | + input_shape[0], |
| 71 | + input_shape[3], |
| 72 | + output_shape[1], |
| 73 | + output_shape[2], |
| 74 | + k, |
| 75 | + ], dtype=np.int64)).output[0], |
| 76 | + ]).output[0], |
| 77 | + ], attr=dict(perm=[0, 2, 3, 4, 1])).output[0], |
| 78 | + g.make_const(utils.make_name("new_shape"), np.array(output_shape, dtype=np.int64)).output[0], |
| 79 | + ]) |
| 80 | + |
| 81 | + # Replace node. |
| 82 | + g.replace_all_inputs(operation.output[0], output_node.output[0]) |
| 83 | + g.remove_node(operation.name) |
| 84 | + return g.get_nodes() |
0 commit comments