Deep Learning Models -- A collection of various deep learning architectures, models, and tips for TensorFlow and PyTorch in Jupyter Notebooks.

In [1]:
%load_ext watermark
%watermark -a 'Sebastian Raschka' -v -p torch,torchvision
Sebastian Raschka 

CPython 3.7.1
IPython 7.4.0

torch 1.0.1.post2
torchvision 0.2.2

Model Zoo -- Torchvision Image Transformation Examples

In [2]:
import torch
import numpy as np
import PIL

from torch.utils.data import Dataset
from torch.utils.data import DataLoader

from torchvision import datasets
from torchvision import transforms

import matplotlib.pyplot as plt
from PIL import Image

RANDOM_SEED = 123
BATCH_SIZE = 10

if torch.cuda.is_available():
    torch.backends.cudnn.deterministic = True

1) No Augmentation

In [3]:
##########################
### CIFAR-10 Dataset
##########################


custom_transform = transforms.Compose([transforms.ToTensor()])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=True)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images1 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images1[i], (1, 2, 0)))
Files already downloaded and verified

2) Horizontal Flip

In [4]:
custom_transform = transforms.Compose([
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ToTensor()])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images2[i], (1, 2, 0)))

3) Random Rotation

In [5]:
custom_transform = transforms.Compose([
    transforms.RandomRotation(degrees=20,
                              resample=PIL.Image.BILINEAR),
    transforms.ToTensor()])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images2[i], (1, 2, 0)))

4) Normalize to [-1, 1] range

In [6]:
custom_transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(std=(0.5, 0.5, 0.5), 
                         mean=(0.5, 0.5, 0.5))
])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break
    
print('Min-Max:', images2[0].min(), images2[0].max())
Min-Max: tensor(-1.) tensor(1.)

5) Random Color Jitter

In [7]:
custom_transform = transforms.Compose([
    transforms.ColorJitter(brightness=(0.75, 1.25),
                           ),
    transforms.ToTensor(),
])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images2[i], (1, 2, 0)))
    
plt.show()
In [8]:
custom_transform = transforms.Compose([
    transforms.ColorJitter(contrast=(0.75, 1.25),
                           ),
    transforms.ToTensor(),
])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images2[i], (1, 2, 0)))
    
plt.show()
In [9]:
custom_transform = transforms.Compose([
    transforms.ColorJitter(saturation=(0.75, 1.25),
                           ),
    transforms.ToTensor(),
])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images2[i], (1, 2, 0)))
    
plt.show()
In [10]:
custom_transform = transforms.Compose([
    transforms.ColorJitter(hue=(-0.1, 0.1),
                           ),
    transforms.ToTensor(),
])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images2[i], (1, 2, 0)))
    
plt.show()

6) Resize and Random Cropt

In [13]:
custom_transform = transforms.Compose([
    transforms.Resize((38, 38)),
    transforms.RandomCrop((32, 32)),
    transforms.ToTensor(),
])


# Note transforms.ToTensor() scales input images
# to 0-1 range
train_dataset = datasets.CIFAR10(root='data', 
                                 train=True, 
                                 transform=custom_transform,
                                 download=False)

train_loader = DataLoader(dataset=train_dataset, 
                          batch_size=BATCH_SIZE, 
                          num_workers=1,
                          shuffle=True)

torch.manual_seed(RANDOM_SEED)
for images, labels in train_loader:  
    images2 = images
    break

fig, axs = plt.subplots(1, 5, figsize=(9, 3), sharey=True)
for i in range(5):
    axs[i].imshow(np.transpose(images2[i], (1, 2, 0)))
    
plt.show()