Download code/agt/eval/rgb.py from MRBSTUDIO/AGT: direct link, hf CLI and curl.
- Browser
- Download file 3.01 kB
-
https://huggingface.co/MRBSTUDIO/AGT/resolve/main/code/agt/eval/rgb.py
- Command line
-
hf download hf://MRBSTUDIO/AGT/code/agt/eval/rgb.py
-
curl -L -o rgb.py https://huggingface.co/MRBSTUDIO/AGT/resolve/main/code/agt/eval/rgb.py
3.01 kB
| import math | |
| import numpy as np | |
| METRICS = ('rgb_psnr', 'rgb_ssim', 'rgb_lpips') | |
| def scalar(value): | |
| return 'inf' if math.isinf(value) and value > 0 else float(value) | |
| def number(value): | |
| return float('inf') if value == 'inf' else value | |
| def mean(values): | |
| values = [number(v) for v in values] | |
| return sum(values) / len(values) | |
| def frame_metrics(img1, img2, loss_fn=None, metrics=METRICS): | |
| from skimage.metrics import peak_signal_noise_ratio, structural_similarity | |
| img1 = img1.clamp(0, 1) | |
| img2 = img2.clamp(0, 1) | |
| out = {m: [] for m in metrics} | |
| for i in range(img1.shape[0]): | |
| im1 = img1[i].cpu().permute(1, 2, 0).numpy() | |
| im2 = img2[i].cpu().permute(1, 2, 0).numpy() | |
| if 'rgb_psnr' in out: | |
| with np.errstate(divide='ignore'): | |
| out['rgb_psnr'].append(float(peak_signal_noise_ratio(im1, im2, data_range=1.0))) | |
| if 'rgb_ssim' in out: | |
| out['rgb_ssim'].append(float(structural_similarity(im1, im2, channel_axis=2, data_range=1.0))) | |
| if 'rgb_lpips' in out: | |
| out['rgb_lpips'].append(loss_fn(img1[i].unsqueeze(0) * 2 - 1, img2[i].unsqueeze(0) * 2 - 1).item()) | |
| return out | |
| def gt_float(gt_u8): | |
| gt_u8 = np.asarray(gt_u8) | |
| if gt_u8.dtype != np.uint8: | |
| raise ValueError('GT must be the decoded uint8 frames') | |
| return gt_u8.astype(np.float32) / np.float32(255) | |
| class RGBScorer: | |
| def __init__(self, device='cpu'): | |
| self.device, self.loss_fn = device, None | |
| def lpips(self): | |
| if self.loss_fn is None: | |
| import lpips | |
| self.loss_fn = lpips.LPIPS(net='alex', verbose=False).eval().to(self.device) | |
| for parameter in self.loss_fn.parameters(): | |
| parameter.requires_grad_(False) | |
| return self.loss_fn | |
| def frames(self, pred, gt_u8, metrics=METRICS): | |
| import torch | |
| pred = np.asarray(pred) | |
| if pred.dtype not in (np.float32, np.float64) or pred.ndim != 4 or pred.shape[-1] != 3: | |
| raise ValueError('Prediction must be float (N,H,W,3) on the scoring grid') | |
| if pred.shape != np.shape(gt_u8): | |
| raise ValueError(f'Prediction {pred.shape} does not match GT {np.shape(gt_u8)}') | |
| if not np.isfinite(pred).all(): | |
| raise ValueError('Prediction has non-finite values') | |
| unknown = set(metrics) - set(METRICS) | |
| if unknown: | |
| raise ValueError(f'Unsupported RGB metric: {sorted(unknown)}') | |
| img1 = torch.from_numpy(np.ascontiguousarray(pred, dtype=np.float32)).permute(0, 3, 1, 2).to(self.device) | |
| img2 = torch.from_numpy(np.ascontiguousarray(gt_float(gt_u8).transpose(0, 3, 1, 2))).to(self.device) | |
| with torch.inference_mode(): | |
| return frame_metrics(img1, img2, self.lpips() if 'rgb_lpips' in metrics else None, metrics) | |
| def score(self, pred, gt_u8, metrics=METRICS): | |
| per_frame = self.frames(pred, gt_u8, metrics) | |
| return {m: scalar(mean(values)) for m, values in per_frame.items()} | |