-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_quick.py
More file actions
725 lines (584 loc) · 25.8 KB
/
Copy pathtrain_quick.py
File metadata and controls
725 lines (584 loc) · 25.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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
"""
Quick training script for shadow detection
Based on "Leave-One-Out Kernel Optimization for Shadow Detection" (Vicente et al., ICCV 2015)
Quick validation version: using fewer samples for preliminary training and testing
Estimated training time: 10-20 minutes (GPU) / 20-30 minutes (CPU)
Training flow:
1. Data loading and splitting
2. Texton dictionary training
3. Region feature extraction
4. LSSVM classifier training (GPU acceleration)
5. Validation set evaluation
Run command: python train_quick.py
Author: Shadow Detection Project
"""
import os
import sys
import time
import pickle
import numpy as np
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Tuple, Optional
# GPU Support (PyTorch)
import torch
USE_GPU = torch.cuda.is_available()
DEVICE = torch.device('cuda' if USE_GPU else 'cpu')
if USE_GPU:
print(f"[GPU] Using CUDA: {torch.cuda.get_device_name(0)}")
print(f"[GPU] Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
else:
print("[CPU] CUDA not available, using CPU")
# Add project root to path
PROJECT_DIR = Path(__file__).parent
sys.path.insert(0, str(PROJECT_DIR))
# Import project modules
from data.dataset_loader import get_dataset
from preprocessing.superpixel import SuperpixelSegmenter
from preprocessing.region import MeanShiftRegionGenerator
from preprocessing.features import PaperCompliantFeatureExtractor
from preprocessing.texton import TextonFeatureExtractor
from models.lssvm import LSSVM
from models.platt_scaling import PlattScaler, balanced_error_rate
from models.distances import emd_1d_matrix, chi_square_distance_matrix
# =============================================================================
# Configuration Parameters - Quick Validation Version
# =============================================================================
QUICK_CONFIG = {
# Data configuration
'n_train_images': 500, # Number of training images
'n_val_images': 100, # Number of validation images
'random_seed': 42,
# Superpixel configuration
'n_segments': 150, # Number of superpixels per image
'compactness': 20,
# Region configuration
'region_bandwidth': 0.3,
# Texton configuration
'n_textons': 128, # number of textons (paper: 128, quick version: 64)
'texton_train_images': 30,
# LSSVM configuration
'gamma': 1.0,
# sigma_multiplier: sigma = multiplier * mean_distance
# Paper recommendation: {1/8, 1/6, 1/4, 1/2, 1, 2, 4, 6, 8}
'sigma_multipliers': [0.25, 0.5, 1.0, 2.0, 4.0],
# Output configuration
'output_dir': PROJECT_DIR / 'output' / 'quick_train',
'save_model': True,
}
# =============================================================================
# Utility Functions
# =============================================================================
def print_section(title):
"""Print section title"""
print("\n" + "="*60)
print(f" {title}")
print("="*60)
def get_region_label(mask: np.ndarray, region_mask: np.ndarray) -> int:
"""Get region label (based on majority vote)"""
if mask is None:
return 0
region_pixels = mask[region_mask]
if len(region_pixels) == 0:
return 0
shadow_ratio = np.mean(region_pixels > 0)
return 1 if shadow_ratio > 0.5 else 0
def compute_pixel_metrics_from_regions(
region_preds: np.ndarray,
region_pixel_stats: np.ndarray
) -> Dict[str, float]:
"""
Compute pixel-level metrics from region predictions.
For each region, all pixels are assigned the same predicted label and then
compared against ground-truth pixel statistics from that region.
"""
region_preds = np.asarray(region_preds, dtype=np.int32).ravel()
region_pixel_stats = np.asarray(region_pixel_stats, dtype=np.int64)
if region_pixel_stats.ndim != 2 or region_pixel_stats.shape[1] != 2:
raise ValueError(
"region_pixel_stats must have shape (n_regions, 2) "
"with columns [n_pixels, n_shadow_pixels]."
)
if len(region_preds) != len(region_pixel_stats):
raise ValueError(
"region_preds and region_pixel_stats must have the same length."
)
n_pixels = region_pixel_stats[:, 0].astype(np.float64)
n_shadow_pixels = region_pixel_stats[:, 1].astype(np.float64)
n_non_shadow_pixels = n_pixels - n_shadow_pixels
pred_shadow = (region_preds == 1)
tp = np.sum(n_shadow_pixels[pred_shadow])
fp = np.sum(n_non_shadow_pixels[pred_shadow])
fn = np.sum(n_shadow_pixels[~pred_shadow])
tn = np.sum(n_non_shadow_pixels[~pred_shadow])
total = tp + fp + fn + tn
accuracy = (tp + tn) / total if total > 0 else 0.0
fpr_den = fp + tn
fnr_den = fn + tp
fpr = fp / fpr_den if fpr_den > 0 else 0.0
fnr = fn / fnr_den if fnr_den > 0 else 0.0
ber = 0.5 * (fpr + fnr)
return {
'ber': float(ber),
'accuracy': float(accuracy),
'fpr': float(fpr),
'fnr': float(fnr),
'tp': float(tp),
'fp': float(fp),
'tn': float(tn),
'fn': float(fn),
}
class Timer:
"""Timer class for benchmarking"""
def __init__(self):
self.start_time = None
self.records = {}
self.current_name = None
def start(self, name):
self.start_time = time.time()
self.current_name = name
print(f"\n[Timer] Starting: {name}")
def stop(self):
elapsed = time.time() - self.start_time
self.records[self.current_name] = elapsed
print(f"[Timer] {self.current_name}: {elapsed:.1f}s")
return elapsed
def summary(self):
print("\n[Timing Summary]")
total = sum(self.records.values())
for name, t in self.records.items():
print(f" {name}: {t:.1f}s ({t/total*100:.1f}%)")
print(f" Total: {total:.1f}s ({total/60:.1f}min)")
# =============================================================================
# Feature Extraction
# =============================================================================
def extract_image_features(
image: np.ndarray,
mask: np.ndarray,
slic: SuperpixelSegmenter,
region_gen: MeanShiftRegionGenerator,
feat_extractor: PaperCompliantFeatureExtractor
) -> Tuple[Dict[str, np.ndarray], List[int], List[List[int]]]:
"""
Extract all region features from a single image
Returns:
features_dict: {'L': (n, 21), 'a': (n, 21), 'b': (n, 21), 'texture': (n, n_textons)}
labels: Region label list
region_pixel_stats: List of [n_pixels, n_shadow_pixels] for valid regions
"""
# 1. Superpixel segmentation
superpixel_labels = slic.segment(image)
# 2. Region generation
region_labels = region_gen.generate_regions(image, superpixel_labels)
n_regions = int(region_labels.max()) + 1
# 3. Extract all region features
all_features = feat_extractor.extract_features_by_channel(image, region_labels, use_gpu=USE_GPU)
# 4. Filter small regions and get labels
valid_indices = []
labels = []
region_pixel_stats = []
for region_id in range(n_regions):
region_mask = (region_labels == region_id)
if np.sum(region_mask) < 10:
continue
valid_indices.append(region_id)
labels.append(get_region_label(mask, region_mask))
n_region_pixels = int(np.sum(region_mask))
n_shadow_pixels = int(np.sum(mask[region_mask] > 0)) if mask is not None else 0
region_pixel_stats.append([n_region_pixels, n_shadow_pixels])
# 5. Keep only features of valid regions
features_dict = {
'L': all_features['L'][valid_indices],
'a': all_features['a'][valid_indices],
'b': all_features['b'][valid_indices],
'texture': all_features['t'][valid_indices]
}
return features_dict, labels, region_pixel_stats
def extract_dataset_features(
dataset,
indices: List[int],
slic: SuperpixelSegmenter,
region_gen: MeanShiftRegionGenerator,
feat_extractor: PaperCompliantFeatureExtractor,
desc: str = "Extracting"
) -> Tuple[Dict[str, np.ndarray], np.ndarray, np.ndarray]:
"""Extract all features of the dataset"""
all_features = {'L': [], 'a': [], 'b': [], 'texture': []}
all_labels = []
all_region_pixel_stats = []
n_images = len(indices)
print(f"\n[{desc}] Processing {n_images} images...")
for i, idx in enumerate(indices):
if (i + 1) % 10 == 0 or i == 0:
print(f" Image {i+1}/{n_images}...")
image, mask = dataset[idx]
try:
features, labels, region_pixel_stats = extract_image_features(
image, mask, slic, region_gen, feat_extractor
)
for key in all_features:
all_features[key].append(features[key])
all_labels.extend(labels)
all_region_pixel_stats.extend(region_pixel_stats)
except Exception as e:
print(f" [Warning] Image {idx} failed: {e}")
continue
# Concatenate all features
for key in all_features:
all_features[key] = np.vstack(all_features[key]) if all_features[key] else np.array([])
all_labels = np.array(all_labels)
all_region_pixel_stats = np.array(all_region_pixel_stats, dtype=np.int64)
print(f" Total regions: {len(all_labels)}")
if len(all_labels) > 0:
print(f" Shadow: {np.sum(all_labels == 1)} ({np.mean(all_labels)*100:.1f}%)")
print(f" Non-shadow: {np.sum(all_labels == 0)} ({np.mean(all_labels == 0)*100:.1f}%)")
return all_features, all_labels, all_region_pixel_stats
# =============================================================================
# Classifier
# =============================================================================
class MultiKernelLSSVM:
"""Multi-kernel LSSVM classifier (supports GPU acceleration)"""
def __init__(self, gamma: float = 1.0, sigma_multiplier: float = 1.0):
"""Initialization
Args:
gamma: LSSVM regularization parameter
sigma_multiplier: sigma multiplier relative to mean distance
sigma_l = multiplier * mean(D_l)
"""
self.gamma = gamma
self.sigma_multiplier = sigma_multiplier
self.lssvm = None
self.platt = None
self.train_features = None
self._train_K = None
self._channel_sigmas = {} # sigma for each channel
def _emd_1d_gpu(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:
"""GPU accelerated EMD distance calculation (1D histogram)"""
# Cumulative Distribution Function
cdf1 = torch.cumsum(X1, dim=1)
cdf2 = torch.cumsum(X2, dim=1)
# EMD = sum of absolute differences of CDFs
# Broadcast calculation of distances between all pairs
D = torch.sum(torch.abs(cdf1.unsqueeze(1) - cdf2.unsqueeze(0)), dim=2)
return D
def _chi_square_gpu(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:
"""GPU accelerated Chi-square distance calculation"""
# chi2(x,y) = sum((x-y)^2 / (x+y+eps))
X1_exp = X1.unsqueeze(1) # (n1, 1, d)
X2_exp = X2.unsqueeze(0) # (1, n2, d)
diff = X1_exp - X2_exp
sum_val = X1_exp + X2_exp + 1e-10
D = torch.sum(diff ** 2 / sum_val, dim=2)
return D
def _compute_kernel(self, X1: Dict[str, np.ndarray], X2: Dict[str, np.ndarray] = None,
compute_sigmas: bool = False):
"""Compute combined kernel matrix (GPU acceleration)
Args:
X1: First set of features
X2: Second set of features (defaults to X1 if None)
compute_sigmas: Whether to compute and store per-channel sigma (True during training only)
"""
if X2 is None:
X2 = X1
is_training = True # Considered training if X1 == X2
else:
is_training = False
n1 = X1['L'].shape[0]
n2 = X2['L'].shape[0]
weights = {'L': 0.25, 'a': 0.25, 'b': 0.25, 'texture': 0.25}
if USE_GPU:
K = torch.zeros((n1, n2), device=DEVICE)
for channel, weight in weights.items():
feat1 = torch.tensor(X1[channel], dtype=torch.float32, device=DEVICE)
feat2 = torch.tensor(X2[channel], dtype=torch.float32, device=DEVICE)
# Compute distance matrix
if channel in ['L', 'a', 'b']:
D = self._emd_1d_gpu(feat1, feat2)
else:
D = self._chi_square_gpu(feat1, feat2)
# Compute or use stored sigma
if compute_sigmas or channel not in self._channel_sigmas:
# Compute sigma based on mean distance (excluding diagonal)
D_np = D.cpu().numpy()
if is_training:
mask = ~np.eye(n1, dtype=bool)
mean_dist = np.mean(D_np[mask]) if n1 > 1 else np.mean(D_np)
else:
mean_dist = np.mean(D_np)
sigma = max(mean_dist * self.sigma_multiplier, 1e-6)
if compute_sigmas:
self._channel_sigmas[channel] = sigma
else:
sigma = self._channel_sigmas[channel]
# Extended Gaussian Kernel
K += weight * torch.exp(-D / (sigma + 1e-8))
return K.cpu().numpy()
else:
# CPU fallback
K = np.zeros((n1, n2))
for channel, weight in weights.items():
feat1 = X1[channel]
feat2 = X2[channel]
if channel in ['L', 'a', 'b']:
D = emd_1d_matrix(feat1, feat2)
else:
D = chi_square_distance_matrix(feat1, feat2)
# Compute or use stored sigma
if compute_sigmas or channel not in self._channel_sigmas:
if is_training:
mask = ~np.eye(n1, dtype=bool)
mean_dist = np.mean(D[mask]) if n1 > 1 else np.mean(D)
else:
mean_dist = np.mean(D)
sigma = max(mean_dist * self.sigma_multiplier, 1e-6)
if compute_sigmas:
self._channel_sigmas[channel] = sigma
else:
sigma = self._channel_sigmas[channel]
K += weight * np.exp(-D / (sigma + 1e-8))
return K
def fit(self, features: Dict[str, np.ndarray], y: np.ndarray):
"""Train classifier"""
print("\n[Training LSSVM]")
n_samples = features['L'].shape[0]
# Compute kernel matrix (GPU accelerated), also computing per-channel sigma
print(f" Computing kernel matrix ({n_samples}x{n_samples})...")
if USE_GPU:
print(f" [GPU] Memory before: {torch.cuda.memory_allocated()/1e6:.1f} MB")
self._train_K = self._compute_kernel(features, compute_sigmas=True)
# Print per-channel sigma
print(f" Channel sigmas (multiplier={self.sigma_multiplier}):")
for ch, sigma in self._channel_sigmas.items():
print(f" {ch}: {sigma:.4f}")
if USE_GPU:
print(f" [GPU] Memory after kernel: {torch.cuda.memory_allocated()/1e6:.1f} MB")
torch.cuda.empty_cache()
# Train LSSVM (using precomputed kernel)
print(" Fitting LSSVM...")
dummy_X = np.zeros((n_samples, 1))
self.lssvm = LSSVM(gamma=self.gamma, use_gpu=USE_GPU)
self.lssvm.fit(dummy_X, y, K=self._train_K)
# Platt scaling
print(" Fitting Platt scaling...")
decision_values = self.lssvm.decision_function(dummy_X, K=self._train_K)
self.platt = PlattScaler()
self.platt.fit(decision_values, y)
# Save training features
self.train_features = features
self._n_train = n_samples
# Compute training BER (using decision value sign)
preds = (decision_values > 0).astype(int)
ber = balanced_error_rate(y, preds)
print(f" Training BER: {ber*100:.2f}%")
return ber
def predict_proba(self, features: Dict[str, np.ndarray]) -> np.ndarray:
"""Predict probability (GPU acceleration)"""
# Compute test kernel matrix K(train, test)
K_test = self._compute_kernel(self.train_features, features)
n_test = features['L'].shape[0]
dummy_X = np.zeros((n_test, 1))
decision_values = self.lssvm.decision_function(dummy_X, K=K_test)
if USE_GPU:
torch.cuda.empty_cache()
return self.platt.predict_proba(decision_values)
def predict(self, features: Dict[str, np.ndarray]) -> np.ndarray:
"""Predict labels (using decision value sign, not dependent on Platt scaling)"""
# Compute test kernel matrix K(train, test)
K_test = self._compute_kernel(self.train_features, features)
n_test = features['L'].shape[0]
dummy_X = np.zeros((n_test, 1))
decision_values = self.lssvm.decision_function(dummy_X, K=K_test)
if USE_GPU:
torch.cuda.empty_cache()
# Directly use decision value sign: >0 for shadow(1), <0 for non-shadow(0)
return (decision_values > 0).astype(int)
def evaluate(
classifier,
features: Dict[str, np.ndarray],
labels: np.ndarray,
region_pixel_stats: np.ndarray,
name: str
):
"""Evaluate classifier"""
print(f"\n[{name}]")
preds = classifier.predict(features)
probs = classifier.predict_proba(features)
# Keep this for compatibility/debugging, but report pixel-level metrics below.
_ = balanced_error_rate(labels, preds)
_ = probs
pixel_metrics = compute_pixel_metrics_from_regions(preds, region_pixel_stats)
print(" Pixel-level metrics:")
print(f" BER: {pixel_metrics['ber']*100:.2f}%")
print(f" Accuracy: {pixel_metrics['accuracy']*100:.2f}%")
print(f" FPR: {pixel_metrics['fpr']*100:.2f}%")
print(f" FNR: {pixel_metrics['fnr']*100:.2f}%")
return pixel_metrics
# =============================================================================
# Main Function
# =============================================================================
def main():
"""Main training function"""
print_section("Shadow Detection Quick Training")
print(f"Start: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
timer = Timer()
config = QUICK_CONFIG
# Create output directory
output_dir = Path(config['output_dir'])
output_dir.mkdir(parents=True, exist_ok=True)
# =========================================================================
# 1. Data loading
# =========================================================================
print_section("1. Loading Dataset")
timer.start("Data Loading")
dataset = get_dataset('sbu', split='train')
n_total = len(dataset)
np.random.seed(config['random_seed'])
indices = np.random.permutation(n_total)
n_train = config['n_train_images']
n_val = config['n_val_images']
train_indices = indices[:n_train].tolist()
val_indices = indices[n_train:n_train + n_val].tolist()
print(f"Total: {n_total}, Train: {n_train}, Val: {n_val}")
timer.stop()
# =========================================================================
# 2. Texton dictionary training
# =========================================================================
print_section("2. Building Texton Dictionary")
timer.start("Texton Dictionary")
n_texton_train = min(config['texton_train_images'], n_train)
print(f"Using {n_texton_train} images for texton dictionary...")
texton_images = []
for i, idx in enumerate(train_indices[:n_texton_train]):
if (i + 1) % 10 == 0:
print(f" Loading image {i+1}/{n_texton_train}...")
img, _ = dataset[idx]
texton_images.append(img)
print("Building texton dictionary...")
texton_extractor = TextonFeatureExtractor(n_textons=config['n_textons'])
texton_extractor.build_dictionary(texton_images, verbose=True)
# Save texton dictionary
texton_path = output_dir / 'texton_dict.pkl'
texton_extractor.save_dictionary(str(texton_path))
print(f"Saved to: {texton_path}")
timer.stop()
# =========================================================================
# 3. Feature extraction
# =========================================================================
print_section("3. Extracting Features")
timer.start("Feature Extraction")
# Create components
slic = SuperpixelSegmenter(
n_segments=config['n_segments'],
compactness=config['compactness']
)
region_gen = MeanShiftRegionGenerator(bandwidth=config['region_bandwidth'])
feat_extractor = PaperCompliantFeatureExtractor(texton_extractor=texton_extractor)
# Extract training set features
train_features, train_labels, train_region_stats = extract_dataset_features(
dataset, train_indices, slic, region_gen, feat_extractor, "Training"
)
# Extract validation set features
val_features, val_labels, val_region_stats = extract_dataset_features(
dataset, val_indices, slic, region_gen, feat_extractor, "Validation"
)
timer.stop()
if len(train_labels) == 0:
print("[ERROR] No training features!")
return None
# =========================================================================
# 4. Train classifier
# =========================================================================
print_section("4. Training Classifier")
timer.start("Training")
best_ber = float('inf')
best_model = None
best_multiplier = None
print("\nSearching for best sigma_multiplier...")
for multiplier in config['sigma_multipliers']:
print(f"\n--- sigma_multiplier={multiplier} ---")
classifier = MultiKernelLSSVM(gamma=config['gamma'], sigma_multiplier=multiplier)
classifier.fit(train_features, train_labels)
if len(val_labels) > 0:
val_result = evaluate(
classifier,
val_features,
val_labels,
val_region_stats,
f"Val (mult={multiplier})"
)
if val_result['ber'] < best_ber:
best_ber = val_result['ber']
best_model = classifier
best_multiplier = multiplier
print(f" [NEW BEST]")
print(f"\nBest: sigma_multiplier={best_multiplier}, BER={best_ber*100:.2f}%")
timer.stop()
# =========================================================================
# 5. Final evaluation
# =========================================================================
print_section("5. Final Evaluation")
timer.start("Evaluation")
train_result = evaluate(
best_model,
train_features,
train_labels,
train_region_stats,
"Final Train"
)
val_result = evaluate(
best_model,
val_features,
val_labels,
val_region_stats,
"Final Val"
) if len(val_labels) > 0 else None
timer.stop()
# =========================================================================
# 6. Save results
# =========================================================================
print_section("6. Saving Results")
if config['save_model']:
model_path = output_dir / 'model.pkl'
with open(model_path, 'wb') as f:
pickle.dump({
'classifier': best_model,
'texton_extractor': texton_extractor,
'config': config,
'best_sigma_multiplier': best_multiplier,
'channel_sigmas': best_model._channel_sigmas if best_model else None,
'train_result': train_result,
'val_result': val_result
}, f)
print(f"Model saved: {model_path}")
# Save results summary
results_path = output_dir / 'results.txt'
with open(results_path, 'w', encoding='utf-8') as f:
f.write("="*50 + "\n")
f.write("Shadow Detection Quick Training Results\n")
f.write(f"Time: {datetime.now()}\n")
f.write("="*50 + "\n\n")
f.write(f"Train images: {n_train}\n")
f.write(f"Val images: {n_val}\n")
f.write(f"Train regions: {len(train_labels)}\n")
f.write(f"Val regions: {len(val_labels)}\n")
f.write(f"Best sigma_multiplier: {best_multiplier}\n")
if best_model and best_model._channel_sigmas:
f.write(f"Channel sigmas: {best_model._channel_sigmas}\n\n")
f.write(f"Train BER: {train_result['ber']*100:.2f}%\n")
f.write(f"Train Acc: {train_result['accuracy']*100:.2f}%\n")
if val_result:
f.write(f"Val BER: {val_result['ber']*100:.2f}%\n")
f.write(f"Val Acc: {val_result['accuracy']*100:.2f}%\n")
print(f"Results saved: {results_path}")
# =========================================================================
# Summary
# =========================================================================
print_section("Training Complete!")
print(f"\nTrain BER: {train_result['ber']*100:.2f}%")
if val_result:
print(f"Val BER: {val_result['ber']*100:.2f}%")
timer.summary()
print(f"\nEnd: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
return {'train': train_result, 'val': val_result, 'best_sigma_multiplier': best_multiplier}
if __name__ == "__main__":
main()