Spaces:
Runtime error
Runtime error
File size: 13,204 Bytes
3bce187 | 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 | """
Phase 3 Student Implementation Template
This file serves as the skeleton/template for each student to implement their models.
Instructions for Students:
1. Student 1 & 2: Implement RNN/GRU and LSTM models in base_sequential_models.py
2. Student 3: Implement Transformer model in base_sequential_models.py
3. Student 4: Use this file as starting point for unified evaluation and comparison
4. Student 5: Will extend this with bonus features (inference, XAI, adversarial)
Key Classes to Extend:
- LSTMModel (Student 2): In base_sequential_models.py
- TransformerModel (Student 3): In base_sequential_models.py
This file demonstrates the complete pipeline.
"""
import torch
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from pathlib import Path
# Import infrastructure components
from src.config import (
DatasetConfig, FeatureExtractorConfig, TrainingConfig, SequentialModelConfig,
LSTMConfig, TransformerConfig
)
from src.detection.feature_extractor_vgg16 import VGG16FeatureExtractor, precompute_gtsrb_features
from src.detection.sequence_dataset import SequenceDataset, create_sequence_dataloaders
from src.models.base_sequential_models import RNNModel, GRUModel, LSTMModel, TransformerModel, create_model
from src.models.unified_trainer import UnifiedTrainer
# ============================================================================
# EXAMPLE 1: EXTRACT VGG16 FEATURES
# ============================================================================
def example_extract_vgg16_features():
"""
Example: Extract and cache VGG16 features from GTSRB dataset.
This should be run ONCE to precompute all features.
Students can skip this if features are already precomputed.
"""
print("\n" + "="*60)
print("EXAMPLE 1: VGG16 Feature Extraction")
print("="*60)
config = FeatureExtractorConfig()
extractor = VGG16FeatureExtractor(config=config)
# Example: Extract from a directory
# Note: You'll need to point to actual GTSRB data
# For now, this uses synthetic data
print("[Example] Feature extraction set up. Ready to process real data when available.")
# ============================================================================
# EXAMPLE 2: CREATE DATA LOADERS
# ============================================================================
def example_create_dataloaders():
"""
Example: Create sequence DataLoaders with temporal data.
"""
print("\n" + "="*60)
print("EXAMPLE 2: Create Sequence DataLoaders")
print("="*60)
train_loader, val_loader, test_loader = create_sequence_dataloaders(
features_dir="./cache/vgg16_sequence_features",
batch_size=32,
num_workers=4,
augment=True,
seed=42
)
print(f"Train batches: {len(train_loader)}")
print(f"Val batches: {len(val_loader)}")
print(f"Test batches: {len(test_loader)}")
# Inspect a batch
batch_sequences, batch_labels = next(iter(train_loader))
print(f"\nBatch sequence shape: {batch_sequences.shape}")
print(f"Batch labels shape: {batch_labels.shape}")
return train_loader, val_loader, test_loader
# ============================================================================
# EXAMPLE 3: TRAIN RNN MODEL
# ============================================================================
def example_train_rnn():
"""
Example: Train RNN model on sequence data.
This demonstrates the complete pipeline for Student 1.
"""
print("\n" + "="*60)
print("EXAMPLE 3: Train RNN Model")
print("="*60)
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
# Create data loaders
train_loader, val_loader, test_loader = example_create_dataloaders()
# Create RNN model
config = SequentialModelConfig()
rnn_model = RNNModel(
input_size=config.INPUT_SIZE,
hidden_size=config.HIDDEN_SIZE,
num_layers=config.NUM_LAYERS,
output_size=config.OUTPUT_SIZE,
dropout=config.DROPOUT,
bidirectional=config.BIDIRECTIONAL,
device=device
)
print(f"\n{rnn_model.get_model_name()} Model Config:")
for key, value in rnn_model.get_config_dict().items():
print(f" {key}: {value}")
# Create trainer
training_config = TrainingConfig()
training_config.NUM_EPOCHS = 5 # Short run for example
training_config.DEVICE = device
trainer = UnifiedTrainer(
model=rnn_model,
train_loader=train_loader,
val_loader=val_loader,
config=training_config,
device=device,
save_dir="checkpoints"
)
# Train
trainer.train(num_epochs=5)
# Evaluate
metrics = trainer.evaluate(test_loader)
# Save curves
trainer.save_training_curves("results")
trainer.save_metrics_json(metrics, "results")
return rnn_model, trainer, metrics
# ============================================================================
# EXAMPLE 4: TRAIN GRU MODEL
# ============================================================================
def example_train_gru():
"""
Example: Train GRU model.
Similar structure to RNN but with gating.
"""
print("\n" + "="*60)
print("EXAMPLE 4: Train GRU Model")
print("="*60)
device = "cuda" if torch.cuda.is_available() else "cpu"
train_loader, val_loader, test_loader = example_create_dataloaders()
config = SequentialModelConfig()
gru_model = GRUModel(
input_size=config.INPUT_SIZE,
hidden_size=config.HIDDEN_SIZE,
num_layers=config.NUM_LAYERS,
output_size=config.OUTPUT_SIZE,
dropout=config.DROPOUT,
bidirectional=config.BIDIRECTIONAL,
device=device
)
training_config = TrainingConfig()
training_config.NUM_EPOCHS = 5
training_config.DEVICE = device
trainer = UnifiedTrainer(
model=gru_model,
train_loader=train_loader,
val_loader=val_loader,
config=training_config,
device=device,
save_dir="checkpoints"
)
trainer.train(num_epochs=5)
metrics = trainer.evaluate(test_loader)
trainer.save_training_curves("results")
trainer.save_metrics_json(metrics, "results")
return gru_model, trainer, metrics
# ============================================================================
# STUDENT TASKS
# ============================================================================
"""
STUDENT 1 - RNN & GRU IMPLEMENTATION:
✓ Already provided in base_sequential_models.py
Next tasks:
1. Train both RNN and GRU models using this template
2. Experiment with:
- Different hidden_size (128, 256, 512)
- Different num_layers (1, 2, 3)
- bidirectional=True/False
3. Compare RNN vs GRU performance
4. Create comparison plots (accuracy, loss, training time)
5. Write analysis in REPORT.md
Commands:
python -m student_implementations
"""
"""
STUDENT 2 - LSTM WITH ATTENTION MECHANISM:
TODO in base_sequential_models.py:
1. Implement LSTMModel class:
- Use nn.LSTM instead of nn.RNN/GRU
- Implement Bahdanau attention mechanism
- Architecture:
* LSTM encoder (bidirectional optional)
* Attention layer over sequence (computes attention weights)
* Context vector from attention
* FC classifier
2. Suggested attention mechanism:
```python
class BahdanauAttention(nn.Module):
def __init__(self, hidden_size):
super().__init__()
self.query = nn.Linear(hidden_size, hidden_size)
self.key = nn.Linear(hidden_size, hidden_size)
self.value = nn.Linear(hidden_size, 1)
def forward(self, lstm_out): # lstm_out: (batch, seq_len, hidden*2)
# Compute attention scores
scores = self.value(torch.tanh(self.query(lstm_out) + self.key(lstm_out)))
weights = torch.softmax(scores, dim=1) # (batch, seq_len, 1)
context = (weights * lstm_out).sum(dim=1) # (batch, hidden*2)
return context, weights
```
3. Forward pass should return logits and attention weights
4. Test using example_train_lstm() below
5. Visualize which frames get highest attention weights
"""
"""
STUDENT 3 - TRANSFORMER MODEL:
TODO in base_sequential_models.py:
1. Implement TransformerModel class:
- Use nn.TransformerEncoder with multi-head self-attention
- Add positional embeddings for frame positions
- Architecture:
* Input projection (512 → hidden_size)
* Positional embeddings
* TransformerEncoder (num_layers, num_heads)
* Temporal pooling (mean over sequence)
* FC classifier
2. Optional: Fine-tune Vision Transformer (ViT) from timm library:
```python
import timm
vit_model = timm.create_model('vit_base_patch16_224', pretrained=True)
# Extract patch embeddings for sequence modeling
```
3. Compare custom Transformer vs ViT
4. Analyze attention patterns via attention weights
"""
"""
STUDENT 4 - UNIFIED COMPARISON & ANALYSIS:
TODO: Create comprehensive_comparison.py
1. Train all 4 models (RNN, GRU, LSTM, Transformer)
2. Evaluate on test set with metrics:
- Accuracy, Precision, Recall, F1
- Inference time per sample
- Model size (parameters)
- Memory usage
3. Create comparison visualizations:
- Accuracy bar chart
- Training curves overlay
- Confusion matrices (4 subplots)
- Per-class F1 scores
4. Statistical analysis:
- t-tests for accuracy differences
- Temporal agreement (do models agree on same prediction across frames?)
5. Write analysis chapter in REPORT.md
6. Recommend best model for deployment
"""
"""
STUDENT 5 - BONUS FEATURES:
Choose 2-3 of:
A) REAL-TIME SEQUENCE PREDICTION:
- Load video files
- Apply models frame-by-frame
- Visualize prediction confidence evolution
- Show: frame_idx vs class_prediction vs confidence
B) ADVERSARIAL ROBUSTNESS:
- Add perturbations to specific frames
- Test: which model is most robust?
- Metrics: accuracy drop under attack
C) TEMPORAL ANOMALY DETECTION:
- Use LSTM hidden states as anomaly features
- Detect "impossible" sign transitions
- One-class SVM or Isolation Forest
D) MULTI-TASK LEARNING:
- Joint training: sign_class + sign_change_detection
- Does auxiliary task improve main task?
E) EXPLAINABILITY (XAI):
- LIME/SHAP for sequences
- Which frames influence prediction most?
- Compare RNN vs Transformer interpretability
"""
# ============================================================================
# COMPARISON FUNCTION (Student 4)
# ============================================================================
def compare_all_models(results_dir: str = "results"):
"""
Placeholder for Student 4: Compare all 4 models.
Should:
1. Train all models
2. Evaluate on test set
3. Generate comparison plots
4. Output summary table
"""
print("\n" + "="*60)
print("MODEL COMPARISON (Student 4)")
print("="*60)
# This will be implemented by Student 4
print("To be implemented by Student 4")
print("See student_implementations.py for template")
# ============================================================================
# MAIN
# ============================================================================
if __name__ == "__main__":
print("\n" + "="*80)
print("PHASE 3: SEQUENTIAL MODELS TRAINING PIPELINE")
print("="*80)
# You can run individual examples:
# Example 1: Feature extraction (one-time setup)
example_extract_vgg16_features()
# Example 2: Create data loaders
train_loader, val_loader, test_loader = example_create_dataloaders()
# Example 3: Train RNN (uncomment to run)
# rnn_model, trainer, metrics = example_train_rnn()
# Example 4: Train GRU (uncomment to run)
# gru_model, trainer, metrics = example_train_gru()
# Example 5: Model comparison (uncomment when Student 4 implements)
# compare_all_models()
print("\n" + "="*80)
print("STUDENT INSTRUCTIONS")
print("="*80)
print("""
1. STUDENT 1: Implement RNN & GRU training script
- Models already provided in base_sequential_models.py
- Use example_train_rnn() as template
- Create rnn_gru_training.py with full experiments
2. STUDENT 2: Implement LSTM with Attention
- Edit LSTMModel in base_sequential_models.py
- Add BahdanauAttention mechanism
- Create lstm_training.py using UnifiedTrainer
3. STUDENT 3: Implement Transformer
- Edit TransformerModel in base_sequential_models.py
- Use nn.TransformerEncoder
- Optionally fine-tune Vision Transformer
- Create transformer_training.py
4. STUDENT 4: Unified Comparison & Analysis
- Create comprehensive_comparison.py
- Train all 4 models
- Generate comparison plots and statistics
5. STUDENT 5: Bonus Features
- Choose 2-3 of: video inference, adversarial robustness,
temporal anomaly detection, multi-task learning, XAI
- Create bonus_features.py
All files should save outputs to results/ directory.
""")
|