Files
pytorch-deep-learning/extras/exercises/09_pytorch_model_deployment_exercises.ipynb
T
Alejandro Casanova 80c250c85f fix wrongly triggering assert
torch is already on version 2, so this assert is triggered unnecessarily
2023-10-04 20:00:04 +02:00

134 KiB

Open In Colab

09. PyTorch Model Deployment Exercises

Welcome to the 09. PyTorch Model Deployment exercises.

Your objective is to write code to satisify each of the exercises below.

Some starter code has been provided to make sure you have all the resources you need.

Note: There may be more than one solution to each of the exercises.

Resources

  1. These exercises/solutions are based on section 09. PyTorch Model Deployment of the Learn PyTorch for Deep Learning course by Zero to Mastery.
  2. See a live walkthrough of the solutions (errors and all) on YouTube (but try the exercises yourself first!).
  3. See all solutions on the course GitHub.

Note: The first section of this notebook is dedicated to getting various helper functions and datasets used for the exercises. The exercises start at the heading "Exercise 1: ...".

Get various imports and helper functions

The code in the following cells prepares imports and data for the exercises below. They are taken from 09. PyTorch Model Deployment.

In [1]:
# For this notebook to run with updated APIs, we need torch 1.12+ and torchvision 0.13+
try:
    import torch
    import torchvision
    assert int(torch.__version__.split(".")[1]) >= 12 or int(torch.__version__.split(".")[0]) > 1, "torch version should be 1.12+"
    assert int(torchvision.__version__.split(".")[1]) >= 13, "torchvision version should be 0.13+"
    print(f"torch version: {torch.__version__}")
    print(f"torchvision version: {torchvision.__version__}")
except:
    print(f"[INFO] torch/torchvision versions not as required, installing nightly versions.")
    !pip3 install -U torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
    import torch
    import torchvision
    print(f"torch version: {torch.__version__}")
    print(f"torchvision version: {torchvision.__version__}")
torch version: 1.12.1+cu113
torchvision version: 0.13.1+cu113
In [2]:
# Continue with regular imports
import matplotlib.pyplot as plt
import torch
import torchvision

from torch import nn
from torchvision import transforms

# Try to get torchinfo, install it if it doesn't work
try:
    from torchinfo import summary
except:
    print("[INFO] Couldn't find torchinfo... installing it.")
    !pip install -q torchinfo
    from torchinfo import summary

# Try to import the going_modular directory, download it from GitHub if it doesn't work
try:
    from going_modular.going_modular import data_setup, engine
    from helper_functions import download_data, set_seeds, plot_loss_curves
except:
    # Get the going_modular scripts
    print("[INFO] Couldn't find going_modular or helper_functions scripts... downloading them from GitHub.")
    !git clone https://github.com/mrdbourke/pytorch-deep-learning
    !mv pytorch-deep-learning/going_modular .
    !mv pytorch-deep-learning/helper_functions.py . # get the helper_functions.py script
    !rm -rf pytorch-deep-learning
    from going_modular.going_modular import data_setup, engine
    from helper_functions import download_data, set_seeds, plot_loss_curves
In [3]:
device = "cuda" if torch.cuda.is_available() else "cpu"
device
Out [3]:
'cuda'

Get data

Want to download the data we've been using in PyTorch Model Deployment: https://www.learnpytorch.io/09_pytorch_model_deployment/#1-getting-data

In [4]:
# Download pizza, steak, sushi images from GitHub
image_path = download_data(source="https://github.com/mrdbourke/pytorch-deep-learning/raw/main/data/pizza_steak_sushi_20_percent.zip",
                           destination="pizza_steak_sushi")
image_path
Out [4]:
[INFO] data/pizza_steak_sushi directory exists, skipping download.
PosixPath('data/pizza_steak_sushi')
In [5]:
# Setup directory paths to train and test images
train_dir = image_path / "train"
test_dir = image_path / "test"

Preprocess data

Turn images into tensors using same code as PyTorch Paper Replicating section 2.1 and 2.2: https://www.learnpytorch.io/08_pytorch_paper_replicating/#21-prepare-transforms-for-images

In [6]:
# Create image size (from Table 3 in the ViT paper) 
IMG_SIZE = 224

# Create transform pipeline manually
manual_transforms = transforms.Compose([
    transforms.Resize((IMG_SIZE, IMG_SIZE)),
    transforms.ToTensor(),
])           
print(f"Manually created transforms: {manual_transforms}")
Manually created transforms: Compose(
    Resize(size=(224, 224), interpolation=bilinear, max_size=None, antialias=None)
    ToTensor()
)
In [7]:
# Set the batch size
BATCH_SIZE = 32 # this is lower than the ViT paper but it's because we're starting small

# Create data loaders
train_dataloader, test_dataloader, class_names = data_setup.create_dataloaders(
    train_dir=train_dir,
    test_dir=test_dir,
    transform=manual_transforms, # use manually created transforms
    batch_size=BATCH_SIZE
)

train_dataloader, test_dataloader, class_names
Out [7]:
(<torch.utils.data.dataloader.DataLoader at 0x7fb52d4b77d0>,
 <torch.utils.data.dataloader.DataLoader at 0x7fb52d46a450>,
 ['pizza', 'steak', 'sushi'])
In [8]:
# Get a batch of images
image_batch, label_batch = next(iter(train_dataloader))

# Get a single image from the batch
image, label = image_batch[0], label_batch[0]

# View the batch shapes
image.shape, label
Out [8]:
(torch.Size([3, 224, 224]), tensor(2))
In [9]:
# Plot image with matplotlib
plt.imshow(image.permute(1, 2, 0)) # rearrange image dimensions to suit matplotlib [color_channels, height, width] -> [height, width, color_channels]
plt.title(class_names[label])
plt.axis(False);

Exercise 1. Make and time predictions with both feature extractor models on the test dataset using the GPU (device="cuda").

In [10]:
# TODO: your code

Exercise 2. The ViT feature extractor seems to have more learning capacity (due to more parameters) than EffNetB2, how does it go on the larger 20% split of the entire Food101 dataset?

  • Train a ViT feature extractor on the 20% Food101 dataset for 5 epochs, just like we did with EffNetB2 in section 10. Creating FoodVision Big.
In [11]:
# TODO: your code

Exercise 3. Make predictions across the 20% Food101 test dataset with the ViT feature extractor from exercise 2 and find the "most wrong" predictions

  • The predictions will be the ones with the highest prediction probability but with the wrong predicted label.
  • Write a sentence or two about why you think the model got these predictions wrong.
In [12]:
# TODO: your code

Exercise 4. Evaluate the ViT feature extractor across the whole Food101 test dataset rather than just the 20% version, how does it perform?

  • Does it beat the original Food101 paper's best result of 56.4% accuracy?
In [13]:
# TODO: your code

Exercise 5. Head to Paperswithcode.com and find the current best performing model on the Food101 dataset.

  • What model architecture does it use?
In [14]:
# TODO: your answer to the above

Exercise 6. Write down 1-3 potential failure points of our deployed FoodVision models and what some potential solutions might be.

  • For example, what happens if someone was to upload a photo that wasn't of food to our FoodVision Mini model?
In [15]:
# TODO: your answer to the above

Exercise 7. Pick any dataset from torchvision.datasets and train a feature extractor model on it using a model from torchvision.models (you could use one of the model's we've already created, e.g. EffNetB2 or ViT) for 5 epochs and then deploy your model as a Gradio app to Hugging Face Spaces.

  • You may want to pick smaller dataset/make a smaller split of it so training doesn't take too long.
  • I'd love to see your deployed models! So be sure to share them in Discord or on the course GitHub Discussions page.
In [16]:
# TODO: your code