Use torchvision weights API in scriptmodule scripts - #133
Open
vishesh9131 wants to merge 1 commit into
Open
vishesh9131 wants to merge 1 commit into
vishesh9131 wants to merge 1 commit into
Conversation
`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.
prabhuomkar
approved these changes
Sep 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:This switches them to the
weights=API:create_resnet18_scriptmodule.py:ResNet18_Weights.IMAGENET1K_V1create_encoder_cnn_backbone_scriptmodule.py:ResNet50_Weights.IMAGENET1K_V1create_vgg19_layers_scriptmodule.py:VGG19_Weights.IMAGENET1K_V1IMAGENET1K_V1is whatpretrained=Trueloaded for all three, so the exported files keep the same weights. For ResNet-50 this matters:ResNet50_Weights.DEFAULTisIMAGENET1K_V2, a different checkpoint, so the weights are pinned explicitly (with a short comment) instead of usingDEFAULT.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:create_encoder_cnn_backbone_scriptmodule.pycreate_vgg19_layers_scriptmodule.pycreate_resnet18_scriptmodule.pyfchead excluded)For ResNet-18, the 40 BatchNorm
running_mean/running_varbuffers differ between old and new, but they also differ between two runs of the unchanged script: it traces the model without callingmodel.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
Related Issues
Development & Code Review
cmake -P cpplint.cmake)