liveBook · Manning

Pretrained model

As explained in Build a Text-to-Image Generator (from Scratch)

Pretrained weights are model parameter values learned previously on another task or dataset and reused for later adaptation.

A pretrained model starts with parameter values learned from another task or dataset instead of random initialization. Reusing those weights supplies learned features or generation capabilities before adaptation, inference, or further training. section 13.3.2 section 13.3.3

The practical workflow is sequential: load a checkpoint, construct the matching architecture, switch to evaluation mode for inference, preprocess inputs as expected, and inspect predictions or generated outputs. Adaptation instead replaces or updates task-specific layers while retaining most learned parameters. section 13.3.3 section 13.3.2

Common uses include:

  • Classify images immediately with ResNet50 across its 1,000 ImageNet categories. section 13.3.3
  • Generate or modify images from prompts with a pretrained latent diffusion model, whose text encoder, denoiser, and VAE work together. section 9.1.2
  • Adapt general visual features to specialized classification by replacing a 1,000-class head with a two-class real/fake head. section 13.3.2

Loading and adapting ResNet50

Load DEFAULT weights through torchvision, call model.eval() for reliable inference, preprocess an image to the expected tensor shape, and obtain the highest-probability ImageNet label. For deepfake detection, retain the feature-extracting layers and train a new two-output head instead. section 13.3.3 section 13.3.2

import torchvision.models as models

weights = models.ResNet50_Weights.DEFAULT 
model = models.resnet50(weights=weights)    #1
model.eval()    #2
print(model)    #3

Inspect the compact torchvision pattern for constructing a pretrained ResNet50, enabling evaluation mode, and printing its architecture. section 13.3.3

Listing 10.1 Image generation with the diffusers library
import torch
from diffusers import StableDiffusionPipeline

torch.manual_seed(42)
prompt="an astronaut in a spacesuit riding a unicorn"
pipe = StableDiffusionPipeline.from_pretrained(    #1
    "CompVis/stable-diffusion-v1-4",    #2
    variant="fp16",    #3
    torch_dtype=torch.float16).to("cuda")
image = pipe(prompt).images[0]
image

Run the diffusers pattern for downloading a Stable Diffusion checkpoint, selecting half precision, and generating an image from a text prompt. section 10.1

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains checkpoint loading, evaluation, preprocessing, inference, and output-head replacement, while stopping short of full training implementation.
Examples
Demonstrates ResNet50 image classification, Stable Diffusion prompt generation, latent image modification, and VAE checkpoint loading.
What you can do after
Apply it via torchvision or diffusers to load checkpoints, run predictions or generation, and plan targeted fine-tuning.
How it connects
Connects transfer learning with fine-tuning, feature extraction, ImageNet, ResNet50, latent diffusion, CLIP, and variational autoencoders.

Also explained in

Compare how 7 books explain pretrained model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.