Deep Learning Foundations / Module 6 of 8
15-min lesson

Pre-Trained Models

An Automated Doggy Door · Loading the Model · Loading an Image · Make a Prediction · Only Dogs

An Automated Doggy Door

Say you want a pet door that only opens for dogs — not raccoons, not the neighbor's cat. You could collect thousands of labeled dog photos and train a model from scratch, like Modules 1–4. Or you could reuse a model someone else already trained on ImageNet — 1.2 million labeled photos across 1,000 everyday categories, dozens of which happen to be dog breeds. That's a pre-trained model: all the learning already done, ready to use.

Loading the Model

VGG16 is one such model, trained on ImageNet by researchers years ago. torchvision ships it built in — one line downloads the architecture and its already-learned weights:

import torchvision.models as models

model = models.vgg16(weights=models.VGG16_Weights.IMAGENET1K_V1)
model.eval()

Loading an Image

VGG16 expects images prepared in one very specific way — the exact way its own training data was prepared. Resize to 224×224 (its fixed input size), then normalize using the mean and standard deviation of the original ImageNet training photos:

import torchvision.transforms as transforms
from PIL import Image

image = Image.open('my_photo.jpg').convert('RGB')

preprocess = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(image).unsqueeze(0)  # add the batch dimension, as always

Those specific mean/std numbers aren't arbitrary — they're the actual average pixel statistics of the dataset VGG16 learned from. Feed it pixels normalized any other way, and they simply won't look like anything it recognizes.

Make a Prediction

Same pattern as every prediction so far: no gradients needed, take the highest-scoring class out of the model's 1,000 possible outputs.

with torch.no_grad():
    output = model(input_tensor)
    prediction = output.argmax(dim=1).item()

# prediction is now a number from 0-999, one of ImageNet's 1,000 categories

Only Dogs

ImageNet's 1,000 categories happen to be ordered so that all the dog breeds sit together in one block: indices 151 through 268, from Chihuahua to Mexican hairless. "Only open for dogs" becomes a simple range check on the number the model just gave you:

is_dog = 151 <= prediction <= 268

if is_dog:
    open_door()
You didn't teach this model anything about dogs. You just asked a model that already knew, and translated its answer into a decision.

Vocabulary

Pre-trained model
A model already trained on a large dataset, ready to reuse without training from scratch.
ImageNet
A dataset of 1.2 million labeled photos across 1,000 categories, commonly used to pretrain vision models.
VGG16
A specific well-known model architecture pretrained on ImageNet.
Normalization (mean/std)
Adjusting pixel values to match the statistics of the data a pretrained model was originally trained on.

15-min quiz