File size: 5,870 Bytes
32da3e8 | 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 | """Distributed reconstruction evaluation — PSNR, SSIM, rFID."""
import os
import sys
from typing import Dict, Optional
import numpy as np
import torch
import torch.distributed as dist
from torch.cuda.amp import autocast
from tqdm import tqdm
from .ref_iqa import calculate_psnr, calculate_ssim, calculate_lpips
from .fid import calculate_rfid
from .distributed import setup_eval_tmpdir, create_eval_dataloader, gather_and_cleanup_shards
def compute_reconstruction_metrics(
ref_arr: np.ndarray,
rec_arr: np.ndarray,
device: torch.device,
batch_size: int = 128,
metrics_to_compute=("psnr", "ssim", "rfid"),
disable_bar: bool = True,
) -> Dict[str, float]:
"""
Compute reconstruction metrics between reference and reconstructed images.
Args:
ref_arr: Reference images [N, H, W, C] uint8
rec_arr: Reconstructed images [N, H, W, C] uint8
device: Device for computation
batch_size: Batch size for metric computation
metrics_to_compute: Which metrics to compute
disable_bar: Whether to disable progress bars
Returns:
Dictionary with metrics: psnr, ssim, rfid
"""
device_str = "cuda" if device.type == "cuda" else "cpu"
results_dict = {}
if 'psnr' in metrics_to_compute:
psnr = calculate_psnr(ref_arr, rec_arr, batch_size, device_str, disable_bar=disable_bar)
results_dict["psnr"] = psnr
if 'ssim' in metrics_to_compute:
ssim = calculate_ssim(ref_arr, rec_arr, batch_size, device_str, disable_bar=disable_bar)
results_dict["ssim"] = ssim
if 'lpips' in metrics_to_compute:
lpips = calculate_lpips(ref_arr, rec_arr, batch_size, device_str, disable_bar=disable_bar)
results_dict["lpips"] = lpips
if 'rfid' in metrics_to_compute:
rfid = calculate_rfid(ref_arr, rec_arr, batch_size, device_str)
results_dict["rfid"] = rfid
assert len(results_dict) > 0, "No metrics were computed."
return results_dict
@torch.no_grad()
def evaluate_reconstruction_distributed(
model,
val_dataset,
num_samples: int,
batch_size: int,
rank: int,
world_size: int,
device: torch.device,
experiment_dir: str,
global_step: int,
autocast_kwargs: dict,
metric_batch_size: int = 128,
reference_npz_path: Optional[str] = None,
metrics_to_compute: Optional[list] = ("psnr", "ssim", "rfid"),
shared_tmpdir: Optional[str] = None,
) -> Optional[Dict[str, float]]:
"""
Evaluate reconstruction metrics using all GPUs in a distributed manner.
Args:
model: Model to evaluate (should be in eval mode)
val_dataset: Validation dataset
num_samples: Number of samples to reconstruct
batch_size: Batch size per GPU for reconstruction
rank: Current GPU rank
world_size: Total number of GPUs
device: Device to use
experiment_dir: Experiment directory
global_step: Current training step
autocast_kwargs: Autocast configuration
metric_batch_size: Batch size for metric computation (on rank 0)
reference_npz_path: Optional path to existing reference NPZ file
metrics_to_compute: Which metrics to compute
shared_tmpdir: Optional shared directory for multi-node eval
Returns:
Dictionary of metrics (only on rank 0, None on other ranks)
"""
temp_dir = setup_eval_tmpdir(experiment_dir, global_step, rank,
shared_tmpdir=shared_tmpdir, eval_type="reconstruction")
loader = create_eval_dataloader(val_dataset, rank, world_size, num_samples, batch_size)
# Reconstruct images on this rank
reconstructions = []
iterator = tqdm(loader, desc=f"[Rank {rank}] Reconstructing", file=sys.stdout) if rank == 0 else loader
with torch.inference_mode():
for images, _ in iterator:
images = images.to(device, non_blocking=True)
with autocast(**autocast_kwargs):
recon = model(images)
recon = recon.clamp(0, 1)
recon_np = recon.mul(255).permute(0, 2, 3, 1).to("cpu", dtype=torch.uint8).numpy()
for img in recon_np:
reconstructions.append(img)
reconstructions = np.stack(reconstructions)
shard_path = os.path.join(temp_dir, f"recon_{global_step:07d}_{rank:02d}.npz")
np.savez(shard_path, arr_0=reconstructions)
if rank == 0:
print(f"[Rank {rank}] Saved {len(reconstructions)} reconstructions to {shard_path}")
# Wait for all ranks to finish reconstruction
dist.barrier()
# Rank 0 computes metrics
metrics = None
if rank == 0:
combined_recons = gather_and_cleanup_shards(temp_dir, "recon", global_step, world_size, num_samples)
print(f"[Eval] Combined reconstruction NPZ shape: {combined_recons.shape}")
ref_npz_path = reference_npz_path
if not os.path.exists(ref_npz_path):
raise FileNotFoundError(f"Reference NPZ not found at {ref_npz_path}")
ref_images = np.load(ref_npz_path)["arr_0"]
print(f"[Eval] Loaded reference NPZ from {ref_npz_path}, shape: {ref_images.shape}")
if ref_images.shape[0] != combined_recons.shape[0]:
print(f"[Eval] Aligning ref to recon size: {ref_images.shape[0]} -> {combined_recons.shape[0]}")
ref_images = ref_images[: combined_recons.shape[0]]
print("[Eval] Computing metrics...")
metrics = compute_reconstruction_metrics(
ref_images,
combined_recons,
device,
metric_batch_size,
metrics_to_compute=metrics_to_compute,
disable_bar=True,
)
print(f"[Eval] Step {global_step} Metrics:")
for key, value in metrics.items():
print(f" {key}: {value:.6f}")
dist.barrier()
return metrics
|