Models and pre-trained weights — Torchvision 0.16 documentation (original) (raw)
The torchvision.models
subpackage contains definitions of models for addressing different tasks, including: image classification, pixelwise semantic segmentation, object detection, instance segmentation, person keypoint detection, video classification, and optical flow.
General information on pre-trained weights¶
TorchVision offers pre-trained weights for every provided architecture, using the PyTorch torch.hub. Instancing a pre-trained model will download its weights to a cache directory. This directory can be set using the TORCH_HOMEenvironment variable. See torch.hub.load_state_dict_from_url() for details.
Note
The pre-trained models provided in this library may have their own licenses or terms and conditions derived from the dataset used for training. It is your responsibility to determine whether you have permission to use the models for your use case.
Note
Backward compatibility is guaranteed for loading a serializedstate_dict
to the model created using old PyTorch version. On the contrary, loading entire saved models or serializedScriptModules
(serialized using older versions of PyTorch) may not preserve the historic behaviour. Refer to the followingdocumentation
Initializing pre-trained models¶
As of v0.13, TorchVision offers a new Multi-weight support APIfor loading different weights to the existing model builder methods:
from torchvision.models import resnet50, ResNet50_Weights
Old weights with accuracy 76.130%
resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)
New weights with accuracy 80.858%
resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
Best available weights (currently alias for IMAGENET1K_V2)
Note that these weights may change across versions
resnet50(weights=ResNet50_Weights.DEFAULT)
Strings are also supported
resnet50(weights="IMAGENET1K_V2")
No weights - random initialization
resnet50(weights=None)
Migrating to the new API is very straightforward. The following method calls between the 2 APIs are all equivalent:
from torchvision.models import resnet50, ResNet50_Weights
Using pretrained weights:
resnet50(weights=ResNet50_Weights.IMAGENET1K_V1) resnet50(weights="IMAGENET1K_V1") resnet50(pretrained=True) # deprecated resnet50(True) # deprecated
Using no weights:
resnet50(weights=None) resnet50() resnet50(pretrained=False) # deprecated resnet50(False) # deprecated
Note that the pretrained
parameter is now deprecated, using it will emit warnings and will be removed on v0.15.
Using the pre-trained models¶
Before using the pre-trained models, one must preprocess the image (resize with right resolution/interpolation, apply inference transforms, rescale the values etc). There is no standard way to do this as it depends on how a given model was trained. It can vary across model families, variants or even weight versions. Using the correct preprocessing method is critical and failing to do so may lead to decreased accuracy or incorrect outputs.
All the necessary information for the inference transforms of each pre-trained model is provided on its weights documentation. To simplify inference, TorchVision bundles the necessary preprocessing transforms into each model weight. These are accessible via the weight.transforms
attribute:
Initialize the Weight Transforms
weights = ResNet50_Weights.DEFAULT preprocess = weights.transforms()
Apply it to the input image
img_transformed = preprocess(img)
Some models use modules which have different training and evaluation behavior, such as batch normalization. To switch between these modes, usemodel.train()
or model.eval()
as appropriate. Seetrain() or eval() for details.
Initialize model
weights = ResNet50_Weights.DEFAULT model = resnet50(weights=weights)
Set model to eval mode
model.eval()
Listing and retrieving available models¶
As of v0.14, TorchVision offers a new mechanism which allows listing and retrieving models and weights by their names. Here are a few examples on how to use them:
List available models
all_models = list_models() classification_models = list_models(module=torchvision.models)
Initialize models
m1 = get_model("mobilenet_v3_large", weights=None) m2 = get_model("quantized_mobilenet_v3_large", weights="DEFAULT")
Fetch weights
weights = get_weight("MobileNet_V3_Large_QuantizedWeights.DEFAULT") assert weights == MobileNet_V3_Large_QuantizedWeights.DEFAULT
weights_enum = get_model_weights("quantized_mobilenet_v3_large") assert weights_enum == MobileNet_V3_Large_QuantizedWeights
weights_enum2 = get_model_weights(torchvision.models.quantization.mobilenet_v3_large) assert weights_enum == weights_enum2
Here are the available public functions to retrieve models and their corresponding weights:
get_model(name, **config) | Gets the model name and configuration and returns an instantiated model. |
---|---|
get_model_weights(name) | Returns the weights enum class associated to the given model. |
get_weight(name) | Gets the weights enum value by its full name. |
list_models([module, include, exclude]) | Returns a list with the names of registered models. |
Using models from Hub¶
Most pre-trained models can be accessed directly via PyTorch Hub without having TorchVision installed:
import torch
Option 1: passing weights param as string
model = torch.hub.load("pytorch/vision", "resnet50", weights="IMAGENET1K_V2")
Option 2: passing weights param as enum
weights = torch.hub.load("pytorch/vision", "get_weight", weights="ResNet50_Weights.IMAGENET1K_V2") model = torch.hub.load("pytorch/vision", "resnet50", weights=weights)
You can also retrieve all the available weights of a specific model via PyTorch Hub by doing:
import torch
weight_enum = torch.hub.load("pytorch/vision", "get_model_weights", name="resnet50") print([weight for weight in weight_enum])
The only exception to the above are the detection models included ontorchvision.models.detection
. These models require TorchVision to be installed because they depend on custom C++ operators.
Classification¶
The following classification models are available, with or without pre-trained weights:
- AlexNet
- ConvNeXt
- DenseNet
- EfficientNet
- EfficientNetV2
- GoogLeNet
- Inception V3
- MaxVit
- MNASNet
- MobileNet V2
- MobileNet V3
- RegNet
- ResNet
- ResNeXt
- ShuffleNet V2
- SqueezeNet
- SwinTransformer
- VGG
- VisionTransformer
- Wide ResNet
Here is an example of how to use the pre-trained image classification models:
from torchvision.io import read_image from torchvision.models import resnet50, ResNet50_Weights
img = read_image("test/assets/encode_jpeg/grace_hopper_517x606.jpg")
Step 1: Initialize model with the best available weights
weights = ResNet50_Weights.DEFAULT model = resnet50(weights=weights) model.eval()
Step 2: Initialize the inference transforms
preprocess = weights.transforms()
Step 3: Apply inference preprocessing transforms
batch = preprocess(img).unsqueeze(0)
Step 4: Use the model and print the predicted category
prediction = model(batch).squeeze(0).softmax(0) class_id = prediction.argmax().item() score = prediction[class_id].item() category_name = weights.meta["categories"][class_id] print(f"{category_name}: {100 * score:.1f}%")
The classes of the pre-trained model outputs can be found at weights.meta["categories"]
.
Table of all available classification weights¶
Accuracies are reported on ImageNet-1K using single crops:
Quantized models¶
The following architectures provide support for INT8 quantized models, with or without pre-trained weights:
- Quantized GoogLeNet
- Quantized InceptionV3
- Quantized MobileNet V2
- Quantized MobileNet V3
- Quantized ResNet
- Quantized ResNeXt
- Quantized ShuffleNet V2
Here is an example of how to use the pre-trained quantized image classification models:
from torchvision.io import read_image from torchvision.models.quantization import resnet50, ResNet50_QuantizedWeights
img = read_image("test/assets/encode_jpeg/grace_hopper_517x606.jpg")
Step 1: Initialize model with the best available weights
weights = ResNet50_QuantizedWeights.DEFAULT model = resnet50(weights=weights, quantize=True) model.eval()
Step 2: Initialize the inference transforms
preprocess = weights.transforms()
Step 3: Apply inference preprocessing transforms
batch = preprocess(img).unsqueeze(0)
Step 4: Use the model and print the predicted category
prediction = model(batch).squeeze(0).softmax(0) class_id = prediction.argmax().item() score = prediction[class_id].item() category_name = weights.meta["categories"][class_id] print(f"{category_name}: {100 * score}%")
The classes of the pre-trained model outputs can be found at weights.meta["categories"]
.
Table of all available quantized classification weights¶
Accuracies are reported on ImageNet-1K using single crops:
Semantic Segmentation¶
Warning
The segmentation module is in Beta stage, and backward compatibility is not guaranteed.
The following semantic segmentation models are available, with or without pre-trained weights:
Here is an example of how to use the pre-trained semantic segmentation models:
from torchvision.io.image import read_image from torchvision.models.segmentation import fcn_resnet50, FCN_ResNet50_Weights from torchvision.transforms.functional import to_pil_image
img = read_image("gallery/assets/dog1.jpg")
Step 1: Initialize model with the best available weights
weights = FCN_ResNet50_Weights.DEFAULT model = fcn_resnet50(weights=weights) model.eval()
Step 2: Initialize the inference transforms
preprocess = weights.transforms()
Step 3: Apply inference preprocessing transforms
batch = preprocess(img).unsqueeze(0)
Step 4: Use the model and visualize the prediction
prediction = model(batch)["out"] normalized_masks = prediction.softmax(dim=1) class_to_idx = {cls: idx for (idx, cls) in enumerate(weights.meta["categories"])} mask = normalized_masks[0, class_to_idx["dog"]] to_pil_image(mask).show()
The classes of the pre-trained model outputs can be found at weights.meta["categories"]
. The output format of the models is illustrated in Semantic segmentation models.
Table of all available semantic segmentation weights¶
All models are evaluated a subset of COCO val2017, on the 20 categories that are present in the Pascal VOC dataset:
Weight | Mean IoU | pixelwise Acc | Params | GFLOPS | Recipe |
---|---|---|---|---|---|
DeepLabV3_MobileNet_V3_Large_Weights.COCO_WITH_VOC_LABELS_V1 | 60.3 | 91.2 | 11.0M | 10.45 | link |
DeepLabV3_ResNet101_Weights.COCO_WITH_VOC_LABELS_V1 | 67.4 | 92.4 | 61.0M | 258.74 | link |
DeepLabV3_ResNet50_Weights.COCO_WITH_VOC_LABELS_V1 | 66.4 | 92.4 | 42.0M | 178.72 | link |
FCN_ResNet101_Weights.COCO_WITH_VOC_LABELS_V1 | 63.7 | 91.9 | 54.3M | 232.74 | link |
FCN_ResNet50_Weights.COCO_WITH_VOC_LABELS_V1 | 60.5 | 91.4 | 35.3M | 152.72 | link |
LRASPP_MobileNet_V3_Large_Weights.COCO_WITH_VOC_LABELS_V1 | 57.9 | 91.2 | 3.2M | 2.09 | link |
Object Detection, Instance Segmentation and Person Keypoint Detection¶
The pre-trained models for detection, instance segmentation and keypoint detection are initialized with the classification models in torchvision. The models expect a list of Tensor[C, H, W]
. Check the constructor of the models for more information.
Warning
The detection module is in Beta stage, and backward compatibility is not guaranteed.
Object Detection¶
The following object detection models are available, with or without pre-trained weights:
Here is an example of how to use the pre-trained object detection models:
from torchvision.io.image import read_image from torchvision.models.detection import fasterrcnn_resnet50_fpn_v2, FasterRCNN_ResNet50_FPN_V2_Weights from torchvision.utils import draw_bounding_boxes from torchvision.transforms.functional import to_pil_image
img = read_image("test/assets/encode_jpeg/grace_hopper_517x606.jpg")
Step 1: Initialize model with the best available weights
weights = FasterRCNN_ResNet50_FPN_V2_Weights.DEFAULT model = fasterrcnn_resnet50_fpn_v2(weights=weights, box_score_thresh=0.9) model.eval()
Step 2: Initialize the inference transforms
preprocess = weights.transforms()
Step 3: Apply inference preprocessing transforms
batch = [preprocess(img)]
Step 4: Use the model and visualize the prediction
prediction = model(batch)[0] labels = [weights.meta["categories"][i] for i in prediction["labels"]] box = draw_bounding_boxes(img, boxes=prediction["boxes"], labels=labels, colors="red", width=4, font_size=30) im = to_pil_image(box.detach()) im.show()
The classes of the pre-trained model outputs can be found at weights.meta["categories"]
. For details on how to plot the bounding boxes of the models, you may refer to Instance segmentation models.
Table of all available Object detection weights¶
Box MAPs are reported on COCO val2017:
Weight | Box MAP | Params | GFLOPS | Recipe |
---|---|---|---|---|
FCOS_ResNet50_FPN_Weights.COCO_V1 | 39.2 | 32.3M | 128.21 | link |
FasterRCNN_MobileNet_V3_Large_320_FPN_Weights.COCO_V1 | 22.8 | 19.4M | 0.72 | link |
FasterRCNN_MobileNet_V3_Large_FPN_Weights.COCO_V1 | 32.8 | 19.4M | 4.49 | link |
FasterRCNN_ResNet50_FPN_V2_Weights.COCO_V1 | 46.7 | 43.7M | 280.37 | link |
FasterRCNN_ResNet50_FPN_Weights.COCO_V1 | 37 | 41.8M | 134.38 | link |
RetinaNet_ResNet50_FPN_V2_Weights.COCO_V1 | 41.5 | 38.2M | 152.24 | link |
RetinaNet_ResNet50_FPN_Weights.COCO_V1 | 36.4 | 34.0M | 151.54 | link |
SSD300_VGG16_Weights.COCO_V1 | 25.1 | 35.6M | 34.86 | link |
SSDLite320_MobileNet_V3_Large_Weights.COCO_V1 | 21.3 | 3.4M | 0.58 | link |
Instance Segmentation¶
The following instance segmentation models are available, with or without pre-trained weights:
For details on how to plot the masks of the models, you may refer to Instance segmentation models.
Table of all available Instance segmentation weights¶
Box and Mask MAPs are reported on COCO val2017:
Weight | Box MAP | Mask MAP | Params | GFLOPS | Recipe |
---|---|---|---|---|---|
MaskRCNN_ResNet50_FPN_V2_Weights.COCO_V1 | 47.4 | 41.8 | 46.4M | 333.58 | link |
MaskRCNN_ResNet50_FPN_Weights.COCO_V1 | 37.9 | 34.6 | 44.4M | 134.38 | link |
Keypoint Detection¶
The following person keypoint detection models are available, with or without pre-trained weights:
The classes of the pre-trained model outputs can be found at weights.meta["keypoint_names"]
. For details on how to plot the bounding boxes of the models, you may refer to Visualizing keypoints.
Table of all available Keypoint detection weights¶
Box and Keypoint MAPs are reported on COCO val2017:
Weight | Box MAP | Keypoint MAP | Params | GFLOPS | Recipe |
---|---|---|---|---|---|
KeypointRCNN_ResNet50_FPN_Weights.COCO_LEGACY | 50.6 | 61.1 | 59.1M | 133.92 | link |
KeypointRCNN_ResNet50_FPN_Weights.COCO_V1 | 54.6 | 65 | 59.1M | 137.42 | link |
Video Classification¶
Warning
The video module is in Beta stage, and backward compatibility is not guaranteed.
The following video classification models are available, with or without pre-trained weights:
Here is an example of how to use the pre-trained video classification models:
from torchvision.io.video import read_video from torchvision.models.video import r3d_18, R3D_18_Weights
vid, _, _ = read_video("test/assets/videos/v_SoccerJuggling_g23_c01.avi", output_format="TCHW") vid = vid[:32] # optionally shorten duration
Step 1: Initialize model with the best available weights
weights = R3D_18_Weights.DEFAULT model = r3d_18(weights=weights) model.eval()
Step 2: Initialize the inference transforms
preprocess = weights.transforms()
Step 3: Apply inference preprocessing transforms
batch = preprocess(vid).unsqueeze(0)
Step 4: Use the model and print the predicted category
prediction = model(batch).squeeze(0).softmax(0) label = prediction.argmax().item() score = prediction[label].item() category_name = weights.meta["categories"][label] print(f"{category_name}: {100 * score}%")
The classes of the pre-trained model outputs can be found at weights.meta["categories"]
.
Table of all available video classification weights¶
Accuracies are reported on Kinetics-400 using single crops for clip length 16:
Weight | Acc@1 | Acc@5 | Params | GFLOPS | Recipe |
---|---|---|---|---|---|
MC3_18_Weights.KINETICS400_V1 | 63.96 | 84.13 | 11.7M | 43.34 | link |
MViT_V1_B_Weights.KINETICS400_V1 | 78.477 | 93.582 | 36.6M | 70.6 | link |
MViT_V2_S_Weights.KINETICS400_V1 | 80.757 | 94.665 | 34.5M | 64.22 | link |
R2Plus1D_18_Weights.KINETICS400_V1 | 67.463 | 86.175 | 31.5M | 40.52 | link |
R3D_18_Weights.KINETICS400_V1 | 63.2 | 83.479 | 33.4M | 40.7 | link |
S3D_Weights.KINETICS400_V1 | 68.368 | 88.05 | 8.3M | 17.98 | link |
Swin3D_B_Weights.KINETICS400_V1 | 79.427 | 94.386 | 88.0M | 140.67 | link |
Swin3D_B_Weights.KINETICS400_IMAGENET22K_V1 | 81.643 | 95.574 | 88.0M | 140.67 | link |
Swin3D_S_Weights.KINETICS400_V1 | 79.521 | 94.158 | 49.8M | 82.84 | link |
Swin3D_T_Weights.KINETICS400_V1 | 77.715 | 93.519 | 28.2M | 43.88 | link |
Optical Flow¶
The following Optical Flow models are available, with or without pre-trained