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






