Skip to content

Adding multiweight support for squeezenet prototype model #4817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions torchvision/prototype/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .regnet import *
from .resnet import *
from .shufflenetv2 import *
from .squeezenet import *
from .vgg import *
from . import detection
from . import quantization
Expand Down
4 changes: 2 additions & 2 deletions torchvision/prototype/models/googlenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class GoogLeNetWeights(Weights):
ImageNet1K_TheCodezV1 = WeightEntry(
ImageNet1K_Community = WeightEntry(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the enum convention for weights that come from the community and are not part of another library.

url="https://download.pytorch.org/models/googlenet-1378be20.pth",
transforms=partial(ImageNetEval, crop_size=224),
meta={
Expand All @@ -32,7 +32,7 @@ class GoogLeNetWeights(Weights):
def googlenet(weights: Optional[GoogLeNetWeights] = None, progress: bool = True, **kwargs: Any) -> GoogLeNet:
if "pretrained" in kwargs:
warnings.warn("The argument pretrained is deprecated, please use weights instead.")
weights = GoogLeNetWeights.ImageNet1K_TheCodezV1 if kwargs.pop("pretrained") else None
weights = GoogLeNetWeights.ImageNet1K_Community if kwargs.pop("pretrained") else None
weights = GoogLeNetWeights.verify(weights)

original_aux_logits = kwargs.get("aux_logits", False)
Expand Down
8 changes: 4 additions & 4 deletions torchvision/prototype/models/mnasnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


class MNASNet0_5Weights(Weights):
ImageNet1K_TrainerV1 = WeightEntry(
ImageNet1K_Community = WeightEntry(
url="https://download.pytorch.org/models/mnasnet0.5_top1_67.823-3ffadce67e.pth",
transforms=partial(ImageNetEval, crop_size=224),
meta={
Expand All @@ -45,7 +45,7 @@ class MNASNet0_75Weights(Weights):


class MNASNet1_0Weights(Weights):
ImageNet1K_TrainerV1 = WeightEntry(
ImageNet1K_Community = WeightEntry(
url="https://download.pytorch.org/models/mnasnet1.0_top1_73.512-f206786ef8.pth",
transforms=partial(ImageNetEval, crop_size=224),
meta={
Expand Down Expand Up @@ -77,7 +77,7 @@ def _mnasnet(alpha: float, weights: Optional[Weights], progress: bool, **kwargs:
def mnasnet0_5(weights: Optional[MNASNet0_5Weights] = None, progress: bool = True, **kwargs: Any) -> MNASNet:
if "pretrained" in kwargs:
warnings.warn("The argument pretrained is deprecated, please use weights instead.")
weights = MNASNet0_5Weights.ImageNet1K_TrainerV1 if kwargs.pop("pretrained") else None
weights = MNASNet0_5Weights.ImageNet1K_Community if kwargs.pop("pretrained") else None

weights = MNASNet0_5Weights.verify(weights)

Expand All @@ -98,7 +98,7 @@ def mnasnet0_75(weights: Optional[MNASNet0_75Weights] = None, progress: bool = T
def mnasnet1_0(weights: Optional[MNASNet1_0Weights] = None, progress: bool = True, **kwargs: Any) -> MNASNet:
if "pretrained" in kwargs:
warnings.warn("The argument pretrained is deprecated, please use weights instead.")
weights = MNASNet1_0Weights.ImageNet1K_TrainerV1 if kwargs.pop("pretrained") else None
weights = MNASNet1_0Weights.ImageNet1K_Community if kwargs.pop("pretrained") else None
weights = MNASNet1_0Weights.verify(weights)

return _mnasnet(1.0, weights, progress, **kwargs)
Expand Down
74 changes: 74 additions & 0 deletions torchvision/prototype/models/squeezenet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import warnings
from functools import partial
from typing import Any, Optional

from torchvision.transforms.functional import InterpolationMode

from ...models.squeezenet import SqueezeNet
from ..transforms.presets import ImageNetEval
from ._api import Weights, WeightEntry
from ._meta import _IMAGENET_CATEGORIES


__all__ = ["SqueezeNet", "SqueezeNet1_0Weights", "SqueezeNet1_1Weights", "squeezenet1_0", "squeezenet1_1"]


_common_meta = {"size": (224, 224), "categories": _IMAGENET_CATEGORIES, "interpolation": InterpolationMode.BILINEAR}


class SqueezeNet1_0Weights(Weights):
ImageNet1K_Community = WeightEntry(
url="https://download.pytorch.org/models/squeezenet1_0-b66bff10.pth",
transforms=partial(ImageNetEval, crop_size=224),
meta={
**_common_meta,
"recipe": "https://github.com/pytorch/vision/pull/49#issuecomment-277560717",
"acc@1": 58.092,
"acc@5": 80.420,
},
)


class SqueezeNet1_1Weights(Weights):
ImageNet1K_Community = WeightEntry(
url="https://download.pytorch.org/models/squeezenet1_1-b8a52dc0.pth",
transforms=partial(ImageNetEval, crop_size=224),
meta={
**_common_meta,
"recipe": "https://github.com/pytorch/vision/pull/49#issuecomment-277560717",
"acc@1": 58.178,
"acc@5": 80.624,
},
)


def squeezenet1_0(weights: Optional[SqueezeNet1_0Weights] = None, progress: bool = True, **kwargs: Any) -> SqueezeNet:
if "pretrained" in kwargs:
warnings.warn("The argument pretrained is deprecated, please use weights instead.")
weights = SqueezeNet1_0Weights.ImageNet1K_Community if kwargs.pop("pretrained") else None
weights = SqueezeNet1_0Weights.verify(weights)
if weights is not None:
kwargs["num_classes"] = len(weights.meta["categories"])

model = SqueezeNet("1_0", **kwargs)

if weights is not None:
model.load_state_dict(weights.state_dict(progress=progress))

return model


def squeezenet1_1(weights: Optional[SqueezeNet1_1Weights] = None, progress: bool = True, **kwargs: Any) -> SqueezeNet:
if "pretrained" in kwargs:
warnings.warn("The argument pretrained is deprecated, please use weights instead.")
weights = SqueezeNet1_1Weights.ImageNet1K_Community if kwargs.pop("pretrained") else None
weights = SqueezeNet1_1Weights.verify(weights)
if weights is not None:
kwargs["num_classes"] = len(weights.meta["categories"])

model = SqueezeNet("1_1", **kwargs)

if weights is not None:
model.load_state_dict(weights.state_dict(progress=progress))

return model