-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculate_metrics.py
More file actions
59 lines (48 loc) · 1.79 KB
/
Copy pathcalculate_metrics.py
File metadata and controls
59 lines (48 loc) · 1.79 KB
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
import os
from skimage import io
from utils import metrics
def calculate_metrics():
"""
Calculates the evaluation metrics for the original and reconstructed images
:return: None
"""
folders = os.listdir("../results")
modes = [
"GAN",
"QSGD_1",
"QSGD_2",
"QSGD_4",
"QSGD_6",
"QSGD_8",
"JPEG_1",
"JPEG_5",
"JPEG_10",
"JPEG_20",
"JPEG_40",
]
SSIM = {mode: [] for mode in modes}
PSNR = {mode: [] for mode in modes}
# Read the first image and second image (as per the mode) and calculate
for folder in folders:
if folder.endswith("txt"):
continue
firstImage = io.imread(f"../results/{folder}/original_image.png")
for mode in modes:
if mode == "GAN":
# 2976 is the index of last saved image of GAN output
secondImage = io.imread(f"../results/{folder}/GAN/image_2976.png")
else:
method, ID = mode.split("_")
if method == "QSGD":
secondImage = io.imread(f"../results/{folder}/QSGD/image_{ID}bits.png")
elif method == "JPEG":
secondImage = io.imread(f"../results/{folder}/JPEG/image_{ID}.jpeg")
image_metrics = metrics(firstImage, secondImage)
SSIM[mode].append(image_metrics["SSIM"])
PSNR[mode].append(image_metrics["PSNR"])
with open("../results/avg_ssim.txt", "w") as file:
for mode in SSIM.keys():
file.write(f"SSIM for {mode} Mode : {sum(SSIM[mode]) / len(SSIM[mode])}\n")
with open("../results/avg_psnr.txt", "w") as file:
for mode in PSNR.keys():
file.write(f"PSNR for {mode} Mode : {sum(PSNR[mode]) / len(PSNR[mode])}\n")