Skip to content

Update trainingyt.py: Migrate to transforms.v2, f-strings, and modern syntax#3871

Closed
Sai-prashanth-2006 wants to merge 6 commits intopytorch:mainfrom
Sai-prashanth-2006:fix-trainingyt-deprecation
Closed

Update trainingyt.py: Migrate to transforms.v2, f-strings, and modern syntax#3871
Sai-prashanth-2006 wants to merge 6 commits intopytorch:mainfrom
Sai-prashanth-2006:fix-trainingyt-deprecation

Conversation

@Sai-prashanth-2006
Copy link
Copy Markdown

@Sai-prashanth-2006 Sai-prashanth-2006 commented May 6, 2026

Fixes #3858

Description

This PR modernizes trainingyt.py by resolving deprecated API usage and aligning the codebase with current Python and PyTorch best practices:

  • Migrated the torchvision.transforms pipeline to the v2 API, replacing the deprecated ToTensor transform
  • Updated GarmentClassifier.__init__ to use the Python 3 super().__init__() syntax
  • Replaced all .format() string literals with f-strings for improved readability and performance

No functional behavior is changed by this PR.

Checklist

  • The issue being fixed is referenced in the description
  • This PR addresses a single issue
  • Labels from the original issue have been added to this PR
  • No unrelated changes are included

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 6, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3871

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented May 6, 2026

Hi @Sai-prashanth-2006!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented May 6, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@sekyondaMeta
Copy link
Copy Markdown
Contributor

@claude Review this PR and compare it to the original issue #3858

@claude
Copy link
Copy Markdown

claude Bot commented May 6, 2026

Claude finished @sekyondaMeta's task in 3m 39s —— View job


Review: PR #3871 vs Issue #3858


Summary

This PR aims to fix issue #3858 (deprecation of transforms.ToTensor()) by migrating to transforms v2, converting .format() to f-strings, and modernizing super() calls. The intent aligns with the issue, but there is a critical bug in the import that will break the tutorial at runtime.


Critical Issue: Incorrect Import

File: beginner_source/introyt/trainingyt.py, line 68

# What the PR does (WRONG):
import torchvision.transforms as v2

# What it should be:
from torchvision.transforms import v2

The PR simply renames the alias of the old torchvision.transforms module to v2. However, ToImage and ToDtype only exist in torchvision.transforms.v2, not in torchvision.transforms. This means v2.ToImage() and v2.ToDtype(...) on lines 76-77 will raise AttributeError at runtime.

The correct pattern (already used in this repo at beginner_source/serving_tutorial.py) is:

from torchvision.transforms import v2

Fix this →


Minor Issue: Pointless f-string

File: beginner_source/introyt/trainingyt.py, line 122

# PR changes this:
print('  '.join(classes[labels[j]] for j in range(4)))

# To this:
print('  '.join(f'{classes[labels[j]]}' for j in range(4)))

Wrapping classes[labels[j]] in an f-string (f'{classes[labels[j]]}') adds no value — it's equivalent to str(classes[labels[j]]), and since classes already contains strings, this is a no-op. The original code was correct. This change should be reverted.


What's Correct

  • super().__init__() (line 139): Good modernization, this is standard Python 3 style.
  • All other f-string conversions (lines 94-95, 181, 256, 281, 289, 310, 322): Correct and improve readability.
  • v2.ToImage() + v2.ToDtype(torch.float32, scale=True) replacing ToTensor(): This is the correct v2 equivalent — once the import is fixed.

Comparison to Issue #3858

Based on the PR description, issue #3858 requests fixing the deprecated transforms.ToTensor() usage. The PR addresses this correctly in concept (replacing ToTensor with ToImage + ToDtype), but the broken import means it won't actually work. The additional f-string and super() changes are reasonable modernization but go beyond the strict scope of the deprecation fix.


Verdict

Needs changes before merge:

  1. Must fix: Change import torchvision.transforms as v2from torchvision.transforms import v2
  2. Should fix: Revert the pointless f-string on line 122 back to classes[labels[j]]

@Sai-prashanth-2006
Copy link
Copy Markdown
Author

thanks for catching that, import fixed and f-string reverted

@Sai-prashanth-2006 Sai-prashanth-2006 marked this pull request as draft May 6, 2026 19:04
@Sai-prashanth-2006 Sai-prashanth-2006 marked this pull request as ready for review May 6, 2026 19:06
@sekyondaMeta
Copy link
Copy Markdown
Contributor

@claude

@claude
Copy link
Copy Markdown

claude Bot commented May 7, 2026

Claude finished @sekyondaMeta's task in 2m 28s —— View job


Re-review of PR #3871

  • Gather context and check current state of the PR diff
  • Verify the import fix was applied correctly
  • Verify the pointless f-string was reverted
  • Check for any other issues
  • Provide final review feedback

Finding: This PR is now redundant

After investigating, I found that this PR has no diff against origin/main. All changes proposed here have already been merged into main via PR #3861 ("Modernize transforms tutorial to torchvision v2 API"), which included a full rewrite of beginner_source/introyt/trainingyt.py with the same v2 migration, f-string conversions, and super() modernization.

Verification:

  • git diff origin/main...HEAD produces empty output
  • The file on origin/main already has from torchvision.transforms import v2, v2.ToImage(), v2.ToDtype(...), f-strings, and super().__init__()

Regarding the previously reported issues

Both issues from the earlier review have been addressed in the PR author's follow-up commit (19254af):

  1. Import fix — correctly changed to from torchvision.transforms import v2
  2. Pointless f-string reverted — line 122 is back to classes[labels[j]]

Recommendation

This PR should be closed as the changes have already landed in main through #3861. There's nothing left to merge.


@sekyondaMeta
Copy link
Copy Markdown
Contributor

Looks like PR #3866 covered this work. Closing this but we will still give you credit for the work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update Intro YouTube Training with PyTorch tutorial — v2 transforms, super(), formatting

2 participants