PyTorch Hub: Discover and Use Pre-trained PyTorch Models

Introduction

PyTorch Hub is a central repository designed to facilitate the discovery and usage of pre-trained machine learning models within the PyTorch ecosystem. Launched by the PyTorch team, its core mission is to accelerate research and development by making a wide array of cutting-edge, pre-trained models easily accessible to researchers, developers, and data scientists. Instead of requiring users to manually download model weights and code, PyTorch Hub provides a simple API, torch.hub.load(), to load models directly from GitHub repositories with minimal boilerplate.

The platform hosts a curated selection of models across various domains like computer vision, natural language processing (NLP), audio processing, and generative AI. By promoting reproducibility and ease of use, PyTorch Hub serves as a valuable resource for transfer learning, benchmarking, educational purposes, and integrating AI capabilities into applications.

Key Features

PyTorch Hub offers several features to streamline the use of pre-trained models:

  • Discovery of Pre-trained Models: A catalog of published models, searchable and categorized by task (e.g., Audio, Generative, NLP, Scriptable, Vision).
  • Simple Loading API (torch.hub.load()): The cornerstone of PyTorch Hub, this function allows users to load pre-trained models (including model architecture and weights) directly from a specified GitHub repository and entry point with a single line of Python code.
  • GitHub-Based Model Repositories: Models listed on PyTorch Hub are hosted in public GitHub repositories. Each repository must contain a hubconf.py file that defines the available models and their entry points (callable functions that create and return the model instance).
  • Reproducibility: Aims to make it easier to load and use models exactly as published by the original authors, facilitating research reproducibility.
  • Variety of Models: Features a diverse range of models, including well-known architectures and new research contributions. Examples include:
    • Vision: ResNet, AlexNet, VGG, Inception, DenseNet, MobileNet, Vision Transformer (ViT), YOLO (via ultralytics repo), SSD, Mask R-CNN.
    • NLP: BERT, RoBERTa, XLM-R, Transformer models for translation, summarization, text generation (often from Hugging Face or fairseq repositories).
    • Audio: Tacotron2 (text-to-speech), Wav2Vec2 (speech recognition), and other audio processing models.
    • Generative: GANs, VAEs, and other generative models.
  • Model Documentation & Examples: Each model entry on the PyTorch Hub website typically links to:
    • The source GitHub repository.
    • The original research paper (if applicable).
    • A brief description and usage examples.
  • torch.hub.list(): A function to list available models or entry points within a specified GitHub repository on the Hub.
  • torch.hub.help(): Provides the docstring and help for a specific model entry point in a Hub repository.
  • Caching Mechanism: Downloaded models and dependencies are cached locally, speeding up subsequent loads.
  • force_reload Option: Allows users to bypass the cache and force a fresh download of the model and its dependencies.
  • skip_validation and trust_repo Options: Provide controls for advanced users regarding repository validation and trusting non-official repositories (use with caution).

Specific Use Cases

PyTorch Hub is a valuable resource for various machine learning tasks and workflows:

  • Transfer Learning: Easily load pre-trained models (e.g., an ImageNet-trained ResNet) and fine-tune them on custom datasets for specific tasks, leveraging the learned features.
  • Benchmarking: Quickly access and evaluate the performance of different pre-trained models on common datasets or custom tasks.
  • Rapid Prototyping: Integrate state-of-the-art AI capabilities into applications with minimal setup, allowing for quick experimentation.
  • Educational Purposes: Learn about different model architectures and see practical examples of how to load and use them in PyTorch.
  • Research Exploration: Discover and experiment with models published by the research community, facilitating the exploration of new ideas and techniques.
  • Inference with Pre-trained Models: Use models directly for inference on new data for tasks like image classification, object detection, text generation, or speech synthesis without needing to train them from scratch.
  • Simplifying Model Sharing: Provides a standardized way for researchers and developers to publish and share their pre-trained PyTorch models.

Usage Guide

Using models from PyTorch Hub is designed to be straightforward:

  1. Ensure PyTorch is Installed: PyTorch Hub is part of the core PyTorch library. Make sure you have PyTorch installed in your Python environment.

    pip install torch torchvision torchaudio
    
  2. Browse or Find a Model:

    • Visit the PyTorch Hub website: https://pytorch.org/hub/
    • Browse models by category (Vision, NLP, Audio, etc.) or use the search functionality.
    • Each model page provides information about the model, its source repository, and usage examples.
  3. Loading a Model using torch.hub.load(): The primary way to load a model is with the torch.hub.load() function. The basic syntax is:

    import torch
    
    model = torch.hub.load('repository_owner/repository_name[:branch_or_tag]', 'model_entrypoint', arg1, arg2, ..., pretrained=True, **kwargs)
    
    • 'repository_owner/repository_name[:branch_or_tag]': The GitHub repository path (e.g., 'pytorch/vision', 'ultralytics/yolov5'). An optional branch or tag can be specified.
    • 'model_entrypoint': The name of the function defined in the repository's hubconf.py file that creates and returns the model (e.g., 'resnet18', 'yolov5s').
    • *args, **kwargs: Any arguments required by the model's entry point function (e.g., pretrained=True is common to load pre-trained weights).
    • source='github': This is the default and usually not needed to be specified explicitly. It can also be 'local' to load from a local directory.
    • force_reload=False (optional): Set to True to discard the existing cache and force a fresh download of the model and dependencies.
    • skip_validation=False (optional): Set to True to skip GitHub API validation for non-default branches/tags (use with caution from untrusted repos).
    • trust_repo=None (optional): Can be set to True or False to explicitly trust or distrust a repository. If None, a prompt may appear for non-official repositories.

    Example (loading a pre-trained ResNet18 model from pytorch/vision):

    import torch
    model = torch.hub.load('pytorch/vision:v0.13.0', 'resnet18', pretrained=True)
    model.eval() # Set the model to evaluation mode
    
  4. Performing Inference: Once the model is loaded, you can use it for inference as you would with any other PyTorch model:

    # Example using the loaded ResNet18 model
    # Assuming you have an image preprocessed as `input_tensor`
    # (Preprocessing steps like resizing, normalization, etc., are model-specific
    # and usually detailed in the model's documentation or Hub page examples)
    #
    # import torchvision.transforms as T
    # from PIL import Image
    #
    # # Example preprocessing (may vary)
    # preprocess = T.Compose([
    #     T.Resize(256),
    #     T.CenterCrop(224),
    #     T.ToTensor(),
    * T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    # ])
    # img = Image.open("path/to/your/image.jpg")
    # input_tensor = preprocess(img)
    # input_batch = input_tensor.unsqueeze(0) # Create a mini-batch as expected by the model
    
    # # Move the input and model to GPU for speed if available
    # if torch.cuda.is_available():
    #     input_batch = input_batch.to('cuda')
    #     model.to('cuda')
    
    with torch.no_grad():
        output = model(input_batch)
    
    # Post-process the output (e.g., get class probabilities, apply softmax)
    # probabilities = torch.nn.functional.softmax(output[0], dim=0)
    # print(probabilities)
    
  5. Preparing for Fine-Tuning: For transfer learning, you typically load a pre-trained model, replace its final classification layer (or other relevant layers) to suit your new task, and then train it on your custom dataset. The exact steps depend on the model architecture and your specific task.

    # Example: Modifying ResNet18 for a new number of classes
    num_ftrs = model.fc.in_features
    model.fc = torch.nn.Linear(num_ftrs, NUM_NEW_CLASSES)
    # Now, model can be trained on your new dataset
    
  6. Exploring Models in a Repository:

    • torch.hub.list(github, force_reload=False): Lists available entry points in a hubconf.py.
      torch.hub.list('pytorch/vision')
      
    • torch.hub.help(github, model, force_reload=False): Shows the docstring for a specific model entry point.
      torch.hub.help('pytorch/vision', 'resnet18')
      

Pricing & Plans

PyTorch Hub itself is a free resource provided as part of the open-source PyTorch project. The models listed on PyTorch Hub are also generally open-source and available for free use, subject to the specific licenses provided by the model contributors in their respective GitHub repositories. There are no subscription fees or charges imposed by PyTorch Hub for accessing or using these models.

Users are responsible for any computational costs incurred if they run or train these models on cloud platforms or their own hardware.

Model Publishing & hubconf.py

Researchers and developers can make their pre-trained PyTorch models available through PyTorch Hub by:

  1. Hosting their model code and weights in a public GitHub repository.
  2. Creating a hubconf.py file in the root of their repository. This file defines one or more entry point functions. Each entry point is a Python callable that:
    • Takes necessary arguments (e.g., pretrained=False, num_classes=1000).
    • Loads the model architecture.
    • Optionally loads pre-trained weights (often from a URL specified within the function or a relative path within the repo).
    • Returns the instantiated PyTorch model (torch.nn.Module).
  3. Optionally, include a dependencies = ['torch', 'torchvision'] list in hubconf.py if your model has specific library dependencies beyond PyTorch itself (though the goal is often minimal dependencies for Hub models).

While torch.hub.list() primarily shows models from repositories explicitly indexed by PyTorch (e.g., in pytorch/hub), torch.hub.load() can load models from any public GitHub repository that has a valid hubconf.py, promoting broader sharing.

Commercial Use & Licensing

  • PyTorch Hub Platform: Free to use.
  • Models on PyTorch Hub: Each model is provided by its respective owner/contributor and is subject to its own license (e.g., MIT, Apache 2.0, BSD). Users must check the license associated with each specific model in its source GitHub repository before using it, especially for commercial purposes. Many popular models are permissively licensed, but it's the user's responsibility to verify.

Frequently Asked Questions (FAQ)

Q1: What is PyTorch Hub? A1: PyTorch Hub is a semi-centralized repository of pre-trained PyTorch models. It provides a simple API (torch.hub.load()) to discover and use these models from GitHub with minimal code, facilitating research reproducibility and easy access to state-of-the-art models.

Q2: How does torch.hub.load() work? A2: It downloads (and caches) a model's definition and pre-trained weights from a specified public GitHub repository (which must contain a hubconf.py file defining model entry points). It then instantiates and returns the PyTorch model ready for use.

Q3: Is PyTorch Hub free? A3: Yes, PyTorch Hub is a free component of the PyTorch open-source project. The models available through it are generally also open-source and free to use, but you must check the specific license of each model you download.

Q4: How are models added to PyTorch Hub? A4: Model authors can make their models discoverable via PyTorch Hub by ensuring their public GitHub repository contains a hubconf.py file that defines the model entry points. While some models are prominently featured on the PyTorch Hub website, torch.hub.load() can fetch from any compliant GitHub repository.

Q5: Can I use PyTorch Hub models for commercial projects? A5: This depends entirely on the license of the specific model you intend to use. PyTorch Hub itself doesn't impose licensing restrictions, but each model contributor specifies their own license (e.g., MIT, Apache 2.0). Always check the model's source repository for its license before commercial use.

Q6: What kind of models are available on PyTorch Hub? A6: A wide variety, including models for image classification (ResNet, VGG), object detection (YOLO, SSD), segmentation, natural language processing (BERT, Transformers for translation/summarization), speech synthesis (Tacotron2), speech recognition (Wav2Vec2), and generative models.

Q7: How does PyTorch Hub ensure reproducibility? A7: By allowing users to load specific versions of models (via Git tags or branches in the repo_or_dir argument) and their pre-trained weights directly from source repositories, PyTorch Hub helps in creating reproducible research workflows.

Q8: What is hubconf.py? A8: hubconf.py is a Python file that must be present in the root of a GitHub repository for its models to be loadable via torch.hub.load(). It defines entry point functions that create and return model instances, and can also list dependencies.

Responsible AI & Model Limitations

  • Model Cards: While PyTorch Hub itself doesn't mandate a strict format, individual model repositories are encouraged to provide comprehensive documentation, ideally in the form of model cards. These should detail the model's architecture, training data, intended use cases, limitations, performance metrics, and any known biases or ethical considerations.
  • User Responsibility: Users of models from PyTorch Hub are responsible for understanding the capabilities and limitations of each model and for using them responsibly and ethically, in accordance with the model's license and any accompanying documentation.
  • Bias & Fairness: Pre-trained models can inherit biases from the data they were trained on. Users should be aware of this and take appropriate steps to evaluate and mitigate potential biases if using these models in sensitive applications.

Last updated: May 26, 2025

Found an error in our documentation?Email us for assistance