_register_onnx_ops.py 2.9 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
11
12
13
14
15
    from torch.onnx.symbolic_helper import (
        parse_args,
        scalar_type_to_onnx,
        scalar_type_to_pytorch_type,
        cast_pytorch_to_onnx,
    )
16
    from torch.onnx.symbolic_opset11 import select, squeeze, unsqueeze
17
    from torch.onnx.symbolic_opset9 import _cast_Long
18

19
    @parse_args("v", "v", "f")
20
21
22
    def symbolic_multi_label_nms(g, boxes, scores, iou_threshold):
        boxes = unsqueeze(g, boxes, 0)
        scores = unsqueeze(g, unsqueeze(g, scores, 0), 0)
23
24
25
26
        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)
27

28
    @parse_args("v", "v", "f", "i", "i", "i", "i")
Negin Raoof's avatar
Negin Raoof committed
29
    def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio, aligned):
30
31
32
33
        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)))
34
        if aligned:
35
36
37
38
            warnings.warn(
                "ONNX export of ROIAlign with aligned=True does not match PyTorch when using malformed boxes,"
                " ONNX forces ROIs to be 1x1 or larger."
            )
39
40
            scale = torch.tensor(0.5 / spatial_scale).to(dtype=torch.float)
            rois = g.op("Sub", rois, scale)
41
42
43

        # ONNX doesn't support negative sampling_ratio
        if sampling_ratio < 0:
44
45
46
            warnings.warn(
                "ONNX doesn't support negative sampling ratio," "therefore is is set to 0 in order to be exported."
            )
47
            sampling_ratio = 0
48
49
50
51
52
53
54
55
56
57
        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,
        )
58

59
    @parse_args("v", "v", "f", "i", "i")
60
    def roi_pool(g, input, rois, spatial_scale, pooled_height, pooled_width):
61
62
63
        roi_pool = g.op(
            "MaxRoiPool", input, rois, pooled_shape_i=(pooled_height, pooled_width), spatial_scale_f=spatial_scale
        )
64
65
66
        return roi_pool, None

    from torch.onnx import register_custom_op_symbolic
67
68
69
70

    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)