-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_segmentation.py
More file actions
590 lines (472 loc) · 19.5 KB
/
train_segmentation.py
File metadata and controls
590 lines (472 loc) · 19.5 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
"""
Segmentation Training Script
Converted from train_mask.ipynb
Trains a segmentation head on top of DINOv2 backbone
"""
import torch
from torch.utils.data import Dataset, DataLoader
import numpy as np
from torch import nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
import torch.optim as optim
import torchvision.transforms as transforms
from PIL import Image
import cv2
import os
import torchvision
from tqdm import tqdm
# Set matplotlib to non-interactive backend
plt.switch_backend('Agg')
# ============================================================================
# Utility Functions
# ============================================================================
def save_image(img, filename):
"""Save an image tensor to file after denormalizing."""
img = np.array(img)
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
img = np.moveaxis(img, 0, -1)
img = (img * std + mean) * 255
cv2.imwrite(filename, img[:, :, ::-1])
# ============================================================================
# Mask Conversion
# ============================================================================
# Mapping from raw pixel values to new class IDs
value_map = {
0: 0, # background
100: 1, # Trees
200: 2, # Lush Bushes
300: 3, # Dry Grass
500: 4, # Dry Bushes
550: 5, # Ground Clutter
700: 6, # Logs
800: 7, # Rocks
7100: 8, # Landscape
10000: 9 # Sky
}
n_classes = len(value_map)
def convert_mask(mask):
"""Convert raw mask values to class IDs."""
arr = np.array(mask)
new_arr = np.zeros_like(arr, dtype=np.uint8)
for raw_value, new_value in value_map.items():
new_arr[arr == raw_value] = new_value
return Image.fromarray(new_arr)
# ============================================================================
# Dataset
# ============================================================================
class MaskDataset(Dataset):
def __init__(self, data_dir, transform=None, mask_transform=None):
self.image_dir = os.path.join(data_dir, 'Color_Images')
self.masks_dir = os.path.join(data_dir, 'Segmentation')
self.transform = transform
self.mask_transform = mask_transform
self.data_ids = os.listdir(self.image_dir)
def __len__(self):
return len(self.data_ids)
def __getitem__(self, idx):
data_id = self.data_ids[idx]
img_path = os.path.join(self.image_dir, data_id)
# Both color images and masks are .png files with same name
mask_path = os.path.join(self.masks_dir, data_id)
image = Image.open(img_path).convert("RGB")
mask = Image.open(mask_path)
mask = convert_mask(mask)
if self.transform:
image = self.transform(image)
mask = self.mask_transform(mask) * 255
return image, mask
# ============================================================================
# Model: Segmentation Head (ConvNeXt-style)
# ============================================================================
class SegmentationHeadConvNeXt(nn.Module):
def __init__(self, in_channels, out_channels, tokenW, tokenH):
super().__init__()
self.H, self.W = tokenH, tokenW
self.stem = nn.Sequential(
nn.Conv2d(in_channels, 128, kernel_size=7, padding=3),
nn.GELU()
)
self.block = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=7, padding=3, groups=128),
nn.GELU(),
nn.Conv2d(128, 128, kernel_size=1),
nn.GELU(),
)
self.classifier = nn.Conv2d(128, out_channels, 1)
def forward(self, x):
B, N, C = x.shape
x = x.reshape(B, self.H, self.W, C).permute(0, 3, 1, 2)
x = self.stem(x)
x = self.block(x)
return self.classifier(x)
# ============================================================================
# Metrics
# ============================================================================
def compute_iou(pred, target, num_classes=10, ignore_index=255):
"""Compute IoU for each class and return mean IoU."""
pred = torch.argmax(pred, dim=1)
pred, target = pred.view(-1), target.view(-1)
iou_per_class = []
for class_id in range(num_classes):
if class_id == ignore_index:
continue
pred_inds = pred == class_id
target_inds = target == class_id
intersection = (pred_inds & target_inds).sum().float()
union = (pred_inds | target_inds).sum().float()
if union == 0:
iou_per_class.append(float('nan'))
else:
iou_per_class.append((intersection / union).cpu().numpy())
return np.nanmean(iou_per_class)
def compute_dice(pred, target, num_classes=10, smooth=1e-6):
"""Compute Dice coefficient (F1 Score) per class and return mean Dice Score."""
pred = torch.argmax(pred, dim=1)
pred, target = pred.view(-1), target.view(-1)
dice_per_class = []
for class_id in range(num_classes):
pred_inds = pred == class_id
target_inds = target == class_id
intersection = (pred_inds & target_inds).sum().float()
dice_score = (2. * intersection + smooth) / (pred_inds.sum().float() + target_inds.sum().float() + smooth)
dice_per_class.append(dice_score.cpu().numpy())
return np.mean(dice_per_class)
def compute_pixel_accuracy(pred, target):
"""Compute pixel accuracy."""
pred_classes = torch.argmax(pred, dim=1)
return (pred_classes == target).float().mean().cpu().numpy()
def evaluate_metrics(model, backbone, data_loader, device, num_classes=10, show_progress=True):
"""Evaluate all metrics on a dataset."""
iou_scores = []
dice_scores = []
pixel_accuracies = []
model.eval()
loader = tqdm(data_loader, desc="Evaluating", leave=False, unit="batch") if show_progress else data_loader
with torch.no_grad():
for imgs, labels in loader:
imgs, labels = imgs.to(device), labels.to(device)
output = backbone.forward_features(imgs)["x_norm_patchtokens"]
logits = model(output.to(device))
outputs = F.interpolate(logits, size=imgs.shape[2:], mode="bilinear", align_corners=False)
labels = labels.squeeze(dim=1).long()
iou = compute_iou(outputs, labels, num_classes=num_classes)
dice = compute_dice(outputs, labels, num_classes=num_classes)
pixel_acc = compute_pixel_accuracy(outputs, labels)
iou_scores.append(iou)
dice_scores.append(dice)
pixel_accuracies.append(pixel_acc)
model.train()
return np.mean(iou_scores), np.mean(dice_scores), np.mean(pixel_accuracies)
# ============================================================================
# Plotting Functions
# ============================================================================
def save_training_plots(history, output_dir):
"""Save all training metric plots to files."""
os.makedirs(output_dir, exist_ok=True)
# Plot 1: Loss curves
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(history['train_loss'], label='train')
plt.plot(history['val_loss'], label='val')
plt.title('Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(history['train_pixel_acc'], label='train')
plt.plot(history['val_pixel_acc'], label='val')
plt.title('Pixel Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(output_dir, 'training_curves.png'))
plt.close()
print(f"Saved training curves to '{output_dir}/training_curves.png'")
# Plot 2: IoU curves
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(history['train_iou'], label='Train IoU')
plt.title('Train IoU vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('IoU')
plt.legend()
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(history['val_iou'], label='Val IoU')
plt.title('Validation IoU vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('IoU')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(output_dir, 'iou_curves.png'))
plt.close()
print(f"Saved IoU curves to '{output_dir}/iou_curves.png'")
# Plot 3: Dice curves
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(history['train_dice'], label='Train Dice')
plt.title('Train Dice vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('Dice Score')
plt.legend()
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(history['val_dice'], label='Val Dice')
plt.title('Validation Dice vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('Dice Score')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(output_dir, 'dice_curves.png'))
plt.close()
print(f"Saved Dice curves to '{output_dir}/dice_curves.png'")
# Plot 4: Combined metrics plot
plt.figure(figsize=(12, 10))
plt.subplot(2, 2, 1)
plt.plot(history['train_loss'], label='train')
plt.plot(history['val_loss'], label='val')
plt.title('Loss vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True)
plt.subplot(2, 2, 2)
plt.plot(history['train_iou'], label='train')
plt.plot(history['val_iou'], label='val')
plt.title('IoU vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('IoU')
plt.legend()
plt.grid(True)
plt.subplot(2, 2, 3)
plt.plot(history['train_dice'], label='train')
plt.plot(history['val_dice'], label='val')
plt.title('Dice Score vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('Dice Score')
plt.legend()
plt.grid(True)
plt.subplot(2, 2, 4)
plt.plot(history['train_pixel_acc'], label='train')
plt.plot(history['val_pixel_acc'], label='val')
plt.title('Pixel Accuracy vs Epoch')
plt.xlabel('Epoch')
plt.ylabel('Pixel Accuracy')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(output_dir, 'all_metrics_curves.png'))
plt.close()
print(f"Saved combined metrics curves to '{output_dir}/all_metrics_curves.png'")
def save_history_to_file(history, output_dir):
"""Save training history to a text file."""
os.makedirs(output_dir, exist_ok=True)
filepath = os.path.join(output_dir, 'evaluation_metrics.txt')
with open(filepath, 'w') as f:
f.write("TRAINING RESULTS\n")
f.write("=" * 50 + "\n\n")
f.write("Final Metrics:\n")
f.write(f" Final Train Loss: {history['train_loss'][-1]:.4f}\n")
f.write(f" Final Val Loss: {history['val_loss'][-1]:.4f}\n")
f.write(f" Final Train IoU: {history['train_iou'][-1]:.4f}\n")
f.write(f" Final Val IoU: {history['val_iou'][-1]:.4f}\n")
f.write(f" Final Train Dice: {history['train_dice'][-1]:.4f}\n")
f.write(f" Final Val Dice: {history['val_dice'][-1]:.4f}\n")
f.write(f" Final Train Accuracy: {history['train_pixel_acc'][-1]:.4f}\n")
f.write(f" Final Val Accuracy: {history['val_pixel_acc'][-1]:.4f}\n")
f.write("=" * 50 + "\n\n")
f.write("Best Results:\n")
f.write(f" Best Val IoU: {max(history['val_iou']):.4f} (Epoch {np.argmax(history['val_iou']) + 1})\n")
f.write(f" Best Val Dice: {max(history['val_dice']):.4f} (Epoch {np.argmax(history['val_dice']) + 1})\n")
f.write(f" Best Val Accuracy: {max(history['val_pixel_acc']):.4f} (Epoch {np.argmax(history['val_pixel_acc']) + 1})\n")
f.write(f" Lowest Val Loss: {min(history['val_loss']):.4f} (Epoch {np.argmin(history['val_loss']) + 1})\n")
f.write("=" * 50 + "\n\n")
f.write("Per-Epoch History:\n")
f.write("-" * 100 + "\n")
headers = ['Epoch', 'Train Loss', 'Val Loss', 'Train IoU', 'Val IoU',
'Train Dice', 'Val Dice', 'Train Acc', 'Val Acc']
f.write("{:<8} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12} {:<12}\n".format(*headers))
f.write("-" * 100 + "\n")
n_epochs = len(history['train_loss'])
for i in range(n_epochs):
f.write("{:<8} {:<12.4f} {:<12.4f} {:<12.4f} {:<12.4f} {:<12.4f} {:<12.4f} {:<12.4f} {:<12.4f}\n".format(
i + 1,
history['train_loss'][i],
history['val_loss'][i],
history['train_iou'][i],
history['val_iou'][i],
history['train_dice'][i],
history['val_dice'][i],
history['train_pixel_acc'][i],
history['val_pixel_acc'][i]
))
print(f"Saved evaluation metrics to {filepath}")
# ============================================================================
# Main Training Function
# ============================================================================
def main():
# Configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {device}")
# Hyperparameters
batch_size = 2
w = int(((960 / 2) // 14) * 14)
h = int(((540 / 2) // 14) * 14)
lr = 1e-4
n_epochs = 10
# Output directory (relative to script location)
script_dir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(script_dir, 'train_stats')
os.makedirs(output_dir, exist_ok=True)
# Transforms
transform = transforms.Compose([
transforms.Resize((h, w)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
mask_transform = transforms.Compose([
transforms.Resize((h, w)),
transforms.ToTensor(),
])
# Dataset paths (relative to script location)
data_dir = os.path.join(script_dir, '..', 'Offroad_Segmentation_Training_Dataset', 'train')
val_dir = os.path.join(script_dir, '..', 'Offroad_Segmentation_Training_Dataset', 'val')
# Create datasets
trainset = MaskDataset(data_dir=data_dir, transform=transform, mask_transform=mask_transform)
train_loader = DataLoader(trainset, batch_size=batch_size, shuffle=True)
valset = MaskDataset(data_dir=val_dir, transform=transform, mask_transform=mask_transform)
val_loader = DataLoader(valset, batch_size=batch_size, shuffle=False)
print(f"Training samples: {len(trainset)}")
print(f"Validation samples: {len(valset)}")
# Load DINOv2 backbone
print("Loading DINOv2 backbone...")
BACKBONE_SIZE = "small"
backbone_archs = {
"small": "vits14",
"base": "vitb14_reg",
"large": "vitl14_reg",
"giant": "vitg14_reg",
}
backbone_arch = backbone_archs[BACKBONE_SIZE]
backbone_name = f"dinov2_{backbone_arch}"
backbone_model = torch.hub.load(repo_or_dir="facebookresearch/dinov2", model=backbone_name)
backbone_model.eval()
backbone_model.to(device)
print("Backbone loaded successfully!")
# Get embedding dimension from backbone
imgs, _ = next(iter(train_loader))
imgs = imgs.to(device)
with torch.no_grad():
output = backbone_model.forward_features(imgs)["x_norm_patchtokens"]
n_embedding = output.shape[2]
print(f"Embedding dimension: {n_embedding}")
print(f"Patch tokens shape: {output.shape}")
# Create segmentation head
classifier = SegmentationHeadConvNeXt(
in_channels=n_embedding,
out_channels=n_classes,
tokenW=w // 14,
tokenH=h // 14
)
classifier = classifier.to(device)
# Loss and optimizer
loss_fct = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(classifier.parameters(), lr=lr, momentum=0.9)
# Training history
history = {
'train_loss': [],
'val_loss': [],
'train_iou': [],
'val_iou': [],
'train_dice': [],
'val_dice': [],
'train_pixel_acc': [],
'val_pixel_acc': []
}
# Training loop
print("\nStarting training...")
print("=" * 80)
epoch_pbar = tqdm(range(n_epochs), desc="Training", unit="epoch")
for epoch in epoch_pbar:
# Training phase
classifier.train()
train_losses = []
train_pbar = tqdm(train_loader, desc=f"Epoch {epoch+1}/{n_epochs} [Train]",
leave=False, unit="batch")
for imgs, labels in train_pbar:
imgs, labels = imgs.to(device), labels.to(device)
with torch.no_grad():
output = backbone_model.forward_features(imgs)["x_norm_patchtokens"]
logits = classifier(output.to(device))
outputs = F.interpolate(logits, size=imgs.shape[2:], mode="bilinear", align_corners=False)
labels = labels.squeeze(dim=1).long()
loss = loss_fct(outputs, labels)
loss.backward()
optimizer.step()
optimizer.zero_grad()
train_losses.append(loss.item())
train_pbar.set_postfix(loss=f"{loss.item():.4f}")
# Validation phase
classifier.eval()
val_losses = []
val_pbar = tqdm(val_loader, desc=f"Epoch {epoch+1}/{n_epochs} [Val]",
leave=False, unit="batch")
with torch.no_grad():
for imgs, labels in val_pbar:
imgs, labels = imgs.to(device), labels.to(device)
output = backbone_model.forward_features(imgs)["x_norm_patchtokens"]
logits = classifier(output.to(device))
outputs = F.interpolate(logits, size=imgs.shape[2:], mode="bilinear", align_corners=False)
labels = labels.squeeze(dim=1).long()
loss = loss_fct(outputs, labels)
val_losses.append(loss.item())
val_pbar.set_postfix(loss=f"{loss.item():.4f}")
# Calculate metrics
train_iou, train_dice, train_pixel_acc = evaluate_metrics(
classifier, backbone_model, train_loader, device, num_classes=n_classes
)
val_iou, val_dice, val_pixel_acc = evaluate_metrics(
classifier, backbone_model, val_loader, device, num_classes=n_classes
)
# Store history
epoch_train_loss = np.mean(train_losses)
epoch_val_loss = np.mean(val_losses)
history['train_loss'].append(epoch_train_loss)
history['val_loss'].append(epoch_val_loss)
history['train_iou'].append(train_iou)
history['val_iou'].append(val_iou)
history['train_dice'].append(train_dice)
history['val_dice'].append(val_dice)
history['train_pixel_acc'].append(train_pixel_acc)
history['val_pixel_acc'].append(val_pixel_acc)
# Update epoch progress bar with metrics
epoch_pbar.set_postfix(
train_loss=f"{epoch_train_loss:.3f}",
val_loss=f"{epoch_val_loss:.3f}",
val_iou=f"{val_iou:.3f}",
val_acc=f"{val_pixel_acc:.3f}"
)
# Save plots
print("\nSaving training curves...")
save_training_plots(history, output_dir)
save_history_to_file(history, output_dir)
# Save model (in scripts directory)
model_path = os.path.join(script_dir, "segmentation_head.pth")
torch.save(classifier.state_dict(), model_path)
print(f"Saved model to '{model_path}'")
# Final evaluation
print("\nFinal evaluation results:")
print(f" Final Val Loss: {history['val_loss'][-1]:.4f}")
print(f" Final Val IoU: {history['val_iou'][-1]:.4f}")
print(f" Final Val Dice: {history['val_dice'][-1]:.4f}")
print(f" Final Val Accuracy: {history['val_pixel_acc'][-1]:.4f}")
print("\nTraining complete!")
if __name__ == "__main__":
main()