Spaces:
Sleeping
Sleeping
File size: 19,077 Bytes
af61511 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | """
model.py – Define, train, and evaluate all three models:
1. Naive baseline (majority class classifier)
2. Classical ML (Random Forest on HOG features)
3. Deep learning (ScribblNet CNN)
Also runs the training size sensitivity experiment and saves results/plots.
Usage:
python scripts/model.py
"""
import json
import sys
import time
from pathlib import Path
from typing import Any
import joblib
import matplotlib
matplotlib.use("Agg") # headless backend
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import (
accuracy_score,
classification_report,
confusion_matrix,
)
from sklearn.preprocessing import StandardScaler
from torch.utils.data import DataLoader, TensorDataset
import seaborn as sns
sys.path.insert(0, str(Path(__file__).parent.parent))
from config import (
CLASSES,
MODELS_DIR,
OUTPUTS_DIR,
PROCESSED_DIR,
NUM_CLASSES,
RF_MAX_DEPTH,
RF_N_ESTIMATORS,
DEEP_BATCH_SIZE,
DEEP_EPOCHS,
DEEP_LR,
DEEP_WEIGHT_DECAY,
IMG_SIZE,
EXPERIMENT_FRACTIONS,
EXPERIMENT_EPOCHS,
)
# Utility
def get_device() -> torch.device:
"""Return the best available torch device (MPS > CUDA > CPU)."""
if torch.backends.mps.is_available():
return torch.device("mps")
if torch.cuda.is_available():
return torch.device("cuda")
return torch.device("cpu")
def load_processed_data() -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
"""Load all processed arrays from disk.
Returns:
X_train_raw, X_test_raw, y_train, y_test, X_train_hog, X_test_hog
"""
X_train_raw = np.load(PROCESSED_DIR / "X_train_raw.npy")
X_test_raw = np.load(PROCESSED_DIR / "X_test_raw.npy")
y_train = np.load(PROCESSED_DIR / "y_train.npy")
y_test = np.load(PROCESSED_DIR / "y_test.npy")
X_train_hog = np.load(PROCESSED_DIR / "X_train_hog.npy")
X_test_hog = np.load(PROCESSED_DIR / "X_test_hog.npy")
return X_train_raw, X_test_raw, y_train, y_test, X_train_hog, X_test_hog
# 1. Naive Baseline
class MajorityClassifier:
"""Naive baseline: always predicts the most frequent class in training."""
def __init__(self) -> None:
self.majority_class: int = 0
def fit(self, y: np.ndarray) -> "MajorityClassifier":
"""Fit by finding the majority class label.
Args:
y: 1-D array of integer class labels.
Returns:
self
"""
counts = np.bincount(y)
self.majority_class = int(np.argmax(counts))
return self
def predict(self, n_samples: int) -> np.ndarray:
"""Return the majority class repeated n_samples times.
Args:
n_samples: Number of predictions to generate.
Returns:
Array of length n_samples, all equal to majority_class.
"""
return np.full(n_samples, self.majority_class, dtype=np.int64)
def train_naive(y_train: np.ndarray, y_test: np.ndarray) -> dict[str, Any]:
"""Train and evaluate the majority class baseline.
Args:
y_train: Training labels.
y_test: Test labels.
Returns:
Dictionary of evaluation metrics.
"""
print(f"\nNaive Baseline")
clf = MajorityClassifier().fit(y_train)
preds = clf.predict(len(y_test))
acc = accuracy_score(y_test, preds)
print(f" Majority class: {CLASSES[clf.majority_class]}")
print(f" Test accuracy: {acc:.4f}")
model_data = {"majority_class": clf.majority_class, "accuracy": acc}
joblib.dump(model_data, MODELS_DIR / "naive_model.pkl")
return {"model": "naive", "accuracy": acc}
# 2. Classical ML
def train_classical(
X_train_hog: np.ndarray,
X_test_hog: np.ndarray,
y_train: np.ndarray,
y_test: np.ndarray,
) -> dict[str, Any]:
"""Train Random Forest on HOG features and evaluate.
Args:
X_train_hog: Training HOG feature matrix.
X_test_hog: Test HOG feature matrix.
y_train: Training labels.
y_test: Test labels.
Returns:
Dictionary of evaluation metrics.
"""
print(f"\nClassical ML (Random Forest on HOG)")
# Standardise features
scaler = StandardScaler()
X_tr = scaler.fit_transform(X_train_hog)
X_te = scaler.transform(X_test_hog)
clf = RandomForestClassifier(
n_estimators=RF_N_ESTIMATORS,
max_depth=RF_MAX_DEPTH,
n_jobs=-1,
random_state=42,
)
t0 = time.time()
clf.fit(X_tr, y_train)
elapsed = time.time() - t0
preds = clf.predict(X_te)
acc = accuracy_score(y_test, preds)
report = classification_report(y_test, preds, target_names=CLASSES)
print(f" Training time: {elapsed:.1f}s")
print(f" Test accuracy: {acc:.4f}")
print(f"\n{report}")
joblib.dump({"clf": clf, "scaler": scaler}, MODELS_DIR / "classical_model.pkl")
_save_confusion_matrix(y_test, preds, "classical_confusion_matrix.png")
return {"model": "classical", "accuracy": acc, "training_time_s": elapsed}
# 3. Deep Model
class ScribblNet(nn.Module):
"""Lightweight CNN for 28×28 grayscale sketch classification.
Architecture:
3 × (Conv2d → BatchNorm → ReLU → MaxPool)
Dropout → FC(1152→256) → ReLU → Dropout → FC(256→num_classes)
"""
def __init__(self, num_classes: int = NUM_CLASSES) -> None:
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
)
# 28→14→7→3 ∴ feature map is 128×3×3 = 1152
self.classifier = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(128 * 3 * 3, 256),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Linear(256, num_classes),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass.
Args:
x: Tensor of shape (B, 1, 28, 28), values in [0, 1].
Returns:
Logits tensor of shape (B, num_classes).
"""
x = self.features(x)
x = x.view(x.size(0), -1)
return self.classifier(x)
def make_dataloaders(
X_raw: np.ndarray,
y: np.ndarray,
X_test_raw: np.ndarray,
y_test: np.ndarray,
batch_size: int = DEEP_BATCH_SIZE,
train_fraction: float = 1.0,
) -> tuple[DataLoader, DataLoader]:
"""Build PyTorch DataLoaders from raw pixel arrays.
Pixel values are normalised to [0, 1]. Training set can be subsampled
via train_fraction for the sensitivity experiment.
Args:
X_raw: Training pixel array (N, 784), uint8.
y: Training labels.
X_test_raw: Test pixel array.
y_test: Test labels.
batch_size: Minibatch size.
train_fraction: Fraction of training samples to use (0 < f ≤ 1).
Returns:
(train_loader, test_loader)
"""
if train_fraction < 1.0:
n = max(1, int(len(X_raw) * train_fraction))
idx = np.random.default_rng(seed=7).permutation(len(X_raw))[:n]
X_raw = X_raw[idx]
y = y[idx]
def _to_tensor(X: np.ndarray, labels: np.ndarray) -> TensorDataset:
imgs = torch.from_numpy(X.astype(np.float32) / 255.0)
imgs = imgs.view(-1, 1, IMG_SIZE, IMG_SIZE)
return TensorDataset(imgs, torch.from_numpy(labels))
train_ds = _to_tensor(X_raw, y)
test_ds = _to_tensor(X_test_raw, y_test)
train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True, num_workers=0)
test_loader = DataLoader(test_ds, batch_size=batch_size, shuffle=False, num_workers=0)
return train_loader, test_loader
def train_one_epoch(
model: nn.Module,
loader: DataLoader,
optimizer: torch.optim.Optimizer,
criterion: nn.Module,
device: torch.device,
) -> float:
"""Run one training epoch and return average loss.
Args:
model: ScribblNet instance.
loader: Training DataLoader.
optimizer: Optimiser (Adam).
criterion: Loss function (CrossEntropyLoss).
device: Torch device.
Returns:
Mean loss over all minibatches.
"""
model.train()
total_loss = 0.0
for imgs, labels in loader:
imgs, labels = imgs.to(device), labels.to(device)
optimizer.zero_grad()
loss = criterion(model(imgs), labels)
loss.backward()
optimizer.step()
total_loss += loss.item()
return total_loss / len(loader)
def evaluate(
model: nn.Module,
loader: DataLoader,
device: torch.device,
) -> tuple[float, np.ndarray]:
"""Evaluate model on a DataLoader.
Args:
model: ScribblNet instance.
loader: Evaluation DataLoader.
device: Torch device.
Returns:
(accuracy, predictions_array)
"""
model.eval()
all_preds, all_labels = [], []
with torch.no_grad():
for imgs, labels in loader:
imgs = imgs.to(device)
preds = model(imgs).argmax(dim=1).cpu().numpy()
all_preds.append(preds)
all_labels.append(labels.numpy())
preds = np.concatenate(all_preds)
labels = np.concatenate(all_labels)
return accuracy_score(labels, preds), preds
def train_deep(
X_train_raw: np.ndarray,
X_test_raw: np.ndarray,
y_train: np.ndarray,
y_test: np.ndarray,
epochs: int = DEEP_EPOCHS,
train_fraction: float = 1.0,
save_model: bool = True,
) -> dict[str, Any]:
"""Train ScribblNet and evaluate on test set.
Args:
X_train_raw: Raw training pixel array.
X_test_raw: Raw test pixel array.
y_train: Training labels.
y_test: Test labels.
epochs: Number of training epochs.
train_fraction: Fraction of training data to use.
save_model: Whether to save weights to disk.
Returns:
Dictionary of evaluation metrics and training history.
"""
print(f"\nDeep Model (ScribblNet, fraction={train_fraction:.0%})")
device = get_device()
print(f" Device: {device}")
train_loader, test_loader = make_dataloaders(
X_train_raw, y_train, X_test_raw, y_test, train_fraction=train_fraction
)
model = ScribblNet(num_classes=NUM_CLASSES).to(device)
optimizer = torch.optim.Adam(
model.parameters(), lr=DEEP_LR, weight_decay=DEEP_WEIGHT_DECAY
)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs)
criterion = nn.CrossEntropyLoss()
history = {"loss": [], "val_acc": []}
best_acc = 0.0
for epoch in range(1, epochs + 1):
loss = train_one_epoch(model, train_loader, optimizer, criterion, device)
acc, _ = evaluate(model, test_loader, device)
scheduler.step()
history["loss"].append(loss)
history["val_acc"].append(acc)
print(f" epoch {epoch:02d}/{epochs} loss={loss:.4f} val_acc={acc:.4f}")
if acc > best_acc:
best_acc = acc
if save_model:
torch.save(model.state_dict(), MODELS_DIR / "deep_model.pth")
# Final evaluation with best weights
if save_model:
model.load_state_dict(torch.load(MODELS_DIR / "deep_model.pth", map_location=device))
final_acc, final_preds = evaluate(model, test_loader, device)
print(f"\n Best test accuracy: {best_acc:.4f}")
if save_model:
report = classification_report(y_test, final_preds, target_names=CLASSES)
print(f"\n{report}")
_save_confusion_matrix(y_test, final_preds, "deep_confusion_matrix.png")
_save_training_curves(history)
return {"model": "deep", "accuracy": best_acc, "history": history}
# Experiment: Training Size Sensitivity
def run_experiment(
X_train_raw: np.ndarray,
X_test_raw: np.ndarray,
y_train: np.ndarray,
y_test: np.ndarray,
X_train_hog: np.ndarray,
X_test_hog: np.ndarray,
) -> None:
"""Training set size sensitivity analysis.
Sweeps over EXPERIMENT_FRACTIONS, training both the deep model and Random
Forest at each fraction, then plots accuracy vs number of training samples.
Motivation: Understanding how each model scales with data volume helps
justify architectural choices and highlights when more data is beneficial.
Args:
X_train_raw: Raw training pixels.
X_test_raw: Raw test pixels.
y_train: Training labels.
y_test: Test labels.
X_train_hog: HOG training features.
X_test_hog: HOG test features.
"""
print(f"\nExperiment: Training Size Sensitivity")
deep_accs, rf_accs, n_samples = [], [], []
scaler = StandardScaler()
X_test_scaled = scaler.fit_transform(X_test_hog)
for frac in EXPERIMENT_FRACTIONS:
n = int(len(X_train_raw) * frac)
n_samples.append(n)
print(f"\n Fraction={frac:.0%} (n={n})")
# Deep model
result = train_deep(
X_train_raw, X_test_raw, y_train, y_test,
epochs=EXPERIMENT_EPOCHS, train_fraction=frac, save_model=False,
)
deep_accs.append(result["accuracy"])
# Random Forest
idx = np.random.default_rng(seed=42).permutation(len(X_train_hog))[:n]
X_tr = scaler.fit_transform(X_train_hog[idx])
rf = RandomForestClassifier(
n_estimators=100, n_jobs=-1, random_state=42
)
rf.fit(X_tr, y_train[idx])
rf_pred = rf.predict(X_test_scaled)
rf_accs.append(accuracy_score(y_test, rf_pred))
print(f" RF acc={rf_accs[-1]:.4f}")
# Plot
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(n_samples, deep_accs, marker="o", linestyle="solid", label="ScribblNet (CNN)", linewidth=2, markersize=7)
ax.plot(n_samples, rf_accs, marker="s", linestyle="dashed", label="Random Forest (HOG)", linewidth=2, markersize=7)
ax.set_xlabel("Training samples", fontsize=12)
ax.set_ylabel("Test accuracy", fontsize=12)
ax.set_title("Training Set Size Sensitivity", fontsize=14)
ax.legend(fontsize=11)
ax.grid(True, alpha=0.3)
ax.set_ylim(0, 1)
plt.tight_layout()
out_path = OUTPUTS_DIR / "experiment_sensitivity.png"
fig.savefig(out_path, dpi=150)
plt.close(fig)
print(f"\n Saved experiment plot → {out_path}")
results = {
"fractions": EXPERIMENT_FRACTIONS,
"n_samples": n_samples,
"deep_accs": deep_accs,
"rf_accs": rf_accs,
}
with open(OUTPUTS_DIR / "experiment_results.json", "w") as f:
json.dump(results, f, indent=2)
print(" Saved experiment_results.json")
# Plotting Helpers
def _save_confusion_matrix(
y_true: np.ndarray,
y_pred: np.ndarray,
filename: str,
) -> None:
"""Save a normalised confusion matrix heatmap.
Args:
y_true: Ground truth labels.
y_pred: Predicted labels.
filename: Output filename (saved under OUTPUTS_DIR).
"""
cm = confusion_matrix(y_true, y_pred, normalize="true")
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(
cm,
annot=True,
fmt=".2f",
xticklabels=CLASSES,
yticklabels=CLASSES,
cmap="Blues",
ax=ax,
linewidths=0.5,
)
ax.set_xlabel("Predicted", fontsize=11)
ax.set_ylabel("True", fontsize=11)
ax.set_title(filename.replace("_", " ").replace(".png", "").title(), fontsize=13)
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
fig.savefig(OUTPUTS_DIR / filename, dpi=150)
plt.close(fig)
print(f" Saved {filename}")
def _save_training_curves(history: dict[str, list[float]]) -> None:
"""Save loss and validation accuracy curves for the deep model.
Args:
history: Dict with keys 'loss' and 'val_acc', each a list of per epoch values.
"""
epochs = range(1, len(history["loss"]) + 1)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 4))
ax1.plot(epochs, history["loss"], color="steelblue", marker="o", linestyle="solid", markersize=5)
ax1.set_xlabel("Epoch")
ax1.set_ylabel("Training Loss")
ax1.set_title("ScribblNet Training Loss")
ax1.grid(True, alpha=0.3)
ax2.plot(epochs, history["val_acc"], color="seagreen", marker="o", linestyle="solid", markersize=5)
ax2.set_xlabel("Epoch")
ax2.set_ylabel("Validation Accuracy")
ax2.set_title("ScribblNet Validation Accuracy")
ax2.grid(True, alpha=0.3)
plt.tight_layout()
fig.savefig(OUTPUTS_DIR / "deep_training_curves.png", dpi=150)
plt.close(fig)
print(" Saved deep_training_curves.png")
def _save_model_comparison(results: list[dict[str, Any]]) -> None:
"""Bar chart comparing test accuracy across all three models.
Args:
results: List of result dicts each containing 'model' and 'accuracy'.
"""
names = [r["model"].capitalize() for r in results]
accs = [r["accuracy"] for r in results]
fig, ax = plt.subplots(figsize=(7, 4))
bars = ax.bar(names, accs, color=["#94a3b8", "#60a5fa", "#34d399"], width=0.5)
ax.set_ylim(0, 1)
ax.set_ylabel("Test Accuracy")
ax.set_title("Model Comparison")
for bar, acc in zip(bars, accs):
ax.text(
bar.get_x() + bar.get_width() / 2,
bar.get_height() + 0.01,
f"{acc:.3f}",
ha="center",
fontsize=12,
)
ax.grid(True, axis="y", alpha=0.3)
plt.tight_layout()
fig.savefig(OUTPUTS_DIR / "model_comparison.png", dpi=150)
plt.close(fig)
print(" Saved model_comparison.png")
# Orchestrator
def train_all() -> None:
"""Train all three models, run the experiment, and save all artefacts."""
X_train_raw, X_test_raw, y_train, y_test, X_train_hog, X_test_hog = (
load_processed_data()
)
r_naive = train_naive(y_train, y_test)
r_classical = train_classical(X_train_hog, X_test_hog, y_train, y_test)
r_deep = train_deep(X_train_raw, X_test_raw, y_train, y_test)
_save_model_comparison([r_naive, r_classical, r_deep])
run_experiment(
X_train_raw, X_test_raw, y_train, y_test, X_train_hog, X_test_hog
)
summary = {
"naive_accuracy": r_naive["accuracy"],
"classical_accuracy": r_classical["accuracy"],
"deep_accuracy": r_deep["accuracy"],
}
with open(OUTPUTS_DIR / "results_summary.json", "w") as f:
json.dump(summary, f, indent=2)
print("\nTraining complete. Summary:")
for k, v in summary.items():
print(f" {k}: {v:.4f}")
if __name__ == "__main__":
train_all()
|