File size: 21,685 Bytes
54d9099 |
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 |
# -*- coding: utf-8 -*-
"""
2_train_CNN.py
This script trains a UNet segmentaiton model for a single detection class.
The user defines the "Session_Name" which is the output folder for the saved
model, plots and metrics.
The user use the Global Configuration to adjust parameters. This includdes:
- A weight factor is included for imbalanced datasets.
- Data used for Traning, Evaluation and Testing is based on csv files.
- The script creates masks used for the fastai packaage which use 1 for defect
and 0 for background. The user defines the pixel value for the class they want
to train the model for.
- Model training parameters are easily adjusted.
- Output includes plots of top 5 best and worst predictions of cracks and
txt files with a summary of the metrics
"""
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from tqdm import tqdm
from PIL import Image
from fastai.vision.all import *
from fastai.callback.tracker import SaveModelCallback, CSVLogger
from fastai.losses import CrossEntropyLossFlat
##################################
# 1. GLOBAL CONFIGURATION
##################################
# --- π·οΈ SESSION SETTINGS ---
SESSION_NAME = "TA+TB+TC_TEST"
# --- βοΈ CLASS IMBALANCE SETTINGS ---
CRACK_CLASS_WEIGHT = 20.0
# --- π CSV LOCATION & SELECTION ---
TRAIN_CSVS = ['TA_train.csv','TB_train.csv','TC_train.csv']
VAL_CSVS = ['TA_val.csv','TB_val.csv','TC_val.csv']
TEST_CSVS = ['TA_test.csv','TB_test.csv','TC_test.csv']
# --- Directory Settings ---
BASE_DIR = os.getcwd()
DATA_ROOT_DIR = os.path.abspath(os.path.join(BASE_DIR, '../'))
CSV_SOURCE_DIR = os.path.join(DATA_ROOT_DIR, '2_model_input/')
ORIGINAL_MASK_DIR = os.path.join(DATA_ROOT_DIR, '3_mask')
SANITIZED_MASK_DIR = os.path.join(DATA_ROOT_DIR, '3_masks_sanitized')
OUTPUT_ROOT = os.path.join(DATA_ROOT_DIR, '5_model_output')
SESSION_DIR = os.path.join(OUTPUT_ROOT, SESSION_NAME)
TRAIN_DIR = os.path.join(SESSION_DIR, 'Training')
TRAIN_MODEL_DIR = os.path.join(TRAIN_DIR, 'Models')
TRAIN_PLOT_DIR = os.path.join(TRAIN_DIR, 'Plots')
TEST_DIR = os.path.join(SESSION_DIR, 'Testing')
TEST_PLOT_DIR = os.path.join(TEST_DIR, 'Plots')
# --- Sanitization Settings ---
ORIGINAL_CLASS_PIXEL_VALUE = 40
SANITIZED_VALUE = 1
# --- Model Training Settings ---
MODEL_ARCH = resnet34
IMG_SIZE = 512
BATCH_SIZE = 8
NUM_EPOCHS = 2
LEARNING_RATE = 4e-4
WD = 1e-2
##################################
# 2. HELPER & METRIC FUNCTIONS
##################################
def check_system_resources():
print("\n--- π₯οΈ System Resource Check ---")
if torch.cuda.is_available():
device_name = torch.cuda.get_device_name(0)
print(f"β
GPU Detected: {device_name}")
else:
print("β GPU NOT Detected. Training will be slow.")
def enforce_dedicated_vram(fraction=0.95):
"""
Configures PyTorch to strictly use only a specific fraction of Dedicated VRAM.
If the model attempts to exceed this, it will throw an OOM error immediately
rather than spilling into slow system RAM (Shared Memory).
"""
if torch.cuda.is_available():
# Empty cache to start fresh
torch.cuda.empty_cache()
# Enforce the limit.
# We use 0.95 (95%) to leave a tiny bit of room for the OS display overhead
# so the driver doesn't panic and swap to RAM.
try:
torch.cuda.set_per_process_memory_fraction(fraction, 0)
print(f"π STRICT MODE: GPU memory capped at {fraction*100}%.")
print(" -> Script will CRASH if this limit is exceeded (preventing slow shared memory usage).")
except RuntimeError as e:
print(f"β οΈ Could not set memory fraction: {e}")
else:
print("β οΈ GPU not available. Running on CPU (slow).")
def get_expected_mask_basename(image_basename):
parts = image_basename.rsplit('_', 1)
if len(parts) == 2:
base_name, tile_id = parts
return f"{base_name}_fuse_{tile_id}_1band"
return image_basename
# --- Metrics ---
def _get_stats(inp, targ, class_idx=1, smooth=1e-6):
pred = inp.argmax(dim=1)
targ = targ.squeeze(1)
tp = ((pred == class_idx) & (targ == class_idx)).sum().float()
fp = ((pred == class_idx) & (targ != class_idx)).sum().float()
fn = ((pred != class_idx) & (targ == class_idx)).sum().float()
tn = ((pred != class_idx) & (targ != class_idx)).sum().float()
return tp, fp, fn, tn, smooth
def iou_crack(inp, targ):
tp, fp, fn, _, smooth = _get_stats(inp, targ)
return (tp + smooth) / (tp + fp + fn + smooth)
def dice_score_crack(inp, targ):
tp, fp, fn, _, smooth = _get_stats(inp, targ)
return (2 * tp + smooth) / (2 * tp + fp + fn + smooth)
def recall_crack(inp, targ):
tp, _, fn, _, smooth = _get_stats(inp, targ)
return (tp + smooth) / (tp + fn + smooth)
def precision_crack(inp, targ):
tp, fp, _, _, smooth = _get_stats(inp, targ)
return (tp + smooth) / (tp + fp + smooth)
def f1_score_crack(inp, targ):
tp, fp, fn, _, smooth = _get_stats(inp, targ)
precision = (tp + smooth) / (tp + fp + smooth)
recall = (tp + smooth) / (tp + fn + smooth)
return 2 * (precision * recall) / (precision + recall + smooth)
# --- Weighted Loss ---
class WeightedCombinedLoss(nn.Module):
def __init__(self, crack_weight=CRACK_CLASS_WEIGHT, dice_weight=0.5, ce_weight=0.5):
super().__init__()
self.dice_weight, self.ce_weight = dice_weight, ce_weight
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class_weights = torch.tensor([1.0, crack_weight]).to(device)
self.ce = CrossEntropyLossFlat(axis=1, weight=class_weights)
self.dice = DiceLoss(axis=1)
def forward(self, inp, targ):
ce_loss = self.ce(inp, targ.long())
dice_loss = self.dice(inp, targ)
return (self.ce_weight * ce_loss) + (self.dice_weight * dice_loss)
##################################
# 3. DATA PROCESSING FUNCTIONS
##################################
def sanitize_dataframe(df, desc="Sanitizing"):
os.makedirs(SANITIZED_MASK_DIR, exist_ok=True)
new_mask_paths = []
image_abs_paths = []
valid_indices = []
found_count = 0
missing_count = 0
for idx, row in tqdm(df.iterrows(), total=len(df), desc=desc):
try:
rel_path = row['filename']
abs_img_path = os.path.normpath(os.path.join(BASE_DIR, rel_path))
img_basename = os.path.splitext(os.path.basename(abs_img_path))[0]
mask_basename_no_ext = get_expected_mask_basename(img_basename)
mask_filename = f"{mask_basename_no_ext}.png"
raw_mask_path = os.path.join(ORIGINAL_MASK_DIR, mask_filename)
clean_mask_path = os.path.join(SANITIZED_MASK_DIR, mask_filename)
if os.path.exists(clean_mask_path):
image_abs_paths.append(abs_img_path)
new_mask_paths.append(clean_mask_path)
valid_indices.append(idx)
found_count += 1
continue
if not os.path.exists(raw_mask_path):
if missing_count < 3:
print(f"β οΈ Raw mask not found for: {os.path.basename(abs_img_path)}")
missing_count += 1
continue
target_class = row.get('target', 0)
mask_arr = np.array(Image.open(raw_mask_path))
if target_class == 1:
new_mask = np.zeros_like(mask_arr, dtype=np.uint8)
new_mask[mask_arr == ORIGINAL_CLASS_PIXEL_VALUE] = SANITIZED_VALUE
Image.fromarray(new_mask).save(clean_mask_path)
else:
blank_mask = np.zeros_like(mask_arr, dtype=np.uint8)
Image.fromarray(blank_mask).save(clean_mask_path)
image_abs_paths.append(abs_img_path)
new_mask_paths.append(clean_mask_path)
valid_indices.append(idx)
found_count += 1
except Exception as e:
print(f"Error on {os.path.basename(abs_img_path)}: {e}")
clean_df = df.iloc[valid_indices].copy()
clean_df['image_abs_path'] = image_abs_paths
clean_df['mask_path_sanitized'] = new_mask_paths
return clean_df
def combine_csvs(csv_list, is_valid_flag=False):
dfs = []
for f in csv_list:
path = os.path.join(CSV_SOURCE_DIR, f)
if os.path.exists(path):
dfs.append(pd.read_csv(path))
else:
print(f"β Warning: CSV file not found: {path}")
if not dfs: return pd.DataFrame()
combined = pd.concat(dfs, ignore_index=True)
combined['is_valid'] = is_valid_flag
return combined
##################################
# 4. REPORTING & VISUALIZATION
##################################
def visualize_sanity_check(df, save_dir, n_samples=5):
os.makedirs(save_dir, exist_ok=True)
crack_df = df[df['target'] == 1]
if len(crack_df) == 0: return
n = min(n_samples, len(crack_df))
samples = crack_df.sample(n=n)
fig, axs = plt.subplots(n, 3, figsize=(15, 5*n))
if n == 1: axs = np.expand_dims(axs, 0)
row_idx = 0
for _, row in samples.iterrows():
img = Image.open(row['image_abs_path'])
mask = np.array(Image.open(row['mask_path_sanitized']))
masked_overlay = np.ma.masked_where(mask == 0, mask)
axs[row_idx, 0].imshow(img, cmap='gray')
axs[row_idx, 0].set_title("Image")
axs[row_idx, 0].axis('off')
axs[row_idx, 1].imshow(mask, cmap='gray')
axs[row_idx, 1].set_title("Sanitized Mask")
axs[row_idx, 1].axis('off')
axs[row_idx, 2].imshow(img, cmap='gray')
axs[row_idx, 2].imshow(masked_overlay, cmap='autumn', alpha=0.6)
axs[row_idx, 2].set_title("Overlay")
axs[row_idx, 2].axis('off')
row_idx += 1
plt.tight_layout()
plt.savefig(os.path.join(save_dir, 'sanity_check_preview.png'))
plt.close()
# --- NEW: Generate Comprehensive Training Report ---
def generate_training_report(log_path, output_dir):
"""
Reads the CSV log, finds best epoch, writes a summary TXT file.
Returns: Dictionary of the best validation metrics found.
"""
if not os.path.exists(log_path):
print("β οΈ No training log found to generate report.")
return {}
df = pd.read_csv(log_path)
# Identify the best epoch based on IoU
# (FastAI columns: epoch, train_loss, valid_loss, [metrics...])
# We assume 'iou_crack' is in the columns
if 'iou_crack' in df.columns:
best_row = df.loc[df['iou_crack'].idxmax()]
best_epoch = int(best_row['epoch'])
else:
# Fallback if iou_crack missing, use last epoch
best_row = df.iloc[-1]
best_epoch = int(best_row['epoch'])
# Prepare stats for return
best_metrics = best_row.to_dict()
txt_path = os.path.join(output_dir, 'training_report.txt')
with open(txt_path, 'w') as f:
f.write("TRAINING SESSION SUMMARY\n")
f.write("========================\n")
f.write(f"Total Epochs Run: {len(df)}\n")
f.write(f"Best Model Saved at Epoch: {best_epoch}\n")
f.write("Note: 'valid_loss' and metrics below are evaluated on the VALIDATION dataset.\n\n")
f.write(f"BEST VALIDATION METRICS (Epoch {best_epoch}):\n")
f.write("-----------------------------------\n")
for k, v in best_metrics.items():
# Format numbers nicely
val_str = f"{v:.6f}" if isinstance(v, (int, float)) else str(v)
f.write(f"{k:<20}: {val_str}\n")
f.write("\n\nFULL TRAINING HISTORY\n")
f.write("=====================\n")
# Write the full dataframe table
f.write(df.to_string(index=False))
print(f"β
Training report with Best Epoch Summary saved to {txt_path}")
return best_metrics
# --- Generate Comparison Testing Report ---
def generate_testing_report(test_metrics, val_metrics, output_dir):
"""
Writes test metrics AND compares them to the best validation metrics.
"""
txt_path = os.path.join(output_dir, 'testing_report.txt')
with open(txt_path, 'w') as f:
f.write("TESTING & COMPARISON REPORT\n")
f.write("===========================\n\n")
# Header for Table
# Columns: Metric Name | Best Validation (Train Phase) | Testing Result
f.write(f"{'METRIC':<25} | {'BEST VALIDATION':<18} | {'TESTING RESULT':<18}\n")
f.write("-" * 65 + "\n")
for k, test_val in test_metrics.items():
# Try to find matching key in validation stats
# Note: FastAI training log might call it 'iou_crack'
# while validate() output might call it 'iou_crack' (should match)
val_val = val_metrics.get(k, "N/A")
# Formatting
if isinstance(test_val, float): test_str = f"{test_val:.6f}"
else: test_str = str(test_val)
if isinstance(val_val, float): val_str = f"{val_val:.6f}"
else: val_str = str(val_val)
f.write(f"{k:<25} | {val_str:<18} | {test_str:<18}\n")
print(f"β
Testing report (with Training comparison) saved to {txt_path}")
def save_best_worst_predictions(learn, dl, save_dir, k=5):
print(f"\n--- πΈ Generating Best/Worst {k} Predictions (CRACKS ONLY) ---")
os.makedirs(save_dir, exist_ok=True)
preds, targs = learn.get_preds(dl=dl)
pred_masks = preds.argmax(dim=1)
results = []
for i in range(len(pred_masks)):
p = pred_masks[i]
t = targs[i]
inter = ((p==1) & (t==1)).sum().item()
union = ((p==1) | (t==1)).sum().item()
if union == 0: iou_val = 1.0
else: iou_val = inter / (union + 1e-6)
has_crack_in_gt = (t == 1).sum().item() > 0
results.append({'idx': i, 'iou': iou_val, 'has_crack': has_crack_in_gt})
res_df = pd.DataFrame(results)
crack_candidates = res_df[res_df['has_crack'] == True].copy()
if len(crack_candidates) == 0:
print("β οΈ No images with cracks found. Skipping plots.")
return
print(f" -> Found {len(crack_candidates)} images with cracks.")
sorted_df = crack_candidates.sort_values(by='iou', ascending=True)
worst_df = sorted_df.head(k)
best_df = sorted_df.tail(k).iloc[::-1]
def plot_batch(df_rows, label_type):
for rank, (_, row_data) in enumerate(df_rows.iterrows()):
idx = int(row_data['idx'])
row_item = dl.dataset.items.iloc[idx]
img = Image.open(row_item['image_abs_path'])
gt_mask = targs[idx]
pred_mask = pred_masks[idx]
iou_val = row_data['iou']
fig, ax = plt.subplots(1, 3, figsize=(12, 4))
ax[0].imshow(img, cmap='gray'); ax[0].set_title(f"Input Image"); ax[0].axis('off')
ax[1].imshow(gt_mask.cpu(), cmap='gray'); ax[1].set_title("Ground Truth"); ax[1].axis('off')
ax[2].imshow(pred_mask.cpu(), cmap='gray'); ax[2].set_title(f"Pred (IoU: {iou_val:.4f})"); ax[2].axis('off')
filename = os.path.basename(row_item['image_abs_path'])
plt.suptitle(f"{label_type} #{rank+1} - {filename}")
plt.tight_layout()
plt.savefig(os.path.join(save_dir, f"{label_type}_{rank+1}_iou_{iou_val:.2f}.png"))
plt.close()
plot_batch(best_df, "BEST_CRACK")
plot_batch(worst_df, "WORST_CRACK")
print(f"β
Plots saved to {save_dir}")
##################################
# 5. MAIN PIPELINE
##################################
def get_metric_name(metric):
if hasattr(metric, '__name__'): return metric.__name__
if hasattr(metric, 'func'): return metric.func.__name__
return str(metric)
def run_pipeline():
check_system_resources()
for d in [TRAIN_DIR, TRAIN_MODEL_DIR, TRAIN_PLOT_DIR, TEST_DIR, TEST_PLOT_DIR]:
os.makedirs(d, exist_ok=True)
print(f"--- Session: {SESSION_NAME} ---")
# ==========================
# PHASE 1: PREPARE TRAINING DATA
# ==========================
print("\n--- π Loading Training Data ---")
df_train = combine_csvs(TRAIN_CSVS, is_valid_flag=False)
df_val = combine_csvs(VAL_CSVS, is_valid_flag=True)
if len(df_train) == 0: raise ValueError("No training data found.")
full_df = pd.concat([df_train, df_val], ignore_index=True)
df_ready = sanitize_dataframe(full_df, desc="Sanitizing Train/Val")
visualize_sanity_check(df_ready, TRAIN_PLOT_DIR)
codes = np.array(['background', 'crack'])
dblock = DataBlock(
blocks=(ImageBlock, MaskBlock(codes)),
get_x=ColReader('image_abs_path'),
get_y=ColReader('mask_path_sanitized'),
splitter=ColSplitter('is_valid'),
batch_tfms=[
*aug_transforms(flip_vert=True, max_rotate=15.0, max_zoom=1.1, max_lighting=0.2),
Normalize.from_stats(*imagenet_stats)
]
)
dls = dblock.dataloaders(
df_ready,
bs=BATCH_SIZE,
num_workers=0,
pin_memory=True
)
# ==========================
# PHASE 2: TRAINING
# ==========================
print(f"\n--- ποΈ Starting Training ({NUM_EPOCHS} epochs) ---")
history_log_path = os.path.join(TRAIN_DIR, 'training_log.csv')
learn = unet_learner(
dls, MODEL_ARCH,
loss_func=WeightedCombinedLoss(crack_weight=CRACK_CLASS_WEIGHT),
metrics=[dice_score_crack, iou_crack, recall_crack, precision_crack, f1_score_crack],
wd=WD,
model_dir=TRAIN_MODEL_DIR
).to_fp16()
callbacks = [
SaveModelCallback(monitor='iou_crack', comp=np.greater, fname='best_model'),
CSVLogger(fname=history_log_path)
]
learn.fit_one_cycle(NUM_EPOCHS, slice(LEARNING_RATE), cbs=callbacks)
# --- UPDATED: Generate Enhanced Training Report ---
# This now captures the best metrics to pass to Phase 3
best_val_metrics = generate_training_report(history_log_path, TRAIN_DIR)
print("\n--- π Evaluating Validation Set ---")
save_best_worst_predictions(learn, dls.valid, TRAIN_PLOT_DIR, k=5)
# ==========================
# PHASE 3: TESTING
# ==========================
print("\n--- π§ͺ Starting Testing Phase ---")
df_test_raw = combine_csvs(TEST_CSVS)
if len(df_test_raw) > 0:
df_test_ready = sanitize_dataframe(df_test_raw, desc="Sanitizing Test Set")
if len(df_test_ready) > 0:
test_dl = learn.dls.test_dl(df_test_ready, with_labels=True)
print("Loading best model for testing...")
learn.load('best_model')
results = learn.validate(dl=test_dl)
metric_names = ['valid_loss'] + [get_metric_name(m) for m in learn.metrics]
# FastAI calls validation loss 'valid_loss' in validate(), but we can just label it Loss
test_metrics_dict = dict(zip(metric_names, results))
print("\nTest Results:")
for k, v in test_metrics_dict.items():
print(f" {k}: {v:.6f}")
# --- UPDATED: Generate Comparison Report ---
generate_testing_report(test_metrics_dict, best_val_metrics, TEST_DIR)
save_best_worst_predictions(learn, test_dl, TEST_PLOT_DIR, k=5)
else:
print("β οΈ Test dataframe empty after sanitization.")
else:
print("β οΈ No test CSVs found or they are empty.")
print(f"\nβ
Pipeline Complete. Output saved to: {SESSION_DIR}")
if __name__ == "__main__":
# 1. Enforce the Lock
enforce_dedicated_vram(fraction=0.90) # Set to 90% to be safe, or 0.95 to push limits
# 2. Run Pipeline with Error Catching
try:
run_pipeline()
except torch.cuda.OutOfMemoryError:
# This is the specific error we want to catch
print("\n" + "="*60)
print("π CRITICAL ERROR: OUT OF DEDICATED GPU MEMORY")
print("="*60)
print("The script was stopped because it filled up the Dedicated VRAM.")
print("We prevented it from using Shared Memory (RAM) to maintain performance.")
print("SUGGESTIONS:")
print("1. Decrease BATCH_SIZE (currently set to {})".format(BATCH_SIZE))
print("2. Decrease IMG_SIZE (currently set to {})".format(IMG_SIZE))
print("3. Use a smaller model architecture (e.g., resnet18)")
sys.exit(1)
except Exception as e:
# Catch other standard errors
print(f"\nβ An unexpected error occurred: {e}")
# print(traceback.format_exc()) # Uncomment if you want full traceback |