Appendix B: Project Snapshots and
Description
B.2 Module 2: Data Preprocessing and Transformation
Description:
This module prepares input images before training. It includes resizing, normalization, and
augmentation.
These steps improve model generalization and robustness.
Code:
from torchvision import transforms
img_size = 224
train_tf = [Link]([
[Link](),
[Link]((img_size, img_size)),
[Link](),
[Link](10),
[Link](),
[Link](mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
])
test_tf = [Link]([
[Link](),
[Link]((img_size, img_size)),
[Link](),
[Link](mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
])
B.3 Module 3: DataLoader Creation
Description:
This module loads data efficiently using batching and shuffling for training.
Code:
from [Link] import DataLoader
train_ds = ImgDataset(train_r_path, train_f_path, train_tf)
test_ds = ImgDataset(test_r_path, test_f_path, test_tf)
train_loader = DataLoader(train_ds, batch_size=32, shuffle=True)
test_loader = DataLoader(test_ds, batch_size=32, shuffle=False)
B.4 Module 4: Model Architecture (CNN Model)
Description:
Defines CNN architecture for feature extraction and classification.
Code:
import torch
import [Link] as nn
import [Link] as F
class CNNModel([Link]):
def __init__(self):
super(CNNModel, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
[Link] = nn.MaxPool2d(2, 2)
self.fc1 = [Link](128 * 28 * 28, 128)
self.fc2 = [Link](128, 1)
def forward(self, x):
x = [Link]([Link](self.conv1(x)))
x = [Link]([Link](self.conv2(x)))
x = [Link]([Link](self.conv3(x)))
x = [Link]([Link](0), -1)
x = [Link](self.fc1(x))
x = [Link](self.fc2(x))
return x
B.5 Module 5: Training Loop
Description:
Handles model training using forward pass, loss computation, and backpropagation.
Code:
import [Link] as optim
model = CNNModel().to(device)
criterion = [Link]()
optimizer = [Link]([Link](), lr=0.0003)
epochs = 12
for epoch in range(epochs):
[Link]()
running_loss = 0
for imgs, labels in train_loader:
imgs, labels = [Link](device), [Link]().to(device)
optimizer.zero_grad()
outputs = model(imgs).squeeze()
loss = criterion(outputs, labels)
[Link]()
[Link]()
running_loss += [Link]()
print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader):.4f}")
B.6 Module 6: Evaluation and Prediction
Description:
Evaluates model performance using confusion matrix and classification metrics.
Code:
from [Link] import confusion_matrix, classification_report
[Link]()
all_preds = []
all_labels = []
with torch.no_grad():
for imgs, labels in test_loader:
imgs = [Link](device)
outputs = model(imgs).squeeze()
preds = (outputs > 0.5).int().cpu()
all_preds.extend([Link]())
all_labels.extend([Link]())
print(confusion_matrix(all_labels, all_preds))
print(classification_report(all_labels, all_preds))