"vscode:/vscode.git/clone" did not exist on "757eeacc1b34c825f5927d2db86d4e73e8fdf52a"
_register_onnx_ops.py 2.87 KB
Newer Older
1
import sys
2
import warnings
3

4
5
import torch

6
7
_onnx_opset_version = 11

8

9
def _register_custom_op():
10
    from torch.onnx.symbolic_helper import parse_args
11
    from torch.onnx.symbolic_opset11 import select, squeeze, unsqueeze
12
    from torch.onnx.symbolic_opset9 import _cast_Long
13

14
    @parse_args("v", "v", "f")
15
16
17
    def symbolic_multi_label_nms(g, boxes, scores, iou_threshold):
        boxes = unsqueeze(g, boxes, 0)
        scores = unsqueeze(g, unsqueeze(g, scores, 0), 0)
18
19
20
21
        max_output_per_class = g.op("Constant", value_t=torch.tensor([sys.maxsize], dtype=torch.long))
        iou_threshold = g.op("Constant", value_t=torch.tensor([iou_threshold], dtype=torch.float))
        nms_out = g.op("NonMaxSuppression", boxes, scores, max_output_per_class, iou_threshold)
        return squeeze(g, select(g, nms_out, 1, g.op("Constant", value_t=torch.tensor([2], dtype=torch.long))), 1)
22

23
    @parse_args("v", "v", "f", "i", "i", "i", "i")
Negin Raoof's avatar
Negin Raoof committed
24
    def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio, aligned):
25
26
27
28
        batch_indices = _cast_Long(
            g, squeeze(g, select(g, rois, 1, g.op("Constant", value_t=torch.tensor([0], dtype=torch.long))), 1), False
        )
        rois = select(g, rois, 1, g.op("Constant", value_t=torch.tensor([1, 2, 3, 4], dtype=torch.long)))
David Fan's avatar
David Fan committed
29
        # TODO: Remove this warning after ONNX opset 16 is supported.
30
        if aligned:
31
            warnings.warn(
David Fan's avatar
David Fan committed
32
33
34
35
                "ROIAlign with aligned=True is not supported in ONNX, but will be supported in opset 16. "
                "The workaround is that the user need apply the patch "
                "https://github.com/microsoft/onnxruntime/pull/8564 "
                "and build ONNXRuntime from source."
36
            )
37
38
39

        # ONNX doesn't support negative sampling_ratio
        if sampling_ratio < 0:
40
            warnings.warn(
41
                "ONNX doesn't support negative sampling ratio, therefore is is set to 0 in order to be exported."
42
            )
43
            sampling_ratio = 0
44
45
46
47
48
49
50
51
52
53
        return g.op(
            "RoiAlign",
            input,
            rois,
            batch_indices,
            spatial_scale_f=spatial_scale,
            output_height_i=pooled_height,
            output_width_i=pooled_width,
            sampling_ratio_i=sampling_ratio,
        )
54

55
    @parse_args("v", "v", "f", "i", "i")
56
    def roi_pool(g, input, rois, spatial_scale, pooled_height, pooled_width):
57
58
59
        roi_pool = g.op(
            "MaxRoiPool", input, rois, pooled_shape_i=(pooled_height, pooled_width), spatial_scale_f=spatial_scale
        )
60
61
62
        return roi_pool, None

    from torch.onnx import register_custom_op_symbolic
63
64
65
66

    register_custom_op_symbolic("torchvision::nms", symbolic_multi_label_nms, _onnx_opset_version)
    register_custom_op_symbolic("torchvision::roi_align", roi_align, _onnx_opset_version)
    register_custom_op_symbolic("torchvision::roi_pool", roi_pool, _onnx_opset_version)