-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
625 lines (498 loc) · 19.8 KB
/
train_model.py
File metadata and controls
625 lines (498 loc) · 19.8 KB
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
618
619
620
621
622
623
624
625
"""
Training Module for Damaged Poster Detection Model
================================================
This module handles training of the deep learning model for damage classification.
Includes data augmentation, model training, validation, and model saving.
"""
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights
import cv2
import numpy as np
from PIL import Image
import albumentations as A
from albumentations.pytorch import ToTensorV2
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
import os
import json
import logging
from pathlib import Path
from typing import Dict, List, Tuple, Optional
from dataclasses import dataclass
from datetime import datetime
import wandb # For experiment tracking
from damaged_poster_detector import DamageClassifier, DamageConfig
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class TrainingConfig:
"""Configuration for model training"""
# Data
data_dir: str = "data"
train_split: float = 0.8
val_split: float = 0.15
test_split: float = 0.05
# Training
batch_size: int = 32
num_epochs: int = 50
learning_rate: float = 1e-4
weight_decay: float = 1e-5
patience: int = 10
# Model
num_classes: int = 7
model_name: str = "efficientnet_b0"
pretrained: bool = True
# Augmentation
use_augmentation: bool = True
augmentation_prob: float = 0.8
# Output
model_save_path: str = "models"
log_dir: str = "logs"
# Hardware
device: str = "auto"
num_workers: int = 4
class DamageDataset(Dataset):
"""Dataset class for damage detection training"""
def __init__(self,
image_paths: List[str],
labels: List[int],
transform=None,
augment: bool = False):
"""
Initialize dataset
Args:
image_paths: List of image file paths
labels: List of damage labels (0=no_damage, 1=tear, 2=crease, etc.)
transform: Image transformations
augment: Whether to apply data augmentation
"""
self.image_paths = image_paths
self.labels = labels
self.transform = transform
self.augment = augment
# Define augmentation pipeline
if augment:
self.augmentation = A.Compose([
A.RandomRotate90(p=0.5),
A.Flip(p=0.5),
A.RandomBrightnessContrast(
brightness_limit=0.2,
contrast_limit=0.2,
p=0.5
),
A.OneOf([
A.GaussNoise(var_limit=(10.0, 50.0), p=1.0),
A.GaussianBlur(blur_limit=3, p=1.0),
], p=0.3),
A.OneOf([
A.HueSaturationValue(
hue_shift_limit=10,
sat_shift_limit=15,
val_shift_limit=10,
p=1.0
),
A.CLAHE(clip_limit=2.0, tile_grid_size=(8, 8), p=1.0),
], p=0.3),
A.CoarseDropout(
max_holes=8,
max_height=32,
max_width=32,
min_holes=1,
min_height=8,
min_width=8,
fill_value=0,
p=0.3
),
])
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
# Load image
image_path = self.image_paths[idx]
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Apply augmentation if enabled
if self.augment:
augmented = self.augmentation(image=image)
image = augmented['image']
# Apply transforms
if self.transform:
image = self.transform(image)
label = torch.tensor(self.labels[idx], dtype=torch.long)
return image, label
class DamageTrainer:
"""Main trainer class for damage detection model"""
def __init__(self, config: TrainingConfig):
"""
Initialize trainer
Args:
config: Training configuration
"""
self.config = config
# Setup device
if config.device == "auto":
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
else:
self.device = torch.device(config.device)
logger.info(f"Using device: {self.device}")
# Create directories
os.makedirs(config.model_save_path, exist_ok=True)
os.makedirs(config.log_dir, exist_ok=True)
# Initialize model
self.model = DamageClassifier(num_classes=config.num_classes).to(self.device)
# Define transforms
self.train_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
self.val_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Training history
self.history = {
'train_loss': [],
'train_acc': [],
'val_loss': [],
'val_acc': []
}
def prepare_data(self, data_dir: str) -> Tuple[DataLoader, DataLoader, DataLoader]:
"""
Prepare training, validation, and test data loaders
Args:
data_dir: Directory containing training data
Returns:
Tuple of (train_loader, val_loader, test_loader)
"""
# Expected directory structure:
# data/
# ├── no_damage/
# ├── tear/
# ├── crease/
# ├── stain/
# ├── fade/
# ├── burn/
# └── water_damage/
damage_types = [
'no_damage', 'tear', 'crease', 'stain',
'fade', 'burn', 'water_damage'
]
image_paths = []
labels = []
# Collect all images and labels
for label_idx, damage_type in enumerate(damage_types):
damage_dir = Path(data_dir) / damage_type
if damage_dir.exists():
for img_path in damage_dir.glob('*'):
if img_path.suffix.lower() in ['.jpg', '.jpeg', '.png', '.bmp']:
image_paths.append(str(img_path))
labels.append(label_idx)
if not image_paths:
raise ValueError(f"No images found in {data_dir}")
logger.info(f"Found {len(image_paths)} images across {len(set(labels))} classes")
# Split data
train_paths, temp_paths, train_labels, temp_labels = train_test_split(
image_paths, labels,
test_size=(self.config.val_split + self.config.test_split),
stratify=labels,
random_state=42
)
val_paths, test_paths, val_labels, test_labels = train_test_split(
temp_paths, temp_labels,
test_size=self.config.test_split / (self.config.val_split + self.config.test_split),
stratify=temp_labels,
random_state=42
)
# Create datasets
train_dataset = DamageDataset(
train_paths, train_labels,
transform=self.train_transform,
augment=self.config.use_augmentation
)
val_dataset = DamageDataset(
val_paths, val_labels,
transform=self.val_transform,
augment=False
)
test_dataset = DamageDataset(
test_paths, test_labels,
transform=self.val_transform,
augment=False
)
# Create data loaders
train_loader = DataLoader(
train_dataset,
batch_size=self.config.batch_size,
shuffle=True,
num_workers=self.config.num_workers,
pin_memory=True if self.device.type == 'cuda' else False
)
val_loader = DataLoader(
val_dataset,
batch_size=self.config.batch_size,
shuffle=False,
num_workers=self.config.num_workers,
pin_memory=True if self.device.type == 'cuda' else False
)
test_loader = DataLoader(
test_dataset,
batch_size=self.config.batch_size,
shuffle=False,
num_workers=self.config.num_workers,
pin_memory=True if self.device.type == 'cuda' else False
)
logger.info(f"Train: {len(train_dataset)}, Val: {len(val_dataset)}, Test: {len(test_dataset)}")
return train_loader, val_loader, test_loader
def train_epoch(self, train_loader: DataLoader, optimizer, criterion) -> Tuple[float, float]:
"""Train for one epoch"""
self.model.train()
running_loss = 0.0
correct = 0
total = 0
for batch_idx, (images, labels) in enumerate(train_loader):
images, labels = images.to(self.device), labels.to(self.device)
# Zero gradients
optimizer.zero_grad()
# Forward pass
outputs = self.model(images)
loss = criterion(outputs, labels)
# Backward pass
loss.backward()
optimizer.step()
# Statistics
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
if batch_idx % 50 == 0:
logger.info(f'Batch {batch_idx}/{len(train_loader)}, Loss: {loss.item():.4f}')
epoch_loss = running_loss / len(train_loader)
epoch_acc = correct / total
return epoch_loss, epoch_acc
def validate(self, val_loader: DataLoader, criterion) -> Tuple[float, float]:
"""Validate the model"""
self.model.eval()
running_loss = 0.0
correct = 0
total = 0
with torch.no_grad():
for images, labels in val_loader:
images, labels = images.to(self.device), labels.to(self.device)
outputs = self.model(images)
loss = criterion(outputs, labels)
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
epoch_loss = running_loss / len(val_loader)
epoch_acc = correct / total
return epoch_loss, epoch_acc
def train(self, data_dir: str, use_wandb: bool = False):
"""
Main training loop
Args:
data_dir: Directory containing training data
use_wandb: Whether to use Weights & Biases for logging
"""
# Initialize wandb if requested
if use_wandb:
wandb.init(
project="damaged-poster-detection",
config=self.config.__dict__
)
wandb.watch(self.model)
# Prepare data
train_loader, val_loader, test_loader = self.prepare_data(data_dir)
# Setup training components
criterion = nn.CrossEntropyLoss()
optimizer = optim.AdamW(
self.model.parameters(),
lr=self.config.learning_rate,
weight_decay=self.config.weight_decay
)
# Learning rate scheduler
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
optimizer, mode='min', factor=0.5, patience=5, verbose=True
)
# Early stopping
best_val_loss = float('inf')
patience_counter = 0
logger.info("Starting training...")
for epoch in range(self.config.num_epochs):
logger.info(f"Epoch {epoch+1}/{self.config.num_epochs}")
# Train
train_loss, train_acc = self.train_epoch(train_loader, optimizer, criterion)
# Validate
val_loss, val_acc = self.validate(val_loader, criterion)
# Update learning rate
scheduler.step(val_loss)
# Log metrics
self.history['train_loss'].append(train_loss)
self.history['train_acc'].append(train_acc)
self.history['val_loss'].append(val_loss)
self.history['val_acc'].append(val_acc)
logger.info(f"Train Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f}")
logger.info(f"Val Loss: {val_loss:.4f}, Val Acc: {val_acc:.4f}")
if use_wandb:
wandb.log({
'train_loss': train_loss,
'train_acc': train_acc,
'val_loss': val_loss,
'val_acc': val_acc,
'learning_rate': optimizer.param_groups[0]['lr']
})
# Early stopping and model saving
if val_loss < best_val_loss:
best_val_loss = val_loss
patience_counter = 0
# Save best model
model_path = os.path.join(
self.config.model_save_path,
f"best_model_epoch_{epoch+1}.pth"
)
torch.save({
'epoch': epoch,
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'val_loss': val_loss,
'config': self.config
}, model_path)
logger.info(f"Saved best model to {model_path}")
else:
patience_counter += 1
if patience_counter >= self.config.patience:
logger.info(f"Early stopping at epoch {epoch+1}")
break
# Final evaluation on test set
test_loss, test_acc = self.validate(test_loader, criterion)
logger.info(f"Final Test Loss: {test_loss:.4f}, Test Acc: {test_acc:.4f}")
if use_wandb:
wandb.log({'test_loss': test_loss, 'test_acc': test_acc})
wandb.finish()
# Save training history
history_path = os.path.join(self.config.log_dir, "training_history.json")
with open(history_path, 'w') as f:
json.dump(self.history, f, indent=2)
# Generate evaluation report
self.evaluate_model(test_loader)
logger.info("Training completed!")
def evaluate_model(self, test_loader: DataLoader):
"""Generate detailed evaluation report"""
self.model.eval()
all_predictions = []
all_labels = []
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(self.device), labels.to(self.device)
outputs = self.model(images)
_, predicted = torch.max(outputs, 1)
all_predictions.extend(predicted.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
# Classification report
damage_types = [
'no_damage', 'tear', 'crease', 'stain',
'fade', 'burn', 'water_damage'
]
report = classification_report(
all_labels, all_predictions,
target_names=damage_types,
output_dict=True
)
# Save report
report_path = os.path.join(self.config.log_dir, "evaluation_report.json")
with open(report_path, 'w') as f:
json.dump(report, f, indent=2)
# Confusion matrix
cm = confusion_matrix(all_labels, all_predictions)
plt.figure(figsize=(10, 8))
sns.heatmap(
cm, annot=True, fmt='d', cmap='Blues',
xticklabels=damage_types,
yticklabels=damage_types
)
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.tight_layout()
cm_path = os.path.join(self.config.log_dir, "confusion_matrix.png")
plt.savefig(cm_path, dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"Evaluation report saved to {report_path}")
logger.info(f"Confusion matrix saved to {cm_path}")
def plot_training_history(self):
"""Plot training history"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5))
# Loss plot
ax1.plot(self.history['train_loss'], label='Train Loss')
ax1.plot(self.history['val_loss'], label='Val Loss')
ax1.set_title('Model Loss')
ax1.set_xlabel('Epoch')
ax1.set_ylabel('Loss')
ax1.legend()
ax1.grid(True)
# Accuracy plot
ax2.plot(self.history['train_acc'], label='Train Accuracy')
ax2.plot(self.history['val_acc'], label='Val Accuracy')
ax2.set_title('Model Accuracy')
ax2.set_xlabel('Epoch')
ax2.set_ylabel('Accuracy')
ax2.legend()
ax2.grid(True)
plt.tight_layout()
plot_path = os.path.join(self.config.log_dir, "training_history.png")
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"Training history plot saved to {plot_path}")
def create_sample_data_structure(base_dir: str):
"""
Create sample directory structure for training data
Args:
base_dir: Base directory to create structure in
"""
damage_types = [
'no_damage', 'tear', 'crease', 'stain',
'fade', 'burn', 'water_damage'
]
os.makedirs(base_dir, exist_ok=True)
for damage_type in damage_types:
damage_dir = os.path.join(base_dir, damage_type)
os.makedirs(damage_dir, exist_ok=True)
logger.info(f"Created sample data structure in {base_dir}")
logger.info("Please organize your training images into the appropriate subdirectories:")
for damage_type in damage_types:
logger.info(f" - {os.path.join(base_dir, damage_type)}/")
def main():
"""Main training function"""
# Configuration
config = TrainingConfig(
data_dir="data/train",
batch_size=16, # Adjust based on GPU memory
num_epochs=30,
learning_rate=1e-4,
use_augmentation=True
)
# Create sample data structure if needed
if not os.path.exists(config.data_dir):
create_sample_data_structure(config.data_dir)
logger.info("Sample data structure created. Please add your training images and run again.")
return
# Initialize trainer
trainer = DamageTrainer(config)
# Start training
trainer.train(config.data_dir, use_wandb=False) # Set to True if you want to use wandb
# Plot training history
trainer.plot_training_history()
if __name__ == "__main__":
main()