Torchvision is a PyTorch domain library providing popular datasets, model architectures, and common image and video transformations for computer vision tasks. It is actively maintained and releases are synchronized with PyTorch versions, with the current version 0.26.0 compatible with torch 2.11.0. It aims to simplify the data loading, preprocessing, and model development workflow for computer vision researchers and practitioners.
pip install torchvisionVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `torchvision` to preprocess an image and perform inference with a pre-trained ResNet-18 model. It covers defining transformations with `torchvision.transforms.v2.Compose`, loading a pre-trained model with `torchvision.models`, and obtaining human-readable predictions.
Migrate any video decoding/encoding code to the `TorchCodec` library (github.com/meta-pytorch/torchcodec).
Users on older versions should plan to migrate to `TorchCodec` before upgrading to 0.26.0 or newer.
If clamping is desired, explicitly use the `SanitizeKeyPoints` transform to remove keypoints outside the image area, or refer to `ClampKeyPoints` for precise control.
Prefer `from torchvision.transforms import v2` over `from torchvision import transforms` for new code and consider migrating existing code for improved functionality and performance.
Always install `torch` and `torchvision` with compatible versions, ideally from the same installation command or by consulting the official PyTorch installation matrix for matching versions.
To ensure reproducibility for random transforms, use `torch.manual_seed(seed_value)`.
Check `torchvision`'s official installation instructions and compatibility matrix for supported Python versions and OS distributions. Consider using a stable Python version (e.g., 3.10, 3.11, 3.12) or a more commonly supported OS distribution (e.g., Debian, Ubuntu) if encountering installation issues. Manual compilation from source might be an option but is generally not recommended unless necessary.
Install torchvision using pip or conda: `pip install torchvision` or `conda install torchvision -c pytorch`.
Convert the NumPy array to a PIL Image using `PIL.Image.fromarray(image_np)` before applying the transform.
Verify that the `root` path is correct and accessible, ensure you have an active internet connection if `download=True` is used, and check disk space. You may need to manually download and extract the dataset.
Set `num_workers=0` in the DataLoader to force data loading in the main process, which will reveal the specific error message and traceback for debugging.
Check your network connectivity, proxy settings, and firewall rules. Alternatively, manually download the pre-trained weights from the official PyTorch model zoo and place them in the appropriate cache directory (`~/.cache/torch/hub/checkpoints/`).