Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconDeep Learning and AI Superhero
Deep Learning and AI Superhero

Chapter 4: Deep Learning with PyTorch

Practical Exercises Chapter 4

Exercise 1: Saving and Loading a Model’s state_dict

Task: Define a simple neural network, train it on the MNIST dataset, save the model’s state_dict, and then load the saved state_dict to continue training from where it left off.

Solution:

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple neural network
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 64)
        self.fc3 = nn.Linear(64, 10)

    def forward(self, x):
        x = x.view(-1, 784)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        return self.fc3(x)

# Instantiate the model and optimizer
model = SimpleNN()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Save model's state_dict after training for a few epochs
torch.save(model.state_dict(), 'simple_nn_state.pth')

# Load the model's state_dict
loaded_model = SimpleNN()
loaded_model.load_state_dict(torch.load('simple_nn_state.pth'))

# Continue training or use the loaded model for inference
print("Model state loaded successfully!")

Exercise 2: Saving and Loading a Model Checkpoint

Task: Train a neural network on the CIFAR-10 dataset, save a checkpoint containing the model’s state_dict and optimizer state, and resume training from the saved checkpoint.

Solution:

import torch
import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple model (ResNet-18 for CIFAR-10)
model = models.resnet18(pretrained=False)
model.fc = nn.Linear(model.fc.in_features, 10)

# Define optimizer and loss function
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Define CIFAR-10 dataset and DataLoader
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Train for a few epochs
for epoch in range(2):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

# Save the model and optimizer state as a checkpoint
checkpoint = {
    'epoch': 2,
    'model_state_dict': model.state_dict(),
    'optimizer_state_dict': optimizer.state_dict(),
    'loss': running_loss
}
torch.save(checkpoint, 'cifar10_checkpoint.pth')

# Load the checkpoint and resume training
checkpoint = torch.load('cifar10_checkpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
start_epoch = checkpoint['epoch']
loss = checkpoint['loss']

print(f"Resumed training from epoch {start_epoch}, Loss: {loss}")

Exercise 3: Deploying a PyTorch Model with TorchServe

Task: Export a trained model (e.g., ResNet-18) as a .pth file, create a custom handler for TorchServe, and deploy the model using TorchServe. Use the TorchServe API to send a test image for prediction.

Solution:

Step 1: Export the model’s weights.

import torch
import torchvision.models as models

# Load a pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Save the model's state_dict for deployment
torch.save(model.state_dict(), 'resnet18.pth')

Step 2: Create a custom handler (if needed).

from torchvision import transforms
from PIL import Image
import torch

class ResNetHandler:
    def __init__(self):
        self.model = None
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    def initialize(self, model_dir):
        self.model = models.resnet18(pretrained=False)
        self.model.load_state_dict(torch.load(f"{model_dir}/resnet18.pth", map_location=self.device))
        self.model.to(self.device)
        self.model.eval()

    def preprocess(self, data):
        transform = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = Image.open(data[0]['body'])
        return transform(image).unsqueeze(0).to(self.device)

    def inference(self, data):
        with torch.no_grad():
            output = self.model(data)
        return torch.argmax(output, dim=1).item()

    def postprocess(self, data):
        return [{"predicted_class": data}]

Step 3: Archive the model using torch-model-archiver.

torch-model-archiver \\
  --model-name resnet18 \\
  --version 1.0 \\
  --serialized-file resnet18.pth \\
  --handler handler.py \\
  --export-path model_store

Step 4: Start TorchServe.

torchserve --start --model-store model_store --models resnet18=resnet18.mar

Step 5: Send a test image for prediction.

import requests

# Prepare the image file for prediction
image_file = {'data': open('test_image.jpg', 'rb')}

# Send a POST request to TorchServe
response = requests.post('<http://localhost:8080/predictions/resnet18>', files=image_file)

# Print the predicted class
print(response.json())

Exercise 4: Loading a Pretrained Model and Fine-Tuning

Task: Load a pretrained ResNet-18 model, replace the final layer, and fine-tune the model on a new dataset (CIFAR-10). Save the fine-tuned model and evaluate it on the test set.

Solution:

import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader

# Load the pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Freeze the parameters of all layers except the last fully connected layer
for param in model.parameters():
    param.requires_grad = False

# Replace the final fully connected layer to match the CIFAR-10 dataset
model.fc = nn.Linear(model.fc.in_features, 10)

# Define the optimizer and loss function
optimizer = optim.Adam(model.fc.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Load CIFAR-10 dataset
transform = transforms.Compose([transforms.Resize(224), transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Fine-tune the model
for epoch in range(5):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

    print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}")

# Save the fine-tuned model
torch.save(model.state_dict(), 'resnet18_finetuned.pth')

These exercises cover essential skills such as saving/loading models and checkpoints, deploying PyTorch models with TorchServe, and fine-tuning pretrained models. By completing these tasks, you will gain practical experience in managing PyTorch models throughout the training, deployment, and inference lifecycle.

Practical Exercises Chapter 4

Exercise 1: Saving and Loading a Model’s state_dict

Task: Define a simple neural network, train it on the MNIST dataset, save the model’s state_dict, and then load the saved state_dict to continue training from where it left off.

Solution:

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple neural network
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 64)
        self.fc3 = nn.Linear(64, 10)

    def forward(self, x):
        x = x.view(-1, 784)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        return self.fc3(x)

# Instantiate the model and optimizer
model = SimpleNN()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Save model's state_dict after training for a few epochs
torch.save(model.state_dict(), 'simple_nn_state.pth')

# Load the model's state_dict
loaded_model = SimpleNN()
loaded_model.load_state_dict(torch.load('simple_nn_state.pth'))

# Continue training or use the loaded model for inference
print("Model state loaded successfully!")

Exercise 2: Saving and Loading a Model Checkpoint

Task: Train a neural network on the CIFAR-10 dataset, save a checkpoint containing the model’s state_dict and optimizer state, and resume training from the saved checkpoint.

Solution:

import torch
import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple model (ResNet-18 for CIFAR-10)
model = models.resnet18(pretrained=False)
model.fc = nn.Linear(model.fc.in_features, 10)

# Define optimizer and loss function
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Define CIFAR-10 dataset and DataLoader
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Train for a few epochs
for epoch in range(2):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

# Save the model and optimizer state as a checkpoint
checkpoint = {
    'epoch': 2,
    'model_state_dict': model.state_dict(),
    'optimizer_state_dict': optimizer.state_dict(),
    'loss': running_loss
}
torch.save(checkpoint, 'cifar10_checkpoint.pth')

# Load the checkpoint and resume training
checkpoint = torch.load('cifar10_checkpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
start_epoch = checkpoint['epoch']
loss = checkpoint['loss']

print(f"Resumed training from epoch {start_epoch}, Loss: {loss}")

Exercise 3: Deploying a PyTorch Model with TorchServe

Task: Export a trained model (e.g., ResNet-18) as a .pth file, create a custom handler for TorchServe, and deploy the model using TorchServe. Use the TorchServe API to send a test image for prediction.

Solution:

Step 1: Export the model’s weights.

import torch
import torchvision.models as models

# Load a pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Save the model's state_dict for deployment
torch.save(model.state_dict(), 'resnet18.pth')

Step 2: Create a custom handler (if needed).

from torchvision import transforms
from PIL import Image
import torch

class ResNetHandler:
    def __init__(self):
        self.model = None
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    def initialize(self, model_dir):
        self.model = models.resnet18(pretrained=False)
        self.model.load_state_dict(torch.load(f"{model_dir}/resnet18.pth", map_location=self.device))
        self.model.to(self.device)
        self.model.eval()

    def preprocess(self, data):
        transform = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = Image.open(data[0]['body'])
        return transform(image).unsqueeze(0).to(self.device)

    def inference(self, data):
        with torch.no_grad():
            output = self.model(data)
        return torch.argmax(output, dim=1).item()

    def postprocess(self, data):
        return [{"predicted_class": data}]

Step 3: Archive the model using torch-model-archiver.

torch-model-archiver \\
  --model-name resnet18 \\
  --version 1.0 \\
  --serialized-file resnet18.pth \\
  --handler handler.py \\
  --export-path model_store

Step 4: Start TorchServe.

torchserve --start --model-store model_store --models resnet18=resnet18.mar

Step 5: Send a test image for prediction.

import requests

# Prepare the image file for prediction
image_file = {'data': open('test_image.jpg', 'rb')}

# Send a POST request to TorchServe
response = requests.post('<http://localhost:8080/predictions/resnet18>', files=image_file)

# Print the predicted class
print(response.json())

Exercise 4: Loading a Pretrained Model and Fine-Tuning

Task: Load a pretrained ResNet-18 model, replace the final layer, and fine-tune the model on a new dataset (CIFAR-10). Save the fine-tuned model and evaluate it on the test set.

Solution:

import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader

# Load the pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Freeze the parameters of all layers except the last fully connected layer
for param in model.parameters():
    param.requires_grad = False

# Replace the final fully connected layer to match the CIFAR-10 dataset
model.fc = nn.Linear(model.fc.in_features, 10)

# Define the optimizer and loss function
optimizer = optim.Adam(model.fc.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Load CIFAR-10 dataset
transform = transforms.Compose([transforms.Resize(224), transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Fine-tune the model
for epoch in range(5):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

    print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}")

# Save the fine-tuned model
torch.save(model.state_dict(), 'resnet18_finetuned.pth')

These exercises cover essential skills such as saving/loading models and checkpoints, deploying PyTorch models with TorchServe, and fine-tuning pretrained models. By completing these tasks, you will gain practical experience in managing PyTorch models throughout the training, deployment, and inference lifecycle.

Practical Exercises Chapter 4

Exercise 1: Saving and Loading a Model’s state_dict

Task: Define a simple neural network, train it on the MNIST dataset, save the model’s state_dict, and then load the saved state_dict to continue training from where it left off.

Solution:

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple neural network
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 64)
        self.fc3 = nn.Linear(64, 10)

    def forward(self, x):
        x = x.view(-1, 784)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        return self.fc3(x)

# Instantiate the model and optimizer
model = SimpleNN()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Save model's state_dict after training for a few epochs
torch.save(model.state_dict(), 'simple_nn_state.pth')

# Load the model's state_dict
loaded_model = SimpleNN()
loaded_model.load_state_dict(torch.load('simple_nn_state.pth'))

# Continue training or use the loaded model for inference
print("Model state loaded successfully!")

Exercise 2: Saving and Loading a Model Checkpoint

Task: Train a neural network on the CIFAR-10 dataset, save a checkpoint containing the model’s state_dict and optimizer state, and resume training from the saved checkpoint.

Solution:

import torch
import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple model (ResNet-18 for CIFAR-10)
model = models.resnet18(pretrained=False)
model.fc = nn.Linear(model.fc.in_features, 10)

# Define optimizer and loss function
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Define CIFAR-10 dataset and DataLoader
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Train for a few epochs
for epoch in range(2):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

# Save the model and optimizer state as a checkpoint
checkpoint = {
    'epoch': 2,
    'model_state_dict': model.state_dict(),
    'optimizer_state_dict': optimizer.state_dict(),
    'loss': running_loss
}
torch.save(checkpoint, 'cifar10_checkpoint.pth')

# Load the checkpoint and resume training
checkpoint = torch.load('cifar10_checkpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
start_epoch = checkpoint['epoch']
loss = checkpoint['loss']

print(f"Resumed training from epoch {start_epoch}, Loss: {loss}")

Exercise 3: Deploying a PyTorch Model with TorchServe

Task: Export a trained model (e.g., ResNet-18) as a .pth file, create a custom handler for TorchServe, and deploy the model using TorchServe. Use the TorchServe API to send a test image for prediction.

Solution:

Step 1: Export the model’s weights.

import torch
import torchvision.models as models

# Load a pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Save the model's state_dict for deployment
torch.save(model.state_dict(), 'resnet18.pth')

Step 2: Create a custom handler (if needed).

from torchvision import transforms
from PIL import Image
import torch

class ResNetHandler:
    def __init__(self):
        self.model = None
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    def initialize(self, model_dir):
        self.model = models.resnet18(pretrained=False)
        self.model.load_state_dict(torch.load(f"{model_dir}/resnet18.pth", map_location=self.device))
        self.model.to(self.device)
        self.model.eval()

    def preprocess(self, data):
        transform = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = Image.open(data[0]['body'])
        return transform(image).unsqueeze(0).to(self.device)

    def inference(self, data):
        with torch.no_grad():
            output = self.model(data)
        return torch.argmax(output, dim=1).item()

    def postprocess(self, data):
        return [{"predicted_class": data}]

Step 3: Archive the model using torch-model-archiver.

torch-model-archiver \\
  --model-name resnet18 \\
  --version 1.0 \\
  --serialized-file resnet18.pth \\
  --handler handler.py \\
  --export-path model_store

Step 4: Start TorchServe.

torchserve --start --model-store model_store --models resnet18=resnet18.mar

Step 5: Send a test image for prediction.

import requests

# Prepare the image file for prediction
image_file = {'data': open('test_image.jpg', 'rb')}

# Send a POST request to TorchServe
response = requests.post('<http://localhost:8080/predictions/resnet18>', files=image_file)

# Print the predicted class
print(response.json())

Exercise 4: Loading a Pretrained Model and Fine-Tuning

Task: Load a pretrained ResNet-18 model, replace the final layer, and fine-tune the model on a new dataset (CIFAR-10). Save the fine-tuned model and evaluate it on the test set.

Solution:

import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader

# Load the pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Freeze the parameters of all layers except the last fully connected layer
for param in model.parameters():
    param.requires_grad = False

# Replace the final fully connected layer to match the CIFAR-10 dataset
model.fc = nn.Linear(model.fc.in_features, 10)

# Define the optimizer and loss function
optimizer = optim.Adam(model.fc.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Load CIFAR-10 dataset
transform = transforms.Compose([transforms.Resize(224), transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Fine-tune the model
for epoch in range(5):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

    print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}")

# Save the fine-tuned model
torch.save(model.state_dict(), 'resnet18_finetuned.pth')

These exercises cover essential skills such as saving/loading models and checkpoints, deploying PyTorch models with TorchServe, and fine-tuning pretrained models. By completing these tasks, you will gain practical experience in managing PyTorch models throughout the training, deployment, and inference lifecycle.

Practical Exercises Chapter 4

Exercise 1: Saving and Loading a Model’s state_dict

Task: Define a simple neural network, train it on the MNIST dataset, save the model’s state_dict, and then load the saved state_dict to continue training from where it left off.

Solution:

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple neural network
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 64)
        self.fc3 = nn.Linear(64, 10)

    def forward(self, x):
        x = x.view(-1, 784)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        return self.fc3(x)

# Instantiate the model and optimizer
model = SimpleNN()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Save model's state_dict after training for a few epochs
torch.save(model.state_dict(), 'simple_nn_state.pth')

# Load the model's state_dict
loaded_model = SimpleNN()
loaded_model.load_state_dict(torch.load('simple_nn_state.pth'))

# Continue training or use the loaded model for inference
print("Model state loaded successfully!")

Exercise 2: Saving and Loading a Model Checkpoint

Task: Train a neural network on the CIFAR-10 dataset, save a checkpoint containing the model’s state_dict and optimizer state, and resume training from the saved checkpoint.

Solution:

import torch
import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define a simple model (ResNet-18 for CIFAR-10)
model = models.resnet18(pretrained=False)
model.fc = nn.Linear(model.fc.in_features, 10)

# Define optimizer and loss function
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Define CIFAR-10 dataset and DataLoader
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Train for a few epochs
for epoch in range(2):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

# Save the model and optimizer state as a checkpoint
checkpoint = {
    'epoch': 2,
    'model_state_dict': model.state_dict(),
    'optimizer_state_dict': optimizer.state_dict(),
    'loss': running_loss
}
torch.save(checkpoint, 'cifar10_checkpoint.pth')

# Load the checkpoint and resume training
checkpoint = torch.load('cifar10_checkpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
start_epoch = checkpoint['epoch']
loss = checkpoint['loss']

print(f"Resumed training from epoch {start_epoch}, Loss: {loss}")

Exercise 3: Deploying a PyTorch Model with TorchServe

Task: Export a trained model (e.g., ResNet-18) as a .pth file, create a custom handler for TorchServe, and deploy the model using TorchServe. Use the TorchServe API to send a test image for prediction.

Solution:

Step 1: Export the model’s weights.

import torch
import torchvision.models as models

# Load a pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Save the model's state_dict for deployment
torch.save(model.state_dict(), 'resnet18.pth')

Step 2: Create a custom handler (if needed).

from torchvision import transforms
from PIL import Image
import torch

class ResNetHandler:
    def __init__(self):
        self.model = None
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    def initialize(self, model_dir):
        self.model = models.resnet18(pretrained=False)
        self.model.load_state_dict(torch.load(f"{model_dir}/resnet18.pth", map_location=self.device))
        self.model.to(self.device)
        self.model.eval()

    def preprocess(self, data):
        transform = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = Image.open(data[0]['body'])
        return transform(image).unsqueeze(0).to(self.device)

    def inference(self, data):
        with torch.no_grad():
            output = self.model(data)
        return torch.argmax(output, dim=1).item()

    def postprocess(self, data):
        return [{"predicted_class": data}]

Step 3: Archive the model using torch-model-archiver.

torch-model-archiver \\
  --model-name resnet18 \\
  --version 1.0 \\
  --serialized-file resnet18.pth \\
  --handler handler.py \\
  --export-path model_store

Step 4: Start TorchServe.

torchserve --start --model-store model_store --models resnet18=resnet18.mar

Step 5: Send a test image for prediction.

import requests

# Prepare the image file for prediction
image_file = {'data': open('test_image.jpg', 'rb')}

# Send a POST request to TorchServe
response = requests.post('<http://localhost:8080/predictions/resnet18>', files=image_file)

# Print the predicted class
print(response.json())

Exercise 4: Loading a Pretrained Model and Fine-Tuning

Task: Load a pretrained ResNet-18 model, replace the final layer, and fine-tune the model on a new dataset (CIFAR-10). Save the fine-tuned model and evaluate it on the test set.

Solution:

import torch.optim as optim
import torch.nn as nn
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader

# Load the pretrained ResNet-18 model
model = models.resnet18(pretrained=True)

# Freeze the parameters of all layers except the last fully connected layer
for param in model.parameters():
    param.requires_grad = False

# Replace the final fully connected layer to match the CIFAR-10 dataset
model.fc = nn.Linear(model.fc.in_features, 10)

# Define the optimizer and loss function
optimizer = optim.Adam(model.fc.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# Load CIFAR-10 dataset
transform = transforms.Compose([transforms.Resize(224), transforms.ToTensor()])
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Fine-tune the model
for epoch in range(5):
    running_loss = 0.0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

    print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}")

# Save the fine-tuned model
torch.save(model.state_dict(), 'resnet18_finetuned.pth')

These exercises cover essential skills such as saving/loading models and checkpoints, deploying PyTorch models with TorchServe, and fine-tuning pretrained models. By completing these tasks, you will gain practical experience in managing PyTorch models throughout the training, deployment, and inference lifecycle.