|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import onnx |
| 10 | + |
| 11 | +from ..onnx_model import ONNXModel |
| 12 | +from .fusion import Fusion |
| 13 | + |
| 14 | + |
| 15 | +class ReplaceUpsampleWithResize(Fusion): |
| 16 | + """Replace Upsample with Resize.""" |
| 17 | + |
| 18 | + def __init__(self, model: ONNXModel, opset): |
| 19 | + """Initialize.""" |
| 20 | + super().__init__(model, "Resize", "Upsample") |
| 21 | + self.opset = opset |
| 22 | + |
| 23 | + def fuse( |
| 24 | + self, |
| 25 | + node: onnx.NodeProto, |
| 26 | + input_name_to_nodes: dict[str, list[onnx.NodeProto]], |
| 27 | + output_name_to_node: dict[str, onnx.NodeProto], |
| 28 | + ): |
| 29 | + """Replace Upsample with Resize.""" |
| 30 | + mode = None |
| 31 | + for attr in node.attribute: |
| 32 | + if attr.name == "mode": |
| 33 | + mode = attr.s.decode("utf-8") |
| 34 | + break |
| 35 | + |
| 36 | + scales_input = None |
| 37 | + if self.opset > 7: |
| 38 | + scales_input = node.input[1] if len(node.input) > 1 else "" |
| 39 | + resize_inputs = [node.input[0], node.name + "_roi", scales_input] |
| 40 | + else: |
| 41 | + if self.opset == 7: |
| 42 | + for attr in node.attribute: |
| 43 | + if attr.name == "scales": |
| 44 | + scales_input = attr.floats |
| 45 | + break |
| 46 | + |
| 47 | + scales_input = np.array(list(scales_input), np.float32) |
| 48 | + else: |
| 49 | + h_scale = 1 |
| 50 | + w_scale = 1 |
| 51 | + for attr in node.attribute: |
| 52 | + if attr.name == "height_scale": |
| 53 | + h_scale = attr.float |
| 54 | + elif attr.name == "width_scale": |
| 55 | + w_scale = attr.float |
| 56 | + |
| 57 | + scales_input = np.array([1, 1, h_scale, w_scale], np.float32) |
| 58 | + |
| 59 | + scales_tensor = onnx.helper.make_tensor( |
| 60 | + name=node.name + "_scales", |
| 61 | + data_type=onnx.TensorProto.FLOAT, |
| 62 | + dims=scales_input.shape, |
| 63 | + vals=scales_input.flatten().tolist(), |
| 64 | + ) |
| 65 | + |
| 66 | + scales_node = onnx.helper.make_node( |
| 67 | + "Constant", inputs=[], outputs=[node.name + "_scales"], value=scales_tensor |
| 68 | + ) |
| 69 | + |
| 70 | + self.nodes_to_add.append(scales_node) |
| 71 | + |
| 72 | + resize_inputs = [node.input[0], node.name + "_roi", node.name + "_scales"] |
| 73 | + |
| 74 | + roi_tensor = onnx.helper.make_tensor( |
| 75 | + name=node.name + "_roi", |
| 76 | + data_type=onnx.TensorProto.FLOAT, |
| 77 | + dims=(len(scales_input) * 2,), |
| 78 | + vals=[0] * len(scales_input) + [1] * len(scales_input), |
| 79 | + ) |
| 80 | + |
| 81 | + roi_node = onnx.helper.make_node("Constant", inputs=[], outputs=[node.name + "_roi"], value=roi_tensor) |
| 82 | + |
| 83 | + resize_node = onnx.helper.make_node( |
| 84 | + op_type="Resize", inputs=resize_inputs, outputs=node.output, mode=mode, nearest_mode="floor" |
| 85 | + ) |
| 86 | + |
| 87 | + self.nodes_to_remove.append(node) |
| 88 | + self.nodes_to_add.append(roi_node) |
| 89 | + self.nodes_to_add.append(resize_node) |
| 90 | + |
| 91 | + def apply(self) -> bool: |
| 92 | + """Apply.""" |
| 93 | + if super().apply(): |
| 94 | + self.model.topological_sort() |
| 95 | + return True |
| 96 | + return False |
0 commit comments