perrigoh / image_classifier_pytorch_udacity

Image classifier model using PyTorch
MIT License
0 stars 0 forks source link

torchvision UserWarning: The parameter 'pretrained' is deprecated since 0.13 and will be removed in 0.15, please use 'weights' instead. #8

Closed perrigoh closed 1 year ago

perrigoh commented 1 year ago
info on TorchVision multi weight support API
difference in script in old and new:
old new
from torchvision import models as M from torchvision.prototype import models as PM
1. model = M.resnet50(pretrained=True) 1. weights = PM.ResNet50_Weights.IMAGENET1K_V1
2. model = PM.resnet50(weights=weights)

from torchvision.prototype.models import get_weight

# Weights can be retrieved by name:
assert get_weight("ResNet50_Weights.IMAGENET1K_V1") == ResNet50_Weights.IMAGENET1K_V1
assert get_weight("ResNet50_Weights.IMAGENET1K_V2") == ResNet50_Weights.IMAGENET1K_V2

# Including using the DEFAULT alias:
assert get_weight("ResNet50_Weights.DEFAULT") == ResNet50_Weights.IMAGENET1K_V2

Development installation: Install PyTorch Nightly

conda install pytorch -c pytorch-nightly
# or with pip (see https://pytorch.org/get-started/locally/)
# pip install numpy
# pip install --pre torch -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html

if using colab, info from stackoverflow: You're using the wrong package name, as mentioned on the pytorch website use this:
!pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html -U
Here, -U option is for upgrade (as there is already pytorch stable version installed on colab).

Thank you, PyTorch and its contributors and stackoverflow and its contributors!!!

perrigoh commented 1 year ago
further info on arch name and weights name: arch name weights name
alexnet AlexNet_Weights
vgg13 VGG13_Weights
vgg16 VGG16_Weights

note: all the 3 models only have one weight option IMAGENET1K_V1, IMAGENET1K_V2 not available

perrigoh commented 1 year ago

decided to leave this userwarning as it is, tried to edit the script to:

weights = models.VGG13_Weights.DEFAULT
model = models.__dict__[arch](weights=weights)

train.py no more warning but a new error occurred for predict.py:

During handling of the above exception, another exception occurred: Traceback (most recent call last): File "predict.py", line 98, in predict(in_arg.save_dir, in_arg.topk, in_arg.category_names) File "predict.py", line 44, in predict checkpoint = torch.load(checkpoint) File "/usr/local/lib/python3.7/dist-packages/torch/serialization.py", line 712, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "/usr/local/lib/python3.7/dist-packages/torch/serialization.py", line 1049, in _load result = unpickler.load() File "/usr/lib/python3.7/enum.py", line 315, in call return cls.new(cls, value) File "/usr/lib/python3.7/enum.py", line 569, in new raise exc File "/usr/lib/python3.7/enum.py", line 553, in new result = cls.missing(value) File "/usr/lib/python3.7/enum.py", line 582, in missing raise ValueError("%r is not a valid %s" % (value, cls.name)) ValueError: Weights(url='https://download.pytorch.org/models/vgg13-19584684.pth', transforms=functools.partial(<class 'torchvision.transforms._presets.ImageClassification'>, crop_size=224), meta={'min_size': (32, 32), 'categories': ['tench', 'goldfish', 'great white shark', 'tiger shark',
... 'toilet tissue'], 'recipe': 'https://github.com/pytorch/vision/tree/main/references/classification#alexnet-and-vgg', '_docs': 'These weights were trained from scratch by using a simplified training recipe.', 'num_params': 133047848, '_metrics': {'ImageNet-1K': {'acc@1': 69.928, 'acc@5': 89.246}}}) is not a valid VGG13_Weights

not sure how to fix it, furthermore, for the 3 models there is only one weight option.