|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +import pytorch_toolbelt.modules.encoders as E |
| 5 | +from pytorch_toolbelt.modules.backbone.inceptionv4 import inceptionv4 |
| 6 | +from pytorch_toolbelt.utils.torch_utils import maybe_cuda, count_parameters |
| 7 | +from pytorch_toolbelt.modules.encoders import timm |
| 8 | + |
| 9 | +skip_if_no_cuda = pytest.mark.skipif(not torch.cuda.is_available(), reason="Cuda is not available") |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + ["encoder", "encoder_params"], |
| 14 | + [ |
| 15 | + [timm.B0Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 16 | + [timm.MixNetXLEncoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 17 | + [timm.SKResNet18Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 18 | + [timm.SWSLResNeXt101Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 19 | + [timm.TimmResnet200D, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 20 | + [timm.HRNetW18Encoder, {"pretrained": False}], |
| 21 | + [timm.DPN68Encoder, {"pretrained": False}], |
| 22 | + [timm.NFNetF0Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 23 | + [timm.NFNetF0SEncoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 24 | + [timm.NFRegNetB0Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 25 | + [timm.TimmRes2Next50Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 26 | + ], |
| 27 | +) |
| 28 | +@skip_if_no_cuda |
| 29 | +def test_onnx_export(encoder, encoder_params): |
| 30 | + import onnx |
| 31 | + |
| 32 | + model = encoder(**encoder_params).eval() |
| 33 | + |
| 34 | + print(model.__class__.__name__, count_parameters(model)) |
| 35 | + print(model.strides) |
| 36 | + print(model.channels) |
| 37 | + dummy_input = torch.rand((1, 3, 256, 256)) |
| 38 | + dummy_input = maybe_cuda(dummy_input) |
| 39 | + model = maybe_cuda(model) |
| 40 | + |
| 41 | + input_names = ["image"] |
| 42 | + output_names = [f"feature_map_{i}" for i in range(len(model.channels))] |
| 43 | + |
| 44 | + torch.onnx.export(model, dummy_input, "tmp.onnx", verbose=True, input_names=input_names, output_names=output_names) |
| 45 | + model = onnx.load("tmp.onnx") |
| 46 | + onnx.checker.check_model(model) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + ["encoder", "encoder_params"], |
| 51 | + [ |
| 52 | + [timm.B0Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 53 | + [timm.MixNetXLEncoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 54 | + [timm.SKResNet18Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 55 | + [timm.SWSLResNeXt101Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 56 | + [timm.TimmResnet200D, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 57 | + [timm.HRNetW18Encoder, {"pretrained": False}], |
| 58 | + [timm.DPN68Encoder, {"pretrained": False}], |
| 59 | + [timm.NFNetF0Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 60 | + [timm.NFNetF0SEncoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 61 | + [timm.NFRegNetB0Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 62 | + [timm.TimmRes2Next50Encoder, {"pretrained": False, "layers": [0, 1, 2, 3, 4]}], |
| 63 | + ], |
| 64 | +) |
| 65 | +@skip_if_no_cuda |
| 66 | +def test_jit_trace(encoder, encoder_params): |
| 67 | + model = encoder(**encoder_params).eval() |
| 68 | + |
| 69 | + print(model.__class__.__name__, count_parameters(model)) |
| 70 | + print(model.strides) |
| 71 | + print(model.channels) |
| 72 | + dummy_input = torch.rand((1, 3, 256, 256)) |
| 73 | + dummy_input = maybe_cuda(dummy_input) |
| 74 | + model = maybe_cuda(model) |
| 75 | + |
| 76 | + model = torch.jit.trace(model, dummy_input, check_trace=True) |
0 commit comments