liveBook · Manning

Chapter 2: Pretrained networks

What you will learn in chapter 2 of Deep Learning with PyTorch, Second Edition

The introduction explains why deep learning has had a major impact on computer vision and why pretrained models are useful starting points.

After this chapter you will be able to

Section by section

Introduction

The introduction explains why deep learning has had a major impact on computer vision and why pretrained models are useful starting points.

Key terms: computer vision, convolutional layers, GPUs, pretrained neural network, architecture, off-the-shelf model, weights

2.1 A pretrained network that recognizes the subject of an image

The section introduces pretrained image classifiers, the ImageNet labels they use, and how an image becomes a ranked prediction.

Key terms: pretrained models, preoptimized weights, torchvision, Hugging Face, ImageNet, ILSVRC, class, label, top five predictions, preprocessed

2.1.1 Obtaining a pretrained network for image recognition

This section introduces TorchVision as a source of image-recognition models and explains how its model classes and convenience functions are named.

Key terms: TorchVision, torchvision.models, architecture, convenience functions

2.1.2 AlexNet

The section introduces AlexNet’s historical significance, sketches how an image passes through it, and explains why running it usefully requires trained weights.

Key terms: AlexNet, ILSVRC, top-five test error rate, filter, intermediate representations, 4,096-element 1D vector, output probabilities, output class, forward pass, weights

2.1.3 The Vision Transformer

This section introduces Vision Transformers for image classification, compares their ImageNet results with AlexNet, and identifies how to obtain a pretrained ViT in PyTorch.

Key terms: vision transformers, ViT, self-attention, top-five error rates, ImageNet, AlexNet, vit_b_16, ViT_B_16_Weights.IMAGENET1K_V1, weights

2.1.4 Ready, set, almost run

This section interprets a printed Vision Transformer model summary and prepares an image in the format the pretrained model expects.

Key terms: modules, layers, submodules, EncoderBlock, fc, out_features, vit, torchvision, transforms, preprocess

2.1.5 Run!

This section runs a pretrained image classifier and shows how to interpret its predictions while recognizing their limits.

Key terms: inference, eval mode, batch normalization, dropout, ImageNet class, out tensor, max, index, .item(), labels

2.2 Generating and editing images

This section introduces diffusion-based inpainting as a way to make localized image edits and explains how a mask separates protected areas from editable ones.

Key terms: mask, black pixels, white pixels, protected areas, primed region, diffusion-based inpainting, text-guided, mask-aware, iterative refinement

2.2.1 The inpainting process

Explains how diffusion-based inpainting uses a prompt, an optional input image, and a mask to edit selected parts of a picture.

Key terms: diffusion model, noising, denoising, prompt, input image, pure noise, image-to-image generation, mask, inpainting, masked region

2.2.2 A network that turns horses into zebras

This section uses a pretrained inpainting pipeline to turn a horse image into a zebra-like image, then explains what the example shows about learned weights and generative images.

Key terms: diffusers, inpaint pipeline, mask, text prompt, weights, direct supervision, distant supervision, generative models, face-swapping, deep fakes

2.3 Model Zoo: Hugging Face

The section introduces model zoos through Hugging Face and uses pretrained GPT-2 text generation to show how readily a published model can be applied.

Key terms: model zoo, pretrained models, uniform interface, Hugging Face, transformers, GPT2, text generation, sampling strategy

2.4 A pretrained network that describes scenes

This section introduces how BLIP learns from paired images and captions and how its image encoder and text decoder generate a caption for a new image.

Key terms: BLIP, multimodal model, embedding, image and caption pairs, inference, image encoder, text decoder, caption

2.4.1 BLIP in action

This section uses BLIP to caption familiar and generated images, then explains that its descriptions arise from learned patterns rather than handcoded rules.

Key terms: BLIP, captioning network, corpus of images and their descriptions, hardcoded criterion or grammar, multiple networks

2.5 Conclusion

The conclusion draws out why task-specific pretrained networks are useful, especially when adapting a model to a new task with limited data.

Key terms: pretrained network, fine-tuning, data points

2.6 Exercises

2.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: Introducing deep learning and the PyTorch library Chapter 3: It starts with a tensor →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.