-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplot_rdcurve.py
140 lines (121 loc) · 3.49 KB
/
plot_rdcurve.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from collections import defaultdict
from pathlib import Path
from matplotlib import pyplot as plt
from matplotlib.colors import to_hex
import pandas as pd
from aof.utils.metrics import BD_RATE
colors = [
(255, 75, 0),
(3, 175, 122),
# (0, 90, 255),
(77, 96, 255),
(255, 128, 130),
(246, 170, 0),
(153, 0, 153),
(128, 64, 0),
(255, 241, 0),
(255, 0, 0),
(0, 0, 255),
(0, 255, 0),
]
# list of str
color_codes = [to_hex((c[0] / 255, c[1] / 255, c[2] / 255)) for c in colors]
markers = [
"s",
"x",
"o",
"^",
"v",
"|",
"h",
"D",
"d",
"p",
"P",
"X",
"H",
"*",
]
plt.rcParams["xtick.major.width"] = 2.0
plt.rcParams["ytick.major.width"] = 2.0
plt.rcParams["font.size"] = 14
plt.rcParams["axes.linewidth"] = 2.0
plt.rcParams["lines.linewidth"] = 2.0
plt.rcParams["lines.markersize"] = 8.0
def get_rdcurve(
csv_paths: list,
index=None,
) -> dict:
rdcurve = dict(bpp_m=list(), bpp=list(), psnr=list(), loss=list())
for i, csv_path in enumerate(csv_paths):
df = pd.read_csv(csv_path, index_col=0)
df["mse"] = 10 ** (-df["psnr"] / 10)
if index is None:
score = df.mean()
else:
score = df.iloc[index]
if "bpp_m" in score:
rdcurve["bpp_m"].append(score["bpp_m"])
rdcurve["bpp"].append(score["bpp"])
rdcurve["psnr"].append(score["psnr"])
return rdcurve
def plot_rdcurve(rdcurve_dict, save_path):
for i, (name, rdcurve) in enumerate(rdcurve_dict.items()):
plt.plot(
rdcurve["bpp"],
rdcurve["psnr"],
label=name,
marker=markers[i],
color=color_codes[i],
)
plt.legend()
plt.grid()
plt.xlabel("Rate (BPP)")
plt.ylabel("PSNR (dB)")
plt.savefig(save_path, bbox_inches="tight")
plt.clf()
def main():
outdir = Path("results/figures/")
outdir.mkdir(parents=True, exist_ok=True)
domains = (
"vector",
"comic",
"line",
"natural",
)
# 1. Rate-distortion curve
table_dict = defaultdict(list)
for domain in domains:
tpl_dict = {
"Ours": "results/ours/wacnn/q{}/{}_2nd.csv",
"Zou et al., ISM 21": "results/zou-ism21/wacnn/q{}/{}_2nd.csv",
"Yang et al., NeurIPS 20": "results/ours/wacnn/q{}/{}_1st.csv",
"Baseline": "results/ours/wacnn/q{}/{}_0th.csv",
"VVC": "results/VVC/q{}/{}.csv",
"Lam et al., MM 20": "results/lam-mm20/wacnn/q{}/{}_2nd.csv",
"Rozendaal et al., ICLR 21": "results/rozendaal-iclr21/wacnn/q{}/{}_1.csv",
}
rdcurve_dict = dict()
for name, tpl in tpl_dict.items():
if name == "VVC":
qualities = list(range(24, 45, 3))
else:
qualities = list(range(1, 7))
paths = [tpl.format(q, domain) for q in qualities]
rdcurve_dict[name] = get_rdcurve(paths)
plot_rdcurve(rdcurve_dict, outdir / f"{domain}.pdf")
for name, rdcurve in rdcurve_dict.items():
bdrate = BD_RATE(
rdcurve_dict["VVC"]["bpp"],
rdcurve_dict["VVC"]["psnr"],
rdcurve["bpp"],
rdcurve["psnr"],
)
table_dict[name].append(bdrate)
df = pd.DataFrame.from_dict(table_dict).T
df.columns = domains
df["Average"] = df.mean(axis=1)
print(df)
print(df.round(2).to_latex())
if __name__ == "__main__":
main()