Lab 9
Lab 9
Enroll:01-136221-054
BS-AI-6A
Dataset Link:https://www.kaggle.com/datasets/kaggleashwin/vehicle-type-recognition/data
# Define transformations
transform = transforms.Compose([
transforms.Resize((224, 224)), # Standard input size for most
models
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229,
0.224, 0.225])
])
# Create DataLoaders
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size,
shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size,
shuffle=False)
# Number of classes
num_classes = len(dataset.classes)
print(f"Number of classes: {num_classes}")
print(f"Classes: {dataset.classes}")
# Training loop
for epoch in range(epochs):
model.train()
running_loss = 0.0
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch {epoch+1}/{epochs} - Loss:
{running_loss/len(train_loader):.4f}")
# Evaluation
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
# Calculate metrics
all_preds = np.array(all_preds)
all_labels = np.array(all_labels)
Number of classes: 4
Classes: ['Bus', 'Car', 'Truck', 'motorcycle']
Using device: cuda
/usr/local/lib/python3.10/dist-packages/torchvision/models/
_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated
since 0.13 and may be removed in the future, please use 'weights'
instead.
warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:2
23: UserWarning: Arguments other than a weight enum or `None` for
'weights' are deprecated since 0.13 and may be removed in the future.
The current behavior is equivalent to passing
`weights=AlexNet_Weights.IMAGENET1K_V1`. You can also use
`weights=AlexNet_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/alexnet-owt-
7be5be79.pth" to /root/.cache/torch/hub/checkpoints/alexnet-owt-
7be5be79.pth
100%|██████████| 233M/233M [00:01<00:00, 150MB/s]
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/
_classification.py:1531: UndefinedMetricWarning: Precision is ill-
defined and being set to 0.0 in labels with no predicted samples. Use
`zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
# Training loop
for epoch in range(epochs):
model.train()
running_loss = 0.0
for inputs, labels in tqdm(train_loader, desc=f"Epoch
{epoch+1}/{epochs}"):
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
# Evaluation
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
# Calculate metrics
all_preds = np.array(all_preds)
all_labels = np.array(all_labels)
/usr/local/lib/python3.10/dist-packages/torchvision/models/
_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated
since 0.13 and may be removed in the future, please use 'weights'
instead.
warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:2
23: UserWarning: Arguments other than a weight enum or `None` for
'weights' are deprecated since 0.13 and may be removed in the future.
The current behavior is equivalent to passing
`weights=VGG16_Weights.IMAGENET1K_V1`. You can also use
`weights=VGG16_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth"
to /root/.cache/torch/hub/checkpoints/vgg16-397923af.pth
100%|██████████| 528M/528M [00:03<00:00, 157MB/s]
Epoch 1/5: 67%|██████▋ | 6/9 [00:10<00:05,
1.72s/it]/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
Epoch 1/5: 100%|██████████| 9/9 [00:14<00:00, 1.56s/it]
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/
_classification.py:1531: UndefinedMetricWarning: Precision is ill-
defined and being set to 0.0 in labels with no predicted samples. Use
`zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
import torch
from torch import nn, optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader, random_split
from tqdm import tqdm
from sklearn.metrics import precision_recall_fscore_support,
accuracy_score
import numpy as np
# Training loop
for epoch in range(epochs):
model.train()
running_loss = 0.0
for inputs, labels in tqdm(train_loader, desc=f"Epoch
{epoch+1}/{epochs}"):
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch {epoch+1}/{epochs} - Loss:
{running_loss/len(train_loader):.4f}")
# Evaluation
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
# Calculate metrics
all_preds = np.array(all_preds)
all_labels = np.array(all_labels)
# Dataset Preparation
data_dir = r"/content/final/Dataset" # Update with your dataset path
transform = transforms.Compose([
transforms.Resize((224, 224)), # Resize for LeNet input
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229,
0.224, 0.225])
])
# Create DataLoaders
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size,
shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size,
shuffle=False)
# Number of classes
num_classes = len(dataset.classes)
print(f"Number of classes: {num_classes}")
print(f"Classes: {dataset.classes}")
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, random_split
from torchvision import datasets, transforms
from torch.autograd import Variable
from tqdm import tqdm
import os
from PIL import Image
# 1. ZFNet Model Definition
class ZFNet(nn.Module):
def __init__(self, num_classes):
super(ZFNet, self).__init__()
# Convolutional Layers
self.conv1 = nn.Conv2d(3, 96, kernel_size=7, stride=2,
padding=1) # Output: 96 x 111 x 111
self.conv2 = nn.Conv2d(96, 256, kernel_size=5, stride=2,
padding=1) # Output: 256 x 27 x 27
self.conv3 = nn.Conv2d(256, 384, kernel_size=3, padding=1) #
Output: 384 x 13 x 13
self.conv4 = nn.Conv2d(384, 384, kernel_size=3, padding=1) #
Output: 384 x 13 x 13
self.conv5 = nn.Conv2d(384, 256, kernel_size=3, padding=1) #
Output: 256 x 6 x 6
# Pooling Layer
self.pool = nn.MaxPool2d(kernel_size=3, stride=2)
# Path to your dataset folder (with subfolders like Bus, Car, etc.)
data_dir = '/content/final/Dataset' # Change this path to your
dataset directory
# Calculate the size for train/val split (80% train, 20% validation)
train_size = int(0.8 * len(dataset))
val_size = len(dataset) - train_size
# Training phase
model.train()
running_loss = 0.0
running_corrects = 0
optimizer.zero_grad()
# Forward
outputs = model(inputs)
loss = criterion(outputs, labels)
# Backward
loss.backward()
optimizer.step()
# Statistics
_, preds = torch.max(outputs, 1)
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
# Validation phase
model.eval()
running_corrects = 0
all_preds = []
all_labels = []
with torch.no_grad():
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
# Calculate metrics
all_preds = np.array(all_preds)
all_labels = np.array(all_labels)
Epoch 0/24
----------
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
n.py:1531: UndefinedMetricWarning: Precision is ill-defined and being
set to 0.0 in labels with no predicted samples. Use `zero_division`
parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
Validation Accuracy: 58.75%
Precision: 0.6079
Recall: 0.5875
F1-Score: 0.5611
Epoch 23/24
----------
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
import torch
from torch import nn, optim
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader, random_split
from tqdm import tqdm
# Dataset path
data_dir = r"/content/final/Dataset" # Update with your dataset path
# Load dataset
dataset = datasets.ImageFolder(data_dir, transform=transform)
# Create DataLoaders
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size,
shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size,
shuffle=False)
# Number of classes
num_classes = len(dataset.classes)
print(f"Number of classes: {num_classes}")
print(f"Classes: {dataset.classes}")
# Evaluation
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
# Calculate metrics
all_preds = np.array(all_preds)
all_labels = np.array(all_labels)
# Call the updated function to train and evaluate the model with
metrics
train_and_evaluate_resnet_with_metrics(train_loader, test_loader,
model, criterion, optimizer, num_classes, epochs=5)
Number of classes: 4
Classes: ['Bus', 'Car', 'Truck', 'motorcycle']
Using device: cuda
/usr/local/lib/python3.10/dist-packages/torchvision/models/
_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated
since 0.13 and may be removed in the future, please use 'weights'
instead.
warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:2
23: UserWarning: Arguments other than a weight enum or `None` for
'weights' are deprecated since 0.13 and may be removed in the future.
The current behavior is equivalent to passing
`weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use
`weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/resnet50-
0676ba61.pth" to /root/.cache/torch/hub/checkpoints/resnet50-
0676ba61.pth
100%|██████████| 97.8M/97.8M [00:00<00:00, 160MB/s]
Epoch 1/5: 100%|██████████| 9/9 [00:10<00:00, 1.19s/it]
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
import torch
from torch import nn, optim
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader, random_split
from tqdm import tqdm
# Dataset path
data_dir = r"/content/final/Dataset" # Update with your dataset path
# Load dataset
dataset = datasets.ImageFolder(data_dir, transform=transform)
# Create DataLoaders
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size,
shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size,
shuffle=False)
# Number of classes
num_classes = len(dataset.classes)
print(f"Number of classes: {num_classes}")
print(f"Classes: {dataset.classes}")
# Evaluation
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
# Calculate metrics
all_preds = np.array(all_preds)
all_labels = np.array(all_labels)
# Call the updated function to train and evaluate the model with
metrics
train_and_evaluate_vgg16_with_metrics(train_loader, test_loader,
model, criterion, optimizer, num_classes, epochs=5)
Number of classes: 4
Classes: ['Bus', 'Car', 'Truck', 'motorcycle']
Using device: cuda
/usr/local/lib/python3.10/dist-packages/torchvision/models/
_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated
since 0.13 and may be removed in the future, please use 'weights'
instead.
warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:2
23: UserWarning: Arguments other than a weight enum or `None` for
'weights' are deprecated since 0.13 and may be removed in the future.
The current behavior is equivalent to passing
`weights=VGG16_Weights.IMAGENET1K_V1`. You can also use
`weights=VGG16_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
Epoch 1/5: 100%|██████████| 9/9 [00:13<00:00, 1.49s/it]
/usr/local/lib/python3.10/dist-packages/PIL/Image.py:1054:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/
_classification.py:1531: UndefinedMetricWarning: Precision is ill-
defined and being set to 0.0 in labels with no predicted samples. Use
`zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is",
len(result))