mirror of
https://github.com/mrdbourke/pytorch-deep-learning.git
synced 2026-11-27 06:07:09 +00:00
1.8 MiB
1.8 MiB
In [54]:
# 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, "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 --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu113
import torch
import torchvision
print(f"torch version: {torch.__version__}")
print(f"torchvision version: {torchvision.__version__}")torch version: 1.12.0+cu113 torchvision version: 0.13.0+cu113
In [55]:
# 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_curvesIn [56]:
# Setup device agnostic code
device = "cuda" if torch.cuda.is_available() else "cpu"
deviceOut [56]:
'cuda'
In [57]:
# 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.zip",
destination="pizza_steak_sushi")
image_pathOut [57]:
[INFO] data/pizza_steak_sushi directory exists, skipping download.
PosixPath('data/pizza_steak_sushi')In [58]:
# Setup directory paths to train and test images
train_dir = image_path / "train"
test_dir = image_path / "test"In [59]:
train_dir, test_dirOut [59]:
(PosixPath('data/pizza_steak_sushi/train'),
PosixPath('data/pizza_steak_sushi/test'))In [60]:
from torchvision import transforms
from going_modular.going_modular import data_setup
# Create image size
IMG_SIZE = 224 # comes from Table 3 of the ViT paper
# Create transforms pipeline
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 [61]:
# Create a batch size of 32 (gthe paper uses 4096 but this may be too big for our smaller hardware... can always scale up later)
BATCH_SIZE = 32
# Create DataLoaders
train_dataloader, test_dataloader, class_names = data_setup.create_dataloaders(
train_dir=train_dir,
test_dir=test_dir,
transform=manual_transforms,
batch_size=BATCH_SIZE
)
len(train_dataloader), len(test_dataloader), class_namesOut [61]:
(8, 3, ['pizza', 'steak', 'sushi'])
In [62]:
# Get a batch of images
image_batch, label_batch = next(iter(train_dataloader))
# Get a single image and label from the batch
image, label = image_batch[0], label_batch[0]
# View the single image and label shapes
image.shape, labelOut [62]:
(torch.Size([3, 224, 224]), tensor(0))
In [63]:
# Plot the image with matplotlib
import matplotlib.pyplot as plt
plt.imshow(image.permute(1, 2, 0)) # (color_channels, height, width) -> (height, width, color_channels)
plt.title(class_names[label])
plt.axis(False);In [64]:
# Create example values
height = 224
width = 224
color_channels = 3
patch_size = 16
# Calculate the number of patches
number_of_patches = int((height * width) / patch_size**2)
number_of_patchesOut [64]:
196
In [65]:
# Input shape
embedding_layer_input_shape = (height, width, color_channels)
# Output shape
embedding_layer_output_shape = (number_of_patches, patch_size**2 * color_channels)
print(f"Input shape (single 2D image): {embedding_layer_input_shape}")
print(f"Output shape (single 1D sequence of patches): {embedding_layer_output_shape} -> (number_of_patches, embedding_dimension)")Input shape (single 2D image): (224, 224, 3) Output shape (single 1D sequence of patches): (196, 768) -> (number_of_patches, embedding_dimension)
In [66]:
# View a single image
plt.imshow(image.permute(1, 2, 0))
plt.title(class_names[label])
plt.axis(False);In [67]:
image.shapeOut [67]:
torch.Size([3, 224, 224])
In [68]:
# Get the top row of the image
image_permuted = image.permute(1, 2, 0) # convert image to color channels last (H, W, C)
# Index to plot the top row of pixels
patch_size = 16
plt.figure(figsize=(patch_size, patch_size))
plt.imshow(image_permuted[:patch_size, :, :])Out [68]:
<matplotlib.image.AxesImage at 0x7fc62c2838d0>
In [69]:
# Setup code to plot top row as patches
img_size = 224
patch_size = 16
num_patches = img_size/patch_size
assert img_size % patch_size == 0, "Image size must be divisible by patch size"
print(f"Number of patches per row: {num_patches}\nPatch size: {patch_size} pixels x {patch_size} pixels")
# Create a series of subplots
fig, axs = plt.subplots(nrows=1,
ncols=img_size // patch_size, # one column for each patch
sharex=True,
sharey=True,
figsize=(patch_size, patch_size))
# Iterate through number of patches in the top row
for i, patch in enumerate(range(0, img_size, patch_size)):
axs[i].imshow(image_permuted[:patch_size, patch:patch+patch_size, :]);
axs[i].set_xlabel(i+1) # set the patch label
axs[i].set_xticks([])
axs[i].set_yticks([])Number of patches per row: 14.0 Patch size: 16 pixels x 16 pixels
In [70]:
# Setup code to plot whole image as patches
img_size = 224
patch_size = 16
num_patches = img_size/patch_size
assert img_size % patch_size == 0, "Image size must be divisible by patch size"
print(f"Number of patches per row: {num_patches}\
\nNumber of patches per column: {num_patches}\
\nTotal patches: {num_patches*num_patches}\
\nPatch size: {patch_size} pixels x {patch_size} pixels")
# Create a series of subplots
fig, axs = plt.subplots(nrows=img_size // patch_size,
ncols=img_size // patch_size,
figsize=(num_patches, num_patches),
sharex=True,
sharey=True)
# Loop through height and width of image
for i, patch_height in enumerate(range(0, img_size, patch_size)): # iterate through height
for j, patch_width in enumerate(range(0, img_size, patch_size)):
# Plot the permuted image on the different axes
axs[i, j].imshow(image_permuted[patch_height:patch_height+patch_size, # iterate through height
patch_width:patch_width+patch_size, # iterate through width
:]) # get all color channels
# Set up label information for each subplot (patch)
axs[i, j].set_ylabel(i+1,
rotation="horizontal",
horizontalalignment="right",
verticalalignment="center")
axs[i, j].set_xlabel(j+1)
axs[i, j].set_xticks([])
axs[i, j].set_yticks([])
axs[i, j].label_outer()
# Set up a title for the plot
fig.suptitle(f"{class_names[label]} -> Patchified", fontsize=14)
plt.show()Number of patches per row: 14.0 Number of patches per column: 14.0 Total patches: 196.0 Patch size: 16 pixels x 16 pixels
In [71]:
# Create conv2d layer to turn image into patches of learnable feature maps (embeddings)
from torch import nn
# Set the patch size
patch_size = 16
# Create a conv2d lyaer with hyperparameters from the ViT paper
conv2d = nn.Conv2d(in_channels=3, # for color images
out_channels=768, # D size from Table 1 for ViT-Base
kernel_size=patch_size,
stride=patch_size,
padding=0)
conv2dOut [71]:
Conv2d(3, 768, kernel_size=(16, 16), stride=(16, 16))
In [72]:
# View single image
plt.imshow(image.permute(1, 2, 0))
plt.title(class_names[label])
plt.axis(False);In [73]:
# Pass the image through the convolutional layer
image_out_of_conv = conv2d(image.unsqueeze(0)) # add batch dimension -> (batch_size, color_channels, height, width)
print(image_out_of_conv.shape) torch.Size([1, 768, 14, 14])
In [74]:
image_out_of_conv.requires_gradOut [74]:
True
In [75]:
# plot random convolutional feature maps (embeddings)
import random
random_indexes = random.sample(range(0, 758), k=5)
print(f"Showing random convolutional feature maps from indexes: {random_indexes}")
# Create plot
fig, axs = plt.subplots(nrows=1, ncols=5, figsize=(12, 12))
# Plot random image feature maps
for i, idx in enumerate(random_indexes):
image_conv_feature_map = image_out_of_conv[:, idx, :, :] # index on the output tensor of the conv2d layer
axs[i].imshow(image_conv_feature_map.squeeze().detach().numpy()) # remove batch dimension, and remove from grad tracking/switch to numpy for matplotlib
axs[i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])Showing random convolutional feature maps from indexes: [507, 71, 166, 241, 272]
In [76]:
# Get a single feature map in tensor form
single_feature_map = image_out_of_conv[:, 0, :, :]
single_feature_map, single_feature_map.requires_gradOut [76]:
(tensor([[[ 0.1407, 0.1090, 0.1021, 0.1104, 0.0994, -0.0023, 0.0833,
0.0281, 0.0831, 0.0941, 0.0986, 0.0203, 0.0206, -0.0152],
[ 0.0804, 0.1664, 0.1979, -0.0078, -0.3030, -0.0536, -0.2039,
-0.0388, -0.0347, -0.1243, -0.1810, -0.0476, 0.1051, 0.0399],
[-0.0716, 0.0355, -0.0671, -0.0396, -0.2541, -0.1525, -0.1335,
-0.1575, 0.0895, -0.0103, -0.1282, -0.0114, -0.0942, -0.0481],
[-0.1791, -0.1164, -0.1725, -0.0481, -0.0625, -0.0877, -0.1679,
-0.0722, -0.1618, -0.0711, -0.0335, 0.1048, 0.1567, 0.1100],
[ 0.0778, -0.2123, -0.0797, -0.1068, -0.0471, -0.0960, 0.0561,
-0.0643, -0.0510, -0.0651, -0.0275, -0.0313, -0.1188, -0.0143],
[-0.1055, -0.0940, 0.0829, -0.0087, -0.0778, -0.1851, -0.1613,
0.1131, 0.0694, -0.1660, -0.2467, 0.0015, 0.1099, 0.1271],
[-0.0018, 0.0883, -0.0462, -0.0074, 0.0591, -0.1006, -0.0257,
-0.2168, -0.1621, 0.1588, 0.0712, 0.0631, -0.1775, -0.1007],
[ 0.0357, -0.0580, -0.1732, -0.2045, -0.0843, -0.1670, -0.0618,
0.0191, 0.0565, -0.1508, 0.0103, 0.0300, 0.1253, 0.0356],
[-0.0979, -0.1481, -0.1128, 0.1911, 0.1118, 0.1514, -0.1875,
-0.2145, 0.0742, 0.0809, 0.2035, 0.0928, 0.1184, 0.1298],
[-0.0954, -0.1065, -0.1638, -0.0156, 0.0218, -0.1327, -0.1192,
-0.1086, -0.1809, -0.0102, -0.1456, 0.0052, -0.0445, 0.0401],
[-0.0196, 0.0171, 0.0129, -0.1425, -0.0151, -0.0344, -0.0826,
-0.0653, -0.1250, 0.0047, -0.0196, -0.0445, 0.0677, 0.0581],
[-0.0221, -0.0131, -0.0670, -0.1478, -0.0123, 0.0458, 0.0050,
-0.0714, -0.0824, -0.0079, -0.0218, 0.0119, -0.1344, -0.0957],
[-0.1727, -0.0127, -0.0364, -0.0090, -0.0312, -0.0084, 0.0170,
-0.0113, -0.1140, -0.1092, -0.1072, -0.0010, 0.0080, -0.1315],
[-0.1228, -0.1214, -0.1354, -0.0643, -0.1033, -0.1008, -0.1099,
-0.0781, -0.0485, -0.1678, -0.1026, -0.0602, -0.0902, 0.0780]]],
grad_fn=<SliceBackward0>), True)In [77]:
print(f"{image_out_of_conv.shape} -> (batch_size, embedding_dim, feature_map_height, feature_map_width)")torch.Size([1, 768, 14, 14]) -> (batch_size, embedding_dim, feature_map_height, feature_map_width)
In [78]:
from torch import nn
flatten_layer = nn.Flatten(start_dim=2,
end_dim=3)
flatten_layer(image_out_of_conv).shapeOut [78]:
torch.Size([1, 768, 196])
In [79]:
# Put everything together
plt.imshow(image.permute(1, 2, 0))
plt.title(class_names[label])
plt.axis(False)
print(f"Original image shape: {image.shape}")
# Turn image into feature maps
image_out_of_conv = conv2d(image.unsqueeze(0)) # add batch dimension
print(f"Image feature map (patches) shape: {image_out_of_conv.shape}")
# Flatten the feature maps
image_out_of_conv_flattened = flatten_layer(image_out_of_conv)
print(f"Flattened image feature map shape: {image_out_of_conv_flattened.shape}")Original image shape: torch.Size([3, 224, 224]) Image feature map (patches) shape: torch.Size([1, 768, 14, 14]) Flattened image feature map shape: torch.Size([1, 768, 196])
In [80]:
# Rearrange output of flattened layer
image_out_of_conv_flattened_permuted = image_out_of_conv_flattened.permute(0, 2, 1)
print(f"{image_out_of_conv_flattened_permuted.shape} -> (batch_size, number_of_patches, embedding_dimension)")torch.Size([1, 196, 768]) -> (batch_size, number_of_patches, embedding_dimension)
In [81]:
# Get a single flattened feature map
single_flattened_feature_map = image_out_of_conv_flattened_permuted[:, :, 0]
# Plot the flattened feature map visually
plt.figure(figsize=(22, 22))
plt.imshow(single_flattened_feature_map.detach().numpy())
plt.title(f"Flattened feature map shape: {single_flattened_feature_map.shape}")
plt.axis(False); In [82]:
# 1. Create a class called PatchEmbedding
class PatchEmbedding(nn.Module):
# 2. Initilaize the layer with appropriate hyperparameters
def __init__(self,
in_channels:int=3,
patch_size:int=16,
embedding_dim:int=768): # from Table 1 for ViT-Base
super().__init__()
self.patch_size = patch_size
# 3. Create a layer to turn an image into embedded patches
self.patcher = nn.Conv2d(in_channels=in_channels,
out_channels=embedding_dim,
kernel_size=patch_size,
stride=patch_size,
padding=0)
# 4. Create a layer to flatten feature map outputs of Conv2d
self.flatten = nn.Flatten(start_dim=2,
end_dim=3)
# 5. Define a forward method to define the forward computation steps
def forward(self, x):
# Create assertion to check that inputs are the correct shape
image_resolution = x.shape[-1]
assert image_resolution % patch_size == 0, f"Input image size must be divisible by patch size, image shape: {image_resolution}, patch size: {self.patch_size}"
# Perform the forward pass
x_patched = self.patcher(x)
x_flattened = self.flatten(x_patched)
# 6. Make the returned sequence embedding dimensions are in the right order (batch_size, number_of_patches, embedding_dimension)
return x_flattened.permute(0, 2, 1)
In [83]:
set_seeds()
# Create an instance of patch embedding layer
patchify = PatchEmbedding(in_channels=3,
patch_size=16,
embedding_dim=768)
# Pass a single image through patch embedding layer
print(f"Input image size: {image.unsqueeze(0).shape}")
patch_embedded_image = patchify(image.unsqueeze(0)) # add an extra batch dimension
print(f"Output patch embedding sequence shape: {patch_embedded_image.shape}")Input image size: torch.Size([1, 3, 224, 224]) Output patch embedding sequence shape: torch.Size([1, 196, 768])
In [84]:
rand_image_tensor = torch.randn(1, 3, 224, 224)
rand_image_tensor_bad = torch.randn(1, 3, 250, 250)
# patchify(rand_image_tensor_bad)In [85]:
patch_embedded_imageOut [85]:
tensor([[[-0.7665, 0.1767, -0.2405, ..., 0.6293, -0.3423, 0.3247],
[-0.7299, 0.1911, -0.1953, ..., 0.5647, -0.3340, 0.2806],
[-0.7201, 0.1827, -0.1611, ..., 0.5443, -0.3373, 0.2574],
...,
[-0.5255, 0.0765, -0.0469, ..., 0.3121, -0.3001, 0.2329],
[-0.4847, 0.1029, -0.0593, ..., 0.2676, -0.2530, 0.0827],
[-0.2441, 0.1201, -0.3067, ..., 0.2972, -0.1575, 0.0815]]],
grad_fn=<PermuteBackward0>)In [86]:
# Get the batch size and embedding dimension
batch_size = patch_embedded_image.shape[0]
embedding_dimension = patch_embedded_image.shape[-1]
batch_size, embedding_dimensionOut [86]:
(1, 768)
In [87]:
# Create class token embedding as a learnable parameter that shares the same size as the embedding dimension (D)
class_token = nn.Parameter(torch.ones(batch_size, 1, embedding_dimension),
requires_grad=True)
class_token.shapeOut [87]:
torch.Size([1, 1, 768])
In [88]:
patch_embedded_image.shapeOut [88]:
torch.Size([1, 196, 768])
In [89]:
# Add the class token embedding to the front of the patch embedding
patch_embedded_image_with_class_embedding = torch.cat((class_token, patch_embedded_image),
dim=1) # number_of_patches dimension
print(patch_embedded_image_with_class_embedding)
print(f"Sequence of patch embeddings with class token prepended shape: {patch_embedded_image_with_class_embedding.shape} -> (batch_size, class_token + number_of_patches, embedding_dim)")tensor([[[ 1.0000, 1.0000, 1.0000, ..., 1.0000, 1.0000, 1.0000],
[-0.7665, 0.1767, -0.2405, ..., 0.6293, -0.3423, 0.3247],
[-0.7299, 0.1911, -0.1953, ..., 0.5647, -0.3340, 0.2806],
...,
[-0.5255, 0.0765, -0.0469, ..., 0.3121, -0.3001, 0.2329],
[-0.4847, 0.1029, -0.0593, ..., 0.2676, -0.2530, 0.0827],
[-0.2441, 0.1201, -0.3067, ..., 0.2972, -0.1575, 0.0815]]],
grad_fn=<CatBackward0>)
Sequence of patch embeddings with class token prepended shape: torch.Size([1, 197, 768]) -> (batch_size, class_token + number_of_patches, embedding_dim)
In [90]:
# Calculate N (number_of_patches)
number_of_patches = int((height * width) / patch_size**2)
# Get the embedding dimension
embedding_dimension = patch_embedded_image_with_class_embedding.shape[-1]
# Create the learnable 1D position embedding
position_embedding = nn.Parameter(torch.ones(1,
number_of_patches+1,
embedding_dimension),
requires_grad=True)
position_embedding, position_embedding.shapeOut [90]:
(Parameter containing:
tensor([[[1., 1., 1., ..., 1., 1., 1.],
[1., 1., 1., ..., 1., 1., 1.],
[1., 1., 1., ..., 1., 1., 1.],
...,
[1., 1., 1., ..., 1., 1., 1.],
[1., 1., 1., ..., 1., 1., 1.],
[1., 1., 1., ..., 1., 1., 1.]]], requires_grad=True),
torch.Size([1, 197, 768]))In [91]:
# View the sequence of patch embeddings with the prepended class embedding
patch_embedded_image_with_class_embedding, patch_embedded_image_with_class_embedding.shapeOut [91]:
(tensor([[[ 1.0000, 1.0000, 1.0000, ..., 1.0000, 1.0000, 1.0000],
[-0.7665, 0.1767, -0.2405, ..., 0.6293, -0.3423, 0.3247],
[-0.7299, 0.1911, -0.1953, ..., 0.5647, -0.3340, 0.2806],
...,
[-0.5255, 0.0765, -0.0469, ..., 0.3121, -0.3001, 0.2329],
[-0.4847, 0.1029, -0.0593, ..., 0.2676, -0.2530, 0.0827],
[-0.2441, 0.1201, -0.3067, ..., 0.2972, -0.1575, 0.0815]]],
grad_fn=<CatBackward0>), torch.Size([1, 197, 768]))In [92]:
# Add the position embedding to the patch and class token embedding
patch_and_position_embedding = patch_embedded_image_with_class_embedding + position_embedding
print(patch_and_position_embedding)
print(f"Patch and position embedding shape: {patch_and_position_embedding.shape}")tensor([[[2.0000, 2.0000, 2.0000, ..., 2.0000, 2.0000, 2.0000],
[0.2335, 1.1767, 0.7595, ..., 1.6293, 0.6577, 1.3247],
[0.2701, 1.1911, 0.8047, ..., 1.5647, 0.6660, 1.2806],
...,
[0.4745, 1.0765, 0.9531, ..., 1.3121, 0.6999, 1.2329],
[0.5153, 1.1029, 0.9408, ..., 1.2676, 0.7470, 1.0827],
[0.7559, 1.1201, 0.6933, ..., 1.2972, 0.8425, 1.0815]]],
grad_fn=<AddBackward0>)
Patch and position embedding shape: torch.Size([1, 197, 768])
In [93]:
# Set seeds
set_seeds()
# 1. Set the patch size
patch_size = 16
# 2. Print shapes of the original image tensor and get the image dimensions
print(f"Image tensor shape: {image.shape}")
height, width = image.shape[1], image.shape[2]
# 3. Get image tensor and add a batch dimension
x = image.unsqueeze(0)
print(f"Input image shape: {x.shape}")
# 4. Create patch embedding layer
patch_embedding_layer = PatchEmbedding(in_channels=3,
patch_size=patch_size,
embedding_dim=768)
# 5. Pass input image through PatchEmbedding
patch_embedding = patch_embedding_layer(x)
print(f"Patch embedding shape: {patch_embedding.shape}")
# 6. Create class token embedding
batch_size = patch_embedding.shape[0]
embedding_dimension = patch_embedding.shape[-1]
class_token = nn.Parameter(torch.ones(batch_size, 1, embedding_dimension),
requires_grad=True) # make sure it's learnable
print(f"Class token embedding shape: {class_token.shape}")
# 7. Prepend the class token embedding to patch embedding
patch_embedding_class_token = torch.cat((class_token, patch_embedding), dim=1)
print(f"Patch embedding with class token shape: {patch_embedding_class_token.shape}")
# 8. Create position embedding
number_of_patches = int((height*width) / patch_size**2)
position_embedding = nn.Parameter(torch.ones(1, number_of_patches+1, embedding_dimension),
requires_grad=True)
# 9. Add the position embedding to patch embedding with class token
patch_and_position_embedding = patch_embedding_class_token + position_embedding
print(f"Patch and position embedding shape: {patch_and_position_embedding.shape} ")Image tensor shape: torch.Size([3, 224, 224]) Input image shape: torch.Size([1, 3, 224, 224]) Patch embedding shape: torch.Size([1, 196, 768]) Class token embedding shape: torch.Size([1, 1, 768]) Patch embedding with class token shape: torch.Size([1, 197, 768]) Patch and position embedding shape: torch.Size([1, 197, 768])
In [94]:
class MultiHeadSelfAttentionBlock(nn.Module):
"""Creates a multi-head self-attention block ("MSA block" for short).
"""
def __init__(self,
embedding_dim:int=768, # Hidden size D (embedding dimension) from Table 1 for ViT-Base
num_heads:int=12, # Heads from Table 1 for ViT-Base
attn_dropout:int=0):
super().__init__()
# Create the norm layer (LN)
self.layer_norm = nn.LayerNorm(normalized_shape=embedding_dim)
# Create multihead attention (MSA) layer
self.multihead_attn = nn.MultiheadAttention(embed_dim=embedding_dim,
num_heads=num_heads,
dropout=attn_dropout,
batch_first=True) # is the batch first? (batch, seq, feature) -> (batch, number_of_patches, embedding_dimension)
def forward(self, x):
x = self.layer_norm(x)
attn_output, _ = self.multihead_attn(query=x,
key=x,
value=x,
need_weights=False)
return attn_outputIn [95]:
# Create an instance MSA block
multihead_self_attention_block = MultiHeadSelfAttentionBlock(embedding_dim=768,
num_heads=12,
attn_dropout=0)
# Pass the patch and position image embedding sequence through MSA block
patched_image_through_msa_block = multihead_self_attention_block(patch_and_position_embedding)
print(f"Input shape of MSA block: {patch_and_position_embedding.shape}")
print(f"Output shape of MSA block: {patched_image_through_msa_block.shape}")Input shape of MSA block: torch.Size([1, 197, 768]) Output shape of MSA block: torch.Size([1, 197, 768])
In [96]:
class MLPBlock(nn.Module):
def __init__(self,
embedding_dim:int=768,
mlp_size:int=3072,
dropout:int=0.1):
super().__init__()
# Create the norm layer (LN)
self.layer_norm = nn.LayerNorm(normalized_shape=embedding_dim)
# Create the MLP
self.mlp = nn.Sequential(
nn.Linear(in_features=embedding_dim,
out_features=mlp_size),
nn.GELU(),
nn.Dropout(p=dropout),
nn.Linear(in_features=mlp_size,
out_features=embedding_dim),
nn.Dropout(p=dropout)
)
def forward(self, x):
x = self.layer_norm(x)
x = self.mlp(x)
return x
# return self.mlp(self.layer_norm(x)) # same as above In [97]:
# Create an instance of MLPBlock
mlp_block = MLPBlock(embedding_dim=768,
mlp_size=3072,
dropout=0.1)
# Pass output the MSABlock through MLPBlock
patched_image_through_mlp_block = mlp_block(patched_image_through_msa_block)
print(f"Input shape of MLP block: {patched_image_through_msa_block.shape}")
print(f"Output shape of MLP block: {patched_image_through_mlp_block.shape}")Input shape of MLP block: torch.Size([1, 197, 768]) Output shape of MLP block: torch.Size([1, 197, 768])
In [98]:
class TransformerEncoderBlock(nn.Module):
def __init__(self,
embedding_dim:int=768, # Hidden size D from table 1, 768 for ViT-Base
num_heads:int=12, # from table 1
mlp_size:int=3072, # from table 1
mlp_dropout:int=0.1, # from table 3
attn_dropout:int=0):
super().__init__()
# Create MSA block (equation 2)
self.msa_block = MultiHeadSelfAttentionBlock(embedding_dim=embedding_dim,
num_heads=num_heads,
attn_dropout=attn_dropout)
# Create MLP block (equation 3)
self.mlp_block = MLPBlock(embedding_dim=embedding_dim,
mlp_size=mlp_size,
dropout=mlp_dropout)
def forward(self, x):
x = self.msa_block(x) + x # residual/skip connection for equation 2
x = self.mlp_block(x) + x # residual/skip connection for equation 3
return x In [99]:
# Create an instance of TransformerEncoderBlock()
transformer_encoder_block = TransformerEncoderBlock()
# Get a summary using torchinfo.summary
summary(model=transformer_encoder_block,
input_size=(1, 197, 768), # (batch_size, number_of_patches, embedding_dimension)
col_names=["input_size", "output_size", "num_params", "trainable"],
col_width=20,
row_settings=["var_names"])Out [99]:
============================================================================================================================================ Layer (type (var_name)) Input Shape Output Shape Param # Trainable ============================================================================================================================================ TransformerEncoderBlock (TransformerEncoderBlock) [1, 197, 768] [1, 197, 768] -- True ├─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] -- True │ └─LayerNorm (layer_norm) [1, 197, 768] [1, 197, 768] 1,536 True │ └─MultiheadAttention (multihead_attn) -- [1, 197, 768] 1,771,776 True │ │ └─NonDynamicallyQuantizableLinear (out_proj) -- -- 590,592 True ├─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] -- True │ └─LayerNorm (layer_norm) [1, 197, 768] [1, 197, 768] 1,536 True │ └─Sequential (mlp) [1, 197, 768] [1, 197, 768] -- True │ │ └─Linear (0) [1, 197, 768] [1, 197, 3072] 2,362,368 True │ │ └─GELU (1) [1, 197, 3072] [1, 197, 3072] -- -- │ │ └─Dropout (2) [1, 197, 3072] [1, 197, 3072] -- -- │ │ └─Linear (3) [1, 197, 3072] [1, 197, 768] 2,360,064 True │ │ └─Dropout (4) [1, 197, 768] [1, 197, 768] -- -- ============================================================================================================================================ Total params: 7,087,872 Trainable params: 7,087,872 Non-trainable params: 0 Total mult-adds (M): 4.73 ============================================================================================================================================ Input size (MB): 0.61 Forward/backward pass size (MB): 8.47 Params size (MB): 21.26 Estimated Total Size (MB): 30.34 ============================================================================================================================================
In [100]:
# Create the same as above with torch.nn.TransformerEncoderLayer()
torch_transformer_encoder_layer = nn.TransformerEncoderLayer(d_model=768, # embedding size from table 1
nhead=12, # heads from table 1
dim_feedforward=3072, # MLP size from table
dropout=0.1,
activation="gelu",
batch_first=True,
norm_first=True)
torch_transformer_encoder_layerOut [100]:
TransformerEncoderLayer(
(self_attn): MultiheadAttention(
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True)
)
(linear1): Linear(in_features=768, out_features=3072, bias=True)
(dropout): Dropout(p=0.1, inplace=False)
(linear2): Linear(in_features=3072, out_features=768, bias=True)
(norm1): LayerNorm((768,), eps=1e-05, elementwise_affine=True)
(norm2): LayerNorm((768,), eps=1e-05, elementwise_affine=True)
(dropout1): Dropout(p=0.1, inplace=False)
(dropout2): Dropout(p=0.1, inplace=False)
)In [101]:
# Get a summary using torchinfo.summary
summary(model=torch_transformer_encoder_layer,
input_size=(1, 197, 768), # (batch_size, number_of_patches, embedding_dimension)
col_names=["input_size", "output_size", "num_params", "trainable"],
col_width=20,
row_settings=["var_names"])Out [101]:
================================================================================================================================== Layer (type (var_name)) Input Shape Output Shape Param # Trainable ================================================================================================================================== TransformerEncoderLayer (TransformerEncoderLayer) [1, 197, 768] [1, 197, 768] 3,072 True ├─LayerNorm (norm1) [1, 197, 768] [1, 197, 768] (recursive) True ├─MultiheadAttention (self_attn) [1, 197, 768] [1, 197, 768] 2,362,368 True ├─Dropout (dropout1) [1, 197, 768] [1, 197, 768] -- -- ├─LayerNorm (norm2) [1, 197, 768] [1, 197, 768] (recursive) True ├─Linear (linear1) [1, 197, 768] [1, 197, 3072] 2,362,368 True ├─LayerNorm (norm1) [1, 197, 768] [1, 197, 768] (recursive) True ├─LayerNorm (norm2) [1, 197, 768] [1, 197, 768] (recursive) True ├─Dropout (dropout) [1, 197, 3072] [1, 197, 3072] -- -- ├─Dropout (dropout2) [1, 197, 768] [1, 197, 768] -- -- ├─Linear (linear2) [1, 197, 3072] [1, 197, 768] 2,360,064 True ├─Dropout (dropout2) [1, 197, 768] [1, 197, 768] -- -- ================================================================================================================================== Total params: 7,087,872 Trainable params: 7,087,872 Non-trainable params: 0 Total mult-adds (M): 4.73 ================================================================================================================================== Input size (MB): 0.61 Forward/backward pass size (MB): 6.05 Params size (MB): 18.89 Estimated Total Size (MB): 25.55 ==================================================================================================================================
In [102]:
# Create a ViT class
class ViT(nn.Module):
def __init__(self,
img_size:int=224, # Table 3 from the ViT paper
in_channels:int=3,
patch_size:int=16,
num_transformer_layers:int=12, # Table 1 for "Layers" for ViT-Base
embedding_dim:int=768, # Hidden size D from Table 1 for ViT-Base
mlp_size:int=3072, # Table 1
num_heads:int=12, # Table 1
attn_dropout:int=0,
mlp_dropout:int=0.1,
embedding_dropout:int=0.1, # Dropout for patch and position embeddings
num_classes:int=1000): # number of classes in our classification problem
super().__init__()
# Make an assertion that the image size is compatible with the patch size
assert img_size % patch_size == 0, f"Image size must be divisible by patch size, image: {img_size}, patch size: {patch_size}"
# Calculate the number of patches (height * width/patch^2)
self.num_patches = (img_size * img_size) // patch_size**2
# Create learnable class embedding (needs to go at front of sequence of patch embeddings)
self.class_embedding = nn.Parameter(data=torch.randn(1, 1, embedding_dim),
requires_grad=True)
# Create learnable position embedding
self.position_embedding = nn.Parameter(data=torch.randn(1, self.num_patches+1, embedding_dim))
# Create embedding dropout value
self.embedding_dropout = nn.Dropout(p=embedding_dropout)
# Create patch embedding layer
self.patch_embedding = PatchEmbedding(in_channels=in_channels,
patch_size=patch_size,
embedding_dim=embedding_dim)
# Create the Transformer Encoder block
self.transformer_encoder = nn.Sequential(*[TransformerEncoderBlock(embedding_dim=embedding_dim,
num_heads=num_heads,
mlp_size=mlp_size,
mlp_dropout=mlp_dropout) for _ in range(num_transformer_layers)])
# Create classifier head
self.classifier = nn.Sequential(
nn.LayerNorm(normalized_shape=embedding_dim),
nn.Linear(in_features=embedding_dim,
out_features=num_classes)
)
def forward(self, x):
# Get the batch size
batch_size = x.shape[0]
# Create class token embedding and expand it to match the batch size (equation 1)
class_token = self.class_embedding.expand(batch_size, -1, -1) # "-1" means to infer the dimensions
# Create the patch embedding (equation 1)
x = self.patch_embedding(x)
# Concat class token embedding and patch embedding (equation 1)
x = torch.cat((class_token, x), dim=1) # (batch_size, number_of_patches, embedding_dim)
# Add position embedding to class token and patch embedding
x = self.position_embedding + x
# Apply dropout to patch embedding ("directly after adding positional- to patch embeddings")
x = self.embedding_dropout(x)
# Pass position and patch embedding to Transformer Encoder (equation 2 & 3)
x = self.transformer_encoder(x)
# Put 0th index logit through classifier (equation 4)
x = self.classifier(x[:, 0])
return x In [103]:
batch_size=32
embedding_dim=768
class_embedding = nn.Parameter(data=torch.randn(1, 1, embedding_dim),
requires_grad=True)
class_embedding_expanded = class_embedding.expand(batch_size, -1, -1)
print(class_embedding.shape)
print(class_embedding_expanded.shape)torch.Size([1, 1, 768]) torch.Size([32, 1, 768])
In [104]:
set_seeds()
# Create a random image tensor with same shape as a single image
random_image_tensor = torch.randn(1, 3, 224, 224)
# Create an instance of ViT with the number of classes we're working with (pizza, steak and sushi)
vit = ViT(num_classes=len(class_names))
# Pass the random image tensor to our ViT instance
vit(random_image_tensor)Out [104]:
tensor([[-0.2377, 0.7360, 1.2137]], grad_fn=<AddmmBackward0>)
In [105]:
from torchinfo import summary
summary(model=ViT(num_classes=len(class_names)),
input_size=(1, 3, 224, 224), # (batch_size, color_channels, height, width)
col_names=["input_size", "output_size", "num_params", "trainable"],
col_width=20,
row_settings=["var_names"])Out [105]:
====================================================================================================================================================== Layer (type (var_name)) Input Shape Output Shape Param # Trainable ====================================================================================================================================================== ViT (ViT) [1, 3, 224, 224] [1, 3] 152,064 True ├─Dropout (embedding_dropout) [1, 197, 768] [1, 197, 768] -- -- ├─PatchEmbedding (patch_embedding) [1, 3, 224, 224] [1, 196, 768] -- True │ └─Conv2d (patcher) [1, 3, 224, 224] [1, 768, 14, 14] 590,592 True │ └─Flatten (flatten) [1, 768, 14, 14] [1, 768, 196] -- -- ├─Dropout (embedding_dropout) [1, 197, 768] [1, 197, 768] -- -- ├─Sequential (transformer_encoder) [1, 197, 768] [1, 197, 768] -- True │ └─TransformerEncoderBlock (0) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (1) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (2) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (3) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (4) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (5) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (6) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (7) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (8) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (9) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (10) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True │ └─TransformerEncoderBlock (11) [1, 197, 768] [1, 197, 768] -- True │ │ └─MultiHeadSelfAttentionBlock (msa_block) [1, 197, 768] [1, 197, 768] 2,363,904 True │ │ └─MLPBlock (mlp_block) [1, 197, 768] [1, 197, 768] 4,723,968 True ├─Sequential (classifier) [1, 768] [1, 3] -- True │ └─LayerNorm (0) [1, 768] [1, 768] 1,536 True │ └─Linear (1) [1, 768] [1, 3] 2,307 True ====================================================================================================================================================== Total params: 85,800,963 Trainable params: 85,800,963 Non-trainable params: 0 Total mult-adds (M): 172.47 ====================================================================================================================================================== Input size (MB): 0.60 Forward/backward pass size (MB): 102.88 Params size (MB): 257.55 Estimated Total Size (MB): 361.03 ======================================================================================================================================================
In [126]:
# Number of parameters in pretrained ViT
num_params = 85,800,963
num_paramsOut [126]:
(85, 800, 963)
In [108]:
# vitIn [113]:
deviceOut [113]:
'cuda'
In [114]:
from going_modular.going_modular import engine
set_seeds()
optimizer = torch.optim.Adam(vit.parameters(),
lr=1e-3,
betas=(0.9, 0.999),
weight_decay=0.1)
loss_fn = torch.nn.CrossEntropyLoss()
results = engine.train(model=vit,
train_dataloader=train_dataloader,
test_dataloader=test_dataloader,
epochs=10,
optimizer=optimizer,
loss_fn=loss_fn,
device=device)0%| | 0/10 [00:00<?, ?it/s]
Epoch: 1 | train_loss: 4.9009 | train_acc: 0.2969 | test_loss: 1.0362 | test_acc: 0.5417 Epoch: 2 | train_loss: 1.5928 | train_acc: 0.2773 | test_loss: 1.5662 | test_acc: 0.1979 Epoch: 3 | train_loss: 1.4393 | train_acc: 0.2617 | test_loss: 1.2755 | test_acc: 0.1979 Epoch: 4 | train_loss: 1.2928 | train_acc: 0.2891 | test_loss: 1.6844 | test_acc: 0.1979 Epoch: 5 | train_loss: 1.2678 | train_acc: 0.2852 | test_loss: 1.7159 | test_acc: 0.2604 Epoch: 6 | train_loss: 1.1954 | train_acc: 0.4102 | test_loss: 1.9489 | test_acc: 0.1979 Epoch: 7 | train_loss: 1.1835 | train_acc: 0.4062 | test_loss: 3.0747 | test_acc: 0.1979 Epoch: 8 | train_loss: 1.3447 | train_acc: 0.4180 | test_loss: 1.9179 | test_acc: 0.2604 Epoch: 9 | train_loss: 1.5294 | train_acc: 0.2383 | test_loss: 1.4440 | test_acc: 0.5417 Epoch: 10 | train_loss: 1.4364 | train_acc: 0.3359 | test_loss: 1.2803 | test_acc: 0.2604
In [116]:
from helper_functions import plot_loss_curves
plot_loss_curves(results) In [117]:
# Cost of a TPUv3 for 30 days
cost = 30*24*8
print(f"Cost of renting a TPUv3 for 30 straight days: ${cost}USD")Cost of renting a TPUv3 for 30 straight days: $5760USD
In [119]:
# The following requires torch v0.12+ and torchvision 0.13+
import torch
import torchvision
print(torch.__version__)
print(torchvision.__version__)1.12.0+cu113 0.13.0+cu113
In [120]:
device = "cuda" if torch.cuda.is_available() else "cpu"
deviceOut [120]:
'cuda'
Warning:
Output truncated. This notebook contains too many cells to display efficiently.


