Pre-trained models help you use ready-made AI models that already learned from lots of images. This saves time and effort when building your own image tasks.
torchvision pre-trained models in PyTorch
import torchvision.models as models model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) model.eval()
Set weights=models.ResNet18_Weights.DEFAULT to load a model with weights learned on ImageNet data.
Call model.eval() to set the model to evaluation mode before using it for predictions.
import torchvision.models as models # Load a pre-trained ResNet18 model model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) model.eval()
import torchvision.models as models # Load a pre-trained MobileNetV2 model model = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.DEFAULT) model.eval()
import torchvision.models as models # Load a pre-trained DenseNet121 model model = models.densenet121(weights=models.DenseNet121_Weights.DEFAULT) model.eval()
This program loads a pre-trained ResNet18 model, downloads an image of a dog, preprocesses it, and predicts the class with confidence.
import torch from torchvision import models, transforms from PIL import Image import requests # Load a pre-trained ResNet18 model model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) model.eval() # Define image transforms to prepare input preprocess = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # Download an example image url = 'https://upload.wikimedia.org/wikipedia/commons/9/9a/Pug_600.jpg' image = Image.open(requests.get(url, stream=True).raw) # Preprocess the image input_tensor = preprocess(image) input_batch = input_tensor.unsqueeze(0) # create batch dimension # Run the model on the input with torch.no_grad(): output = model(input_batch) # Load ImageNet class names labels_url = 'https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt' labels = requests.get(labels_url).text.splitlines() # Get the predicted class index probabilities = torch.nn.functional.softmax(output[0], dim=0) confidence, class_idx = torch.max(probabilities, dim=0) # Print the result print(f'Predicted class: {labels[class_idx]}') print(f'Confidence: {confidence.item():.4f}')
Pre-trained models are trained on ImageNet with 1000 classes, so predictions match those classes.
Always preprocess images the same way the model expects (resize, crop, normalize).
Use model.eval() to turn off training features like dropout for correct predictions.
Pre-trained models let you use powerful image models without training from scratch.
They are great for quick experiments, small datasets, or as a starting point for your own tasks.
Remember to preprocess images correctly and set the model to evaluation mode before predicting.