|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +import platform |
| 4 | +import os |
| 5 | +import fnmatch |
| 6 | + |
| 7 | +from timm import list_models, create_model |
| 8 | + |
| 9 | + |
| 10 | +if 'GITHUB_ACTIONS' in os.environ and 'Linux' in platform.system(): |
| 11 | + # GitHub Linux runner is slower and hits memory limits sooner than MacOS, exclude bigger models |
| 12 | + EXCLUDE_FILTERS = ['*efficientnet_l2*', '*resnext101_32x48d'] |
| 13 | +else: |
| 14 | + EXCLUDE_FILTERS = [] |
| 15 | +MAX_FWD_SIZE = 384 |
| 16 | +MAX_BWD_SIZE = 128 |
| 17 | +MAX_FWD_FEAT_SIZE = 448 |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.timeout(120) |
| 21 | +@pytest.mark.parametrize('model_name', list_models(exclude_filters=EXCLUDE_FILTERS)) |
| 22 | +@pytest.mark.parametrize('batch_size', [1]) |
| 23 | +def test_model_forward(model_name, batch_size): |
| 24 | + """Run a single forward pass with each model""" |
| 25 | + model = create_model(model_name, pretrained=False) |
| 26 | + model.eval() |
| 27 | + |
| 28 | + input_size = model.default_cfg['input_size'] |
| 29 | + if any([x > MAX_FWD_SIZE for x in input_size]): |
| 30 | + # cap forward test at max res 448 * 448 to keep resource down |
| 31 | + input_size = tuple([min(x, MAX_FWD_SIZE) for x in input_size]) |
| 32 | + inputs = torch.randn((batch_size, *input_size)) |
| 33 | + outputs = model(inputs) |
| 34 | + |
| 35 | + assert outputs.shape[0] == batch_size |
| 36 | + assert not torch.isnan(outputs).any(), 'Output included NaNs' |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.timeout(120) |
| 40 | +# DLA models have an issue TBD, add them to exclusions |
| 41 | +@pytest.mark.parametrize('model_name', list_models(exclude_filters=EXCLUDE_FILTERS + ['dla*'])) |
| 42 | +@pytest.mark.parametrize('batch_size', [2]) |
| 43 | +def test_model_backward(model_name, batch_size): |
| 44 | + """Run a single forward pass with each model""" |
| 45 | + model = create_model(model_name, pretrained=False, num_classes=42) |
| 46 | + num_params = sum([x.numel() for x in model.parameters()]) |
| 47 | + model.eval() |
| 48 | + |
| 49 | + input_size = model.default_cfg['input_size'] |
| 50 | + if any([x > MAX_BWD_SIZE for x in input_size]): |
| 51 | + # cap backward test at 128 * 128 to keep resource usage down |
| 52 | + input_size = tuple([min(x, MAX_BWD_SIZE) for x in input_size]) |
| 53 | + inputs = torch.randn((batch_size, *input_size)) |
| 54 | + outputs = model(inputs) |
| 55 | + outputs.mean().backward() |
| 56 | + num_grad = sum([x.grad.numel() for x in model.parameters() if x.grad is not None]) |
| 57 | + |
| 58 | + assert outputs.shape[-1] == 42 |
| 59 | + assert num_params == num_grad, 'Some parameters are missing gradients' |
| 60 | + assert not torch.isnan(outputs).any(), 'Output included NaNs' |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.timeout(120) |
| 64 | +@pytest.mark.parametrize('model_name', list_models()) |
| 65 | +@pytest.mark.parametrize('batch_size', [1]) |
| 66 | +def test_model_default_cfgs(model_name, batch_size): |
| 67 | + """Run a single forward pass with each model""" |
| 68 | + model = create_model(model_name, pretrained=False) |
| 69 | + model.eval() |
| 70 | + state_dict = model.state_dict() |
| 71 | + cfg = model.default_cfg |
| 72 | + |
| 73 | + classifier = cfg['classifier'] |
| 74 | + first_conv = cfg['first_conv'] |
| 75 | + pool_size = cfg['pool_size'] |
| 76 | + input_size = model.default_cfg['input_size'] |
| 77 | + |
| 78 | + if all([x <= MAX_FWD_FEAT_SIZE for x in input_size]) and \ |
| 79 | + not any([fnmatch.fnmatch(model_name, x) for x in EXCLUDE_FILTERS]): |
| 80 | + # pool size only checked if default res <= 448 * 448 to keep resource down |
| 81 | + input_size = tuple([min(x, MAX_FWD_FEAT_SIZE) for x in input_size]) |
| 82 | + outputs = model.forward_features(torch.randn((batch_size, *input_size))) |
| 83 | + assert outputs.shape[-1] == pool_size[-1] and outputs.shape[-2] == pool_size[-2] |
| 84 | + assert any([k.startswith(classifier) for k in state_dict.keys()]), f'{classifier} not in model params' |
| 85 | + assert any([k.startswith(first_conv) for k in state_dict.keys()]), f'{first_conv} not in model params' |
0 commit comments