DL Pipeline and Tutorial
DL Pipeline and Tutorial
01 04
Define the Goal Feature Engineering
02 05
Collect Data and Modeling
Clean Data (Model Selection, Hyper-parameter
Optimization, Validation)
03 06
Exploratory Data Deployment
Analysis
3
Deep Learning Pipeline
6
Tuning Steps
Select model based on the nature of the data, and literature work, i.e.,
https://paperswithcode.com/ , conferences in different research domains.
8
Choosing the Optimizer
Start with the most popular optimizer for the type of problem at hand.
Adam, NAdam, Nesterov
How to fairly compare different optimizers?
• Performance and training speed.
• Tune the best hyper-parameters for the
optimizer!
9
Choosing the Batch Size
Why batch size is important? Training time = (time per step) x (total number of steps)
Practice: Often, the ideal batch size will be the largest batch size supported
by the available hardware.
How?
12
Build your first CNN model for Food
Image Classification
13
Step 1: Data Exploration
How to upload data to colab?
Option 1: GUI based, upload local file
#upload file from local system
from google.colab import files
uploades = files.upload()
14
Step 1: Data Exploration (cont.)
Pick a random image to check, print its original shape
import imageio
import os
import glob
from collections import Counter
import random
myseed = 12345 # set a random seed for reproducibility
16
Step 2: Data Loader
test_tfm = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor(),
]) If you want to scale images or crop the image (around the center),
and then transform it to tensor, you should also do it here.
17
Step 2: Data Loader
18
Step 3: Define a Neural Network Model
Let’s first look at a simple case:
If we have 32 * 32 * 3 channel image
We want to fit it into a fully connected model, with a hidden layer containing 512 neurons.
19
Step 3: Define a
Neural Network Model
21
Input Channel and Output Channel
Animation source: link
Input: 3 * 3 * 3 Output: 3 * 3 * 2
Kernel: 1 * 1 * 3 * 2 parameters
Stride = 1, Padding = 0
22
Step 4: Initialize the Train/validation Process
First, set up data loader
_exp_name = "sample”
batch_size = 64
_dataset_dir = "./food11"
# Construct datasets.
# The argument "loader" tells how torchvision reads the data.
train_set = FoodDataset(os.path.join(_dataset_dir,"training"), tfm=train_tfm)
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True,
num_workers=0, pin_memory=True)
valid_set = FoodDataset(os.path.join(_dataset_dir,"validation"),
tfm=test_tfm)
valid_loader = DataLoader(valid_set, batch_size=batch_size, shuffle=True,
num_workers=0, pin_memory=True)
23
Step 4: Initialize the Train/validation Process
Then set up hyper-parameters
# "cuda" only when GPUs are available.
device = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize optimizer, you may fine-tune some hyperparameters such as learning rate
on your own.
optimizer = torch.optim.Adam(model.parameters(), lr=0.0003, weight_decay=1e-5)
# Forward the data. (Make sure data and model are on the same device.)
logits = model(imgs.to(device))
# Gradients stored in the parameters in the previous step should be cleared out first.
optimizer.zero_grad()
27
How to improve?
A scientific approach to improving model
performance
• Identify an appropriately-scoped goal for the next round
of experiments. Sample goal: a new regularizer, preprocessing
choice, which hyper-parameter matters, which hyper-parameters
need to be re-tuned together, etc.
• Design and run a set of experiments that makes progress
towards this goal.
• Learn what we can from the results.
• Consider whether to launch the new best configuration.
28
Warning: we will give a few tricks for performance improvement, there are many more to explore
Preprocessing Choice for Image Classification:
Data Augmentation
Modify the image data so non-identical inputs are given to the model each
epoch, to prevent overfitting of the model.
Image source: link
29
Visit torchvision.transforms for a list of choices and their corresponding effect.
Data Augmentation
train_tfm = transforms.Compose([
# Data augmentation
transforms.RandomResizedCrop(128),
transforms.RandomHorizontalFlip(0.5),
If the best points cluster towards the edge of a search space (in some dimension), then the search space boundaries might
need to be expanded until the best observed point is no longer close to the boundary.
31
How to visualize running results for one pipeline?
Tutorial:https://pytorch.org/tutorials/recipes/recipes
/tensorboard_with_pytorch.html 32
Wandb: Weights & Biases (Tune + Visualize)
https://wandb.ai/site
33
Wandb + PyTorch (step 1)
First, you need to sign up for wandb to get an API token (free)
# use wandb for trial tracking and hyper-parameter tuning
import wandb
wandb.login(relogin=True)
34
Wandb + PyTorch (step 2)
Put the train and validation process in one train function, pass configuration values in
35
Wandb + PyTorch (step 3)
Start experiment:
36