Spaces:
Running on Zero
Running on Zero
File size: 7,921 Bytes
904d76c | 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 | #!/usr/bin/env python3
"""Execute and profile PRISM Hybrid (B2) pipeline on a verified Bayer RAW input."""
from __future__ import annotations
import argparse, hashlib, json, platform, sys, time
from pathlib import Path
import numpy as np
import torch
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'src'))
from prism.isp.raw import load_sidd_cc_mat
from prism.models.b1 import B1PackedUNet
from prism.hybrid.pipeline_b2 import run_b2
from prism.audit.sensor_audit import TileState
def save_heatmap(path: Path, state_map: np.ndarray, score_map: np.ndarray):
"""Save a colored PNG visualizing tile audit states and priorities."""
path.parent.mkdir(parents=True, exist_ok=True)
rows, cols = state_map.shape
# Palette: green=low relative residual, red=elevated residual candidate,
# yellow=uncertain. These colors do not imply accuracy or repair truth.
img = np.zeros((rows, cols, 3), dtype=np.uint8)
for r in range(rows):
for c in range(cols):
st = state_map[r, c]
if st == TileState.LOW_RELATIVE_RESIDUAL.value:
img[r, c] = [50, 200, 50] # Green
elif st == TileState.HIGH_RELATIVE_RESIDUAL_CANDIDATE.value:
intensity = min(255, int(150 + score_map[r, c] * 50))
img[r, c] = [intensity, 40, 40] # Red
else:
img[r, c] = [220, 200, 50] # Yellow (UNCERTAIN)
# Scale up for visibility
scale = 16
scaled = np.repeat(np.repeat(img, scale, axis=0), scale, axis=1)
Image.fromarray(scaled).save(path)
def main():
parser = argparse.ArgumentParser(description='Execute PRISM Hybrid (B2) pipeline')
parser.add_argument('--raw', type=Path, default=None,
help='Path to input RAW .mat file')
parser.add_argument('--metadata', type=Path, default=None,
help='Path to paired metadata .mat file')
parser.add_argument('--checkpoint', type=Path, required=True,
help='Explicit B1 refiner checkpoint; no implicit/random fallback')
parser.add_argument('--budget', type=float, default=0.5,
help='Maximum fraction [0,1] of all tiles available for refinement; selection is capped by eligible tiles')
parser.add_argument('--allow-uncalibrated-selection', action='store_true',
help='Experimental residual ranking on non-extreme uncertain tiles; not sensor-calibrated')
parser.add_argument('--device', default='auto', choices=('auto', 'cpu', 'cuda', 'mps'))
parser.add_argument('--tile-size', type=int, default=256)
parser.add_argument('--overlap', type=int, default=32)
parser.add_argument('--output-dir', type=Path, default=ROOT / 'experiments/P3/B2_outputs')
parser.add_argument('--save-images', action='store_true', help='Save output PNG images')
args = parser.parse_args()
# Default RAW if not specified
if args.raw is None:
candidates = list((ROOT / 'data/external/sidd_cc_expanded/Raw/train/Data').glob('*/*_NOISY_RAW_*.MAT'))
if not candidates:
candidates = list((ROOT / 'data/external/sidd_subset/Raw/train/Data').glob('*/*_NOISY_RAW_*.MAT'))
if not candidates:
parser.error('No RAW file specified and none found in data directories')
args.raw = candidates[0]
# Select device
if args.device == 'auto':
device = torch.device('cuda' if torch.cuda.is_available() else ('mps' if torch.backends.mps.is_available() else 'cpu'))
else:
device = torch.device(args.device)
if device.type == 'mps' and not torch.backends.mps.is_available():
parser.error('MPS requested but unavailable in this Python runtime')
if device.type == 'cuda' and not torch.cuda.is_available():
parser.error('CUDA requested but unavailable')
if not 0.0 <= args.budget <= 1.0:
parser.error('--budget must be a fraction in [0,1] of total sensor tiles')
print('Loading RAW frame from: {}'.format(args.raw), flush=True)
frame = load_sidd_cc_mat(args.raw, args.metadata)
print('RAW shape: {}, CFA: {}, Neutral: {}'.format(
frame.mosaic.shape, frame.cfa_pattern, frame.as_shot_neutral), flush=True)
# A B2 inference result must identify a real trained checkpoint. Never
# silently run with random initialization or an implicit fallback.
model = B1PackedUNet()
checkpoint_path = args.checkpoint
if checkpoint_path is None or not checkpoint_path.is_file():
parser.error('A valid trained B1 checkpoint is required; pass --checkpoint explicitly')
print('Loading B1 checkpoint: {}'.format(checkpoint_path), flush=True)
ckpt = torch.load(checkpoint_path, map_location='cpu', weights_only=False)
if 'model_state_dict' not in ckpt:
parser.error('Checkpoint lacks model_state_dict: {}'.format(checkpoint_path))
model.load_state_dict(ckpt['model_state_dict'], strict=True)
model.to(device).eval()
print('Executing B2 hybrid pipeline on device: {} with budget: {:.0%}...'.format(
device, args.budget), flush=True)
result = run_b2(
frame=frame,
model=model,
device=device,
budget_fraction=args.budget,
tile_sensor_size=args.tile_size,
overlap_sensor=args.overlap,
allow_uncalibrated_selection=args.allow_uncalibrated_selection
)
args.output_dir.mkdir(parents=True, exist_ok=True)
# Save images if requested
if args.save_images:
b0_u8 = np.rint(np.clip(result.b0_result.srgb_encoded, 0, 1) * 255).astype(np.uint8)
b2_u8 = np.rint(np.clip(result.final_srgb, 0, 1) * 255).astype(np.uint8)
Image.fromarray(b0_u8).save(args.output_dir / 'b0_classical.png')
Image.fromarray(b2_u8).save(args.output_dir / 'b2_hybrid.png')
save_heatmap(args.output_dir / 'audit_heatmap.png',
result.initial_audit.state_map,
result.initial_audit.score_map)
print('Saved outputs to: {}'.format(args.output_dir), flush=True)
report = {
"pipeline": "B2_PRISM_Hybrid",
"raw_file": str(args.raw),
"cfa_pattern": frame.cfa_pattern,
"device": str(device),
"checkpoint": str(checkpoint_path),
"checkpoint_sha256": hashlib.sha256(checkpoint_path.read_bytes()).hexdigest(),
"checkpoint_epoch": ckpt.get('epoch'),
"checkpoint_config": ckpt.get('config'),
"platform": platform.platform(),
"budget_fraction": args.budget,
"timings": result.stage_timings,
"stats": result.stats,
"audit_summary": result.initial_audit.summary,
"noise_calibrated": result.initial_audit.noise_model.calibrated,
"selection_mode": result.stats['selection_mode'],
"post_audit_status": result.stats['post_audit_status'],
}
summary_path = args.output_dir / 'b2_run_summary.json'
summary_path.write_text(json.dumps(report, indent=2) + '\n')
print('\n=== B2 Hybrid Execution Report ===')
print('Total tiles: {}'.format(result.stats['total_tiles']))
print('Eligible (refinement): {}'.format(result.stats['eligible_tiles']))
print('Tiles refined: {} ({:.1f}%)'.format(
result.stats['tiles_refined'], result.stats['refinement_percentage']))
print('B0 fast-path time: {:.3f} s'.format(result.stage_timings['b0_seconds']))
print('Audit overhead: {:.3f} s'.format(result.stage_timings['audit_seconds']))
print('Scheduler time: {:.4f} s'.format(result.stage_timings['scheduler_seconds']))
print('Refinement time: {:.3f} s'.format(result.stage_timings['refinement_seconds']))
print('Total pipeline time: {:.3f} s'.format(result.stage_timings['total_seconds']))
print('Summary written to: {}'.format(summary_path))
if __name__ == '__main__':
main()
|