#!/usr/bin/env python # coding: utf-8 # Deep Learning Models -- A collection of various deep learning architectures, models, and tips for TensorFlow and PyTorch in Jupyter Notebooks. # - Author: Sebastian Raschka # - GitHub Repository: https://github.com/rasbt/deeplearning-models # # Model Zoo -- Transfer Learning Example (VGG-16) # In[1]: get_ipython().run_line_magic('load_ext', 'watermark') get_ipython().run_line_magic('watermark', "-a 'Sebastian Raschka' -v -p torch,torchvision") # In[2]: import torch import time import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from torchvision import datasets from torchvision import transforms from torch.utils.data import DataLoader ####################################### ### PRE-TRAINED MODELS AVAILABLE HERE ## https://pytorch.org/docs/stable/torchvision/models.html from torchvision import models ####################################### if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True # ### Loading an Example Dataset # In this example, we are going to work with CIFAR-10, because you are familiar with it and it is easier (smaller) than ImageNet. However, note that in a "real-world application", images with dimension > 224x224 are recommended. Here, we resize the images as a workaround # # - Note that due to the average pooling in the final layer, it is also possible to feed in 32x32-pixel images directly. However, I noticed that the performance is rather low (~65% test accuracy after 10 and 100 epochs). # In[3]: ########################## ### SETTINGS ########################## # Device DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print('Device:', DEVICE) NUM_CLASSES = 10 # Hyperparameters random_seed = 1 learning_rate = 0.0001 num_epochs = 10 batch_size = 128 ########################## ### MNIST DATASET ########################## custom_transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) ## Note that this particular normalization scheme is ## necessary since it was used for pre-training ## the network on ImageNet. ## These are the channel-means and standard deviations ## for z-score normalization. train_dataset = datasets.CIFAR10(root='data', train=True, transform=custom_transform, download=True) test_dataset = datasets.CIFAR10(root='data', train=False, transform=custom_transform) train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, num_workers=8, shuffle=True) test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, num_workers=8, shuffle=False) # Checking the dataset for images, labels in train_loader: print('Image batch dimensions:', images.shape) print('Image label dimensions:', labels.shape) break # ## Loading the Pre-Trained Model # In[4]: model = models.vgg16(pretrained=True) model # ## Freezing the Model # In[5]: for param in model.parameters(): param.requires_grad = False # Assume we want to train the penultimate layer: # In[6]: model.classifier[3].requires_grad = True # Now, replace the output layer with your own output layer (here, we actually add two more output layers): # In[7]: model.classifier[6] = nn.Sequential( nn.Linear(4096, 512), nn.ReLU(), nn.Dropout(0.5), nn.Linear(512, NUM_CLASSES)) # ## Training (as usual) # In[8]: model = model.to(DEVICE) optimizer = torch.optim.Adam(model.parameters()) # In[9]: def compute_accuracy(model, data_loader): model.eval() correct_pred, num_examples = 0, 0 for i, (features, targets) in enumerate(data_loader): features = features.to(DEVICE) targets = targets.to(DEVICE) logits = model(features) _, predicted_labels = torch.max(logits, 1) num_examples += targets.size(0) correct_pred += (predicted_labels == targets).sum() return correct_pred.float()/num_examples * 100 def compute_epoch_loss(model, data_loader): model.eval() curr_loss, num_examples = 0., 0 with torch.no_grad(): for features, targets in data_loader: features = features.to(DEVICE) targets = targets.to(DEVICE) logits = model(features) loss = F.cross_entropy(logits, targets, reduction='sum') num_examples += targets.size(0) curr_loss += loss curr_loss = curr_loss / num_examples return curr_loss start_time = time.time() for epoch in range(num_epochs): model.train() for batch_idx, (features, targets) in enumerate(train_loader): features = features.to(DEVICE) targets = targets.to(DEVICE) ### FORWARD AND BACK PROP logits = model(features) cost = F.cross_entropy(logits, targets) optimizer.zero_grad() cost.backward() ### UPDATE MODEL PARAMETERS optimizer.step() ### LOGGING if not batch_idx % 50: print ('Epoch: %03d/%03d | Batch %04d/%04d | Cost: %.4f' %(epoch+1, num_epochs, batch_idx, len(train_loader), cost)) model.eval() with torch.set_grad_enabled(False): # save memory during inference print('Epoch: %03d/%03d | Train: %.3f%% | Loss: %.3f' % ( epoch+1, num_epochs, compute_accuracy(model, train_loader), compute_epoch_loss(model, train_loader))) print('Time elapsed: %.2f min' % ((time.time() - start_time)/60)) print('Total Training Time: %.2f min' % ((time.time() - start_time)/60)) # In[10]: with torch.set_grad_enabled(False): # save memory during inference print('Test accuracy: %.2f%%' % (compute_accuracy(model, test_loader))) # In[11]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt # In[12]: classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') for batch_idx, (features, targets) in enumerate(test_loader): features = features targets = targets break logits = model(features.to(DEVICE)) _, predicted_labels = torch.max(logits, 1) # In[13]: def unnormalize(tensor, mean, std): for t, m, s in zip(tensor, mean, std): t.mul_(s).add_(m) return tensor n_images = 10 fig, axes = plt.subplots(nrows=1, ncols=n_images, sharex=True, sharey=True, figsize=(20, 2.5)) orig_images = features[:n_images] for i in range(n_images): curr_img = orig_images[i].detach().to(torch.device('cpu')) curr_img = unnormalize(curr_img, torch.tensor([0.485, 0.456, 0.406]), torch.tensor([0.229, 0.224, 0.225])) curr_img = curr_img.permute((1, 2, 0)) axes[i].imshow(curr_img) axes[i].set_title(classes[predicted_labels[i]])