-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_segmentation.py
More file actions
487 lines (386 loc) · 16.8 KB
/
test_segmentation.py
File metadata and controls
487 lines (386 loc) · 16.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
"""
Segmentation Validation Script
Converted from val_mask.ipynb
Evaluates a trained segmentation head on validation data and saves predictions
"""
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 torchvision.transforms as transforms
from PIL import Image
import cv2
import os
import argparse
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
img = np.clip(img, 0, 255).astype(np.uint8)
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
}
# Class names for visualization
class_names = [
'Background', 'Trees', 'Lush Bushes', 'Dry Grass', 'Dry Bushes',
'Ground Clutter', 'Logs', 'Rocks', 'Landscape', 'Sky'
]
n_classes = len(value_map)
# Color palette for visualization (10 distinct colors)
color_palette = np.array([
[0, 0, 0], # Background - black
[34, 139, 34], # Trees - forest green
[0, 255, 0], # Lush Bushes - lime
[210, 180, 140], # Dry Grass - tan
[139, 90, 43], # Dry Bushes - brown
[128, 128, 0], # Ground Clutter - olive
[139, 69, 19], # Logs - saddle brown
[128, 128, 128], # Rocks - gray
[160, 82, 45], # Landscape - sienna
[135, 206, 235], # Sky - sky blue
], dtype=np.uint8)
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)
def mask_to_color(mask):
"""Convert a class mask to a colored RGB image."""
h, w = mask.shape
color_mask = np.zeros((h, w, 3), dtype=np.uint8)
for class_id in range(n_classes):
color_mask[mask == class_id] = color_palette[class_id]
return color_mask
# ============================================================================
# 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, data_id
# ============================================================================
# Model: Segmentation Head (ConvNeXt-style) - Must match training
# ============================================================================
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), 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), 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()
# ============================================================================
# Visualization Functions
# ============================================================================
def save_prediction_comparison(img_tensor, gt_mask, pred_mask, output_path, data_id):
"""Save a side-by-side comparison of input, ground truth, and prediction."""
# Denormalize image
img = img_tensor.cpu().numpy()
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
img = np.clip(img, 0, 1)
# Convert masks to color
gt_color = mask_to_color(gt_mask.cpu().numpy().astype(np.uint8))
pred_color = mask_to_color(pred_mask.cpu().numpy().astype(np.uint8))
# Create figure
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(img)
axes[0].set_title('Input Image')
axes[0].axis('off')
axes[1].imshow(gt_color)
axes[1].set_title('Ground Truth')
axes[1].axis('off')
axes[2].imshow(pred_color)
axes[2].set_title('Prediction')
axes[2].axis('off')
plt.suptitle(f'Sample: {data_id}')
plt.tight_layout()
plt.savefig(output_path, dpi=150, bbox_inches='tight')
plt.close()
def save_metrics_summary(results, output_dir):
"""Save metrics summary to a text file and create bar chart."""
os.makedirs(output_dir, exist_ok=True)
# Save text summary
filepath = os.path.join(output_dir, 'evaluation_metrics.txt')
with open(filepath, 'w') as f:
f.write("EVALUATION RESULTS\n")
f.write("=" * 50 + "\n")
f.write(f"Mean IoU: {results['mean_iou']:.4f}\n")
f.write("=" * 50 + "\n\n")
f.write("Per-Class IoU:\n")
f.write("-" * 40 + "\n")
for i, (name, iou) in enumerate(zip(class_names, results['class_iou'])):
iou_str = f"{iou:.4f}" if not np.isnan(iou) else "N/A"
f.write(f" {name:<20}: {iou_str}\n")
print(f"\nSaved evaluation metrics to {filepath}")
# Create bar chart for per-class IoU
fig, ax = plt.subplots(figsize=(10, 6))
valid_iou = [iou if not np.isnan(iou) else 0 for iou in results['class_iou']]
ax.bar(range(n_classes), valid_iou, color=[color_palette[i] / 255 for i in range(n_classes)],
edgecolor='black')
ax.set_xticks(range(n_classes))
ax.set_xticklabels(class_names, rotation=45, ha='right')
ax.set_ylabel('IoU')
ax.set_title(f'Per-Class IoU (Mean: {results["mean_iou"]:.4f})')
ax.set_ylim(0, 1)
ax.axhline(y=results['mean_iou'], color='red', linestyle='--', label='Mean')
ax.legend()
ax.grid(axis='y', alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(output_dir, 'per_class_metrics.png'), dpi=150, bbox_inches='tight')
plt.close()
print(f"Saved per-class metrics chart to '{output_dir}/per_class_metrics.png'")
# ============================================================================
# Main Validation Function
# ============================================================================
def main():
# Get script directory for default paths
script_dir = os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser(description='Segmentation prediction/inference script')
parser.add_argument('--model_path', type=str, default=os.path.join(script_dir, 'segmentation_head.pth'),
help='Path to trained model weights')
parser.add_argument('--data_dir', type=str, default=os.path.join(script_dir, '..', 'Offroad_Segmentation_testImages'),
help='Path to validation dataset')
parser.add_argument('--output_dir', type=str, default='./predictions',
help='Directory to save prediction visualizations')
parser.add_argument('--batch_size', type=int, default=2,
help='Batch size for validation')
parser.add_argument('--num_samples', type=int, default=5,
help='Number of comparison visualizations to save (predictions saved for ALL images)')
args = parser.parse_args()
# Create output directory
os.makedirs(args.output_dir, exist_ok=True)
# Configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {device}")
# Image dimensions (must match training)
w = int(((960 / 2) // 14) * 14)
h = int(((540 / 2) // 14) * 14)
# 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(),
])
# Create dataset
print(f"Loading dataset from {args.data_dir}...")
valset = MaskDataset(data_dir=args.data_dir, transform=transform, mask_transform=mask_transform)
val_loader = DataLoader(valset, batch_size=args.batch_size, shuffle=False)
print(f"Loaded {len(valset)} samples")
# 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
sample_img, _, _ = valset[0]
sample_img = sample_img.unsqueeze(0).to(device)
with torch.no_grad():
output = backbone_model.forward_features(sample_img)["x_norm_patchtokens"]
n_embedding = output.shape[2]
print(f"Embedding dimension: {n_embedding}")
# Load classifier
print(f"Loading model from {args.model_path}...")
classifier = SegmentationHeadConvNeXt(
in_channels=n_embedding,
out_channels=n_classes,
tokenW=w // 14,
tokenH=h // 14
)
classifier.load_state_dict(torch.load(args.model_path, map_location=device))
classifier = classifier.to(device)
classifier.eval()
print("Model loaded successfully!")
# Create subdirectories for outputs
masks_dir = os.path.join(args.output_dir, 'masks')
masks_color_dir = os.path.join(args.output_dir, 'masks_color')
comparisons_dir = os.path.join(args.output_dir, 'comparisons')
os.makedirs(masks_dir, exist_ok=True)
os.makedirs(masks_color_dir, exist_ok=True)
os.makedirs(comparisons_dir, exist_ok=True)
# Run evaluation and save predictions for ALL images
print(f"\nRunning evaluation and saving predictions for all {len(valset)} images...")
iou_scores = []
dice_scores = []
pixel_accuracies = []
all_class_iou = []
all_class_dice = []
sample_count = 0
with torch.no_grad():
pbar = tqdm(val_loader, desc="Processing", unit="batch")
for batch_idx, (imgs, labels, data_ids) in enumerate(pbar):
imgs, labels = imgs.to(device), labels.to(device)
# Forward pass
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_squeezed = labels.squeeze(dim=1).long()
predicted_masks = torch.argmax(outputs, dim=1)
# Calculate metrics
iou, class_iou = compute_iou(outputs, labels_squeezed, num_classes=n_classes)
dice, class_dice = compute_dice(outputs, labels_squeezed, num_classes=n_classes)
pixel_acc = compute_pixel_accuracy(outputs, labels_squeezed)
iou_scores.append(iou)
dice_scores.append(dice)
pixel_accuracies.append(pixel_acc)
all_class_iou.append(class_iou)
all_class_dice.append(class_dice)
# Save predictions for every image
for i in range(imgs.shape[0]):
data_id = data_ids[i]
base_name = os.path.splitext(data_id)[0]
# Save raw prediction mask (class IDs 0-9)
pred_mask = predicted_masks[i].cpu().numpy().astype(np.uint8)
pred_img = Image.fromarray(pred_mask)
pred_img.save(os.path.join(masks_dir, f'{base_name}_pred.png'))
# Save colored prediction mask (RGB visualization)
pred_color = mask_to_color(pred_mask)
cv2.imwrite(os.path.join(masks_color_dir, f'{base_name}_pred_color.png'),
cv2.cvtColor(pred_color, cv2.COLOR_RGB2BGR))
# Save comparison visualization for first N samples
if sample_count < args.num_samples:
save_prediction_comparison(
imgs[i], labels_squeezed[i], predicted_masks[i],
os.path.join(comparisons_dir, f'sample_{sample_count}_comparison.png'),
data_id
)
sample_count += 1
# Update progress bar with metrics
pbar.set_postfix(iou=f"{iou:.3f}")
# Aggregate results
mean_iou = np.nanmean(iou_scores)
mean_dice = np.nanmean(dice_scores)
mean_pixel_acc = np.mean(pixel_accuracies)
# Average per-class metrics
avg_class_iou = np.nanmean(all_class_iou, axis=0)
avg_class_dice = np.nanmean(all_class_dice, axis=0)
results = {
'mean_iou': mean_iou,
'class_iou': avg_class_iou
}
# Print results
print("\n" + "=" * 50)
print("EVALUATION RESULTS")
print("=" * 50)
print(f"Mean IoU: {mean_iou:.4f}")
print("=" * 50)
# Save all results
save_metrics_summary(results, args.output_dir)
print(f"\nPrediction complete! Processed {len(valset)} images.")
print(f"\nOutputs saved to {args.output_dir}/")
print(f" - masks/ : Raw prediction masks (class IDs 0-9)")
print(f" - masks_color/ : Colored prediction masks (RGB)")
print(f" - comparisons/ : Side-by-side comparison images ({args.num_samples} samples)")
print(f" - evaluation_metrics.txt")
print(f" - per_class_metrics.png")
if __name__ == "__main__":
main()