1
1
import io
2
2
from collections import OrderedDict
3
- from typing import List , Tuple
3
+ from typing import List , Optional , Tuple
4
4
5
5
import pytest
6
6
import torch
11
11
from torchvision .models .detection .roi_heads import RoIHeads
12
12
from torchvision .models .detection .rpn import AnchorGenerator , RegionProposalNetwork , RPNHead
13
13
from torchvision .models .detection .transform import GeneralizedRCNNTransform
14
- from torchvision .ops . _register_onnx_ops import _onnx_opset_version
14
+ from torchvision .ops import _register_onnx_ops
15
15
16
16
# In environments without onnxruntime we prefer to
17
17
# invoke all tests in the repo and have this one skipped rather than fail.
@@ -32,7 +32,11 @@ def run_model(
32
32
dynamic_axes = None ,
33
33
output_names = None ,
34
34
input_names = None ,
35
+ opset_version : Optional [int ] = None ,
35
36
):
37
+ if opset_version is None :
38
+ opset_version = _register_onnx_ops .base_onnx_opset_version
39
+
36
40
model .eval ()
37
41
38
42
onnx_io = io .BytesIO ()
@@ -46,10 +50,11 @@ def run_model(
46
50
torch_onnx_input ,
47
51
onnx_io ,
48
52
do_constant_folding = do_constant_folding ,
49
- opset_version = _onnx_opset_version ,
53
+ opset_version = opset_version ,
50
54
dynamic_axes = dynamic_axes ,
51
55
input_names = input_names ,
52
56
output_names = output_names ,
57
+ verbose = True ,
53
58
)
54
59
# validate the exported model with onnx runtime
55
60
for test_inputs in inputs_list :
@@ -140,39 +145,39 @@ def test_roi_align(self):
140
145
model = ops .RoIAlign ((5 , 5 ), 1 , - 1 )
141
146
self .run_model (model , [(x , single_roi )])
142
147
143
- @pytest .mark .skip (reason = "ROIAlign with aligned=True is not supported in ONNX, but will be supported in opset 16." )
144
148
def test_roi_align_aligned (self ):
149
+ supported_onnx_version = _register_onnx_ops ._onnx_opset_version_16
145
150
x = torch .rand (1 , 1 , 10 , 10 , dtype = torch .float32 )
146
151
single_roi = torch .tensor ([[0 , 1.5 , 1.5 , 3 , 3 ]], dtype = torch .float32 )
147
152
model = ops .RoIAlign ((5 , 5 ), 1 , 2 , aligned = True )
148
- self .run_model (model , [(x , single_roi )])
153
+ self .run_model (model , [(x , single_roi )], opset_version = supported_onnx_version )
149
154
150
155
x = torch .rand (1 , 1 , 10 , 10 , dtype = torch .float32 )
151
156
single_roi = torch .tensor ([[0 , 0.2 , 0.3 , 4.5 , 3.5 ]], dtype = torch .float32 )
152
157
model = ops .RoIAlign ((5 , 5 ), 0.5 , 3 , aligned = True )
153
- self .run_model (model , [(x , single_roi )])
158
+ self .run_model (model , [(x , single_roi )], opset_version = supported_onnx_version )
154
159
155
160
x = torch .rand (1 , 1 , 10 , 10 , dtype = torch .float32 )
156
161
single_roi = torch .tensor ([[0 , 0.2 , 0.3 , 4.5 , 3.5 ]], dtype = torch .float32 )
157
162
model = ops .RoIAlign ((5 , 5 ), 1.8 , 2 , aligned = True )
158
- self .run_model (model , [(x , single_roi )])
163
+ self .run_model (model , [(x , single_roi )], opset_version = supported_onnx_version )
159
164
160
165
x = torch .rand (1 , 1 , 10 , 10 , dtype = torch .float32 )
161
166
single_roi = torch .tensor ([[0 , 0.2 , 0.3 , 4.5 , 3.5 ]], dtype = torch .float32 )
162
167
model = ops .RoIAlign ((2 , 2 ), 2.5 , 0 , aligned = True )
163
- self .run_model (model , [(x , single_roi )])
168
+ self .run_model (model , [(x , single_roi )], opset_version = supported_onnx_version )
164
169
165
170
x = torch .rand (1 , 1 , 10 , 10 , dtype = torch .float32 )
166
171
single_roi = torch .tensor ([[0 , 0.2 , 0.3 , 4.5 , 3.5 ]], dtype = torch .float32 )
167
172
model = ops .RoIAlign ((2 , 2 ), 2.5 , - 1 , aligned = True )
168
- self .run_model (model , [(x , single_roi )])
173
+ self .run_model (model , [(x , single_roi )], opset_version = supported_onnx_version )
169
174
170
- @pytest .mark .skip (reason = "Issue in exporting ROIAlign with aligned = True for malformed boxes" )
171
175
def test_roi_align_malformed_boxes (self ):
176
+ supported_onnx_version = _register_onnx_ops ._onnx_opset_version_16
172
177
x = torch .randn (1 , 1 , 10 , 10 , dtype = torch .float32 )
173
178
single_roi = torch .tensor ([[0 , 2 , 0.3 , 1.5 , 1.5 ]], dtype = torch .float32 )
174
179
model = ops .RoIAlign ((5 , 5 ), 1 , 1 , aligned = True )
175
- self .run_model (model , [(x , single_roi )])
180
+ self .run_model (model , [(x , single_roi )], opset_version = supported_onnx_version )
176
181
177
182
def test_roi_pool (self ):
178
183
x = torch .rand (1 , 1 , 10 , 10 , dtype = torch .float32 )
0 commit comments