File size: 1,434 Bytes
f877698 2ad60ce f877698 2ad60ce f877698 2ad60ce f877698 2ad60ce f877698 | 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 | from __future__ import annotations
import json
import math
import os
from pathlib import Path
import numpy as np
from .rgb import mean, number, scalar
METRICS = ('full_psnr', 'full_ssim', 'full_lpips', 'observed_psnr', 'observed_ssim', 'observed_lpips',
'unobserved_psnr', 'unobserved_lpips', 'unobserved_dists', 'depth_absrel', 'depth_rmse')
def write_json(path, value):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_name('.' + path.name + f'.{os.getpid()}.tmp')
temporary.write_text(json.dumps(value, ensure_ascii=False, indent=2, allow_nan=False) + '\n')
temporary.replace(path)
def valid_value(value):
if value is None:
return False
value = number(value)
return isinstance(value, (int, float, np.floating)) and not math.isnan(value) and value != -float('inf')
def summary(per_clip):
out, counts = {}, {}
for key in METRICS:
values = [row['values'][key] for row in per_clip if valid_value(row['values'][key])]
out[key], counts[key] = (scalar(mean(values)) if values else None), len(values)
out['n'] = len(per_clip)
return dict(metrics=out, valid_clip_counts=counts)
def export(per_clip, destination):
destination = Path(destination)
write_json(destination / 'per_clip.json', per_clip)
result = summary(per_clip)
write_json(destination / 'summary.json', result)
return result
|