Skip to content

Instantly share code, notes, and snippets.

@rsomani95
Created January 11, 2021 17:47
Show Gist options
  • Save rsomani95/3db1b7bf7d4e470d57d6f8cc29edf3ef to your computer and use it in GitHub Desktop.
Save rsomani95/3db1b7bf7d4e470d57d6f8cc29edf3ef to your computer and use it in GitHub Desktop.
import torch, torchvision
from onnxruntime import InferenceSession
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
x = torch.rand(1, 3, 360, 640)
torch.onnx.export(
model,
x, # ONNX requires fixed input size
"test-mask-rcnn-export.onnx",
do_constant_folding=True,
dynamic_axes={
"images_tensors": [0, 1, 2, 3],
"boxes": [0, 1],
"labels": [0],
"scores": [0],
"masks": [0, 1, 2, 3],
},
# dynamic_axes={"input_image": {0: "sequence"}, "output": {0: "sequence"}},
input_names=["input_image"],
output_names=["bbox_coords", "bbox_labels", "bbox_scores", "bbox_masks"],
opset_version=11, # opset_version 11 required for Mask R-CNN
)
session = InferenceSession("test-mask-rcnn-export.onnx")
session.run(None, {"input_image": x.detach().cpu().numpy()} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment