Download test_rbs_debug.py from zhurong2333/CaliBench: direct link, hf CLI and curl.
- Browser
- Download file 4.66 kB
-
https://huggingface.co/datasets/zhurong2333/CaliBench/resolve/main/test_rbs_debug.py
- Command line
-
hf download hf://datasets/zhurong2333/CaliBench/test_rbs_debug.py
-
curl -L -o test_rbs_debug.py https://huggingface.co/datasets/zhurong2333/CaliBench/resolve/main/test_rbs_debug.py
4.66 kB
| #!/usr/bin/env python3 | |
| """ | |
| Quick test script to verify RBS debug logging works correctly. | |
| This loads cached logits and runs a quick calibration test. | |
| """ | |
| import sys | |
| import os | |
| import logging | |
| import numpy as np | |
| import torch | |
| import torch.nn.functional as F | |
| # Add paths | |
| sys.path.append('/hdd/haolan') | |
| sys.path.append('/hdd/haolan/SMART') | |
| from calibrator.Component.utils.utils import get_all_metrics | |
| from calibrator.Component.model.temperature_scaling import TemperatureScalingCalibrator | |
| # Set up logging to see debug messages | |
| logging.basicConfig(level=logging.INFO, format='%(name)s - %(levelname)s - %(message)s') | |
| print("="*80) | |
| print("Testing RBS Debug Logging") | |
| print("="*80) | |
| # Load some cached logits for testing | |
| cache_dir = "/hdd/haolan/SMART/cache/cifar10_resnet50_seed1" | |
| test_logits = np.load(f"{cache_dir}/test_logits.npy") | |
| test_labels = np.load(f"{cache_dir}/test_labels.npy") | |
| val_logits = np.load(f"{cache_dir}/val_logits.npy") | |
| val_labels = np.load(f"{cache_dir}/val_labels.npy") | |
| # Convert to tensors | |
| test_logits_tensor = torch.tensor(test_logits, dtype=torch.float32) | |
| test_labels_tensor = torch.tensor(test_labels, dtype=torch.long) | |
| val_logits_tensor = torch.tensor(val_logits, dtype=torch.float32) | |
| val_labels_tensor = torch.tensor(val_labels, dtype=torch.long) | |
| print(f"\nLoaded data:") | |
| print(f" Test logits shape: {test_logits_tensor.shape}") | |
| print(f" Test labels shape: {test_labels_tensor.shape}") | |
| # Test 1: Uncalibrated | |
| print("\n" + "="*80) | |
| print("TEST 1: Uncalibrated Logits") | |
| print("="*80) | |
| uncal_metrics = get_all_metrics( | |
| labels=test_labels_tensor, | |
| logits=test_logits_tensor, | |
| probs=None, | |
| n_bins=15 | |
| ) | |
| print(f"Uncalibrated RBS: {uncal_metrics['rbs']:.6f}") | |
| print(f"Uncalibrated NLL: {uncal_metrics['nll']:.6f}") | |
| print(f"Uncalibrated ECE: {uncal_metrics['ece']:.6f}") | |
| # Test 2: Temperature Scaling | |
| print("\n" + "="*80) | |
| print("TEST 2: Temperature Scaling") | |
| print("="*80) | |
| # Train temperature scaling | |
| ts_calibrator = TemperatureScalingCalibrator(loss_type='CE') | |
| ts_calibrator.fit(val_logits_tensor, val_labels_tensor) | |
| optimal_temp = ts_calibrator.temperature.item() | |
| print(f"Optimal temperature: {optimal_temp:.6f}") | |
| # Get calibrated logits | |
| calibrated_logits = ts_calibrator.calibrate(test_logits_tensor, return_logits=True) | |
| print(f"Calibrated logits stats:") | |
| print(f" Mean: {calibrated_logits.mean().item():.6f}") | |
| print(f" Std: {calibrated_logits.std().item():.6f}") | |
| print(f" Sample [0, :5]: {calibrated_logits[0, :5].tolist()}") | |
| # Compute metrics with calibrated logits | |
| cal_metrics = get_all_metrics( | |
| labels=test_labels_tensor, | |
| logits=calibrated_logits, | |
| probs=None, | |
| n_bins=15 | |
| ) | |
| print(f"\nTemperature Scaling RBS: {cal_metrics['rbs']:.6f}") | |
| print(f"Temperature Scaling NLL: {cal_metrics['nll']:.6f}") | |
| print(f"Temperature Scaling ECE: {cal_metrics['ece']:.6f}") | |
| # Test 3: Using probabilities directly | |
| print("\n" + "="*80) | |
| print("TEST 3: Direct Probabilities (simulating SMART)") | |
| print("="*80) | |
| # Convert calibrated logits to probabilities | |
| ts_probs = F.softmax(calibrated_logits, dim=1) | |
| print(f"Probability stats:") | |
| print(f" Mean: {ts_probs.mean().item():.6f}") | |
| print(f" Std: {ts_probs.std().item():.6f}") | |
| print(f" Sample [0, :5]: {ts_probs[0, :5].tolist()}") | |
| print(f" Sum [0]: {ts_probs[0].sum().item():.6f}") | |
| # Compute metrics with probabilities | |
| probs_metrics = get_all_metrics( | |
| labels=test_labels_tensor, | |
| logits=None, | |
| probs=ts_probs, | |
| n_bins=15 | |
| ) | |
| print(f"\nDirect Probs RBS: {probs_metrics['rbs']:.6f}") | |
| print(f"Direct Probs NLL: {probs_metrics['nll']:.6f}") | |
| print(f"Direct Probs ECE: {probs_metrics['ece']:.6f}") | |
| # Compare results | |
| print("\n" + "="*80) | |
| print("COMPARISON") | |
| print("="*80) | |
| print(f"Method RBS NLL ECE") | |
| print(f"Uncalibrated: {uncal_metrics['rbs']:.6f} {uncal_metrics['nll']:.6f} {uncal_metrics['ece']:.6f}") | |
| print(f"TS (logits): {cal_metrics['rbs']:.6f} {cal_metrics['nll']:.6f} {cal_metrics['ece']:.6f}") | |
| print(f"TS (probs): {probs_metrics['rbs']:.6f} {probs_metrics['nll']:.6f} {probs_metrics['ece']:.6f}") | |
| print(f"\nRBS Change (uncal→TS): {(cal_metrics['rbs'] - uncal_metrics['rbs']):.6f} ({100*(cal_metrics['rbs'] - uncal_metrics['rbs'])/uncal_metrics['rbs']:.2f}%)") | |
| print(f"ECE Change (uncal→TS): {(cal_metrics['ece'] - uncal_metrics['ece']):.6f} ({100*(cal_metrics['ece'] - uncal_metrics['ece'])/uncal_metrics['ece']:.2f}%)") | |
| print(f"\nNote: TS (logits) and TS (probs) should give identical results!") | |
| print(f"Match: {abs(cal_metrics['rbs'] - probs_metrics['rbs']) < 1e-6}") | |
| print("\n" + "="*80) | |
| print("Test complete! Check the debug logs above.") | |
| print("="*80) | |