test_extended_models.py 10.3 KB
Newer Older
1
import importlib
Philip Meier's avatar
Philip Meier committed
2
import os
3
4

import pytest
5
import test_models as TM
6
import torch
7
8
9
from torchvision import models
from torchvision.models._api import WeightsEnum, Weights
from torchvision.models._utils import handle_legacy_interface
10

11

12
13
14
15
run_if_test_with_extended = pytest.mark.skipif(
    os.getenv("PYTORCH_TEST_WITH_EXTENDED", "0") != "1",
    reason="Extended tests are disabled by default. Set PYTORCH_TEST_WITH_EXTENDED=1 to run them.",
)
16
17


18
19
20
21
22
23
def _get_parent_module(model_fn):
    parent_module_name = ".".join(model_fn.__module__.split(".")[:-1])
    module = importlib.import_module(parent_module_name)
    return module


24
25
26
27
28
29
30
31
32
33
34
35
36
def _get_model_weights(model_fn):
    module = _get_parent_module(model_fn)
    weights_name = "_QuantizedWeights" if module.__name__.split(".")[-1] == "quantization" else "_Weights"
    try:
        return next(
            v
            for k, v in module.__dict__.items()
            if k.endswith(weights_name) and k.replace(weights_name, "").lower() == model_fn.__name__
        )
    except StopIteration:
        return None


37
@pytest.mark.parametrize(
38
    "name, weight",
39
    [
40
41
        ("ResNet50_Weights.IMAGENET1K_V1", models.ResNet50_Weights.IMAGENET1K_V1),
        ("ResNet50_Weights.DEFAULT", models.ResNet50_Weights.IMAGENET1K_V2),
42
        (
43
44
            "ResNet50_QuantizedWeights.DEFAULT",
            models.quantization.ResNet50_QuantizedWeights.IMAGENET1K_FBGEMM_V2,
45
        ),
46
        (
47
48
            "ResNet50_QuantizedWeights.IMAGENET1K_FBGEMM_V1",
            models.quantization.ResNet50_QuantizedWeights.IMAGENET1K_FBGEMM_V1,
49
        ),
50
51
    ],
)
52
53
def test_get_weight(name, weight):
    assert models.get_weight(name) == weight
54
55


56
57
58
59
60
61
@pytest.mark.parametrize(
    "model_fn",
    TM.get_models_from_module(models)
    + TM.get_models_from_module(models.detection)
    + TM.get_models_from_module(models.quantization)
    + TM.get_models_from_module(models.segmentation)
62
63
    + TM.get_models_from_module(models.video)
    + TM.get_models_from_module(models.optical_flow),
64
65
)
def test_naming_conventions(model_fn):
66
67
    weights_enum = _get_model_weights(model_fn)
    assert weights_enum is not None
68
    assert len(weights_enum) == 0 or hasattr(weights_enum, "DEFAULT")
69
70


71
72
73
74
75
76
@pytest.mark.parametrize(
    "model_fn",
    TM.get_models_from_module(models)
    + TM.get_models_from_module(models.detection)
    + TM.get_models_from_module(models.quantization)
    + TM.get_models_from_module(models.segmentation)
77
78
    + TM.get_models_from_module(models.video)
    + TM.get_models_from_module(models.optical_flow),
79
)
80
@run_if_test_with_extended
81
def test_schema_meta_validation(model_fn):
82
83
84
85
86
87
    # list of all possible supported high-level fields for weights meta-data
    permitted_fields = {
        "backend",
        "categories",
        "keypoint_names",
        "license",
88
        "_metrics",
89
90
91
92
        "min_size",
        "num_params",
        "recipe",
        "unquantized",
93
        "_docs",
94
95
    }
    # mandatory fields for each computer vision task
96
    classification_fields = {"categories", ("_metrics", "ImageNet-1K", "acc@1"), ("_metrics", "ImageNet-1K", "acc@5")}
97
    defaults = {
98
        "all": {"_metrics", "min_size", "num_params", "recipe", "_docs"},
99
        "models": classification_fields,
100
        "detection": {"categories", ("_metrics", "COCO-val2017", "box_map")},
101
        "quantization": classification_fields | {"backend", "unquantized"},
102
103
104
105
106
107
        "segmentation": {
            "categories",
            ("_metrics", "COCO-val2017-VOC-labels", "miou"),
            ("_metrics", "COCO-val2017-VOC-labels", "pixel_acc"),
        },
        "video": {"categories", ("_metrics", "Kinetics-400", "acc@1"), ("_metrics", "Kinetics-400", "acc@5")},
108
        "optical_flow": set(),
109
    }
110
    model_name = model_fn.__name__
111
    module_name = model_fn.__module__.split(".")[-2]
112
    expected_fields = defaults["all"] | defaults[module_name]
113
114

    weights_enum = _get_model_weights(model_fn)
115
116
    if len(weights_enum) == 0:
        pytest.skip(f"Model '{model_name}' doesn't have any pre-trained weights.")
117
118

    problematic_weights = {}
119
    incorrect_params = []
120
    bad_names = []
121
    for w in weights_enum:
122
123
124
125
126
127
128
        actual_fields = set(w.meta.keys())
        actual_fields |= set(
            ("_metrics", dataset, metric_key)
            for dataset in w.meta.get("_metrics", {}).keys()
            for metric_key in w.meta.get("_metrics", {}).get(dataset, {}).keys()
        )
        missing_fields = expected_fields - actual_fields
129
130
131
        unsupported_fields = set(w.meta.keys()) - permitted_fields
        if missing_fields or unsupported_fields:
            problematic_weights[w] = {"missing": missing_fields, "unsupported": unsupported_fields}
132
        if w == weights_enum.DEFAULT:
133
            if module_name == "quantization":
134
                # parameters() count doesn't work well with quantization, so we check against the non-quantized
135
136
137
138
139
140
141
                unquantized_w = w.meta.get("unquantized")
                if unquantized_w is not None and w.meta.get("num_params") != unquantized_w.meta.get("num_params"):
                    incorrect_params.append(w)
            else:
                if w.meta.get("num_params") != sum(p.numel() for p in model_fn(weights=w).parameters()):
                    incorrect_params.append(w)
        else:
142
            if w.meta.get("num_params") != weights_enum.DEFAULT.meta.get("num_params"):
143
144
                if w.meta.get("num_params") != sum(p.numel() for p in model_fn(weights=w).parameters()):
                    incorrect_params.append(w)
145
146
        if not w.name.isupper():
            bad_names.append(w)
147
148

    assert not problematic_weights
149
    assert not incorrect_params
150
    assert not bad_names
151
152


153
@pytest.mark.parametrize(
154
155
156
157
158
    "model_fn",
    TM.get_models_from_module(models)
    + TM.get_models_from_module(models.detection)
    + TM.get_models_from_module(models.quantization)
    + TM.get_models_from_module(models.segmentation)
159
160
    + TM.get_models_from_module(models.video)
    + TM.get_models_from_module(models.optical_flow),
161
)
162
163
164
165
166
167
168
@run_if_test_with_extended
def test_transforms_jit(model_fn):
    model_name = model_fn.__name__
    weights_enum = _get_model_weights(model_fn)
    if len(weights_enum) == 0:
        pytest.skip(f"Model '{model_name}' doesn't have any pre-trained weights.")

169
    defaults = {
170
171
172
        "models": {
            "input_shape": (1, 3, 224, 224),
        },
173
174
175
        "detection": {
            "input_shape": (3, 300, 300),
        },
176
177
178
        "quantization": {
            "input_shape": (1, 3, 224, 224),
        },
179
180
181
182
        "segmentation": {
            "input_shape": (1, 3, 520, 520),
        },
        "video": {
183
            "input_shape": (1, 4, 112, 112, 3),
184
        },
185
186
187
        "optical_flow": {
            "input_shape": (1, 3, 128, 128),
        },
188
    }
189
    module_name = model_fn.__module__.split(".")[-2]
190

191
192
193
    kwargs = {**defaults[module_name], **TM._model_params.get(model_name, {})}
    input_shape = kwargs.pop("input_shape")
    x = torch.rand(input_shape)
194
    if module_name == "optical_flow":
195
        args = (x, x)
196
    else:
197
        args = (x,)
198

199
200
201
202
203
204
205
    problematic_weights = []
    for w in weights_enum:
        transforms = w.transforms()
        try:
            TM._check_jit_scriptable(transforms, args)
        except Exception:
            problematic_weights.append(w)
206

207
    assert not problematic_weights
Philip Meier's avatar
Philip Meier committed
208
209
210
211
212


# With this filter, every unexpected warning will be turned into an error
@pytest.mark.filterwarnings("error")
class TestHandleLegacyInterface:
213
    class ModelWeights(WeightsEnum):
Philip Meier's avatar
Philip Meier committed
214
215
216
217
218
219
220
        Sentinel = Weights(url="https://pytorch.org", transforms=lambda x: x, meta=dict())

    @pytest.mark.parametrize(
        "kwargs",
        [
            pytest.param(dict(), id="empty"),
            pytest.param(dict(weights=None), id="None"),
221
            pytest.param(dict(weights=ModelWeights.Sentinel), id="Weights"),
Philip Meier's avatar
Philip Meier committed
222
223
224
        ],
    )
    def test_no_warn(self, kwargs):
225
        @handle_legacy_interface(weights=("pretrained", self.ModelWeights.Sentinel))
Philip Meier's avatar
Philip Meier committed
226
227
228
229
230
231
232
        def builder(*, weights=None):
            pass

        builder(**kwargs)

    @pytest.mark.parametrize("pretrained", (True, False))
    def test_pretrained_pos(self, pretrained):
233
        @handle_legacy_interface(weights=("pretrained", self.ModelWeights.Sentinel))
Philip Meier's avatar
Philip Meier committed
234
235
236
237
238
239
240
241
        def builder(*, weights=None):
            pass

        with pytest.warns(UserWarning, match="positional"):
            builder(pretrained)

    @pytest.mark.parametrize("pretrained", (True, False))
    def test_pretrained_kw(self, pretrained):
242
        @handle_legacy_interface(weights=("pretrained", self.ModelWeights.Sentinel))
Philip Meier's avatar
Philip Meier committed
243
244
245
246
247
248
249
250
251
        def builder(*, weights=None):
            pass

        with pytest.warns(UserWarning, match="deprecated"):
            builder(pretrained)

    @pytest.mark.parametrize("pretrained", (True, False))
    @pytest.mark.parametrize("positional", (True, False))
    def test_equivalent_behavior_weights(self, pretrained, positional):
252
        @handle_legacy_interface(weights=("pretrained", self.ModelWeights.Sentinel))
Philip Meier's avatar
Philip Meier committed
253
254
255
256
        def builder(*, weights=None):
            pass

        args, kwargs = ((pretrained,), dict()) if positional else ((), dict(pretrained=pretrained))
257
        with pytest.warns(UserWarning, match=f"weights={self.ModelWeights.Sentinel if pretrained else None}"):
Philip Meier's avatar
Philip Meier committed
258
259
260
261
262
263
264
265
            builder(*args, **kwargs)

    def test_multi_params(self):
        weights_params = ("weights", "weights_other")
        pretrained_params = [param.replace("weights", "pretrained") for param in weights_params]

        @handle_legacy_interface(
            **{
266
                weights_param: (pretrained_param, self.ModelWeights.Sentinel)
Philip Meier's avatar
Philip Meier committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
                for weights_param, pretrained_param in zip(weights_params, pretrained_params)
            }
        )
        def builder(*, weights=None, weights_other=None):
            pass

        for pretrained_param in pretrained_params:
            with pytest.warns(UserWarning, match="deprecated"):
                builder(**{pretrained_param: True})

    def test_default_callable(self):
        @handle_legacy_interface(
            weights=(
                "pretrained",
281
                lambda kwargs: self.ModelWeights.Sentinel if kwargs["flag"] else None,
Philip Meier's avatar
Philip Meier committed
282
283
284
285
286
287
288
289
290
291
            )
        )
        def builder(*, weights=None, flag):
            pass

        with pytest.warns(UserWarning, match="deprecated"):
            builder(pretrained=True, flag=True)

        with pytest.raises(ValueError, match="weights"):
            builder(pretrained=True, flag=False)