Skip to content

Expose on Hub the public methods of the registration API #6364

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 4 commits into from
Aug 8, 2022
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
9 changes: 9 additions & 0 deletions docs/source/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ Most pre-trained models can be accessed directly via PyTorch Hub without having
weights = torch.hub.load("pytorch/vision", "get_weight", weights="ResNet50_Weights.IMAGENET1K_V2")
model = torch.hub.load("pytorch/vision", "resnet50", weights=weights)

You can also retrieve all the available weights of a specific model via PyTorch Hub by doing:

.. code:: python

import torch

weight_enum = torch.hub.load("pytorch/vision", "get_model_weights", name="resnet50")
print([weight for weight in weight_enum])

The only exception to the above are the detection models included on
:mod:`torchvision.models.detection`. These models require TorchVision
to be installed because they depend on custom C++ operators.
Expand Down
2 changes: 1 addition & 1 deletion hubconf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Optional list of dependencies required by the package
dependencies = ["torch"]

from torchvision.models import get_weight
from torchvision.models import get_model_weights, get_weight
from torchvision.models.alexnet import alexnet
from torchvision.models.convnext import convnext_base, convnext_large, convnext_small, convnext_tiny
from torchvision.models.densenet import densenet121, densenet161, densenet169, densenet201
Expand Down
5 changes: 2 additions & 3 deletions torchvision/models/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_weight(name: str) -> WeightsEnum:
W = TypeVar("W", bound=WeightsEnum)


def get_model_weights(model: Union[Callable, str]) -> W:
def get_model_weights(name: Union[Callable, str]) -> W:
Copy link
Member

Choose a reason for hiding this comment

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

Curious what the reason is to change this? name is less accurate to describe this parameter IMO, since it can also be a callable / model builder. For example

from torchvision.models import resnet50
weights = get_model_weights(name=resnet50)

looks unnatural.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The load() method already a has a model parameter. Keeping it as model leads to TypeError:

TypeError: load() got multiple values for argument 'model'

Given that the specific parameter can be either string or callable, I think it's OK. If you have a better name, let me know.

"""
Retuns the weights enum class associated to the given model.

Expand All @@ -127,8 +127,7 @@ def get_model_weights(model: Union[Callable, str]) -> W:
Returns:
weights_enum (W): The weights enum class associated with the model.
"""
if isinstance(model, str):
model = find_model(model)
model = find_model(name) if isinstance(name, str) else name
return cast(W, _get_enum_from_fn(model))


Expand Down