- 
                Notifications
    You must be signed in to change notification settings 
- Fork 7.2k
Skeleton for keypoints tutorial #9209
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
      
      
            AntoineSimoulin
  merged 8 commits into
  pytorch:main
from
AntoineSimoulin:keypoints-tutorial
  
      
      
   
  Sep 10, 2025 
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a07475e
              
                Skeleton for keypoints tutorial
              
              
                AntoineSimoulin 3ab6f75
              
                Merge branch 'main' into keypoints-tutorial
              
              
                AntoineSimoulin 1a46d07
              
                Update gallery/transforms/plot_keypoints_transforms.py
              
              
                AntoineSimoulin b812a99
              
                remove mention to bounding boxes
              
              
                AntoineSimoulin 6e7b15f
              
                remove box parameters
              
              
                AntoineSimoulin 84cdfb3
              
                lint
              
              
                AntoineSimoulin e43c617
              
                add reference to image
              
              
                AntoineSimoulin 28605ad
              
                lint
              
              
                AntoineSimoulin File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              
      
      Loading
      
  Sorry, something went wrong. Reload?
      Sorry, we cannot display this file.
      Sorry, this file is invalid so it cannot be displayed.
      
    
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """ | ||
| =============================================================== | ||
| Transforms on Rotated Bounding Boxes | ||
| =============================================================== | ||
|  | ||
| This example illustrates how to define and use rotated bounding boxes. | ||
|  | ||
| .. note:: | ||
| Support for rotated bounding boxes was released in TorchVision 0.23 and is | ||
| currently a BETA feature. We don't expect the API to change, but there may | ||
| be some rare edge-cases. If you find any issues, please report them on | ||
| our bug tracker: https://github.com/pytorch/vision/issues?q=is:open+is:issue | ||
|  | ||
| First, a bit of setup code: | ||
| """ | ||
|  | ||
| # %% | ||
| from PIL import Image | ||
| from pathlib import Path | ||
| import matplotlib.pyplot as plt | ||
|  | ||
|  | ||
| import torch | ||
| from torchvision.tv_tensors import KeyPoints | ||
| from torchvision.transforms import v2 | ||
| from helpers import plot | ||
|  | ||
| plt.rcParams["figure.figsize"] = [10, 5] | ||
| plt.rcParams["savefig.bbox"] = "tight" | ||
|  | ||
| # if you change the seed, make sure that the randomly-applied transforms | ||
| # properly show that the image can be both transformed and *not* transformed! | ||
|         
                  AntoineSimoulin marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| torch.manual_seed(0) | ||
|  | ||
| # If you're trying to run that on Colab, you can download the assets and the | ||
| # helpers from https://github.com/pytorch/vision/tree/main/gallery/ | ||
| orig_img = Image.open(Path('../assets') / 'pottery.jpg') | ||
|  | ||
| # %% | ||
| # Creating KeyPoints | ||
| # ------------------------------- | ||
| # Key points are created by instantiating the | ||
| # :class:`~torchvision.tv_tensors.KeyPoints` class. | ||
|  | ||
|  | ||
| orig_pts = KeyPoints( | ||
| [ | ||
| [ | ||
| [445, 700], # nose | ||
| [320, 660], | ||
| [370, 660], | ||
| [420, 660], # left eye | ||
| [300, 620], | ||
| [420, 620], # left eyebrow | ||
| [475, 665], | ||
| [515, 665], | ||
| [555, 655], # right eye | ||
| [460, 625], | ||
| [560, 600], # right eyebrow | ||
| [370, 780], | ||
| [450, 760], | ||
| [540, 780], | ||
| [450, 820], # mouth | ||
| ], | ||
| ], | ||
| canvas_size=(orig_img.size[1], orig_img.size[0]), | ||
| ) | ||
|  | ||
| plot([(orig_img, orig_pts)]) | ||
|  | ||
| # %% | ||
| # Transforms illustrations | ||
| # ------------------------ | ||
| # | ||
| # Using :class:`~torchvision.transforms.RandomRotation`: | ||
| rotater = v2.RandomRotation(degrees=(0, 180), expand=True) | ||
| rotated_imgs = [rotater((orig_img, orig_pts)) for _ in range(4)] | ||
| plot([(orig_img, orig_pts)] + rotated_imgs, bbox_width=10) | ||
|  | ||
| # %% | ||
| # Using :class:`~torchvision.transforms.Pad`: | ||
| padded_imgs_and_points = [ | ||
| v2.Pad(padding=padding)(orig_img, orig_pts) | ||
| for padding in (30, 50, 100, 200) | ||
| ] | ||
| plot([(orig_img, orig_pts)] + padded_imgs_and_points, bbox_width=10) | ||
|  | ||
| # %% | ||
| # Using :class:`~torchvision.transforms.Resize`: | ||
| resized_imgs = [ | ||
| v2.Resize(size=size)(orig_img, orig_pts) | ||
| for size in (300, 500, 1000, orig_img.size) | ||
| ] | ||
| plot([(orig_img, orig_pts)] + resized_imgs, bbox_width=5) | ||
|  | ||
| # %% | ||
| # Using :class:`~torchvision.transforms.RandomPerspective`: | ||
| perspective_transformer = v2.RandomPerspective(distortion_scale=0.6, p=1.0) | ||
| perspective_imgs = [perspective_transformer(orig_img, orig_pts) for _ in range(4)] | ||
| plot([(orig_img, orig_pts)] + perspective_imgs, bbox_width=10) | ||
|  | ||
| # %% | ||
| # Using :class:`~torchvision.transforms.CenterCrop`: | ||
| center_crops_and_points = [ | ||
| v2.CenterCrop(size=size)(orig_img, orig_pts) | ||
| for size in (300, 500, 1000, orig_img.size) | ||
| ] | ||
| plot([(orig_img, orig_pts)] + center_crops_and_points) | ||
|  | ||
| # %% | ||
| # Using :class:`~torchvision.transforms.RandomRotation`: | ||
| rotater = v2.RandomRotation(degrees=(0, 180)) | ||
| rotated_imgs = [rotater((orig_img, orig_pts)) for _ in range(4)] | ||
| plot([(orig_img, orig_pts)] + rotated_imgs) | ||
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.