Vehicle Type Recognition with Deep Learning
Vehicle Type Recognition with Deep Learning
Enroll:01-136221-054
BS-AI-6A
Dataset Link:[Link]
# Define transformations
transform = [Link]([
[Link]((224, 224)), # Standard input size for most
models
[Link](),
[Link](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([Link])
print(f"Number of classes: {num_classes}")
print(f"Classes: {[Link]}")
# Training loop
for epoch in range(epochs):
[Link]()
running_loss = 0.0
for inputs, labels in train_loader:
inputs, labels = [Link](device), [Link](device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
[Link]()
[Link]()
running_loss += [Link]()
print(f"Epoch {epoch+1}/{epochs} - Loss:
{running_loss/len(train_loader):.4f}")
# Evaluation
[Link]()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = [Link](device), [Link](device)
outputs = model(inputs)
_, preds = [Link](outputs, 1)
all_preds.extend([Link]().numpy())
all_labels.extend([Link]().numpy())
# Calculate metrics
all_preds = [Link](all_preds)
all_labels = [Link](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.
[Link](
/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.
[Link](msg)
Downloading: "[Link]
[Link]" to /root/.cache/torch/hub/checkpoints/alexnet-owt-
[Link]
100%|██████████| 233M/233M [00:01<00:00, 150MB/s]
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/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"{[Link]()} is",
len(result))
# Training loop
for epoch in range(epochs):
[Link]()
running_loss = 0.0
for inputs, labels in tqdm(train_loader, desc=f"Epoch
{epoch+1}/{epochs}"):
inputs, labels = [Link](device), [Link](device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
[Link]()
[Link]()
running_loss += [Link]()
# Evaluation
[Link]()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = [Link](device), [Link](device)
outputs = model(inputs)
_, preds = [Link](outputs, 1)
all_preds.extend([Link]().numpy())
all_labels.extend([Link]().numpy())
# Calculate metrics
all_preds = [Link](all_preds)
all_labels = [Link](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.
[Link](
/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.
[Link](msg)
Downloading: "[Link]
to /root/.cache/torch/hub/checkpoints/[Link]
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/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
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"{[Link]()} is",
len(result))
import torch
from torch import nn, optim
from torchvision import datasets, transforms
from [Link] import DataLoader, random_split
from tqdm import tqdm
from [Link] import precision_recall_fscore_support,
accuracy_score
import numpy as np
# Training loop
for epoch in range(epochs):
[Link]()
running_loss = 0.0
for inputs, labels in tqdm(train_loader, desc=f"Epoch
{epoch+1}/{epochs}"):
inputs, labels = [Link](device), [Link](device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
[Link]()
[Link]()
running_loss += [Link]()
print(f"Epoch {epoch+1}/{epochs} - Loss:
{running_loss/len(train_loader):.4f}")
# Evaluation
[Link]()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = [Link](device), [Link](device)
outputs = model(inputs)
_, preds = [Link](outputs, 1)
all_preds.extend([Link]().numpy())
all_labels.extend([Link]().numpy())
# Calculate metrics
all_preds = [Link](all_preds)
all_labels = [Link](all_labels)
# Dataset Preparation
data_dir = r"/content/final/Dataset" # Update with your dataset path
transform = [Link]([
[Link]((224, 224)), # Resize for LeNet input
[Link](),
[Link](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([Link])
print(f"Number of classes: {num_classes}")
print(f"Classes: {[Link]}")
import torch
import [Link] as nn
import [Link] as optim
from [Link] import DataLoader, random_split
from torchvision import datasets, transforms
from [Link] import Variable
from tqdm import tqdm
import os
from PIL import Image
# 1. ZFNet Model Definition
class ZFNet([Link]):
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
[Link] = 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
[Link]()
running_loss = 0.0
running_corrects = 0
optimizer.zero_grad()
# Forward
outputs = model(inputs)
loss = criterion(outputs, labels)
# Backward
[Link]()
[Link]()
# Statistics
_, preds = [Link](outputs, 1)
running_loss += [Link]() * [Link](0)
running_corrects += [Link](preds == [Link])
# Validation phase
[Link]()
running_corrects = 0
all_preds = []
all_labels = []
with torch.no_grad():
outputs = model(inputs)
_, preds = [Link](outputs, 1)
# Calculate metrics
all_preds = [Link](all_preds)
all_labels = [Link](all_labels)
Epoch 0/24
----------
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classificatio
[Link]: 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"{[Link]()} is",
len(result))
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
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/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
import torch
from torch import nn, optim
from torchvision import datasets, transforms, models
from [Link] import DataLoader, random_split
from tqdm import tqdm
# Dataset path
data_dir = r"/content/final/Dataset" # Update with your dataset path
# Load dataset
dataset = [Link](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([Link])
print(f"Number of classes: {num_classes}")
print(f"Classes: {[Link]}")
# Evaluation
[Link]()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = [Link](device), [Link](device)
outputs = model(inputs)
_, preds = [Link](outputs, 1)
# Calculate metrics
all_preds = [Link](all_preds)
all_labels = [Link](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.
[Link](
/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.
[Link](msg)
Downloading: "[Link]
[Link]" to /root/.cache/torch/hub/checkpoints/resnet50-
[Link]
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/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
import torch
from torch import nn, optim
from torchvision import datasets, transforms, models
from [Link] import DataLoader, random_split
from tqdm import tqdm
# Dataset path
data_dir = r"/content/final/Dataset" # Update with your dataset path
# Load dataset
dataset = [Link](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([Link])
print(f"Number of classes: {num_classes}")
print(f"Classes: {[Link]}")
# Evaluation
[Link]()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = [Link](device), [Link](device)
outputs = model(inputs)
_, preds = [Link](outputs, 1)
# Calculate metrics
all_preds = [Link](all_preds)
all_labels = [Link](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.
[Link](
/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.
[Link](msg)
Epoch 1/5: 100%|██████████| 9/9 [00:13<00:00, 1.49s/it]
/usr/local/lib/python3.10/dist-packages/PIL/[Link]:
UserWarning: Palette images with Transparency expressed in bytes
should be converted to RGBA images
[Link](
/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"{[Link]()} is",
len(result))