|
| 1 | +import warnings |
| 2 | +from functools import partial |
| 3 | +from typing import Any, Optional |
| 4 | + |
| 5 | +from torchvision.transforms.functional import InterpolationMode |
| 6 | + |
| 7 | +from ...models.squeezenet import SqueezeNet |
| 8 | +from ..transforms.presets import ImageNetEval |
| 9 | +from ._api import Weights, WeightEntry |
| 10 | +from ._meta import _IMAGENET_CATEGORIES |
| 11 | + |
| 12 | + |
| 13 | +__all__ = ["SqueezeNet", "SqueezeNet1_0Weights", "SqueezeNet1_1Weights", "squeezenet1_0", "squeezenet1_1"] |
| 14 | + |
| 15 | + |
| 16 | +_common_meta = {"size": (224, 224), "categories": _IMAGENET_CATEGORIES, "interpolation": InterpolationMode.BILINEAR} |
| 17 | + |
| 18 | + |
| 19 | +class SqueezeNet1_0Weights(Weights): |
| 20 | + ImageNet1K_Community = WeightEntry( |
| 21 | + url="https://download.pytorch.org/models/squeezenet1_0-b66bff10.pth", |
| 22 | + transforms=partial(ImageNetEval, crop_size=224), |
| 23 | + meta={ |
| 24 | + **_common_meta, |
| 25 | + "recipe": "https://github.com/pytorch/vision/pull/49#issuecomment-277560717", |
| 26 | + "acc@1": 58.092, |
| 27 | + "acc@5": 80.420, |
| 28 | + }, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +class SqueezeNet1_1Weights(Weights): |
| 33 | + ImageNet1K_Community = WeightEntry( |
| 34 | + url="https://download.pytorch.org/models/squeezenet1_1-b8a52dc0.pth", |
| 35 | + transforms=partial(ImageNetEval, crop_size=224), |
| 36 | + meta={ |
| 37 | + **_common_meta, |
| 38 | + "recipe": "https://github.com/pytorch/vision/pull/49#issuecomment-277560717", |
| 39 | + "acc@1": 58.178, |
| 40 | + "acc@5": 80.624, |
| 41 | + }, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def squeezenet1_0(weights: Optional[SqueezeNet1_0Weights] = None, progress: bool = True, **kwargs: Any) -> SqueezeNet: |
| 46 | + if "pretrained" in kwargs: |
| 47 | + warnings.warn("The argument pretrained is deprecated, please use weights instead.") |
| 48 | + weights = SqueezeNet1_0Weights.ImageNet1K_Community if kwargs.pop("pretrained") else None |
| 49 | + weights = SqueezeNet1_0Weights.verify(weights) |
| 50 | + if weights is not None: |
| 51 | + kwargs["num_classes"] = len(weights.meta["categories"]) |
| 52 | + |
| 53 | + model = SqueezeNet("1_0", **kwargs) |
| 54 | + |
| 55 | + if weights is not None: |
| 56 | + model.load_state_dict(weights.state_dict(progress=progress)) |
| 57 | + |
| 58 | + return model |
| 59 | + |
| 60 | + |
| 61 | +def squeezenet1_1(weights: Optional[SqueezeNet1_1Weights] = None, progress: bool = True, **kwargs: Any) -> SqueezeNet: |
| 62 | + if "pretrained" in kwargs: |
| 63 | + warnings.warn("The argument pretrained is deprecated, please use weights instead.") |
| 64 | + weights = SqueezeNet1_1Weights.ImageNet1K_Community if kwargs.pop("pretrained") else None |
| 65 | + weights = SqueezeNet1_1Weights.verify(weights) |
| 66 | + if weights is not None: |
| 67 | + kwargs["num_classes"] = len(weights.meta["categories"]) |
| 68 | + |
| 69 | + model = SqueezeNet("1_1", **kwargs) |
| 70 | + |
| 71 | + if weights is not None: |
| 72 | + model.load_state_dict(weights.state_dict(progress=progress)) |
| 73 | + |
| 74 | + return model |
0 commit comments