0% found this document useful (0 votes)
4 views5 pages

Final Performance

The document outlines a deep learning model training process using PyTorch, achieving a train accuracy of approximately 91%, validation accuracy of 79.29%, and test accuracy of 77.84%. It includes details on data preprocessing, model architecture, loss function, optimizer, and training loop with early stopping. The model is based on EfficientNet and utilizes mixed precision training for improved performance.

Uploaded by

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

Final Performance

The document outlines a deep learning model training process using PyTorch, achieving a train accuracy of approximately 91%, validation accuracy of 79.29%, and test accuracy of 77.84%. It includes details on data preprocessing, model architecture, loss function, optimizer, and training loop with early stopping. The model is based on EfficientNet and utilizes mixed precision training for improved performance.

Uploaded by

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

Final performance:

Train Accuracy: ~91%


Validation Accuracy: ~79.29% (best)
Test Accuracy: 77.84%

import torch
import [Link] as nn
import timm
from torchvision import datasets, transforms
from [Link] import DataLoader
from collections import Counter

# =========================
# SETTINGS
# =========================
BATCH_SIZE = 16
EPOCHS = 30
LR = 5e-5 # safe increase from your working version

device = "cuda" if [Link].is_available() else "cpu"


print("Device:", device)

# SPLIT_DIR must already exist


# SPLIT_DIR = "/content/dataset_split"

# =========================
# TRANSFORMS (SAFE VERSION)
# =========================
train_transform = [Link]([
[Link]((256, 256)),
[Link](224, scale=(0.9, 1.0)),
[Link](p=0.5),
[Link](8),
[Link](
brightness=0.15,
contrast=0.15,
saturation=0.15
),
[Link](),
[Link](
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]
)
])

val_transform = [Link]([
[Link]((224, 224)),
[Link](),
[Link](
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]
)
])

# =========================
# DATASETS
# =========================
train_data = [Link](
f"{SPLIT_DIR}/train",
transform=train_transform
)

val_data = [Link](
f"{SPLIT_DIR}/val",
transform=val_transform
)

test_data = [Link](
f"{SPLIT_DIR}/test",
transform=val_transform
)

print("Classes:", train_data.class_to_idx)

# =========================
# CLASS WEIGHTS
# =========================
class_counts = Counter(train_data.targets)
class_counts = [Link](list(class_counts.values()), dtype=[Link])

class_weights = 1.0 / class_counts


class_weights = class_weights / class_weights.sum() * len(class_counts)
class_weights = class_weights.to(device)

# =========================
# DATALOADERS
# =========================
train_loader = DataLoader(
train_data,
batch_size=BATCH_SIZE,
shuffle=True,
num_workers=2,
pin_memory=True
)

val_loader = DataLoader(
val_data,
batch_size=BATCH_SIZE,
shuffle=False,
num_workers=2,
pin_memory=True
)

test_loader = DataLoader(
test_data,
batch_size=BATCH_SIZE,
shuffle=False,
num_workers=2,
pin_memory=True
)

# =========================
# MODEL (STABLE)
# =========================
model = timm.create_model(
"efficientnet_b0", # back to stable version
pretrained=True,
num_classes=len(train_data.classes),
drop_rate=0.3
).to(device)

# =========================
# LOSS (NO LABEL SMOOTHING)
# =========================
criterion = [Link](weight=class_weights)

# =========================
# OPTIMIZER
# =========================
optimizer = [Link](
[Link](),
lr=LR,
weight_decay=1e-4
)

# =========================
# SCHEDULER
# =========================
scheduler = [Link].lr_scheduler.ReduceLROnPlateau(
optimizer,
mode="min",
factor=0.5,
patience=2
)

# =========================
# MIXED PRECISION (SAFE)
# =========================
scaler = [Link]()

# =========================
# EARLY STOPPING
# =========================
best_val_acc = 0
patience = 5
counter = 0

# =========================
# TRAINING LOOP
# =========================
for epoch in range(EPOCHS):

print(f"\n========== Epoch {epoch+1}/{EPOCHS} ==========")

# ---------------- TRAIN ----------------


[Link]()
train_loss = 0
correct = 0
total = 0

for images, labels in train_loader:


images, labels = [Link](device), [Link](device)

optimizer.zero_grad()
with [Link]():
outputs = model(images)
loss = criterion(outputs, labels)

[Link](loss).backward()
[Link](optimizer)
[Link]()

train_loss += [Link]()
_, preds = [Link](outputs, 1)

correct += (preds == labels).sum().item()


total += [Link](0)

train_acc = 100 * correct / total


train_loss /= len(train_loader)

print(f"Train Loss: {train_loss:.4f} | Train Acc: {train_acc:.2f}%")

# ---------------- VALIDATION ----------------


[Link]()
val_loss = 0
val_correct = 0
val_total = 0

with torch.no_grad():
for images, labels in val_loader:
images, labels = [Link](device), [Link](device)

with [Link]():
outputs = model(images)
loss = criterion(outputs, labels)

val_loss += [Link]()
_, preds = [Link](outputs, 1)

val_correct += (preds == labels).sum().item()


val_total += [Link](0)

val_acc = 100 * val_correct / val_total


val_loss /= len(val_loader)

print(f"Val Loss: {val_loss:.4f} | Val Acc: {val_acc:.2f}%")

[Link](val_loss)

# ---------------- SAVE BEST ----------------


if val_acc > best_val_acc:
best_val_acc = val_acc
counter = 0
[Link](model.state_dict(), "best_model.pth")
print("✅ Best model saved!")
else:
counter += 1
print(f"⚠️ No improvement ({counter}/{patience})")

# ---------------- EARLY STOP ----------------


if counter >= patience:
print("🛑 Early stopping triggered")
break

# =========================
# TESTING
# =========================
print("\n🚀 Evaluating on TEST set...")

model.load_state_dict([Link]("best_model.pth"))
[Link]()

test_loss = 0
test_correct = 0
test_total = 0

with torch.no_grad():
for images, labels in test_loader:
images, labels = [Link](device), [Link](device)

with [Link]():
outputs = model(images)
loss = criterion(outputs, labels)

test_loss += [Link]()
_, preds = [Link](outputs, 1)

test_correct += (preds == labels).sum().item()


test_total += [Link](0)

test_acc = 100 * test_correct / test_total


test_loss /= len(test_loader)

print("\n=========== FINAL TEST RESULTS ===========")


print(f"Test Loss: {test_loss:.4f}")
print(f"Test Accuracy: {test_acc:.2f}%")
print(f"Correct: {test_correct}/{test_total}")
print("=========================================")

You might also like