-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain_C3VD.py
More file actions
553 lines (444 loc) · 21.8 KB
/
Train_C3VD.py
File metadata and controls
553 lines (444 loc) · 21.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
'''
Copyright (c) 2025 Bashayer Abdallah
Licensed under CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
Commercial use is prohibited.
'''
import os
import glob
import torch
import numpy as np
from tqdm import tqdm
import torch.nn as nn
import torch.optim as optim
from datetime import datetime
from torch.utils.data import DataLoader
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from function import scharrEdgeDetector, compute_errors, earlyStoppingWithCheckpoint
from torch.optim.lr_scheduler import OneCycleLR, CosineAnnealingLR, ReduceLROnPlateau
import torchvision.transforms as transforms
from autodistill_sam_clip import SAMCLIP # Import SAMCLIP from autodistill_sam_clip
from loss import siLogLoss, edgeLoss, depth_loss_function
from c3vd_loader import c3VD_Dataset
from depthClass import depthClass
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
torch.cuda.empty_cache()
# Utility to extract classes from a Subset
from torch.utils.data import Subset as _TorchSubset
def subset_classes(subset: _TorchSubset):
"""
Given a torch.utils.data.Subset, return the unique class labels present in that subset.
"""
return sorted({ subset.dataset.labels[i] for i in subset.indices })
####################################
# Training Function
####################################
def train(model, dataloader, criterion_depth, criterion_class, criterion_edge,
optimizer, device, scheduler=None):
model.train()
# Loss weights
depth_alfa = 0.60
class_alfa = 0.30
edge_alfa = 0.10
total_depth_loss = 0.0
total_class_loss = 0.0
total_edge_loss = 0.0
total_loss = 0.0
total_a1 = 0.0 # Depth accuracy metric (a1)
total_rmse = 0.0 # RMSE
total_class_accuracy = 0.0 # Classification accuracy
max_grad_norm = 1.0 # For gradient clipping
edge_detector = ScharrEdgeDetector()
# Use the model's internal ontology to map descriptive labels to indices.
ontology = (model.module.sam_clip_encoder.ontology
if hasattr(model, "module")
else model.sam_clip_encoder.ontology)
# print("Ontology (classes):", ontology)
print("Starting training...")
for rgb, depth, norm, mask, class_labels in tqdm(dataloader, desc="Training", leave=False):
rgb, depth = rgb.to(device), depth.to(device)
edge = edge_detector(depth)
# Convert descriptive labels to integer targets.
# initializes an empty list target_indices to store numeric class indices.
target_indices = []
for label in class_labels:
try:
target_indices.append(ontology.index(label))
except ValueError:
raise ValueError(f"Label '{label}' not found in the ontology: {ontology}")
# print("target_indices:", target_indices)
targets = torch.tensor(target_indices, dtype=torch.long, device=device)
# print("targets:", targets)
optimizer.zero_grad()
# Forward pass.
depth_pred, class_logits, probs, preds, prompts = model(rgb, edge, prompts=ontology)
predicted_edges = edge_detector(depth_pred)
# Compute losses.
loss_depth = criterion_depth(depth_pred, depth)
loss_class = criterion_class(class_logits, targets)
loss_edge = criterion_edge(predicted_edges, edge)
loss = (depth_alfa * loss_depth) + (class_alfa * loss_class) + (edge_alfa * loss_edge)
loss.backward()
# Gradient clipping and optimizer step.
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
optimizer.step()
with torch.no_grad():
rmse = torch.sqrt(torch.mean((depth_pred - depth) ** 2)).item()
thresh = torch.max(depth / depth_pred, depth_pred / depth)
a1 = (thresh < 1.25).float().mean().item()
pred_classes = torch.argmax(class_logits, dim=1)
batch_class_accuracy = (pred_classes == targets).float().mean().item()
# Get batch size.
batch_size = rgb.size(0)
# Accumulate losses weighted by batch size.
total_depth_loss += loss_depth.item() * batch_size
total_edge_loss += loss_edge.item() * batch_size
total_loss += loss.item() * batch_size
total_rmse += rmse * batch_size
total_a1 += a1 * batch_size
total_class_loss += loss_class.item() * batch_size
total_class_accuracy += batch_class_accuracy * batch_size
# Step scheduler per batch (if using a per-batch scheduler).
if scheduler is not None:
scheduler.step(total_loss / len(dataloader))
# Compute averages over the entire dataset.
dataset_size = len(dataloader.dataset)
avg_depth_loss = total_depth_loss / dataset_size
avg_class_loss = total_class_loss / dataset_size
avg_edge_loss = total_edge_loss / dataset_size
avg_total_loss = total_loss / dataset_size
avg_rmse = total_rmse / dataset_size
avg_a1 = total_a1 / dataset_size
avg_class_accuracy = total_class_accuracy / dataset_size
print(f"Train - Depth Loss: {avg_depth_loss:.4f}, Class Loss: {avg_class_loss:.4f}, "
f"Edge Loss: {avg_edge_loss:.4f}, Total Loss: {avg_total_loss:.4f}, "
f"RMSE: {avg_rmse:.4f}, a1: {avg_a1:.4f}, Classification Accuracy: {avg_class_accuracy:.4f}")
return avg_total_loss, avg_rmse, avg_a1, avg_class_accuracy
####################################
# Validation Function
####################################
def validate(model, dataloader, criterion_depth, criterion_class, criterion_edge, device):
depth_alfa = 0.60
class_alfa = 0.30
edge_alfa = 0.10
edge_detector = ScharrEdgeDetector()
total_depth_loss = 0.0
total_class_loss = 0.0
total_edge_loss = 0.0
total_loss = 0.0
total_a1 = 0.0
total_rmse = 0.0
total_class_accuracy = 0.0
ontology = model.sam_clip_encoder.ontology
num_classes = len(ontology)
# print("Ontology (of validation):", ontology)
print("Starting validation...")
model.eval()
with torch.no_grad():
for rgb, depth, norm, mask, class_labels in tqdm(dataloader, desc="Validation", leave=False):
rgb, depth = rgb.to(device), depth.to(device)
greyScale_trans = transforms.Grayscale(num_output_channels=1)
grayRGB = greyScale_trans(rgb)
edge_rgb = edge_detector.forward(grayRGB)
# Convert descriptive labels to indices.
target_indices = []
for label in class_labels:
try:
target_indices.append(ontology.index(label))
except ValueError:
raise ValueError(f"Label '{label}' not found in the ontology: {ontology}")
targets = torch.tensor(target_indices, dtype=torch.long, device=device)
depth_pred, class_logits, probs, preds, prompts = model(rgb, edge_rgb, prompts=ontology)
predicted_edges = edge_detector(depth_pred)
# Sanity checks (adjust as needed).
assert torch.all(depth_pred >= 0) and torch.all(depth_pred <= 1), "Depth output is not in [0, 1]!"
assert class_logits.shape[
1] == num_classes, f"Expected {num_classes} classes, but got {class_logits.shape[1]}"
loss_depth = criterion_depth(depth_pred, depth)
loss_class = criterion_class(class_logits, targets)
loss_edge = criterion_edge(predicted_edges, edge_rgb)
loss = (depth_alfa * loss_depth) + (class_alfa * loss_class) + (edge_alfa * loss_edge)
rmse = torch.sqrt(torch.mean((depth_pred - depth) ** 2)).item()
thresh = torch.max(depth / depth_pred, depth_pred / depth)
a1 = (thresh < 1.25).float().mean().item()
pred_classes = torch.argmax(class_logits, dim=1)
batch_class_accuracy = (pred_classes == targets).float().mean().item()
# print("Predicted classes:", pred_classes.cpu().numpy())
# print("Target indices:", targets.cpu().numpy())
# print("Ontology:", ontology)
# print("Predicted logits:", class_logits[:5].detach().cpu().numpy())
# print("Predicted classes:", torch.argmax(class_logits, dim=1)[:5].detach().cpu().numpy())
# print("Targets:", targets[:5].detach().cpu().numpy())
batch_size = rgb.size(0)
total_depth_loss += loss_depth.item() * batch_size
total_class_loss += loss_class.item() * batch_size
total_edge_loss += loss_edge.item() * batch_size
total_loss += loss.item() * batch_size
total_rmse += rmse * batch_size
total_a1 += a1 * batch_size
total_class_accuracy += batch_class_accuracy * batch_size
avg_depth_loss = total_depth_loss / len(dataloader.dataset)
avg_class_loss = total_class_loss / len(dataloader.dataset)
avg_edge_loss = total_edge_loss / len(dataloader.dataset)
avg_total_loss = total_loss / len(dataloader.dataset)
avg_rmse = total_rmse / len(dataloader.dataset)
avg_a1 = total_a1 / len(dataloader.dataset)
avg_class_accuracy = total_class_accuracy / len(dataloader.dataset)
print(f"Validation - Depth Loss: {avg_depth_loss:.4f}, Class Loss: {avg_class_loss:.4f}, "
f"Edge Loss: {avg_edge_loss:.4f}, Total Loss: {avg_total_loss:.4f}, "
f"RMSE: {avg_rmse:.4f}, a1: {avg_a1:.4f}, Classification Accuracy: {avg_class_accuracy:.4f}")
return avg_total_loss, avg_rmse, avg_a1, avg_class_accuracy
####################################
# Test Function
####################################
def test(model, dataloader, device, save_dir):
model.eval()
edge_detector = ScharrEdgeDetector()
predictions, ground_truths = [], []
class_predictions, class_ground_truths = [], []
# Optionally, you can also collect predicted edges.
edge_predictions = []
edge_ground_truths = []
ontology = model.sam_clip_encoder.ontology
print("Starting testing...")
with torch.no_grad():
for rgb, depth, norm, mask, label in tqdm(dataloader, desc="Testing", leave=False):
rgb, depth = rgb.to(device), depth.to(device)
greyScale_trans = transforms.Grayscale(num_output_channels=1)
grayRGB = greyScale_trans(rgb)
edge_rgb = edge_detector.forward(grayRGB)
# Convert descriptive labels to indices.
target_indices = []
for labels in label:
try:
target_indices.append(ontology.index(labels))
except ValueError:
raise ValueError(f"Label '{labels}' not found in the ontology: {ontology}")
targets = torch.tensor(target_indices, dtype=torch.long, device=device)
depth_pred, class_logits, probs, preds, prompts = model(rgb, edge_rgb, prompts=ontology)
predicted_edges = edge_detector(depth_pred)
predictions.append(depth_pred.cpu().numpy())
ground_truths.append(depth.cpu().numpy())
# Compute and store edge predictions.
edge_predictions.append(predicted_edges.cpu().numpy())
edge_ground_truths.append(edge_rgb.cpu().numpy())
pred_classes = torch.argmax(class_logits, dim=1)
class_predictions.extend(pred_classes.cpu().numpy())
class_ground_truths.extend(targets.cpu().numpy())
print("Unique classes in class_ground_truths:", np.unique(class_ground_truths))
print("Unique classes in class_predictions:", np.unique(class_predictions))
# Compute depth error metrics (assumes you have defined compute_errors).
depth_metrics = compute_errors(np.array(ground_truths), np.array(predictions))
class_accuracy = accuracy_score(class_ground_truths, class_predictions)
# Combine both true and predicted labels to ensure you capture all unique classes
unique_classes = np.unique(np.concatenate((class_ground_truths, class_predictions)))
target_names = [f"Class {int(i)}" for i in unique_classes]
class_report = classification_report(
class_ground_truths, class_predictions,
labels=unique_classes, # explicitly specify which labels to use
target_names=target_names
)
print("Depth Error Metrics:")
for key, value in depth_metrics.items():
print(f"{key}: {value:.4f}")
print("\nClassification Metrics:")
print(f"Accuracy: {class_accuracy:.4f}")
print(class_report)
test_metrics_file = os.path.join(save_dir, 'test_metrics.txt')
with open(test_metrics_file, 'w') as f:
f.write("Depth Metrics:\n")
for key, value in depth_metrics.items():
f.write(f"{key}: {value:.4f}\n")
f.write("\nClassification Metrics:\n")
f.write(f"Accuracy: {class_accuracy:.4f}\n")
f.write(class_report)
confusion = confusion_matrix(class_ground_truths, class_predictions)
conf_matrix_file = os.path.join(save_dir, 'confusion_matrix.txt')
np.savetxt(conf_matrix_file, confusion, fmt='%d', header='Confusion Matrix')
print(f"Confusion Matrix saved to {conf_matrix_file}.")
return (predictions, ground_truths, class_predictions, class_ground_truths)
if __name__ == "__main__":
# Hyperparameters
batch_size = 6
learning_rate = 0.000357
num_epochs = 50
num_classes = 6
img_size = 224
# GPU setup
print(torch.cuda.device_count()) # Number of available GPUs
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
script_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(script_dir, 'Data', 'C3VD')
if not os.path.isdir(data_dir):
raise RuntimeError(f"Data directory not found: {data_dir}")
full_ds = C3VD_Dataset(data_dir=data_dir, image_size=256)
total = len(full_ds)
n_train = int(0.80 * total)
n_val = int(0.10 * total)
n_test = total - n_train - n_val
train_ds, val_ds, test_ds = torch.utils.data.random_split(
full_ds,
[n_train, n_val, n_test],
generator=torch.Generator().manual_seed(42)
)
train_idx = set(train_ds.indices)
val_idx = set(val_ds.indices)
test_idx = set(test_ds.indices)
assert train_idx.isdisjoint(val_idx), "ERROR: train ∩ val not empty!"
assert train_idx.isdisjoint(test_idx), "ERROR: train ∩ test not empty!"
assert val_idx.isdisjoint(test_idx), "ERROR: val ∩ test not empty!"
print("✔︎ Splits are disjoint.")
print(f"Train dataset size: {len(train_ds)}")
print(f"Validation dataset size: {len(val_ds)}")
print(f"Test dataset size: {len(test_ds)}")
train_loader = DataLoader(train_ds, batch_size=6, shuffle=True, num_workers=4, pin_memory=True)
val_loader = DataLoader(val_ds, batch_size=6, shuffle=False, num_workers=4, pin_memory=True)
test_loader = DataLoader(test_ds, batch_size=1, shuffle=False, num_workers=1, pin_memory=True)
train_labels = subset_classes(train_ds)
val_labels = subset_classes(val_ds)
test_labels = subset_classes(test_ds)
print("Train classes:")
for c in train_labels:
print(" ", c)
print("Validation classes:")
for c in val_labels:
print(" ", c)
print("Test classes:")
for c in test_labels:
print(" ", c)
all_labels = set(train_labels) | set(val_labels) | set(test_labels)
# Build ontology from dataset labels
# all_labels = set(train_loader.class_label) | set(val_loader.class_label) | set(test_loader.class_label)
ontology = sorted(set(all_labels))
print("Ontology (union of classes):", ontology)
################## to use GPU by id ##################
# Create model
model = DepthClass(ontology=ontology).to(device)
# model = DepthClass_concat(ontology=ontology).to(device)
# if torch.cuda.device_count() > 1:
# print("Using multiple GPUs...")
# model = nn.DataParallel(model)
# Loss functions
criterion_depth = SILogLoss()
criterion_edge = EdgeLoss()
criterion_class = nn.CrossEntropyLoss()
depth_nLoss = nn.SmoothL1Loss() # Huber Loss for robust training
# ============================
# 1. Set up directories
# ============================
model_name = model.__class__.__name__
model_folder = os.path.join('models_checkpoints', model_name)
os.makedirs(model_folder, exist_ok=True)
checkpoint_dir = os.path.join(model_folder, "checkpoints")
os.makedirs(checkpoint_dir, exist_ok=True)
metrics_file = os.path.join(checkpoint_dir, 'training_validation_metrics.txt')
with open(metrics_file, 'w') as f:
f.write("Epoch\tTrain_Loss\tTrain_RMSE\tTrain_a1\tVal_Loss\tVal_RMSE\tVal_a1\tTrain_Class_Acc\tVal_Class_Acc\n")
# ============================
# 2. Load the latest checkpoint (if any)
# ============================
checkpoint_files = glob.glob(os.path.join(checkpoint_dir, 'epoch_*.pth'))
start_epoch = 0
optimizer_state = None # placeholder for optimizer state
if checkpoint_files:
# Select the checkpoint with the latest modification time
latest_checkpoint = max(checkpoint_files, key=os.path.getmtime)
checkpoint = torch.load(latest_checkpoint)
# Compare the keys of the checkpoint with the current model's keys
checkpoint_keys = set(checkpoint['model_state_dict'].keys())
model_keys = set(model.state_dict().keys())
# If the checkpoint keys include all model keys, load the checkpoint.
if model_keys.issubset(checkpoint_keys):
model.load_state_dict(checkpoint['model_state_dict'])
optimizer_state = checkpoint.get('optimizer_state_dict', None)
start_epoch = checkpoint['epoch'] + 1 # next epoch to run
print(f"Loaded checkpoint from {latest_checkpoint}. Starting from epoch {start_epoch}.")
else:
start_epoch = 0
epoch = start_epoch
# ============================
# 3. Create optimizer, early stopping, and scheduler
# ============================
optimizer = optim.AdamW(model.parameters(), lr=learning_rate)
# Restore the optimizer state if available.
if optimizer_state is not None:
optimizer.load_state_dict(optimizer_state)
# Initialize early stopping unconditionally.
best_checkpoint_path = os.path.join(checkpoint_dir, 'best_checkpoint.pth')
# early_stopping = EarlyStoppingWithCheckpoint(patience=5, min_delta=0.001, checkpoint_path=best_checkpoint_path)
# Use ReduceLROnPlateau as the scheduler.
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=3, verbose=True)
train_loss = None
val_loss = None
# ============================
# 4. Training and validation loop
# ============================
for epoch in range(start_epoch, num_epochs):
print(f"Epoch [{epoch + 1}/{num_epochs}] starting...")
# Training: note that criterion_edge is now required.
train_loss, train_rmse, train_a1, train_class_accuracy = train(
model,
train_loader,
criterion_depth,
criterion_class,
criterion_edge, # edge loss function
optimizer,
device,
scheduler=None # remove per-batch scheduler stepping
)
# Validation also requires the edge loss criterion.
val_loss, val_rmse, val_a1, val_class_accuracy = validate(
model,
val_loader,
criterion_depth,
criterion_class,
criterion_edge,
device
)
print(f"Epoch [{epoch + 1}/{num_epochs}] - "
f"Train Loss: {train_loss:.4f}, Train RMSE: {train_rmse:.4f}, "
f"Train a1: {train_a1:.4f}, Train Class Acc: {train_class_accuracy:.4f}, "
f"Val Loss: {val_loss:.4f}, Val RMSE: {val_rmse:.4f}, "
f"Val a1: {val_a1:.4f}, Val Class Acc: {val_class_accuracy:.4f}")
# Append training and validation metrics to the file after each epoch.
with open(metrics_file, 'a') as f:
f.write(f"{epoch + 1}\t{train_loss:.4f}\t{train_rmse:.4f}\t{train_a1:.4f}\t"
f"{val_loss:.4f}\t{val_rmse:.4f}\t{val_a1:.4f}\t{train_class_accuracy:.4f}\t{val_class_accuracy:.4f}\n")
# Step the learning rate scheduler on validation loss
scheduler.step(val_loss)
# Save a checkpoint for the current epoch
current_date = datetime.now().strftime('%Y-%m-%d')
epoch_checkpoint_path = os.path.join(checkpoint_dir, f'epoch_{epoch + 1}_{current_date}.pth')
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss': train_loss,
'train_rmse': train_rmse,
'train_a1': train_a1,
'train_class_accuracy': train_class_accuracy,
'val_loss': val_loss,
'val_rmse': val_rmse,
'val_a1': val_a1,
'val_class_accuracy': val_class_accuracy
}, epoch_checkpoint_path)
print(f"Checkpoint for epoch {epoch + 1} saved at {epoch_checkpoint_path}.")
# Save the final model checkpoint (using a fixed filename)
final_model_path = os.path.join(checkpoint_dir, 'final_model.pth')
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss': train_loss,
'val_loss': val_loss
}, final_model_path)
print(f"Final model saved as {final_model_path}")
# Load the final fine-tuned model for testing.
checkpoint = torch.load(final_model_path)
model.load_state_dict(checkpoint['model_state_dict'])
print("Loaded the final fine-tuned model for testing.")
predictions, ground_truths, class_predictions, class_ground_truths = test(model, test_loader, device,
checkpoint_dir)
print("Testing completed.")
'''