From 5450f4de6bba5d7d200ad4f93b65bcae33c9f52f Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Thu, 14 Jan 2021 16:24:50 -0800 Subject: [PATCH 1/8] add tests --- test/test_onnx.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/test/test_onnx.py b/test/test_onnx.py index b2a7624fc61..b3def2b46d6 100644 --- a/test/test_onnx.py +++ b/test/test_onnx.py @@ -12,7 +12,8 @@ from torchvision.models.detection.mask_rcnn import MaskRCNNHeads, MaskRCNNPredictor from collections import OrderedDict - +from torchvision import transforms as T +from torchvision.io.image import decode_jpeg # onnxruntime requires python 3.5 or above try: import onnxruntime @@ -39,7 +40,7 @@ def run_model(self, model, inputs_list, tolerate_small_mismatch=False, do_consta else: torch_onnx_input = inputs_list[0] # export to onnx with the first input - torch.onnx.export(model, torch_onnx_input, onnx_io, + torch.onnx.export(model, torch_onnx_input, onnx_io, verbose=True, do_constant_folding=do_constant_folding, opset_version=_onnx_opset_version, dynamic_axes=dynamic_axes, input_names=input_names, output_names=output_names) # validate the exported model with onnx runtime @@ -81,6 +82,14 @@ def to_numpy(tensor): else: raise + def test_decode(self): + class Module(torch.nn.Module): + def forward(self, x): + return decode_jpeg(x) + + x = torch.randn(2, 3, 225, 225) + self.run_model(Module(), (x,)) + def test_nms(self): boxes = torch.rand(5, 4) boxes[:, 2:] += torch.rand(5, 2) @@ -487,6 +496,23 @@ def test_keypoint_rcnn(self): dynamic_axes={"images_tensors": [0, 1, 2]}, tolerate_small_mismatch=True) + + # from torchvision import transforms as T + def test_resize(self): + x = torch.rand(3, 32, 46) + m = T.Resize(size=38) + self.run_model(m, (x,)) + + def test_pad(self): + x = torch.rand(3, 32, 46) + m = T.Pad(2) + self.run_model(m, (x,)) + + def test_gauss(self): + x = torch.randn(3, 32, 46) + m = T.GaussianBlur([1, 1]) + self.run_model(m, (x,)) + def test_shufflenet_v2_dynamic_axes(self): model = models.shufflenet_v2_x0_5(pretrained=True) dummy_input = torch.randn(1, 3, 224, 224, requires_grad=True) From d431919b6b171d8e75fb32b3c79596a0d7ef5bb6 Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Fri, 5 Feb 2021 10:26:14 -0800 Subject: [PATCH 2/8] fix bug --- test/test_onnx.py | 10 ++++++++++ torchvision/ops/_register_onnx_ops.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/test/test_onnx.py b/test/test_onnx.py index b3def2b46d6..6dd8589869d 100644 --- a/test/test_onnx.py +++ b/test/test_onnx.py @@ -122,6 +122,11 @@ def test_roi_align(self): model = ops.RoIAlign((5, 5), 1, 2) self.run_model(model, [(x, single_roi)]) + x = torch.rand(1, 1, 10, 10, dtype=torch.float32) + single_roi = torch.tensor([[0, 0, 0, 4, 4]], dtype=torch.float32) + model = ops.RoIAlign((5, 5), 1, -1) + self.run_model(model, [(x, single_roi)]) + def test_roi_align_aligned(self): x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 1.5, 1.5, 3, 3]], dtype=torch.float32) @@ -143,6 +148,11 @@ def test_roi_align_aligned(self): model = ops.RoIAlign((2, 2), 2.5, 0, aligned=True) self.run_model(model, [(x, single_roi)]) + x = torch.rand(1, 1, 10, 10, dtype=torch.float32) + single_roi = torch.tensor([[0, 0.2, 0.3, 4.5, 3.5]], dtype=torch.float32) + model = ops.RoIAlign((2, 2), 2.5, -1, aligned=True) + self.run_model(model, [(x, single_roi)]) + @unittest.skip # Issue in exporting ROIAlign with aligned = True for malformed boxes def test_roi_align_malformed_boxes(self): x = torch.randn(1, 1, 10, 10, dtype=torch.float32) diff --git a/torchvision/ops/_register_onnx_ops.py b/torchvision/ops/_register_onnx_ops.py index 02013844aac..f746f0f8dcc 100644 --- a/torchvision/ops/_register_onnx_ops.py +++ b/torchvision/ops/_register_onnx_ops.py @@ -29,6 +29,10 @@ def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampli " ONNX forces ROIs to be 1x1 or larger.") scale = torch.tensor(0.5 / spatial_scale).to(dtype=torch.float) rois = g.op("Sub", rois, scale) + + # ONX doesn't support negative sampling_raatio + if sampling_ratio < 0: + sampling_ratio = 0 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) From f2eaa86467237917f5ea224c8c0d8df30eeb02d1 Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Fri, 5 Feb 2021 10:30:09 -0800 Subject: [PATCH 3/8] remove tests --- test/test_onnx.py | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/test/test_onnx.py b/test/test_onnx.py index 6dd8589869d..7f2ad000c61 100644 --- a/test/test_onnx.py +++ b/test/test_onnx.py @@ -12,8 +12,7 @@ from torchvision.models.detection.mask_rcnn import MaskRCNNHeads, MaskRCNNPredictor from collections import OrderedDict -from torchvision import transforms as T -from torchvision.io.image import decode_jpeg + # onnxruntime requires python 3.5 or above try: import onnxruntime @@ -40,7 +39,7 @@ def run_model(self, model, inputs_list, tolerate_small_mismatch=False, do_consta else: torch_onnx_input = inputs_list[0] # export to onnx with the first input - torch.onnx.export(model, torch_onnx_input, onnx_io, verbose=True, + torch.onnx.export(model, torch_onnx_input, onnx_io, do_constant_folding=do_constant_folding, opset_version=_onnx_opset_version, dynamic_axes=dynamic_axes, input_names=input_names, output_names=output_names) # validate the exported model with onnx runtime @@ -82,14 +81,6 @@ def to_numpy(tensor): else: raise - def test_decode(self): - class Module(torch.nn.Module): - def forward(self, x): - return decode_jpeg(x) - - x = torch.randn(2, 3, 225, 225) - self.run_model(Module(), (x,)) - def test_nms(self): boxes = torch.rand(5, 4) boxes[:, 2:] += torch.rand(5, 2) @@ -506,23 +497,6 @@ def test_keypoint_rcnn(self): dynamic_axes={"images_tensors": [0, 1, 2]}, tolerate_small_mismatch=True) - - # from torchvision import transforms as T - def test_resize(self): - x = torch.rand(3, 32, 46) - m = T.Resize(size=38) - self.run_model(m, (x,)) - - def test_pad(self): - x = torch.rand(3, 32, 46) - m = T.Pad(2) - self.run_model(m, (x,)) - - def test_gauss(self): - x = torch.randn(3, 32, 46) - m = T.GaussianBlur([1, 1]) - self.run_model(m, (x,)) - def test_shufflenet_v2_dynamic_axes(self): model = models.shufflenet_v2_x0_5(pretrained=True) dummy_input = torch.randn(1, 3, 224, 224, requires_grad=True) From a02602d655248512466f37b4020f4fad1e0ce878 Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Fri, 5 Feb 2021 10:30:54 -0800 Subject: [PATCH 4/8] fix comment --- torchvision/ops/_register_onnx_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/ops/_register_onnx_ops.py b/torchvision/ops/_register_onnx_ops.py index f746f0f8dcc..1e14bb69a67 100644 --- a/torchvision/ops/_register_onnx_ops.py +++ b/torchvision/ops/_register_onnx_ops.py @@ -30,7 +30,7 @@ def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampli scale = torch.tensor(0.5 / spatial_scale).to(dtype=torch.float) rois = g.op("Sub", rois, scale) - # ONX doesn't support negative sampling_raatio + # ONNX doesn't support negative sampling_raatio if sampling_ratio < 0: sampling_ratio = 0 return g.op('RoiAlign', input, rois, batch_indices, spatial_scale_f=spatial_scale, From b8f43d4d3837d6c0a2530e9398680e53490e3579 Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Fri, 5 Feb 2021 10:35:08 -0800 Subject: [PATCH 5/8] fix comment --- torchvision/ops/_register_onnx_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/ops/_register_onnx_ops.py b/torchvision/ops/_register_onnx_ops.py index 1e14bb69a67..ceebbbf7d67 100644 --- a/torchvision/ops/_register_onnx_ops.py +++ b/torchvision/ops/_register_onnx_ops.py @@ -30,7 +30,7 @@ def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampli scale = torch.tensor(0.5 / spatial_scale).to(dtype=torch.float) rois = g.op("Sub", rois, scale) - # ONNX doesn't support negative sampling_raatio + # ONNX doesn't support negative sampling_ratio if sampling_ratio < 0: sampling_ratio = 0 return g.op('RoiAlign', input, rois, batch_indices, spatial_scale_f=spatial_scale, From 6b4855d491a263c74b3c0d6623e85b473671a82c Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Mon, 1 Mar 2021 11:49:13 -0800 Subject: [PATCH 6/8] add warning --- torchvision/ops/_register_onnx_ops.py | 1 + 1 file changed, 1 insertion(+) diff --git a/torchvision/ops/_register_onnx_ops.py b/torchvision/ops/_register_onnx_ops.py index ceebbbf7d67..917d638e221 100644 --- a/torchvision/ops/_register_onnx_ops.py +++ b/torchvision/ops/_register_onnx_ops.py @@ -32,6 +32,7 @@ def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampli # ONNX doesn't support negative sampling_ratio if sampling_ratio < 0: + warnings.warn("ONNX doesn't support negative sampling ratio, therefore is is set to 0 in order to be exported.) sampling_ratio = 0 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) From 434704e5eaab7275e55f92877f15ee2302267a65 Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Thu, 11 Mar 2021 15:45:07 -0800 Subject: [PATCH 7/8] fix syntax error --- torchvision/ops/_register_onnx_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/ops/_register_onnx_ops.py b/torchvision/ops/_register_onnx_ops.py index 917d638e221..a28934655ad 100644 --- a/torchvision/ops/_register_onnx_ops.py +++ b/torchvision/ops/_register_onnx_ops.py @@ -32,7 +32,7 @@ def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampli # ONNX doesn't support negative sampling_ratio if sampling_ratio < 0: - warnings.warn("ONNX doesn't support negative sampling ratio, therefore is is set to 0 in order to be exported.) + warnings.warn("ONNX doesn't support negative sampling ratio, therefore is is set to 0 in order to be exported.") sampling_ratio = 0 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) From 8c7045c43c74266c90721c937ab36069026406cb Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Thu, 11 Mar 2021 17:25:55 -0800 Subject: [PATCH 8/8] fix python lint --- torchvision/ops/_register_onnx_ops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/torchvision/ops/_register_onnx_ops.py b/torchvision/ops/_register_onnx_ops.py index a28934655ad..8e8ed331803 100644 --- a/torchvision/ops/_register_onnx_ops.py +++ b/torchvision/ops/_register_onnx_ops.py @@ -32,7 +32,8 @@ def roi_align(g, input, rois, spatial_scale, pooled_height, pooled_width, sampli # ONNX doesn't support negative sampling_ratio if sampling_ratio < 0: - warnings.warn("ONNX doesn't support negative sampling ratio, therefore is is set to 0 in order to be exported.") + warnings.warn("ONNX doesn't support negative sampling ratio," + "therefore is is set to 0 in order to be exported.") sampling_ratio = 0 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)