Going modular
with
Where can you get help?
“If in doubt, run the code”
• Follow along with the code
• Try it for yourself
• Press SHIFT + CMD + SPACE to read the docstring
• Search for it
• Try again
• Ask
https://www.github.com/mrdbourke/pytorch-deep-learning/discussions
“What is going modular?”
“I’ve written some nice code in a notebook, can I reuse it elsewhere?”
Yes.
What we’re going to build
FoodVision Mini
🍕
Load data Build/train a model Predict with the model
We’re go in g to tu rn th e c o de to d o
this from n o te b o ok c el l c od e in to a
series of Python scripts
What we’re going to build
FoodVision Mini
Prepares data
Functions to train/test
Build🥩
s a PyTorch model
Trains a PyTorch model
Utility functions
🍣
Trained models
Data in standard
🍕 classification
image
format
Load data Build/train a model Predict with the model
We’re go in g to tu rn th e c o de to d o
this from n o te b o ok c el l c od e in to a
series of Python scripts
PyTorch from the command line
Target Python script How big should the batch size be? Train for how long?
python train.py —model MODEL_NAME —batch_size BATCH_SIZE —lr LEARNING_RATE —num_epochs NUM_EPOCHS
Model to train What should the learning rate be?
python train.py —model tinyvgg —batch_size 32 —lr 0.001 —num_epochs 10
Note: there are many more
h y pe rpar am et er s y ou co u ld ad d h er e
“Train the TinyVGG model with a batch size of 32
and a learning rate of 0.001 for 10 epochs.”
PyTorch in the wild (exam p le s o f P y th o n s c ri p ts )
Source: torchvision object detection GitHub.
Source: Detectron2 documentation.
Source: ConvMAE paper GitHub.
Source: Training state-of-the-art computer vision models with torchvision from the PyTorch blog.
My workflow (one of many options)
en t, ex p er im en t, ex p erim en t! ) data_setup.py
(experim
Start with Jupyter/Google Colab notebooks Move most useful code to Python scripts
Cell mode vs. Script mode
Notebook 05 Part 1: Cell mode Notebook 05 Part 2: Script mode
e in to P y th on s c rip ts )
(turns useful cod
torchvision.transforms
torch.utils.data.Dataset
torch.save
torch.utils.data.DataLoader torchmetrics torch.load
torch.optim torch.nn torch.utils.tensorboard
torch.nn.Module n e d
Ea c h o f t h e s e c o u ld be t u r
torchvision.models
into a Python script!
See more: https://pytorch.org/tutorials/beginner/ptcheat.html
What we’re going to cover
(broadly)
• Transforming data for use with a model
• Loading custom data with pre-built functions
• Building FoodVision Mini to classify 🍕🥩🍣 images
• Turning useful notebook code (all of the above) into Python
scripts
• Training a PyTorch model from the command line
(we’ ll b e cook in g u p lo ts of co d e!)
How: 👩🍳 👩🔬
Let’s code!
Standard image classification data format
Your own data format
will depend on what
you’re working
The premi
se remain
write code s:
to get you
data into r
tensors fo
use with P r
yTorch