File size: 1,900 Bytes
813c1b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()