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

Deploying Your Model

Loading the Model · Preparing an Image for the Model · Making Predictions

Loading the Model

Everything so far has trained a model and used it in the same script. Deployment means a different script, maybe days later — one that never trained anything, and just wants to use what was already learned. As Module 4 covered: state_dict() is only the learned numbers, so you rebuild the exact same architecture first, then load the weights into it.

def build_model():
    return nn.Sequential(
        nn.Conv2d(1, 25, kernel_size=3, stride=1, padding=1),
        nn.BatchNorm2d(25), nn.ReLU(), nn.MaxPool2d(2, stride=2),
        nn.Conv2d(25, 50, kernel_size=3, stride=1, padding=1),
        nn.BatchNorm2d(50), nn.ReLU(), nn.Dropout(0.2), nn.MaxPool2d(2, stride=2),
        nn.Conv2d(50, 75, kernel_size=3, stride=1, padding=1),
        nn.BatchNorm2d(75), nn.ReLU(), nn.MaxPool2d(2, stride=2),
        nn.Flatten(),
        nn.Linear(75 * 3 * 3, 512), nn.Dropout(0.3), nn.ReLU(),
        nn.Linear(512, 25)
    )

model = build_model()
model.load_state_dict(torch.load('asl_model.pth'))
model.eval()

Preparing an Image for the Model

A real photo someone hands you will not already be a neat 28×28 grayscale square — it might be 4000×3000 pixels, in color, from a phone camera. The model only ever learned to read exactly what it was trained on, so the new image has to be reshaped into that same format before it means anything to the model:

from PIL import Image
import numpy as np
import torch

image = Image.open('my_photo.jpg').convert('L')   # 'L' = grayscale
image = image.resize((28, 28))                     # match training size, whatever it started as
image_array = np.array(image) / 255.0              # same 0-1 normalization as training

image_tensor = torch.tensor(image_array, dtype=torch.float32)
image_tensor = image_tensor.reshape(1, 1, 28, 28)  # (batch of 1, 1 channel, 28, 28)

That leading 1 in reshape(1, 1, 28, 28) is a batch dimension — the model was trained expecting a batch of images, even a batch of exactly one. Skip it, and the shapes won't line up.

Making Predictions

Same eval() and no_grad() as validation in earlier modules — there's no training happening, so no need for dropout's randomness or gradient tracking. argmax picks the highest-scoring letter, same as Module 2; .item() just pulls the plain number out of the result so you can use it normally:

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

print(f"Predicted letter index: {prediction}")
A trained model is a function. Deployment is just calling that function on something new.

Vocabulary

Deployment
Using an already-trained model in a new script or environment, separate from training.
Batch dimension
The leading dimension every PyTorch model expects — even one image is treated as "a batch of one."
.item()
Pulls a plain Python number out of a single-value tensor.

15-min quiz