0% found this document useful (0 votes)
15 views33 pages

Task1 Lakshya - Ipynb - Colab

The document details a machine learning assignment involving the training of a convolutional neural network (CNN) using PyTorch to classify images of bears and pandas. It includes data preparation steps, model definition, training, and evaluation processes, along with visualizations of training batches and augmented data. The model's performance is tracked through training and validation losses and accuracy over ten epochs.

Uploaded by

mananchavda18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views33 pages

Task1 Lakshya - Ipynb - Colab

The document details a machine learning assignment involving the training of a convolutional neural network (CNN) using PyTorch to classify images of bears and pandas. It includes data preparation steps, model definition, training, and evaluation processes, along with visualizations of training batches and augmented data. The model's performance is tracked through training and validation losses and accuracy over ten epochs.

Uploaded by

mananchavda18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

!

pip install torchsummary

Requirement already satisfied: torchsummary in /usr/local/lib/python3.10/dist-packages (1.5.1)

keyboard_arrow_down ES335 Machine Learning Assignment -4 : Task-1


Lakshya Kesarwani
Manan Chavda

from torchvision import models, transforms, datasets


from torch.utils.data import Dataset, DataLoader
from torchsummary import summary
import torch.nn.functional as F
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from torch import optim, cuda
import torch
import torch.nn as nn
import os
from PIL import Image
import warnings
from time import time
warnings.filterwarnings('ignore', category = FutureWarning)
from torch.utils.tensorboard import SummaryWriter

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"


DEVICE

 

from google.colab import drive


drive.mount('/content/drive')

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).

Start coding or generate with AI.

traindir = '/content/drive/MyDrive/dataset/train/'
testdir = '/content/drive/MyDrive/dataset/test/'

categories = []
img_categories = []
n_train = []
n_test = []
hs = []
ws = []

# Iterate through each category


for d in os.listdir(traindir):
categories.append(d)

# Number of each image


train_imgs = os.listdir(traindir + d)
test_imgs = os.listdir(testdir + d)
n_train.append(len(train_imgs))
n_test.append(len(test_imgs))

# Find stats for train images


for i in train_imgs:
img_categories.append(d)
img = Image.open(traindir + d + '/' + i)
img_array = np.array(img)
# Shape
hs.append(img_array.shape[0])
ws.append(img_array.shape[1])
# Dataframe of categories
num_df = pd.DataFrame({'category': categories,
'n_train': n_train,
'n_test': n_test}).\
sort_values('category')
print(num_df)

category n_train n_test


1 bear 80 20
0 panda 80 20

image_df = pd.DataFrame({
'category': img_categories,
'height': hs,
'width': ws
})
print(image_df)

category height width


0 panda 256 256
1 panda 256 256
2 panda 256 256
3 panda 256 256
4 panda 256 256
.. ... ... ...
155 bear 256 256
156 bear 256 256
157 bear 256 256
158 bear 256 256
159 bear 256 256

[160 rows x 3 columns]

# Function to display a tensor image


def imshow_tensor(tensor, title=None):

unnormalize = transforms.Normalize(
mean=[-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225],
std=[1 / 0.229, 1 / 0.224, 1 / 0.225]
)

image_tensor = unnormalize(tensor)
image = image_tensor.numpy().transpose((1, 2, 0))
image = image.clip(0, 1)

plt.imshow(image)
if title is not None:
plt.title(title)
plt.axis('off')

# Simple transformation for models without augmentation


simple_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])
])

# Augmented transformation for models with augmentation


image_aug_transforms = transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

print(f"Simple transformation: {simple_transform}")


print(f"Augmented transformation: {image_aug_transforms}")

Simple transformation: Compose(


Resize(size=(224, 224), interpolation=bilinear, max_size=None, antialias=True)
ToTensor()
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
)
Augmented transformation: Compose(
Resize(size=(224, 224), interpolation=bilinear, max_size=None, antialias=True)
RandomHorizontalFlip(p=0.5)
RandomRotation(degrees=[-10.0, 10.0], interpolation=nearest, expand=False, fill=0)
ColorJitter(brightness=(0.8, 1.2), contrast=(0.8, 1.2), saturation=(0.8, 1.2), hue=(-0.1, 0.1))
ToTensor()
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
)

images = {
'train': datasets.ImageFolder(root=traindir, transform=simple_transform),
'test': datasets.ImageFolder(root=testdir, transform=simple_transform)
}

images_aug = {
'train': datasets.ImageFolder(root=traindir, transform=image_aug_transforms),
'test': datasets.ImageFolder(root=testdir, transform=simple_transform)
}

batch_size = 8

dataloaders = {
'train': DataLoader(torch.utils.data.Subset(images['train'], range(0, 140)), batch_size=batch_size, shuffle=True),
'val' : DataLoader(torch.utils.data.Subset(images['train'], range(140, 160)), batch_size=batch_size, shuffle=True),
'test': DataLoader(images['test'], batch_size=batch_size, shuffle=True)
}

aug_dataloaders = {
'train': DataLoader(torch.utils.data.Subset(images_aug['train'], range(0, 140)), batch_size=batch_size, shuffle=True),
'val' : DataLoader(torch.utils.data.Subset(images_aug['train'], range(140, 160)), batch_size=batch_size, shuffle=True),
'test': DataLoader(images_aug['test'], batch_size=batch_size, shuffle=True)
}

print(f"Number of training images: {len(images['train'])}")


print(f"Number of testing images: {len(images['test'])}")
print(f"Number of classes: {len(images['train'].classes)}")
print(f"Batch size: {batch_size}")

Number of training images: 160


Number of testing images: 40
Number of classes: 2
Batch size: 8

train_data_iter = iter(dataloaders['train'])
images_batch, labels = next(train_data_iter)

print(f"Image batch shape: {images_batch.shape}")


print(f"Label batch shape: {labels.shape}")
print(f"Batch labels: {labels}")

plt.figure(figsize=(12, 8))
for i in range(6):
plt.subplot(2, 3, i + 1)
imshow_tensor(images_batch[i])
plt.axis('off')
Image batch shape: torch.Size([8, 3, 224, 224])
Label batch shape: torch.Size([8])
Batch labels: tensor([0, 0, 1, 0, 1, 1, 1, 0])

# Augmented data visualization


train_data_iter_aug = iter(aug_dataloaders['train'])
images_aug, labels_aug = next(train_data_iter_aug)

print(f"Augmented image batch shape: {images_aug.shape}")


print(f"Label batch shape: {labels_aug.shape}")
print(f"Batch labels (augmented): {labels_aug}")

plt.figure(figsize=(12, 8))
for i in range(6):
plt.subplot(2, 3, i + 1)
imshow_tensor(images_aug[i])
plt.axis('off')
Augmented image batch shape: torch.Size([8, 3, 224, 224])
Label batch shape: torch.Size([8])
Batch labels (augmented): tensor([0, 0, 1, 0, 0, 1, 0, 1])

 

category_count = pd.DataFrame({
'Category': [category for category, _ in images['train'].class_to_idx.items()],
'Number of images': [len(os.listdir(os.path.join(traindir, category))) for category in images['train'].class_to_idx]
})

print("Class distribution:")
print(category_count)

image_sizes = [(img.shape[1], img.shape[2]) for img, _ in dataloaders['train'].dataset]


size_df = pd.DataFrame(image_sizes, columns=['Height', 'Width'])

print("Image size statistics:")


print(size_df.describe())

Class distribution:
Category Number of images
0 bear 80
1 panda 80
Image size statistics:
Height Width
count 140.0 140.0
mean 224.0 224.0
std 0.0 0.0
min 224.0 224.0
25% 224.0 224.0
50% 224.0 224.0
75% 224.0 224.0
max 224.0 224.0

keyboard_arrow_down VGG
def define_model():
model = nn.Sequential(
nn.Conv2d(3, 32, 3),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(32*111*111 , 128),
nn.ReLU(),
nn.Linear(128, 2)
)
return model
vgg1 = define_model()
if DEVICE == 'cuda':
vgg1.to('cuda')

summary(vgg1, input_size = (3, 224, 224))

----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 32, 222, 222] 896
ReLU-2 [-1, 32, 222, 222] 0
MaxPool2d-3 [-1, 32, 111, 111] 0
Flatten-4 [-1, 394272] 0
Linear-5 [-1, 128] 50,466,944
ReLU-6 [-1, 128] 0
Linear-7 [-1, 2] 258
================================================================
Total params: 50,468,098
Trainable params: 50,468,098
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 30.08
Params size (MB): 192.52
Estimated Total Size (MB): 223.18
----------------------------------------------------------------

vgg1.class_to_idx = images['train'].class_to_idx
vgg1.idx_to_class = {
idx: class_
for class_, idx in vgg1.class_to_idx.items()
}

list(vgg1.idx_to_class.items())[:10]

[(0, 'bear'), (1, 'panda')]

total_params_vgg = sum(p.numel() for p in vgg1.parameters())


total_params_vgg

50468098

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(vgg1.parameters())
n_epochs = 10

train_losses = []
val_losses = []
train_accuracy = []
valid_accuracy = []
training_time = []

writer = SummaryWriter(f'runs/vgg1/')
step = 0
overall_start_time = time()
for epoch in range(n_epochs):
train_loss = 0.0
val_loss = 0.0
train_acc = 0
valid_acc = 0

vgg1.train()
start_time = time()
for data, target in dataloaders['train']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = vgg1(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()*data.size(0)
pred = torch.argmax(input = output, dim = 1)
correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
train_acc += acc.item() * data.size(0)

writer.add_scalar('Trainig Loss/ Iteration', loss, global_step = step)


writer.add_scalar('Training Accuracy / Iteration', acc, global_step = step)
step += 1

train_time = time() - start_time


print(f'Training time in {epoch+1} is {train_time}\n')

# Evaluate the model


vgg1.eval()
for data, target in dataloaders['val']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
output = vgg1(data)
loss = criterion(output, target)
val_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
valid_acc += acc.item() * data.size(0)

train_loss = train_loss/len(dataloaders['train'].sampler)
val_loss = val_loss/len(dataloaders['val'].sampler)
train_acc = train_acc / len(dataloaders['train'].sampler)
valid_acc = valid_acc / len(dataloaders['val'].sampler)

train_losses.append(train_loss)
val_losses.append(val_loss)
train_accuracy.append(train_acc)
valid_accuracy.append(valid_acc)
training_time.append(train_time)

print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(


epoch+1,
train_loss,
val_loss
))

overall_train_time = time() - overall_start_time


print(f'Overall time taken for {epoch} including training and validation {overall_train_time}')

Training time in 1 is 3.775172472000122

Epoch: 1 Training Loss: 19.407068 Validation Loss: 0.842203


Training time in 2 is 3.5907931327819824

Epoch: 2 Training Loss: 2.436795 Validation Loss: 3.821975


Training time in 3 is 3.411329984664917

Epoch: 3 Training Loss: 0.729626 Validation Loss: 0.816698


Training time in 4 is 3.594712257385254

Epoch: 4 Training Loss: 0.067548 Validation Loss: 0.963672


Training time in 5 is 3.5915799140930176

Epoch: 5 Training Loss: 0.029516 Validation Loss: 1.019338


Training time in 6 is 3.3935701847076416

Epoch: 6 Training Loss: 0.005200 Validation Loss: 0.579713


Training time in 7 is 3.3497912883758545

Epoch: 7 Training Loss: 0.000668 Validation Loss: 0.534069


Training time in 8 is 3.5637784004211426

Epoch: 8 Training Loss: 0.000482 Validation Loss: 0.589411


Training time in 9 is 3.4949288368225098

Epoch: 9 Training Loss: 0.000387 Validation Loss: 0.631204


Training time in 10 is 3.5218162536621094

Epoch: 10 Training Loss: 0.000341 Validation Loss: 0.658016


Overall time taken for 9 including training and validation 36.67531085014343

vgg1_acc_loss_time = pd.DataFrame([], columns = ['TrainingLoss','ValidationLoss', 'TrainingTime', 'TrainingAccuracy','ValidAccuracy', 'T


vgg1_acc_loss_time = pd.DataFrame({'TrainingLoss' : train_losses,
'ValidationLoss' : val_losses,
'TrainingTime' : training_time,
'TrainingAccuracy' : train_accuracy,
'ValidAccuracy' : valid_accuracy,
'TotalParameters': total_params_vgg
})
vgg1_acc_loss_time
TrainingLoss ValidationLoss TrainingTime TrainingAccuracy ValidAccuracy TotalParameters

0 19.407068 0.842203 3.775172 0.578571 0.90 50468098

1 2.436795 3.821975 3.590793 0.792857 0.50 50468098

2 0.729626 0.816698 3.411330 0.857143 0.80 50468098

3 0.067548 0.963672 3.594712 0.964286 0.70 50468098

4 0.029516 1.019338 3.591580 0.992857 0.65 50468098

5 0.005200 0.579713 3.393570 1.000000 0.70 50468098

6 0.000668 0.534069 3.349791 1.000000 0.70 50468098

7 0.000482 0.589411 3.563778 1.000000 0.70 50468098

8 0.000387 0.631204 3.494929 1.000000 0.70 50468098

9 0 0003 1 0 801 3 2181 1 000000 0 0 8098


 

fig , ax =plt.subplots(nrows=1,ncols =2, figsize = (15,5))


ax[0].plot( range(10), vgg1_acc_loss_time['TrainingLoss'], label= 'Training Loss')
ax[0].plot( range(10), vgg1_acc_loss_time['ValidationLoss'], label= 'Validation Loss')
ax[0].set_xlabel("Epochs")
ax[1].set_ylabel("Loss")

ax[1].plot( range(10), vgg1_acc_loss_time['TrainingAccuracy'], label= 'Training Accuracy')


ax[1].plot( range(10), vgg1_acc_loss_time['ValidAccuracy'], label= 'Validation Accuracy')
ax[1].set_xlabel("Epochs")
ax[0].set_ylabel("Accuracy")

ax[0].legend()
ax[1].legend()

<matplotlib.legend.Legend at 0x79377e26b460>

test_acc = 0
step = 0

test_images = []
test_labels = []
predictions = []

for input, target in dataloaders['test']:


if DEVICE == 'cuda':
input, target = input.cuda(), target.cuda()

output = vgg1(input)
pred = torch.argmax(input=output, dim=1)

correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
test_acc += acc.item() * input.size(0)

test_images.append(input.cpu())
test_labels.append(target.cpu())
predictions.append(pred.cpu())
img = input[0].cpu().squeeze().permute(1, 2, 0).numpy()
img = np.clip(img, 0, 1)

fig, ax = plt.subplots()
ax.imshow(img)
ax.set_title(f'Predicted: {vgg1.idx_to_class[pred[0].item()]}')
ax.set_axis_off()

writer.add_scalar('Testing Accuracy / Iteration', acc, global_step=step)


writer.add_figure('Test images & Label', fig, global_step=step)

step += 1

test_acc = test_acc / len(dataloaders['test'].sampler)


vgg1_acc_loss_time['TestAccuracy'] = np.repeat(test_acc, len(vgg1_acc_loss_time))

print('Testing accuracy is {:.6f}'.format(test_acc))

test_images = torch.cat(test_images, dim=0)


test_labels = torch.cat(test_labels, dim=0)
predictions = torch.cat(predictions, dim=0)

fig, axes = plt.subplots(nrows=5, ncols=4, figsize=(12, 12))


axes = axes.flatten()

for i, ax in enumerate(axes):
if i < test_images.size(0):
img = test_images[i].squeeze().permute(1, 2, 0).numpy()
img = np.clip(img, 0, 1)
ax.imshow(img)
ax.set_title(f'Pred: {vgg1.idx_to_class[predictions[i].item()]}', fontsize=10)
ax.set_axis_off()

plt.tight_layout()
writer.add_figure('All Test Images & Predictions', fig)
plt.show()

Testing accuracy is 0.825000

%reload_ext tensorboard

%tensorboard --logdir runs/vgg1


Reusing TensorBoard on port 6006 (pid 16975), started 0:17:34 ago. (Use '!kill 16975' to kill it.)

TensorBoard TIME SERIES SCALARS IMAGES INACTIVE

Filter runs (regex) Filter tags (regex) All Scalars Image Histogram Settings

Run Pinned  Settings 

Pin cards for a quick view and comparison GENERAL


.
Horizontal Axis
All Test Images & Predictions
Step

Enable step selection and data ta


All Test Images & Predictions
(Scalars only)
. Step 0
Enable Range Selection

Link by step 4

Card Width

SCALARS
Smoothing

0.6

Tooltip sorting method

Alphabetical
Test images & Label
Ignore outliers in chart scaling

Test images & Label Partition non-monotonic X axis

. Step 4

HISTOGRAMS
Mode
 
 

Start coding or generate with AI.

keyboard_arrow_down VGG 3
def define_model():
model = nn.Sequential(
nn.Conv2d(3, 32, 3), # 3 input channel, 32 filters of size 3x3
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(26 * 26 *128 , 128),
nn.ReLU(),
nn.Linear(128, 2)
)
return model

vgg3 = define_model()

if DEVICE == 'cuda':
vgg3.to('cuda')

summary(vgg3, input_size = (3, 224, 224))

----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 32, 222, 222] 896
ReLU-2 [-1, 32, 222, 222] 0
MaxPool2d-3 [-1, 32, 111, 111] 0
Conv2d-4 [-1, 64, 109, 109] 18,496
ReLU-5 [-1, 64, 109, 109] 0
MaxPool2d-6 [-1, 64, 54, 54] 0
Conv2d-7 [-1, 128, 52, 52] 73,856
ReLU-8 [-1, 128, 52, 52] 0
MaxPool2d-9 [-1, 128, 26, 26] 0
Flatten-10 [-1, 86528] 0
Linear-11 [-1, 128] 11,075,712
ReLU-12 [-1, 128] 0
Linear-13 [-1, 2] 258
================================================================
Total params: 11,169,218
Trainable params: 11,169,218
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 46.70
Params size (MB): 42.61
Estimated Total Size (MB): 89.88
----------------------------------------------------------------

vgg3.class_to_idx = images['train'].class_to_idx
vgg3.idx_to_class = {
idx: class_
for class_, idx in vgg3.class_to_idx.items()
}

list(vgg3.idx_to_class.items())[:10]

[(0, 'bear'), (1, 'panda')]

total_params = sum(p.numel() for p in vgg3.parameters())


total_params

11169218

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(vgg3.parameters())

n_epochs = 10

train_losses = []
val_losses = []
train_accuracy = []
valid_accuracy = []
training_time = []

writer = SummaryWriter(f'runs/vgg3/')
step = 0
overall_start_time = time()
for epoch in range(n_epochs):
train_loss = 0.0
val_loss = 0.0
train_acc = 0
valid_acc = 0

# Train the model


vgg3.train()
start_time = time()
for data, target in dataloaders['train']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()

optimizer.zero_grad()
output = vgg3(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
train_acc += acc.item() * data.size(0)

writer.add_scalar('Trainig Loss/ Iteration', loss, global_step = step)


writer.add_scalar('Training Accuracy / Iteration', acc, global_step = step)

step += 1
train_time = time() - start_time
print(f'Training time in {epoch+1} is {train_time}\n')

# Evaluate the model


vgg3.eval()
for data, target in dataloaders['val']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
output = vgg3(data)
loss = criterion(output, target)
val_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
valid_acc += acc.item() * data.size(0)

train_loss = train_loss/len(dataloaders['train'].sampler)
val_loss = val_loss/len(dataloaders['val'].sampler)
train_acc = train_acc / len(dataloaders['train'].sampler)
valid_acc = valid_acc / len(dataloaders['val'].sampler)

train_losses.append(train_loss)
val_losses.append(val_loss)
train_accuracy.append(train_acc)
valid_accuracy.append(valid_acc)
training_time.append(train_time)

print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(


epoch+1,
train_loss,
val_loss
))

overall_train_time = time() - overall_start_time


print(f'Overall time taken for {epoch} including training and validation {overall_train_time}')

Training time in 1 is 1.6244268417358398

Epoch: 1 Training Loss: 1.005911 Validation Loss: 1.118446


Training time in 2 is 1.607591152191162

Epoch: 2 Training Loss: 0.566271 Validation Loss: 0.262608


Training time in 3 is 1.633286714553833

Epoch: 3 Training Loss: 0.418649 Validation Loss: 0.317003


Training time in 4 is 1.603534460067749

Epoch: 4 Training Loss: 0.306267 Validation Loss: 0.421265


Training time in 5 is 1.5263245105743408

Epoch: 5 Training Loss: 0.167595 Validation Loss: 0.601951


Training time in 6 is 1.5164196491241455

Epoch: 6 Training Loss: 0.099391 Validation Loss: 0.650097


Training time in 7 is 1.5137481689453125

Epoch: 7 Training Loss: 0.055275 Validation Loss: 1.087733


Training time in 8 is 1.5472466945648193

Epoch: 8 Training Loss: 0.020599 Validation Loss: 0.723526


Training time in 9 is 1.5690116882324219

Epoch: 9 Training Loss: 0.133519 Validation Loss: 2.776593


Training time in 10 is 1.7232489585876465

Epoch: 10 Training Loss: 0.184887 Validation Loss: 0.589679


Overall time taken for 9 including training and validation 17.425607919692993

vgg3_acc_loss_time = pd.DataFrame([], columns = ['TrainingLoss','ValidationLoss', 'TrainingTime', 'TrainingAccuracy','ValidAccuracy', 'T


vgg3_acc_loss_time = pd.DataFrame({'TrainingLoss' : train_losses,
'ValidationLoss' : val_losses,
'TrainingTime' : training_time,
'TrainingAccuracy' : train_accuracy,
'ValidAccuracy' : valid_accuracy,
'TotalParameters': total_params
})
vgg3_acc_loss_time
TrainingLoss ValidationLoss TrainingTime TrainingAccuracy ValidAccuracy TotalParameters

0 1.005911 1.118446 1.624427 0.592857 0.00 11169218

1 0.566271 0.262608 1.607591 0.728571 0.95 11169218

2 0.418649 0.317003 1.633287 0.857143 0.95 11169218

3 0.306267 0.421265 1.603534 0.850000 0.85 11169218

4 0.167595 0.601951 1.526325 0.942857 0.90 11169218

5 0.099391 0.650097 1.516420 0.964286 0.90 11169218

6 0.055275 1.087733 1.513748 0.978571 0.80 11169218

7 0.020599 0.723526 1.547247 0.992857 0.95 11169218

8 0.133519 2.776593 1.569012 0.957143 0.40 11169218

9 0.184887 0.589679 1.723249 0.942857 0.95 11169218

fig , ax =plt.subplots(nrows=1,ncols =2, figsize = (15,5))


ax[0].plot( range(10), vgg3_acc_loss_time['TrainingLoss'], label= 'Training Loss')
ax[0].plot( range(10), vgg3_acc_loss_time['ValidationLoss'], label= 'Validation Loss')
ax[1].plot( range(10), vgg3_acc_loss_time['TrainingAccuracy'], label= 'Training Accuracy')
ax[1].plot( range(10), vgg3_acc_loss_time['ValidAccuracy'], label= 'Validation Accuracy')
ax[0].legend()
ax[1].legend()

<matplotlib.legend.Legend at 0x79377f8c2950>

test_acc = 0
step = 0

test_images = []
test_labels = []
predictions = []

for input, target in dataloaders['test']:


if DEVICE == 'cuda':
input, target = input.cuda(), target.cuda()

output = vgg3(input)
pred = torch.argmax(input=output, dim=1)

correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
test_acc += acc.item() * input.size(0)

test_images.append(input.cpu())
test_labels.append(target.cpu())
predictions.append(pred.cpu())

img = input[0].cpu().squeeze().permute(1, 2, 0).numpy()


img = np.clip(img, 0, 1)

fig, ax = plt.subplots()
ax.imshow(img)
ax.set_title(f'Predicted: {vgg3.idx_to_class[pred[0].item()]}')
ax.set_axis_off()
writer.add_scalar('Testing Accuracy / Iteration', acc, global_step=step)
writer.add_figure('Test images & Label', fig, global_step=step)

step += 1

test_acc = test_acc / len(dataloaders['test'].sampler)


vgg3_acc_loss_time['TestAccuracy'] = np.repeat(test_acc, len(vgg3_acc_loss_time))

print(f'Testing accuracy is {test_acc}')

test_images = torch.cat(test_images, dim=0)


test_labels = torch.cat(test_labels, dim=0)
predictions = torch.cat(predictions, dim=0)

fig, axes = plt.subplots(nrows=5, ncols=4, figsize=(12, 12))


axes = axes.flatten()

for i, ax in enumerate(axes):
if i < test_images.size(0):
img = test_images[i].squeeze().permute(1, 2, 0).numpy()
img = np.clip(img, 0, 1)
ax.imshow(img)
ax.set_title(f'Pred: {vgg3.idx_to_class[predictions[i].item()]}', fontsize=10)
ax.set_axis_off()

plt.tight_layout()
writer.add_figure('All Test Images & Predictions', fig)
plt.show()

Testing accuracy is 0.85

%reload_ext tensorboard

%tensorboard --logdir runs/vgg3

Reusing TensorBoard on port 6007 (pid 18277), started 0:14:12 ago. (Use '!kill 18277' to kill it.)

TensorBoard TIME SERIES SCALARS IMAGES INACTIVE

Filter runs (regex) Filter tags (regex) All Scalars Image Histogram Settings

Run Pinned  Settings 

Pin cards for a quick view and comparison GENERAL


.
Horizontal Axis
All Test Images & Predictions
Step

Enable step selection and data ta


All Test Images & Predictions
(Scalars only)
. Step 0
Enable Range Selection

Link by step 4

Card Width

SCALARS
Smoothing

0.6

Tooltip sorting method

Alphabetical
Test images & Label
Ignore outliers in chart scaling

Test images & Label Partition non-monotonic X axis

. Step 4

HISTOGRAMS
Mode
 
Offset
Double-click (or enter) to edit

Start coding or generate with AI.

keyboard_arrow_down VGG 3 Augmented


vgg3_aug = define_model()

total_params = sum(p.numel() for p in vgg3_aug.parameters())


total_params

11169218

if DEVICE == 'cuda':
vgg3_aug.to('cuda')

class_to_idx = aug_dataloaders['train'].dataset.dataset.class_to_idx
idx_to_class = {idx: cls for cls, idx in class_to_idx.items()}

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(vgg3_aug.parameters())
n_epochs = 10

train_losses = []
val_losses = []
train_accuracy = []
valid_accuracy = []
training_time = []

writer1 = SummaryWriter(f'runs/vgg3_aug/')
step = 0
overall_start_time = time()
for epoch in range(n_epochs):
train_loss = 0.0
val_loss = 0.0
train_acc = 0
valid_acc = 0

# Train the model


vgg3_aug.train()
start_time = time()
for data, target in aug_dataloaders['train']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = vgg3_aug(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
train_acc += acc.item() * data.size(0)

writer1.add_scalar('Trainig Loss/ Iteration', loss, global_step = step)


writer1.add_scalar('Training Accuracy / Iteration', acc, global_step = step)
step += 1

train_time = time() - start_time


print(f'Training time in {epoch+1} is {train_time}\n')

# Evaluate the model


vgg3_aug.eval()
for data, target in aug_dataloaders['val']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
output = vgg3_aug(data)
loss = criterion(output, target)
val_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
valid_acc += acc.item() * data.size(0)

train_loss = train_loss/len(aug_dataloaders['train'].sampler)
val_loss = val_loss/len(aug_dataloaders['val'].sampler)
train_acc = train_acc / len(aug_dataloaders['train'].sampler)
valid_acc = valid_acc / len(aug_dataloaders['val'].sampler)

train_losses.append(train_loss)
val_losses.append(val_loss)
train_accuracy.append(train_acc)
valid_accuracy.append(valid_acc)
training_time.append(train_time)

print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(


epoch+1,
train_loss,
val_loss
))

overall_train_time = time() - overall_start_time


print(f'Overall time taken for {epoch} including training and validation {overall_train_time}')

Training time in 1 is 2.70255708694458

Epoch: 1 Training Loss: 0.826859 Validation Loss: 0.488249


Training time in 2 is 2.4396800994873047

Epoch: 2 Training Loss: 0.659416 Validation Loss: 1.155672


Training time in 3 is 2.527523994445801

Epoch: 3 Training Loss: 0.595067 Validation Loss: 1.509668


Training time in 4 is 2.5981292724609375

Epoch: 4 Training Loss: 0.542424 Validation Loss: 0.350658


Training time in 5 is 2.608424186706543

Epoch: 5 Training Loss: 0.452834 Validation Loss: 0.102236


Training time in 6 is 2.491647481918335

Epoch: 6 Training Loss: 0.392272 Validation Loss: 0.225083


Training time in 7 is 2.7404043674468994

Epoch: 7 Training Loss: 0.322003 Validation Loss: 0.430184


Training time in 8 is 2.308973789215088

Epoch: 8 Training Loss: 0.263566 Validation Loss: 0.381442


Training time in 9 is 2.641191244125366

Epoch: 9 Training Loss: 0.323171 Validation Loss: 0.096240


Training time in 10 is 2.755953788757324

Epoch: 10 Training Loss: 0.218162 Validation Loss: 0.994375


Overall time taken for 9 including training and validation 28.66791558265686

vgg3_aug_acc_loss_time = pd.DataFrame([], columns = ['TrainingLoss', 'ValidationLoss', 'TrainingTime', 'TrainingAccuracy','ValidAccuracy


vgg3_aug_acc_loss_time = pd.DataFrame({'TrainingLoss' : train_losses,
'ValidationLoss' : val_losses,
'TrainingTime' : training_time,
'TrainingAccuracy' : train_accuracy,
'ValidAccuracy' : valid_accuracy,
'TotalParameters': total_params
})
vgg3_aug_acc_loss_time
TrainingLoss ValidationLoss TrainingTime TrainingAccuracy ValidAccuracy TotalParameters

0 0.826859 0.488249 2.702557 0.557143 0.90 11169218

1 0.659416 1.155672 2.439680 0.642857 0.00 11169218

2 0.595067 1.509668 2.527524 0.664286 0.00 11169218

3 0.542424 0.350658 2.598129 0.714286 0.90 11169218

4 0.452834 0.102236 2.608424 0.800000 1.00 11169218

5 0.392272 0.225083 2.491647 0.814286 1.00 11169218

6 0.322003 0.430184 2.740404 0.871429 0.75 11169218

7 0.263566 0.381442 2.308974 0.892857 0.90 11169218

8 0.323171 0.096240 2.641191 0.864286 0.95 11169218

9 0.218162 0.994375 2.755954 0.928571 0.55 11169218

fig , ax =plt.subplots(nrows=1,ncols =2, figsize = (15,5))


ax[0].plot( range(10), vgg3_aug_acc_loss_time['TrainingLoss'], label= 'Training Loss')
ax[0].plot( range(10), vgg3_aug_acc_loss_time['ValidationLoss'], label= 'Validation Loss')
ax[1].plot( range(10), vgg3_aug_acc_loss_time['TrainingAccuracy'], label= 'Training Accuracy')
ax[1].plot( range(10), vgg3_aug_acc_loss_time['ValidAccuracy'], label= 'Validation Accuracy')
ax[0].legend()
ax[1].legend()

<matplotlib.legend.Legend at 0x79377fc152a0>

test_acc = 0
step = 0

test_images = []
test_labels = []
predictions = []

for input, target in aug_dataloaders['test']:


if DEVICE == 'cuda':
input, target = input.cuda(), target.cuda()

output = vgg3_aug(input)
pred = torch.argmax(input=output, dim=1)

correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
test_acc += acc.item() * input.size(0)

test_images.append(input.cpu())
test_labels.append(target.cpu())
predictions.append(pred.cpu())

img = input[0].cpu().squeeze().permute(1, 2, 0).numpy()


img = np.clip(img, 0, 1)

fig, ax = plt.subplots()
ax.imshow(img)
ax.set_title(f'Predicted: {idx_to_class[pred[0].item()]}')
ax.set_axis_off()
writer1.add_scalar('Testing Accuracy / Iteration', acc, global_step=step)
writer1.add_figure('Test images & Label', fig, global_step=step)

step += 1

test_acc = test_acc / len(aug_dataloaders['test'].sampler)


vgg3_aug_acc_loss_time['TestAccuracy'] = np.repeat(test_acc, len(vgg3_aug_acc_loss_time))

print(f'Testing accuracy is {test_acc}')

test_images = torch.cat(test_images, dim=0)


test_labels = torch.cat(test_labels, dim=0)
predictions = torch.cat(predictions, dim=0)

fig, axes = plt.subplots(nrows=5, ncols=4, figsize=(12, 12))


axes = axes.flatten()

for i, ax in enumerate(axes):
if i < test_images.size(0):
img = test_images[i].squeeze().permute(1, 2, 0).numpy()
img = np.clip(img, 0, 1)
ax.imshow(img)
ax.set_title(f'Pred: {idx_to_class[predictions[i].item()]}', fontsize=10)
ax.set_axis_off()

plt.tight_layout()
writer1.add_figure('All Test Images & Predictions', fig)
plt.show()

Testing accuracy is 0.775

%reload_ext tensorboard

%tensorboard --logdir runs/vgg3_aug

Reusing TensorBoard on port 6008 (pid 20259), started 0:08:35 ago. (Use '!kill 20259' to kill it.)

TensorBoard TIME SERIES SCALARS IMAGES INACTIVE

Filter runs (regex) Filter tags (regex) All Scalars Image Histogram Settings

Run Pinned  Settings 

Pin cards for a quick view and comparison GENERAL


.
Horizontal Axis
All Test Images & Predictions
Step

Enable step selection and data ta


All Test Images & Predictions
(Scalars only)
. Step 0
Enable Range Selection

Link by step 4

Card Width

SCALARS
Smoothing

0.6

Tooltip sorting method

Alphabetical
Test images & Label
Ignore outliers in chart scaling

Test images & Label Partition non-monotonic X axis

. Step 4

HISTOGRAMS
Mode
 
Offset
Start coding or generate with AI.

keyboard_arrow_down VGG 16 (Fully connected layer)


vgg16 = models.vgg16(pretrained=True)

/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated sinc


warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None`
warnings.warn(msg)

 

vgg16.classifier[6] = nn.Linear(vgg16.classifier[6].in_features, 2)
for params in vgg16.parameters():
params.requires_grad = False

for params in vgg16.classifier.parameters():


params.requires_grad = True

if DEVICE == 'cuda':
vgg16.to('cuda')

summary(vgg16, input_size = (3, 224, 224))

----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 224, 224] 1,792
ReLU-2 [-1, 64, 224, 224] 0
Conv2d-3 [-1, 64, 224, 224] 36,928
ReLU-4 [-1, 64, 224, 224] 0
MaxPool2d-5 [-1, 64, 112, 112] 0
Conv2d-6 [-1, 128, 112, 112] 73,856
ReLU-7 [-1, 128, 112, 112] 0
Conv2d-8 [-1, 128, 112, 112] 147,584
ReLU-9 [-1, 128, 112, 112] 0
MaxPool2d-10 [-1, 128, 56, 56] 0
Conv2d-11 [-1, 256, 56, 56] 295,168
ReLU-12 [-1, 256, 56, 56] 0
Conv2d-13 [-1, 256, 56, 56] 590,080
ReLU-14 [-1, 256, 56, 56] 0
Conv2d-15 [-1, 256, 56, 56] 590,080
ReLU-16 [-1, 256, 56, 56] 0
MaxPool2d-17 [-1, 256, 28, 28] 0
Conv2d-18 [-1, 512, 28, 28] 1,180,160
ReLU-19 [-1, 512, 28, 28] 0
Conv2d-20 [-1, 512, 28, 28] 2,359,808
ReLU-21 [-1, 512, 28, 28] 0
Conv2d-22 [-1, 512, 28, 28] 2,359,808
ReLU-23 [-1, 512, 28, 28] 0
MaxPool2d-24 [-1, 512, 14, 14] 0
Conv2d-25 [-1, 512, 14, 14] 2,359,808
ReLU-26 [-1, 512, 14, 14] 0
Conv2d-27 [-1, 512, 14, 14] 2,359,808
ReLU-28 [-1, 512, 14, 14] 0
Conv2d-29 [-1, 512, 14, 14] 2,359,808
ReLU-30 [-1, 512, 14, 14] 0
MaxPool2d-31 [-1, 512, 7, 7] 0
AdaptiveAvgPool2d-32 [-1, 512, 7, 7] 0
Linear-33 [-1, 4096] 102,764,544
ReLU-34 [-1, 4096] 0
Dropout-35 [-1, 4096] 0
Linear-36 [-1, 4096] 16,781,312
ReLU-37 [-1, 4096] 0
Dropout-38 [-1, 4096] 0
Linear-39 [-1, 2] 8,194
================================================================
Total params: 134,268,738
Trainable params: 119,554,050
Non-trainable params: 14,714,688
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 218.77
Params size (MB): 512.19
Estimated Total Size (MB): 731.54
----------------------------------------------------------------

vgg16.class_to_idx = images['train'].class_to_idx
vgg16.idx_to_class = {
idx: class_
for class_, idx in vgg16.class_to_idx.items()
}

list(vgg16.idx_to_class.items())[:10]

[(0, 'bear'), (1, 'panda')]

total_params = sum(p.numel() for p in vgg16.parameters())


total_params

134268738

total_parameters = sum(params.numel() for params in vgg16.parameters())


print(f'Total Parameters {total_parameters}')

trainable_parameters = sum(params.numel() for params in vgg16.parameters() if params.requires_grad)


print(f'Trainable Parameters {trainable_parameters}')

Total Parameters 134268738


Trainable Parameters 119554050

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(vgg16.parameters())
n_epochs = 10

train_losses = []
val_losses = []
train_accuracy = []
valid_accuracy = []
training_time = []

writer = SummaryWriter(f'runs/vgg16/')
step = 0
overall_start_time = time()
for epoch in range(n_epochs):
train_loss = 0.0
val_loss = 0.0
train_acc = 0
valid_acc = 0

# Train the model


vgg16.train()
start_time = time()
for data, target in dataloaders['train']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = vgg16(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
train_acc += acc.item() * data.size(0)

writer.add_scalar('Trainig Loss/ Iteration', loss, global_step = step)


writer.add_scalar('Training Accuracy / Iteration', acc, global_step = step)

step += 1

train_time = time() - start_time


print(f'Training time in {epoch+1} is {train_time}\n')

# Evaluate the model


vgg16.eval()
for data, target in dataloaders['val']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
output = vgg16(data)
loss = criterion(output, target)
val_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
valid_acc += acc.item() * data.size(0)
# Calculate average losses
train_loss = train_loss/len(dataloaders['train'].sampler)
val_loss = val_loss/len(dataloaders['val'].sampler)
train_acc = train_acc / len(dataloaders['train'].sampler)
valid_acc = valid_acc / len(dataloaders['val'].sampler)

train_losses.append(train_loss)
val_losses.append(val_loss)
train_accuracy.append(train_acc)
valid_accuracy.append(valid_acc)
training_time.append(train_time)

print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(


epoch+1,
train_loss,
val_loss
))

overall_train_time = time() - overall_start_time


print(f'Overall time taken for {epoch} including training and validation {overall_train_time}')

Training time in 1 is 10.078190326690674

Epoch: 1 Training Loss: 1.390581 Validation Loss: 1.625252


Training time in 2 is 9.64910078048706

Epoch: 2 Training Loss: 0.768356 Validation Loss: 0.151178


Training time in 3 is 10.787003755569458

Epoch: 3 Training Loss: 0.845181 Validation Loss: 0.683452


Training time in 4 is 10.34420108795166

Epoch: 4 Training Loss: 0.318441 Validation Loss: 2.667511


Training time in 5 is 10.3950355052948

Epoch: 5 Training Loss: 1.017271 Validation Loss: 3.559473


Training time in 6 is 10.367368936538696

Epoch: 6 Training Loss: 1.810231 Validation Loss: 2.791055


Training time in 7 is 9.986860036849976

Epoch: 7 Training Loss: 1.484839 Validation Loss: 1.207939


Training time in 8 is 10.253744840621948

Epoch: 8 Training Loss: 1.765452 Validation Loss: 0.331748


Training time in 9 is 10.198379278182983

Epoch: 9 Training Loss: 2.968292 Validation Loss: 5.056544


Training time in 10 is 10.494478464126587

Epoch: 10 Training Loss: 1.611107 Validation Loss: 42.895118


Overall time taken for 9 including training and validation 108.75304245948792

vgg16_acc_loss_time = pd.DataFrame([], columns = ['TrainingLoss','ValidationLoss', 'TrainingTime', 'TrainingAccuracy','ValidAccuracy',


vgg16_acc_loss_time = pd.DataFrame({'TrainingLoss' : train_losses,
'ValidationLoss' : val_losses,
'TrainingTime' : training_time,
'TrainingAccuracy' : train_accuracy,
'ValidAccuracy' : valid_accuracy,
'TotalParameters': total_params
})
vgg16_acc_loss_time

TrainingLoss ValidationLoss TrainingTime TrainingAccuracy ValidAccuracy TotalParameters

0 1.390581 1.625252 10.078190 0.835714 0.85 134268738

1 0.768356 0.151178 9.649101 0.971429 0.95 134268738

2 0.845181 0.683452 10.787004 0.964286 0.95 134268738

3 0.318441 2.667511 10.344201 0.985714 0.90 134268738

4 1.017271 3.559473 10.395036 0.985714 0.90 134268738

5 1.810231 2.791055 10.367369 0.971429 0.95 134268738

6 1.484839 1.207939 9.986860 0.985714 0.95 134268738

7 1.765452 0.331748 10.253745 0.978571 0.95 134268738

8 2.968292 5.056544 10.198379 0.971429 0.90 134268738

9 1.611107 42.895118 10.494478 0.978571 0.85 134268738


fig , ax =plt.subplots(nrows=1,ncols =2, figsize = (15,5))
ax[0].plot( range(10), vgg16_acc_loss_time['TrainingLoss'], label= 'Training Loss')
ax[0].plot( range(10), vgg16_acc_loss_time['ValidationLoss'], label= 'Validation Loss')
ax[1].plot( range(10), vgg16_acc_loss_time['TrainingAccuracy'], label= 'Training Accuracy')
ax[1].plot( range(10), vgg16_acc_loss_time['ValidAccuracy'], label= 'Validation Accuracy')
ax[0].legend()
ax[1].legend()

<matplotlib.legend.Legend at 0x79379ed6f3a0>

test_acc = 0
step = 0

test_images = []
test_labels = []
predictions = []

for input, target in dataloaders['test']:


if DEVICE == 'cuda':
input, target = input.cuda(), target.cuda()

output = vgg16(input)
pred = torch.argmax(input=output, dim=1)

correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
test_acc += acc.item() * input.size(0)

test_images.append(input.cpu())
test_labels.append(target.cpu())
predictions.append(pred.cpu())

img = input[0].cpu().squeeze().permute(1, 2, 0).numpy()


img = np.clip(img, 0, 1)

fig, ax = plt.subplots()
ax.imshow(img)
ax.set_title(f'Predicted: {vgg16.idx_to_class[pred[0].item()]}')
ax.set_axis_off()

writer.add_scalar('Testing Accuracy / Iteration', acc, global_step=step)


writer.add_figure('Test images & Label', fig, global_step=step)

step += 1

test_acc = test_acc / len(dataloaders['test'].sampler)


vgg16_acc_loss_time['TestAccuracy'] = np.repeat(test_acc, len(vgg16_acc_loss_time))

print(f'Testing accuracy is {test_acc}')

test_images = torch.cat(test_images, dim=0)


test_labels = torch.cat(test_labels, dim=0)
predictions = torch.cat(predictions, dim=0)

fig, axes = plt.subplots(nrows=5, ncols=4, figsize=(12, 12))


axes = axes.flatten()

for i, ax in enumerate(axes):
if i < test_images.size(0):
img = test_images[i].squeeze().permute(1, 2, 0).numpy()
img = np.clip(img, 0, 1)
ax.imshow(img)
ax.set_title(f'Pred: {vgg16.idx_to_class[predictions[i].item()]}', fontsize=10)
ax.set_axis_off()

plt.tight_layout()
writer.add_figure('All Test Images & Predictions', fig)
plt.show()

Testing accuracy is 0.925

%reload_ext tensorboard

%tensorboard --logdir runs/vgg16

Reusing TensorBoard on port 6009 (pid 21159), started 0:08:18 ago. (Use '!kill 21159' to kill it.)

TensorBoard TIME SERIES SCALARS IMAGES INACTIVE

Filter runs (regex) Filter tags (regex) All Scalars Image Histogram Settings

Run Pinned  Settings 

Pin cards for a quick view and comparison GENERAL


.
Horizontal Axis
All Test Images & Predictions
Step

Enable step selection and data ta


All Test Images & Predictions
(Scalars only)
. Step 0
Enable Range Selection

Link by step 4

Card Width

SCALARS
Smoothing

0.6

Tooltip sorting method

Alphabetical
Test images & Label
Ignore outliers in chart scaling

Test images & Label Partition non-monotonic X axis

. Step 4

HISTOGRAMS
Mode
 
Offset

Start coding or generate with AI.

keyboard_arrow_down VGG16 (Fine Tuning)


vgg16_ft = models.vgg16(pretrained=True)

/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated sinc


warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None`
warnings.warn(msg)

 

vgg16_ft.classifier[6] = nn.Linear(vgg16_ft.classifier[6].in_features, 2)
if DEVICE == 'cuda':
vgg16_ft.to('cuda')

vgg16_ft.class_to_idx = images['train'].class_to_idx
vgg16_ft.idx_to_class = {
idx: class_
for class_, idx in vgg16_ft.class_to_idx.items()
}

list(vgg16_ft.idx_to_class.items())[:10]

[(0, 'bear'), (1, 'panda')]

summary(vgg16_ft, input_size = (3, 224, 224))

----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 224, 224] 1,792
ReLU-2 [-1, 64, 224, 224] 0
Conv2d-3 [-1, 64, 224, 224] 36,928
ReLU-4 [-1, 64, 224, 224] 0
MaxPool2d-5 [-1, 64, 112, 112] 0
Conv2d-6 [-1, 128, 112, 112] 73,856
ReLU-7 [-1, 128, 112, 112] 0
Conv2d-8 [-1, 128, 112, 112] 147,584
ReLU-9 [-1, 128, 112, 112] 0
MaxPool2d-10 [-1, 128, 56, 56] 0
Conv2d-11 [-1, 256, 56, 56] 295,168
ReLU-12 [-1, 256, 56, 56] 0
Conv2d-13 [-1, 256, 56, 56] 590,080
ReLU-14 [-1, 256, 56, 56] 0
Conv2d-15 [-1, 256, 56, 56] 590,080
ReLU-16 [-1, 256, 56, 56] 0
MaxPool2d-17 [-1, 256, 28, 28] 0
Conv2d-18 [-1, 512, 28, 28] 1,180,160
ReLU-19 [-1, 512, 28, 28] 0
Conv2d-20 [-1, 512, 28, 28] 2,359,808
ReLU-21 [-1, 512, 28, 28] 0
Conv2d-22 [-1, 512, 28, 28] 2,359,808
ReLU-23 [-1, 512, 28, 28] 0
MaxPool2d-24 [-1, 512, 14, 14] 0
Conv2d-25 [-1, 512, 14, 14] 2,359,808
ReLU-26 [-1, 512, 14, 14] 0
Conv2d-27 [-1, 512, 14, 14] 2,359,808
ReLU-28 [-1, 512, 14, 14] 0
Conv2d-29 [-1, 512, 14, 14] 2,359,808
ReLU-30 [-1, 512, 14, 14] 0
MaxPool2d-31 [-1, 512, 7, 7] 0
AdaptiveAvgPool2d-32 [-1, 512, 7, 7] 0
Linear-33 [-1, 4096] 102,764,544
ReLU-34 [-1, 4096] 0
Dropout-35 [-1, 4096] 0
Linear-36 [-1, 4096] 16,781,312
ReLU-37 [-1, 4096] 0
Dropout-38 [-1, 4096] 0
Linear-39 [-1, 2] 8,194
================================================================
Total params: 134,268,738
Trainable params: 134,268,738
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 218.77
Params size (MB): 512.19
Estimated Total Size (MB): 731.54
----------------------------------------------------------------

total_params = sum(p.numel() for p in vgg16_ft.parameters())


total_params

134268738

total_parameters = sum(params.numel() for params in vgg16_ft.parameters())


print(f'Total Parameters {total_parameters}')

trainable_parameters = sum(params.numel() for params in vgg16_ft.parameters() if params.requires_grad)


print(f'Trainable Parameters {trainable_parameters}')

Total Parameters 134268738


Trainable Parameters 134268738

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(vgg16_ft.parameters())
n_epochs = 10

train_losses = []
val_losses = []
train_accuracy = []
valid_accuracy = []
training_time = []

writer = SummaryWriter(f'runs/vgg16_ft/')
step = 0
overall_start_time = time()
for epoch in range(n_epochs):
train_loss = 0.0
val_loss = 0.0
train_acc = 0
valid_acc = 0

# Train the model


vgg16_ft.train()
start_time = time()
for data, target in dataloaders['train']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = vgg16_ft(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
train_acc += acc.item() * data.size(0)

writer.add_scalar('Trainig Loss/ Iteration', loss, global_step = step)


writer.add_scalar('Training Accuracy / Iteration', acc, global_step = step)

step += 1

train_time = time() - start_time


print(f'Training time in {epoch+1} is {train_time}\n')

# Evaluate the model


vgg16_ft.eval()
for data, target in dataloaders['val']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
output = vgg16_ft(data)
loss = criterion(output, target)
val_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
valid_acc += acc.item() * data.size(0)

# Calculate average losses


train_loss = train_loss/len(dataloaders['train'].sampler)
val_loss = val_loss/len(dataloaders['val'].sampler)
train_acc = train_acc / len(dataloaders['train'].sampler)
valid_acc = valid_acc / len(dataloaders['val'].sampler)

train_losses.append(train_loss)
val_losses.append(val_loss)
train_accuracy.append(train_acc)
valid_accuracy.append(valid_acc)
training_time.append(train_time)

print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(


epoch+1,
train_loss,
val_loss
))

overall_train_time = time() - overall_start_time


print(f'Overall time taken for {epoch} including training and validation {overall_train_time}')
Training time in 1 is 18.06322717666626

Epoch: 1 Training Loss: 1.204417 Validation Loss: 1.224240


Training time in 2 is 18.483985424041748

Epoch: 2 Training Loss: 0.916644 Validation Loss: 0.529989


Training time in 3 is 18.472381114959717

Epoch: 3 Training Loss: 0.711795 Validation Loss: 1.132741


Training time in 4 is 18.537716150283813

Epoch: 4 Training Loss: 0.708094 Validation Loss: 0.890049


Training time in 5 is 19.132248640060425

Epoch: 5 Training Loss: 0.707964 Validation Loss: 0.822148


Training time in 6 is 19.173149347305298

Epoch: 6 Training Loss: 0.714994 Validation Loss: 0.647918


Training time in 7 is 18.1473171710968

Epoch: 7 Training Loss: 0.697645 Validation Loss: 0.997393


Training time in 8 is 18.288151025772095

Epoch: 8 Training Loss: 0.706296 Validation Loss: 1.000884


Training time in 9 is 18.43344259262085

Epoch: 9 Training Loss: 0.710237 Validation Loss: 0.835774


Training time in 10 is 18.89774489402771

Epoch: 10 Training Loss: 0.701423 Validation Loss: 0.932102


Overall time taken for 9 including training and validation 192.6191029548645

vgg16_ft_acc_loss_time = pd.DataFrame([], columns = ['TrainingLoss','ValidationLoss', 'TrainingTime', 'TrainingAccuracy','ValidAccuracy


vgg16_ft_acc_loss_time = pd.DataFrame({'TrainingLoss' : train_losses,
'ValidationLoss' : val_losses,
'TrainingTime' : training_time,
'TrainingAccuracy' : train_accuracy,
'ValidAccuracy' : valid_accuracy,
'TotalParameters': total_params
})
vgg16_ft_acc_loss_time

TrainingLoss ValidationLoss TrainingTime TrainingAccuracy ValidAccuracy TotalParameters

0 1.204417 1.224240 18.063227 0.514286 0.0 134268738

1 0.916644 0.529989 18.483985 0.550000 1.0 134268738

2 0.711795 1.132741 18.472381 0.550000 0.0 134268738

3 0.708094 0.890049 18.537716 0.457143 0.0 134268738

4 0.707964 0.822148 19.132249 0.571429 0.0 134268738

5 0.714994 0.647918 19.173149 0.507143 1.0 134268738

6 0.697645 0.997393 18.147317 0.557143 0.0 134268738

7 0.706296 1.000884 18.288151 0.521429 0.0 134268738

8 0.710237 0.835774 18.433443 0.535714 0.0 134268738

9 0.701423 0.932102 18.897745 0.571429 0.0 134268738

fig , ax =plt.subplots(nrows=1,ncols =2, figsize = (15,5))


ax[0].plot( range(10), vgg16_ft_acc_loss_time['TrainingLoss'], label= 'Training Loss')
ax[0].plot( range(10), vgg16_ft_acc_loss_time['ValidationLoss'], label= 'Validation Loss')
ax[1].plot( range(10), vgg16_ft_acc_loss_time['TrainingAccuracy'], label= 'Training Accuracy')
ax[1].plot( range(10), vgg16_ft_acc_loss_time['ValidAccuracy'], label= 'Validation Accuracy')
ax[0].legend()
ax[1].legend()
<matplotlib.legend.Legend at 0x7937c4751240>

test_acc = 0
step = 0

test_images = []
test_labels = []
predictions = []

for input, target in dataloaders['test']:


if DEVICE == 'cuda':
input, target = input.cuda(), target.cuda()

output = vgg16_ft(input)
pred = torch.argmax(input=output, dim=1)

correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
test_acc += acc.item() * input.size(0)

test_images.append(input.cpu())
test_labels.append(target.cpu())
predictions.append(pred.cpu())

img = input[0].cpu().squeeze().permute(1, 2, 0).numpy()


img = np.clip(img, 0, 1)

fig, ax = plt.subplots()
ax.imshow(img)
ax.set_title(f'Predicted: {vgg3.idx_to_class[pred[0].item()]}')
ax.set_axis_off()

writer.add_scalar('Testing Accuracy / Iteration', acc, global_step=step)


writer.add_figure('Test images & Label', fig, global_step=step)

step += 1

test_acc = test_acc / len(dataloaders['test'].sampler)


vgg16_ft_acc_loss_time['TestAccuracy'] = np.repeat(test_acc, len(vgg16_ft_acc_loss_time))

print(f'Testing accuracy is {test_acc}')

test_images = torch.cat(test_images, dim=0)


test_labels = torch.cat(test_labels, dim=0)
predictions = torch.cat(predictions, dim=0)

fig, axes = plt.subplots(nrows=5, ncols=4, figsize=(12, 12))


axes = axes.flatten()

for i, ax in enumerate(axes):
if i < test_images.size(0):
img = test_images[i].squeeze().permute(1, 2, 0).numpy()
img = np.clip(img, 0, 1)
ax.imshow(img)
ax.set_title(f'Pred: {vgg16_ft.idx_to_class[predictions[i].item()]}', fontsize=10)
ax.set_axis_off()

plt.tight_layout()
writer.add_figure('All Test Images & Predictions', fig)
plt.show()

Testing accuracy is 0.5

%reload_ext tensorboard

%tensorboard --logdir runs/vgg16_ft

Reusing TensorBoard on port 6010 (pid 22365), started 0:08:09 ago. (Use '!kill 22365' to kill it.)

TensorBoard TIME SERIES SCALARS IMAGES INACTIVE

Filter runs (regex) Filter tags (regex) All Scalars Image Histogram Settings

Run Pinned  Settings 

Pin cards for a quick view and comparison GENERAL


.
Horizontal Axis
All Test Images & Predictions
Step

Enable step selection and data ta


All Test Images & Predictions
(Scalars only)
. Step 0
Enable Range Selection

Link by step 4

Card Width

SCALARS
Smoothing

0.6

Tooltip sorting method

Alphabetical
Test images & Label
Ignore outliers in chart scaling

Test images & Label Partition non-monotonic X axis

. Step 4

HISTOGRAMS
Mode
 
Offset

Start coding or generate with AI.

keyboard_arrow_down Training MLP


MLP = nn.Sequential(
nn.Linear(150528, 900),
nn.ReLU(),
nn.Linear(900, 512),
nn.ReLU(),
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256,128),
nn.ReLU(),
nn.Linear(128,2)
)
if DEVICE == 'cuda':
MLP.to('cuda')

MLP.class_to_idx = images['train'].class_to_idx
MLP.idx_to_class = {
idx: class_
for class_, idx in images['train'].class_to_idx.items()
}
list(MLP.idx_to_class.items())[:10]

[(0, 'bear'), (1, 'panda')]

summary(MLP, input_size = (150528,))

----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Linear-1 [-1, 900] 135,476,100
ReLU-2 [-1, 900] 0
Linear-3 [-1, 512] 461,312
ReLU-4 [-1, 512] 0
Linear-5 [-1, 256] 131,328
ReLU-6 [-1, 256] 0
Linear-7 [-1, 128] 32,896
ReLU-8 [-1, 128] 0
Linear-9 [-1, 2] 258
================================================================
Total params: 136,101,894
Trainable params: 136,101,894
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 0.03
Params size (MB): 519.19
Estimated Total Size (MB): 519.79
----------------------------------------------------------------

total_params = sum(p.numel() for p in MLP.parameters())


total_params

136101894

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(MLP.parameters())
n_epochs = 10

train_losses = []
val_losses = []
train_accuracy = []
valid_accuracy = []
training_time = []

writer = SummaryWriter(f'runs/MLP/')
step = 0
overall_start_time = time()
for epoch in range(n_epochs):
train_loss = 0.0
val_loss = 0.0
train_acc = 0
valid_acc = 0

# Train the model


MLP.train()
start_time = time()
for data, target in dataloaders['train']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = MLP(data.view(data.size(0), -1))
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
train_acc += acc.item() * data.size(0)

writer.add_scalar('Trainig Loss/ Iteration', loss, global_step = step)


writer.add_scalar('Training Accuracy / Iteration', acc, global_step = step)

step += 1

train_time = time() - start_time


print(f'Training time in {epoch+1} is {train_time}\n')

# Evaluate the model


MLP.eval()
for data, target in dataloaders['val']:
if DEVICE == 'cuda':
data, target = data.cuda(), target.cuda()
output = MLP(data.view(data.size(0), -1))
loss = criterion(output, target)
val_loss += loss.item()*data.size(0)

pred = torch.argmax(input = output, dim = 1)


correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
valid_acc += acc.item() * data.size(0)

# Calculate average losses


train_loss = train_loss/len(dataloaders['train'].sampler)
val_loss = val_loss/len(dataloaders['val'].sampler)
train_acc = train_acc / len(dataloaders['train'].sampler)
valid_acc = valid_acc / len(dataloaders['val'].sampler)

train_losses.append(train_loss)
val_losses.append(val_loss)
train_accuracy.append(train_acc)
valid_accuracy.append(valid_acc)
training_time.append(train_time)

# Print training/validation statistics


print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
epoch+1,
train_loss,
val_loss
))

overall_train_time = time() - overall_start_time


print(f'Overall time taken for {epoch} including training and validation {overall_train_time}')

Training time in 1 is 7.442136526107788

Epoch: 1 Training Loss: 3.018635 Validation Loss: 3.662441


Training time in 2 is 7.357541084289551

Epoch: 2 Training Loss: 1.792229 Validation Loss: 0.422779


Training time in 3 is 7.337904214859009

Epoch: 3 Training Loss: 0.939166 Validation Loss: 3.253857


Training time in 4 is 7.434175968170166

Epoch: 4 Training Loss: 0.608798 Validation Loss: 1.662657


Training time in 5 is 7.275660276412964

Epoch: 5 Training Loss: 0.537548 Validation Loss: 13.349070


Training time in 6 is 7.454406976699829

Epoch: 6 Training Loss: 1.231312 Validation Loss: 2.227165


Training time in 7 is 7.239527702331543

Epoch: 7 Training Loss: 0.605446 Validation Loss: 0.353173


Training time in 8 is 7.429474353790283

Epoch: 8 Training Loss: 0.327622 Validation Loss: 1.013269


Training time in 9 is 7.262490510940552

Epoch: 9 Training Loss: 0.290014 Validation Loss: 0.321266


Training time in 10 is 7.452860116958618

Epoch: 10 Training Loss: 0.566660 Validation Loss: 0.169961


Overall time taken for 9 including training and validation 75.3909842967987

mlp_acc_loss_time = pd.DataFrame([], columns = ['TrainingLoss','ValidationLoss', 'TrainingTime', 'TrainingAccuracy','ValidAccuracy', 'Te


mlp_acc_loss_time = pd.DataFrame({'TrainingLoss' : train_losses,
'ValidationLoss' : val_losses,
'TrainingTime' : training_time,
'TrainingAccuracy' : train_accuracy,
'ValidAccuracy' : valid_accuracy,
'TotalParameters':total_params
})
mlp_acc_loss_time
TrainingLoss ValidationLoss TrainingTime TrainingAccuracy ValidAccuracy TotalParameters

0 3.018635 3.662441 7.442137 0.557143 0.40 136101894

1 1.792229 0.422779 7.357541 0.735714 0.85 136101894

2 0.939166 3.253857 7.337904 0.785714 0.60 136101894

3 0.608798 1.662657 7.434176 0.864286 0.80 136101894

4 0.537548 13.349070 7.275660 0.914286 0.40 136101894

5 1.231312 2.227165 7.454407 0.828571 0.60 136101894

6 0.605446 0.353173 7.239528 0.921429 0.95 136101894

7 0.327622 1.013269 7.429474 0.964286 0.85 136101894

8 0.290014 0.321266 7.262491 0.978571 0.95 136101894

9 0.566660 0.169961 7.452860 0.842857 0.95 136101894

fig , ax =plt.subplots(nrows=1,ncols =2, figsize = (15,5))


ax[0].plot( range(10), mlp_acc_loss_time['TrainingLoss'], label= 'Training Loss')
ax[0].plot( range(10), mlp_acc_loss_time['ValidationLoss'], label= 'Validation Loss')
ax[1].plot( range(10), mlp_acc_loss_time['TrainingAccuracy'], label= 'Training Accuracy')
ax[1].plot( range(10), mlp_acc_loss_time['ValidAccuracy'], label= 'Validation Accuracy')
ax[0].legend()
ax[1].legend()

<matplotlib.legend.Legend at 0x79379d49b0a0>

test_acc = 0
step = 0

test_images = []
test_labels = []
predictions = []

for input, target in dataloaders['test']:


if DEVICE == 'cuda':
input, target = input.cuda(), target.cuda()

output = MLP(input.view(input.size(0), -1))


pred = torch.argmax(input=output, dim=1)

correct_labels = pred.eq(target.data.view_as(pred))
acc = torch.mean(correct_labels.type(torch.FloatTensor))
test_acc += acc.item() * input.size(0)

test_images.append(input.cpu())
test_labels.append(target.cpu())
predictions.append(pred.cpu())

img = input[0].cpu().squeeze().permute(1, 2, 0).numpy()


img = np.clip(img, 0, 1)

fig, ax = plt.subplots()
ax.imshow(img)
ax.set_title(f'Predicted: {vgg3.idx_to_class[pred[0].item()]}')
ax.set_axis_off()
writer.add_scalar('Testing Accuracy / Iteration', acc, global_step=step)
writer.add_figure('Test images & Label', fig, global_step=step)

step += 1

test_acc = test_acc / len(dataloaders['test'].sampler)


mlp_acc_loss_time['TestAccuracy'] = np.repeat(test_acc, len(mlp_acc_loss_time))

print(f'Testing accuracy is {test_acc}')

test_images = torch.cat(test_images, dim=0)


test_labels = torch.cat(test_labels, dim=0)
predictions = torch.cat(predictions, dim=0)

fig, axes = plt.subplots(nrows=5, ncols=4, figsize=(12, 12))


axes = axes.flatten()

for i, ax in enumerate(axes):
if i < test_images.size(0):
img = test_images[i].squeeze().permute(1, 2, 0).numpy()
img = np.clip(img, 0, 1)
ax.imshow(img)
ax.set_title(f'Pred: {MLP.idx_to_class[predictions[i].item()]}', fontsize=10)
ax.set_axis_off()

plt.tight_layout()
writer.add_figure('All Test Images & Predictions', fig)
plt.show()

Testing accuracy is 0.625

%reload_ext tensorboard

%tensorboard --logdir runs/MLP

TensorBoard TIME SERIES SCALARS IMAGES INACTIVE

Filter runs (regex) Filter tags (regex) All Scalars Image Histogram Settings

Run Pinned  Settings 

Pin cards for a quick view and comparison GENERAL


.
Horizontal Axis
Test images & Label
Step

Enable step selection and data ta


Test images & Label
(Scalars only)
. Step 4
Enable Range Selection

Link by step 4

Card Width

SCALARS
Smoothing

0.6

Tooltip sorting method

Alphabetical
Testing Accuracy
Ignore outliers in chart scaling

Testing Accuracy/ Iteration Partition non-monotonic X axis

1.2

HISTOGRAMS
1
Mode
0.8  
Offset
Start coding or generate with AI.

keyboard_arrow_down Results
combined_table = pd.concat([vgg1_acc_loss_time.mean(),vgg3_acc_loss_time.mean(), vgg3_aug_acc_loss_time.mean(),vgg16_acc_loss_time.mean(
combined_table.index = ['VGG1','VGG3','VGG3_augmentation','VGG16_FC','VGG16_entire','MLP']
combined_table

TrainingLoss ValidationLoss TrainingTime TrainingAccuracy ValidAccuracy TotalParameters TestAccuracy

VGG1 2.267763 1.045630 3.528747 0.918571 0.700 50468098.0 0.825

VGG3 0.295836 0.854890 1.586484 0.880714 0.765 11169218.0 0.850

VGG3_augmentation 0.459577 0.573380 2.581449 0.775000 0.695 11169218.0 0.775

VGG16_FC 1.397975 6.096927 10.255436 0.962857 0.915 134268738.0 0.925

VGG16_entire 0.777951 0.901324 18.562936 0.533571 0.200 134268738.0 0.500

MLP 0.991743 2.643564 7.368618 0.839286 0.735 136101894.0 0.625

keyboard_arrow_down Theory
Are the results as expected? Why or why not?

VGG1, being the simplest, shows lower accuracy compared to other models, which have more layers and can capture more complex features
from the images VGG3 and VGG3_aug should perform slightly better than VGG1 due to the increased depth and the effect of data
augmentation. Data augmentation (such as random rotations, flips, or color changes) usually improves the generalization of the model by
preventing overfitting on the training data, which may explain better performance

VGG16: This model is typically stronger in performance because it has many more layers, allowing it to capture finer details in images, as it has
more parameters and can capture complex features. When fine-tuning the entire VGG16 model, the entire model adapts to the specific task of
distinguishing between pandas and bears. Fine-tuning allows the model to better adjust the convolutional layers for specific features in the new
dataset.

MLP (Multilayer Perceptron): MLP models typically underperform on image data compared to CNN-based models like VGG. Since MLPs don’t
have convolutional layers, they don't capture spatial hierarchies effectively, making them less suitable for tasks involving images. However, if
trained properly, MLPs may still give reasonable performance on simpler datasets.

Does data augmentation help? Why or why not?

Data augmentation artificially increases the size and variability of the training dataset by applying random transformations (e.g., rotation,
flipping, zoom, cropping). This helps the models generalize better, especially in tasks like distinguishing between pandas and bears, where
visual differences might be subtle. For example, an ant on a leaf might look very similar to a dog in the background of an image. Augmentation
makes sure the model doesn’t memorize specific features of the training set but learns generalizable patterns.

Does it matter how many epochs you fine-tune the model? Why or why not?

Yes, the number of epochs is critical in fine-tuning. Underfitting and overfitting: If you fine-tune for too few epochs, the model won’t have enough
time to adjust its weights, especially the convolutional layers, to the new dataset (dogs vs ants). The model will perform poorly on the test set.

Are there any particular images that the model is confused about? Why or why not?

Intra-class variability: Both pandas and bears can appear in a variety of poses, colors, and sizes. The model could get confused if the dog or ant
in the image has an unusual appearance. For instance, a fluffy white dog may resemble a large white ant in certain environments.

Inter-class similarity: Ants and dogs may share visual similarities, especially in terms of color patterns, textures, and object sizes. For example,
ants may appear as small, textured, dark objects on a similarly colored background, which may resemble a dog’s fur in certain positions.

import torch
from torchvision import transforms
from PIL import Image
import matplotlib.pyplot as plt

transform = transforms.Compose([
transforms.Resize((224, 224)),
t f T T ()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy