PyTorch

Getting Started with PyTorch

Course Introduction

Welcome to the PyTorch course! PyTorch is a versatile, open-source machine learning library celebrated for its flexibility and efficiency in constructing sophisticated AI models. Leaders in the tech industry—including Meta, Microsoft, and Tesla—rely on PyTorch for cutting-edge machine learning projects. Its powerful computational capabilities and vibrant community support make it an invaluable tool in the ever-evolving AI and machine learning landscape.

In this hands-on course, you will assume the role of an AI engineer tasked with developing an innovative application to accelerate breast cancer diagnosis. This project not only hones your PyTorch skills but also contributes to advancing healthcare through faster, more accurate treatment planning. I’m Mumshad Mannambeth, and I will be your guide throughout this journey.

Throughout the course, you will engage in a series of practical labs that convert theoretical concepts into real-world applications, allowing you to experiment, learn from mistakes, and build the confidence to tackle genuine PyTorch challenges.

Let’s explore the topics covered in this course:

PyTorch Essentials

In this section, we dive into the basics of PyTorch. You’ll learn how to set up your environment, work with tensors, and leverage automatic differentiation—a core component of training models. For example, see the code snippet below which demonstrates how to create a simple tensor and print its attributes:

# Create a simple tensor from a list and print it
import torch

simple_tensor = torch.tensor([1, 2, 3])
print(simple_tensor)

# Print the shape of the tensor
print(simple_tensor.shape)

Data Handling with PyTorch

Next, we focus on data manipulation using PyTorch. Managing datasets, data loaders, and transformation pipelines is fundamental to building robust machine learning models. The helper function below shows how you can display images—a common requirement when working with custom datasets:

import matplotlib.pyplot as plt

def display_images(original_image, new_image=None, new_image_name=None):
    """
    Helper function to display images side by side.
    """
    if new_image is not None:
        # Create a figure with 1 row and 2 columns
        fig, axes = plt.subplots(1, 2, figsize=(10, 5))
        # Display the original image
        axes[0].imshow(original_image)
        axes[0].axis('on')  # Turn on axis for clarity
        axes[0].set_title('Original Image')
        # Display the new image
        axes[1].imshow(new_image)
        axes[1].axis('on')  # Turn on axis for clarity
        axes[1].set_title(new_image_name)
    else:
        # If there is no new image, display only the original image
        plt.imshow(original_image)

Model Training and Advanced Techniques

After preparing your data, you will embark on model training, performance optimization, and comprehensive evaluation. In this section, we also explore advanced strategies, including transfer learning and utilizing pre-trained models to enhance both accuracy and efficiency.

Below is an example code block demonstrating how to set up image transformations using torchvision:

import pandas as pd
import torch
import os
from torch.utils.data import Dataset, DataLoader
from PIL import Image
from torchvision import transforms as v2

# Define our transformations for testing
test_transform = v2.Compose([
    v2.Resize((128, 128)),
    v2.ToTensor(),
    v2.Normalize(mean=[0.485, 0.456, 0.406],
                 std=[0.229, 0.224, 0.225]),
])

Note

Ensure you experiment with various transformation strategies to best fit your dataset. Adjust parameters such as image size and normalization values based on your specific data characteristics.

Deployment

In the final phase, we cover model deployment. You will learn how to serve your model using Flask, package your application into a Docker container, and deploy it on Kubernetes for scalable and resilient performance.

Below are the essential commands for setting up Docker on Ubuntu:

apt-get update
apt-get install ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu/ $(lsb_release -cs) stable" \
  | tee /etc/apt/sources.list.d/docker.list > /dev/null

Note

After configuring Docker, explore additional documentation on Docker's official site and Kubernetes basics to deepen your understanding of containerized deployments.

By the end of this course, you will have built, trained, and deployed a complete image classification solution. Are you ready to make a significant impact in the fields of AI and healthcare? Let's get started on this transformative journey to elevate your PyTorch expertise!

Watch Video

Watch video content