Pretrained Models
A thousand objects, one line.
Some of the best vision models in the world were trained on over a million images, using huge amounts of computing power. And they're free. You can load one with a single line, and it'll instantly recognise a thousand kinds of object.

- Trained on a million images
- Free to download
- One line of code
Classifying real photos
We load ConvNeXt, a modern convolutional network. Transforms gives exactly the preprocessing that it expects, which is a very common source of bugs. We classify three photos. Softmax gives chances. Run it. It recognises the cat's breed, the coffee, and the rocket, with good confidence. It's never seen these exact images. Note how it gives two possible answers. Top five accuracy is often reported for that reason.
import torch
from torchvision import models
from PIL import Image
from skimage import data
weights = models.ConvNeXt_Tiny_Weights.DEFAULT
model = models.convnext_tiny(weights=weights).eval()
prep = weights.transforms()
for name, image in [("cat", data.chelsea()), ("coffee", data.coffee()), ("rocket", data.rocket())]:
x = prep(Image.fromarray(image)).unsqueeze(0)
with torch.no_grad():
p = model(x).softmax(1)[0]
top = p.topk(2)
labels = [weights.meta["categories"][i] for i in top.indices]
print(f"{name:<7} {labels[0]:<16} {top.values[0]:.2f} then {labels[1]}")cat Egyptian cat 0.70 then tabby coffee espresso 0.83 then cup rocket drilling platform 0.29 then crane
Summary
A pretrained network gives you a thousand classes for free. But it doesn't know what a bruised apple is. To teach it, we use the most useful trick in practical vision.
Key takeaways- Free models, a line away
- Always use the right preprocessing
- Softmax gives chances