Skip to content

Use torchvision weights API in scriptmodule scripts - #133

Open
vishesh9131 wants to merge 1 commit into
prabhuomkar:masterfrom
vishesh9131:torchvision-weights-api
Open

vishesh9131 wants to merge 1 commit into
prabhuomkar:masterfrom
vishesh9131:torchvision-weights-api

Conversation

@vishesh9131

Copy link
Copy Markdown

Description of the change

The three scriptmodule export scripts load pretrained models with pretrained=True, which torchvision has deprecated since 0.13 and warns about on every run:

UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.

This switches them to the weights= API:

  • create_resnet18_scriptmodule.py: ResNet18_Weights.IMAGENET1K_V1
  • create_encoder_cnn_backbone_scriptmodule.py: ResNet50_Weights.IMAGENET1K_V1
  • create_vgg19_layers_scriptmodule.py: VGG19_Weights.IMAGENET1K_V1

IMAGENET1K_V1 is what pretrained=True loaded for all three, so the exported files keep the same weights. For ResNet-50 this matters: ResNet50_Weights.DEFAULT is IMAGENET1K_V2, a different checkpoint, so the weights are pinned explicitly (with a short comment) instead of using DEFAULT.

The weights= API was added in torchvision 0.13, which pairs with PyTorch 1.12, the minimum version the README supports.

Testing

I regenerated each file with the old and the new script (PyTorch 2.10, torchvision 0.25) and compared every tensor with torch.equal:

Script Tensors compared Result
create_encoder_cnn_backbone_scriptmodule.py 319 identical
create_vgg19_layers_scriptmodule.py 32 identical
create_resnet18_scriptmodule.py 80 learned parameters (randomly initialized fc head excluded) identical

For ResNet-18, the 40 BatchNorm running_mean/running_var buffers differ between old and new, but they also differ between two runs of the unchanged script: it traces the model without calling model.eval(), so the forward pass during tracing updates them with the random example input. That is pre-existing and independent of this change; I'm happy to send a separate fix for it.

The new scripts print no deprecation warnings (the old ones print two per run), and the C++ tutorials that load these files (pytorch-basics, neural-style-transfer, image-captioning) run with the regenerated files.

Type Of Change

  • Bug Fix (non-breaking change that fixes an issue)
  • New Feature
  • New PyTorch tutorial
  • Breaking Change (cmake changes, fix or feature that would cause existing functionality to not work as expected)

Related Issues

None; found while running all tutorials locally (no existing issue).

Development & Code Review

  • cpplint rules passes locally (run cmake -P cpplint.cmake)
  • CI is passing
  • Changes have been reviewed by at least one of the maintainers

`pretrained=True` is deprecated since torchvision 0.13. Load the same
IMAGENET1K_V1 checkpoints through `weights=` instead. ResNet-50 is pinned
explicitly because its DEFAULT weights are IMAGENET1K_V2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants