Skip to content

Commit

Permalink
Cleanup unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqwang committed Feb 15, 2022
1 parent 22b5735 commit 2e0e968
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 55 deletions.
Empty file removed test/test_runtime.py
Empty file.
71 changes: 16 additions & 55 deletions test/test_onnx.py → test/test_runtime_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from torchvision.io import read_image
from torchvision.ops._register_onnx_ops import _onnx_opset_version
from yolort import models
from yolort.runtime.ort_helper import export_onnx
from yolort.utils.image_utils import to_numpy

# In environments without onnxruntime we prefer to
Expand All @@ -18,37 +19,18 @@


class TestONNXExporter:
def run_model(
self,
model,
inputs_list,
do_constant_folding=True,
input_names=None,
output_names=None,
dynamic_axes=None,
):
def run_model(self, model, inputs_list):
"""
The core part of exporting model to ONNX and inference with ONNX Runtime
Copy-paste from <https://github.com/pytorch/vision/blob/07fb8ba/test/test_onnx.py#L34>
"""
model = model.eval()

onnx_io = io.BytesIO()
if isinstance(inputs_list[0][-1], dict):
torch_onnx_input = inputs_list[0] + ({},)
else:
torch_onnx_input = inputs_list[0]
# export to onnx with the first input
torch.onnx.export(
model,
torch_onnx_input,
onnx_io,
do_constant_folding=do_constant_folding,
opset_version=_onnx_opset_version,
input_names=input_names,
output_names=output_names,
dynamic_axes=dynamic_axes,
)

# export to onnx models
export_onnx(onnx_io, model=model, opset_version=_onnx_opset_version)

# validate the exported model with onnx runtime
for test_inputs in inputs_list:
with torch.no_grad():
Expand Down Expand Up @@ -88,16 +70,16 @@ def get_test_images(self):
@pytest.mark.parametrize(
"arch, fixed_size, upstream_version",
[
("yolov5s", False, "r3.1"),
("yolov5m", True, "r4.0"),
("yolov5m", False, "r4.0"),
("yolov5n", True, "r6.0"),
("yolov5n", False, "r6.0"),
("yolov5n6", True, "r6.0"),
# ("yolov5s", False, "r3.1"),
# ("yolov5m", True, "r4.0"),
# ("yolov5m", False, "r4.0"),
# ("yolov5n", True, "r6.0"),
# ("yolov5n", False, "r6.0"),
# ("yolov5n6", True, "r6.0"),
("yolov5n6", False, "r6.0"),
],
)
def test_yolort_onnx_export(self, arch, fixed_size, upstream_version):
def test_onnx_export(self, arch, fixed_size, upstream_version):
images_one, images_two = self.get_test_images()
images_dummy = [torch.ones(3, 1080, 720) * 0.3]

Expand All @@ -111,28 +93,7 @@ def test_yolort_onnx_export(self, arch, fixed_size, upstream_version):
model = model.eval()
model(images_one)
# Test exported model on images of different size, or dummy input
self.run_model(
model,
[(images_one,), (images_two,), (images_dummy,)],
input_names=["images"],
output_names=["scores", "labels", "boxes"],
dynamic_axes={
"images": [1, 2],
"boxes": [0, 1],
"labels": [0],
"scores": [0],
},
)
self.run_model(model, [(images_one,), (images_two,), (images_dummy,)])

# Test exported model for an image with no detections on other images
self.run_model(
model,
[(images_dummy,), (images_one,)],
input_names=["images"],
output_names=["scores", "labels", "boxes"],
dynamic_axes={
"images": [1, 2],
"boxes": [0, 1],
"labels": [0],
"scores": [0],
},
)
self.run_model(model, [(images_dummy,), (images_one,)])

0 comments on commit 2e0e968

Please sign in to comment.