flowlensing / evals /eval_image_quality.py
hamees's picture
Add FlowLensing models, code and evals
813c1b9 verified
Raw History Blame Contribute Delete
1.9 kB
"""PSNR / SSIM between paired real and generated images (matched by filename).
NOTE: the original script that produced the PSNR and SSIM numbers in the paper
is not present in the recovered project. This is a standard reimplementation
using torchmetrics; treat its output as indicative, not as the paper's exact
procedure.
python eval_image_quality.py --real DIR --generated DIR
"""
import argparse, os, sys
import torch
from tqdm import tqdm
from torchmetrics.functional import peak_signal_noise_ratio as psnr
from torchmetrics.functional import structural_similarity_index_measure as ssim
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "code"))
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common import load_npy_image, pick_device
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--real", required=True)
ap.add_argument("--generated", required=True)
ap.add_argument("--device", default="auto")
args = ap.parse_args()
device = pick_device(args.device)
shared = sorted(set(f for f in os.listdir(args.real) if f.endswith(".npy")) &
set(f for f in os.listdir(args.generated) if f.endswith(".npy")))
if not shared:
raise SystemExit("no filenames common to both directories; "
"generated samples must be saved under the real filename")
print(f"device: {device} | {len(shared)} paired images")
ps, ss = [], []
for f in tqdm(shared):
r = load_npy_image(os.path.join(args.real, f)).unsqueeze(0).to(device)
g = load_npy_image(os.path.join(args.generated, f)).unsqueeze(0).to(device)
ps.append(psnr(g, r, data_range=1.0).item())
ss.append(ssim(g, r, data_range=1.0).item())
print(f"\nPSNR: {sum(ps)/len(ps):.4f}")
print(f"SSIM: {sum(ss)/len(ss):.6f}")
if __name__ == "__main__":
main()