Binary and multi-
class image
classification
DEEP LEARNING FOR IMAGES WITH PYTORCH
Michal Oleszak
Machine Learning Engineer
What will we learn with PyTorch?
DEEP LEARNING FOR IMAGES WITH PYTORCH
What will we learn with PyTorch?
DEEP LEARNING FOR IMAGES WITH PYTORCH
What will we learn with PyTorch?
DEEP LEARNING FOR IMAGES WITH PYTORCH
What will we learn with PyTorch?
DEEP LEARNING FOR IMAGES WITH PYTORCH
Prerequisites
Convolutional Neural Networks
Model training in PyTorch
Prerequisite course: Intermediate Deep Learning with PyTorch
DEEP LEARNING FOR IMAGES WITH PYTORCH
PyTorch library
DEEP LEARNING FOR IMAGES WITH PYTORCH
PyTorch library
DEEP LEARNING FOR IMAGES WITH PYTORCH
PyTorch library
DEEP LEARNING FOR IMAGES WITH PYTORCH
PyTorch library
DEEP LEARNING FOR IMAGES WITH PYTORCH
Image classification
Binary classification Multi-class classification
Two distinct classes (cats, dogs) Multiple classes (boat, train, car)
Activation function: Sigmoid Activation function: Softmax
Highest probability is the prediction
DEEP LEARNING FOR IMAGES WITH PYTORCH
Convolutional Neural Network model
DEEP LEARNING FOR IMAGES WITH PYTORCH
Convolutional Neural Network model
DEEP LEARNING FOR IMAGES WITH PYTORCH
Convolutional Neural Network model
DEEP LEARNING FOR IMAGES WITH PYTORCH
Convolutional Neural Network model
DEEP LEARNING FOR IMAGES WITH PYTORCH
Convolutional Neural Network model
DEEP LEARNING FOR IMAGES WITH PYTORCH
Datasets: class labels
classes = train_dataset.classes
print(classes)
['cat', 'dog']
print(train_dataset.class_to_idx)
{'cat': 0, 'dog': 1}
from torchvision import datasets
import [Link] as transforms
train_dir = '/data/train'
train_dataset = ImageFolder(root=train_dir,
transform=[Link]())
DEEP LEARNING FOR IMAGES WITH PYTORCH
Binary image classification: convolutional layer
Conv2d() : class BinaryCNN([Link]):
Input: 3 RGB channels (red, green, blue) def __init__(self):
super(BinaryCNN, self).__init__()
Output: 16 channels self.conv1 = nn.Conv2d(3, 16,
kernel_size=3, stride=1, padding=1)
Kernel: 3 x 3 matrix [Link] = [Link]()
[Link] = nn.MaxPool2d(kernel_size=2,
Stride = 1: the kernel moves 1 step
stride=2)
Padding = 1: 1 pixel around the border
ReLU() :
def forward(self, x):
A non-linear activation function
MaxPool2d() :
Kernel: 2×2
return x
Stride: 2 steps
DEEP LEARNING FOR IMAGES WITH PYTORCH
Binary image classification: fully connected layer
Flatten() : class BinaryCNN([Link]):
Tensors flattened into 1-D vector def __init__(self):
super(BinaryCNN, self).__init__()
Linear() : self.conv1 = nn.Conv2d(3, 16,
kernel_size=3, stride=1, padding=1)
Input: feature maps x height x width
[Link] = [Link]()
Output: a single class [Link] = nn.MaxPool2d(kernel_size=2,
stride=2)
Sigmoid() : [Link] = [Link]()
[0,1] self.fc1 = [Link](16 * 112 * 112, 1)
[Link] = [Link]()
def forward(self, x):
x = [Link]([Link](self.conv1(x)))
x = self.fc1([Link](x))
x = [Link](x)]
return x
DEEP LEARNING FOR IMAGES WITH PYTORCH
Multi-class image classification with CNN
class MultiClassCNN([Link]):
def __init__(self, num_classes):
super(MultiClassCNN, self).__init__()
...
[Link] = [Link](16 * 112 * 112, num_classes)
[Link] = [Link](dim=1)
def forward(self, x):
...
x = [Link](x)
return x
DEEP LEARNING FOR IMAGES WITH PYTORCH
Let's practice!
DEEP LEARNING FOR IMAGES WITH PYTORCH
Convolutional layers
for images
DEEP LEARNING FOR IMAGES WITH PYTORCH
Michal Oleszak
Machine Learning Engineer
Convolutional layers for images
Apply convolutional layers to image data
Access and add convolutional layers
Create convolutional blocks
Used to adapt models to a specific task
DEEP LEARNING FOR IMAGES WITH PYTORCH
Conv2d: input channels
Grayscale image: in_channels=1
RGB image (red, green, blue): in_channels=3
Transparency includes alpha channel: in_channels=4
from [Link] import functional
image = [Link]("[Link]")
num_channels = functional.get_image_num_channels(image)
print("Number of channels: ", num_channels)
Number of channels: 3
DEEP LEARNING FOR IMAGES WITH PYTORCH
Conv2d: kernel
Input tensor Kernel Output tensor (feature map)
Kernel (colored in green) moves from left to right, top to bottom of the image1
1 Thevenot, Axel. 2020. A visual and mathematical explanation of the 2D convolution layer.
DEEP LEARNING FOR IMAGES WITH PYTORCH
Kernel sizes
The most common kernel sizes: 3×3 ( Conv2d ) and 2×2 ( MaxPool2d )
Convolution is a dot product of the kernel (green) and the image region (pink)
The sum of the dot product creates a feature map (blue)
DEEP LEARNING FOR IMAGES WITH PYTORCH
Kernel is a filter
Capture image patterns
DEEP LEARNING FOR IMAGES WITH PYTORCH
Conv2d: output channels
Input channel Kernel filters Output channels
The number of output channels determines how many filters are applied
Each output channel corresponds to a distinct filter
A higher number of output channels allows the layer to learn more complex features
Output channel numbers are commonly chosen as powers of 2 (16, 32, 64, 128)
It simplifies the process of combining and dividing channels in subsequent layers
DEEP LEARNING FOR IMAGES WITH PYTORCH
Adding convolutional layers
import torch
import [Link] as nn
class Net([Link]):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1)
conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1)
model = Net()
model.add_module('conv2', conv2)
DEEP LEARNING FOR IMAGES WITH PYTORCH
Accessing convolutional layers
print(model)
Net(
(conv1): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(conv2): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
)
model.conv2
Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
DEEP LEARNING FOR IMAGES WITH PYTORCH
Creating convolutional blocks
Stacking convolutional layers in a block with [Link]()
class BinaryImageClassification([Link]):
def __init__(self):
super(BinaryImageClassification, self).__init__()
self.conv_block = [Link](
nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1),
[Link](),
nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
[Link](),
nn.MaxPool2d(kernel_size=2, stride=2)
)
def forward(self, x):
x = self.conv_block(x)
DEEP LEARNING FOR IMAGES WITH PYTORCH
Let's practice!
DEEP LEARNING FOR IMAGES WITH PYTORCH
Working with pre-
trained models
DEEP LEARNING FOR IMAGES WITH PYTORCH
Michal Oleszak
Machine Learning Engineer
Leveraging pre-trained models
Training models from scratch:
Long process
Requires lots of data
Pre-trained models - models already trained on a task
Directly reusable on a new task
Require adjustment to the new task (transfer learning)
Steps to leveraging pre-trained models:
Saving & loading models locally
Downloading torchvision models
DEEP LEARNING FOR IMAGES WITH PYTORCH
Saving a complete PyTorch model
[Link]()
Model extension: .pt or .pth
Save model weights with .state_dict()
[Link](model.state_dict(), "[Link]")
DEEP LEARNING FOR IMAGES WITH PYTORCH
Loading PyTorch models
Instantiate a new model
new_model = BinaryCNN()
Load saved parameters
new_model.load_state_dict([Link]('[Link]'))
DEEP LEARNING FOR IMAGES WITH PYTORCH
Downloading torchvision models
Import resnet architecture and weights
from [Link] import (
resnet18, ResNet18_Weights Extract weights
)
Instantiate a model passing it weights
weights = ResNet18_Weights.DEFAULT
Store required data transforms
model = resnet18(weights=weights)
transforms = [Link]()
DEEP LEARNING FOR IMAGES WITH PYTORCH
Prepare new input images
from PIL import Image Load image
Transform image
image = [Link]("[Link]")
image_tensor = transform(image) Reshape image
image_reshaped = image_tensors.unsqueeze(0)
DEEP LEARNING FOR IMAGES WITH PYTORCH
Generating a new prediction
[Link]() Evaluation mode for inference
Disable gradients
with torch.no_grad():
pred = model(image_reshaped).squeeze(0) Pass image to model and remove batch
dimension
pred_cls = [Link](0)
cls_id = pred_cls.argmax().item()
Apply softmax
cls_name = [Link]["categories"][cls_id] Select the highest-probability class and
extract its index
print(cls_name)
Map class index to label
Egyptian cat Print class label
DEEP LEARNING FOR IMAGES WITH PYTORCH
Let's practice
DEEP LEARNING FOR IMAGES WITH PYTORCH