-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvis_results.py
320 lines (259 loc) · 13.8 KB
/
vis_results.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import numpy as np
import matplotlib.pyplot as plt
import os
import imageio.v3 as imageio
from models.options import load_options
import json
from matplotlib.ticker import FuncFormatter
import matplotlib.pyplot as plt
project_folder_path = os.path.dirname(os.path.abspath(__file__))
output_folder = os.path.join(project_folder_path, "output")
save_folder = os.path.join(project_folder_path, "savedModels")
def millions(x, pos):
'The two args are the value and tick position'
return '%1.1fM' % (x * 1e-6)
def plot_chart(dataset_name, dataset_results):
num_metrics = 3
with plt.style.context("seaborn-paper"):
fig, axs = plt.subplots(1, num_metrics, figsize=(4*3, 4))
fig.suptitle(dataset_name)
num_options = len(dataset_results.keys())
formatter = FuncFormatter(millions)
colors = ['blue', 'green', 'red', 'purple', 'orange', 'brown', "pink"]
markers = ['o','v','x','s','^',"."]
if(num_options > min(len(colors), len(markers))):
print(f"Need more color/marker choices for the options you have!")
return
option_no = 0
for option in dataset_results.keys():
num_params = np.array(dataset_results[option]["num_params"])
sorted_order = np.argsort(num_params)
sorted_params = num_params[sorted_order]
metric_no = 0
for metric in dataset_results[option].keys():
if metric == "num_params":
continue
ax = axs[metric_no]
ax.xaxis.set_major_formatter(millions)
ax.set_title(metric)
ax.set_xlabel("Num params")
# Periodic primitives
color=colors[option_no]
marker=markers[option_no]
sorted_metric = np.array(dataset_results[option][metric])[sorted_order]
ax.plot(sorted_params, sorted_metric, color=color, marker=marker, label=option)
metric_no += 1
option_no += 1
axs[-1].legend()
fig.show()
plt.waitforbuttonpress()
def generate_charts():
# an object to hold all results, broken up by dataset type
results = {}
# iterate through all saved models
for savedModel in os.listdir(save_folder):
# check if the model, options file, and results file exists
checks = os.path.exists(os.path.join(save_folder, savedModel, "model.ckpt.npz")) and \
os.path.exists(os.path.join(save_folder, savedModel, "options.json")) and \
os.path.exists(os.path.join(save_folder, savedModel, "results.json"))
# skip this model if it is missing some part
if not checks:
continue
# get the model details from the options file
opt = load_options(os.path.join(save_folder, savedModel))
# load the model's results
fp = open(os.path.join(save_folder, savedModel, "results.json"))
result = json.load(fp)
fp.close()
# categorize results by image and by the training setup
image_name = opt['training_data'].split(".")[0]
suboptions = "Gaussian" if opt['gaussian_only'] else f"Gabor"
# for frequency only tests
#if opt['gaussian_only']:
# continue
# for ours (2 frequencies) vs GS only
#if opt['num_frequencies'] != 2 and not opt['gaussian_only']:
# continue
if image_name not in results.keys():
results[image_name] = {}
if suboptions not in results[image_name].keys():
results[image_name][suboptions] = {}
for metric in result.keys():
if "PSNR" in metric or "SSIM" in metric or "LPIPS" in metric or "num_params" in metric:
if metric not in results[image_name][suboptions].keys():
results[image_name][suboptions][metric] = []
results[image_name][suboptions][metric].append(result[metric])
# next, make a chart for each image
for image_name in results.keys():
plot_chart(image_name, results[image_name])
def crop_imgs():
import PIL.Image
PIL.Image.MAX_IMAGE_PIXELS = 933120000
project_folder_path = os.path.dirname(os.path.abspath(__file__))
data_folder = os.path.join(project_folder_path, "data")
output_folder = os.path.join(project_folder_path, "output")
save_folder = os.path.join(project_folder_path, "savedModels")
pluto_gt = imageio.imread(os.path.join(data_folder, "pluto.png"))
earring_gt = imageio.imread(os.path.join(data_folder, "girlwithpearlearring.jpg"))
baboon_gt = imageio.imread(os.path.join(data_folder, "baboon.jpg"))
gigapixel_gt = imageio.imread(os.path.join(data_folder, "gigapixel.jpg"))
pluto_gaussians = imageio.imread(os.path.join(output_folder, "pluto_1000000_gaussians.jpg"))
earring_gaussians = imageio.imread(os.path.join(output_folder, "earring_1000000_gaussians.jpg"))
baboon_gaussians = imageio.imread(os.path.join(output_folder, "baboon_100000_gaussians.jpg"))
gigapixel_gaussians = imageio.imread(os.path.join(output_folder, "gigapixel_1000000_gaussians.jpg"))
pluto_ours = imageio.imread(os.path.join(output_folder, "pluto_700000.jpg"))
earring_ours = imageio.imread(os.path.join(output_folder, "earring_700000.jpg"))
baboon_ours = imageio.imread(os.path.join(output_folder, "baboon_50000.jpg"))
gigapixel_ours = imageio.imread(os.path.join(output_folder, "gigapixel_700000.jpg"))
if(not os.path.exists(os.path.join(output_folder, "crops", "pluto"))):
os.makedirs(os.path.join(output_folder, "crops", "pluto"))
if(not os.path.exists(os.path.join(output_folder, "crops", "earring"))):
os.makedirs(os.path.join(output_folder, "crops", "earring"))
if(not os.path.exists(os.path.join(output_folder, "crops", "baboon"))):
os.makedirs(os.path.join(output_folder, "crops", "baboon"))
if(not os.path.exists(os.path.join(output_folder, "crops", "gigapixel"))):
os.makedirs(os.path.join(output_folder, "crops", "gigapixel"))
pluto_gt_c1 = pluto_gt[2145:2800, 5500:6000,:]
pluto_gaussians_c1 = pluto_gaussians[2145:2800, 5500:6000,:]
pluto_ours_c1 = pluto_ours[2145:2800, 5500:6000,:]
pluto_gt_c2 = pluto_gt[4800:5300, 5500:6000,:]
pluto_gaussians_c2 = pluto_gaussians[4800:5300, 5500:6000,:]
pluto_ours_c2 = pluto_ours[4800:5300, 5500:6000,:]
pluto_gt_c3 = pluto_gt[3800:4200, 2000:2500,:]
pluto_gaussians_c3 = pluto_gaussians[3800:4200, 2000:2500,:]
pluto_ours_c3 = pluto_ours[3800:4200, 2000:2500,:]
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_gt_c1.png"), pluto_gt_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_gaussians_c1.png"), pluto_gaussians_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_ours_c1.png"), pluto_ours_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_gt_c2.png"), pluto_gt_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_gaussians_c2.png"), pluto_gaussians_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_ours_c2.png"), pluto_ours_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_gt_c3.png"), pluto_gt_c3)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_gaussians_c3.png"), pluto_gaussians_c3)
imageio.imwrite(os.path.join(output_folder, "crops", "pluto", "pluto_ours_c3.png"), pluto_ours_c3)
earring_gt_c1 = earring_gt[2145:2800, 5500:6000,:]
earring_gaussians_c1 = earring_gaussians[2145:2800, 5500:6000,:]
earring_ours_c1 = earring_ours[2145:2800, 5500:6000,:]
earring_gt_c2 = earring_gt[7000:7500, 4500:5000,:]
earring_gaussians_c2 = earring_gaussians[7000:7500, 4500:5000,:]
earring_ours_c2 = earring_ours[7000:7500, 4500:5000,:]
earring_gt_c3 = earring_gt[3000:4000, 3000:4000,:]
earring_gaussians_c3 = earring_gaussians[3000:4000, 3000:4000,:]
earring_ours_c3 = earring_ours[3000:4000, 3000:4000,:]
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_gt_c1.png"), earring_gt_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_gaussians_c1.png"), earring_gaussians_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_ours_c1.png"), earring_ours_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_gt_c2.png"), earring_gt_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_gaussians_c2.png"), earring_gaussians_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_ours_c2.png"), earring_ours_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_gt_c3.png"), earring_gt_c3)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_gaussians_c3.png"), earring_gaussians_c3)
imageio.imwrite(os.path.join(output_folder, "crops", "earring", "earring_ours_c3.png"), earring_ours_c3)
baboon_gt_c1 = baboon_gt[256:512, 0:128,:]
baboon_gaussians_c1 = baboon_gaussians[256:512, 0:128,:]
baboon_ours_c1 = baboon_ours[256:512, 0:128,:]
baboon_gt_c2 = baboon_gt[0:128, 256:440,:]
baboon_gaussians_c2 = baboon_gaussians[0:128, 256:440,:]
baboon_ours_c2 = baboon_ours[0:128, 256:440,:]
imageio.imwrite(os.path.join(output_folder, "crops", "baboon", "baboon_gt_c1.png"), baboon_gt_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "baboon", "baboon_gaussians_c1.png"), baboon_gaussians_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "baboon", "baboon_ours_c1.png"), baboon_ours_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "baboon", "baboon_gt_c2.png"), baboon_gt_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "baboon", "baboon_gaussians_c2.png"), baboon_gaussians_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "baboon", "baboon_ours_c2.png"), baboon_ours_c2)
gigapixel_gt_c1 = gigapixel_gt[15000:19000, 25000:29000,:]
gigapixel_gaussians_c1 = gigapixel_gaussians[15000:19000, 25000:29000,:]
gigapixel_ours_c1 = gigapixel_ours[15000:19000, 25000:29000,:]
gigapixel_gt_c2 = gigapixel_gt[4000:8000, 34000:38000,:]
gigapixel_gaussians_c2 = gigapixel_gaussians[4000:8000, 34000:38000,:]
gigapixel_ours_c2 = gigapixel_ours[4000:8000, 34000:38000,:]
imageio.imwrite(os.path.join(output_folder, "crops", "gigapixel", "gigapixel_gt_c1.png"), gigapixel_gt_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "gigapixel", "gigapixel_gaussians_c1.png"), gigapixel_gaussians_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "gigapixel", "gigapixel_ours_c1.png"), gigapixel_ours_c1)
imageio.imwrite(os.path.join(output_folder, "crops", "gigapixel", "gigapixel_gt_c2.png"), gigapixel_gt_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "gigapixel", "gigapixel_gaussians_c2.png"), gigapixel_gaussians_c2)
imageio.imwrite(os.path.join(output_folder, "crops", "gigapixel", "gigapixel_ours_c2.png"), gigapixel_ours_c2)
def avg_metrics_F_test():
fold = os.path.join(save_folder, "F_test")
metrics = {}
for model in os.listdir(fold):
if(".txt" in model):
continue
k = int(model.split("_")[-1])
if k not in metrics.keys():
metrics[k] = {"n": 0}
fp = open(os.path.join(fold, model, "results.json"))
result = json.load(fp)
fp.close()
for key in result.keys():
if key not in metrics[k].keys():
metrics[k][key] = 0
metrics[k][key] += float(result[key])
metrics[k]['n'] += 1
for k in metrics.keys():
print(f"F={k}")
n = metrics[k]["n"]
for key in metrics[k]:
print(f"Average {key}: {metrics[k][key]/n : 0.03f}")
print()
def avg_metrics_k_test():
fold = os.path.join(save_folder, "k_test")
metrics = {}
for model in os.listdir(fold):
if(".txt" in model):
continue
k = int(model.split("_")[-1])
if k not in metrics.keys():
metrics[k] = {"n": 0}
fp = open(os.path.join(fold, model, "results.json"))
result = json.load(fp)
fp.close()
for key in result.keys():
if key not in metrics[k].keys():
metrics[k][key] = 0
metrics[k][key] += float(result[key])
metrics[k]['n'] += 1
for k in metrics.keys():
print(f"k={k}")
n = metrics[k]["n"]
for key in metrics[k]:
print(f"Average {key}: {metrics[k][key]/n : 0.03f}")
print()
def avg_metrics_max_freq_test():
fold = os.path.join(save_folder, "max_frequency_test")
metrics = {}
for model in os.listdir(fold):
if(".txt" in model):
continue
k = int(model.split("_")[-1])
if k not in metrics.keys():
metrics[k] = {"n": 0}
fp = open(os.path.join(fold, model, "results.json"))
result = json.load(fp)
fp.close()
for key in result.keys():
if key not in metrics[k].keys():
metrics[k][key] = 0
metrics[k][key] += float(result[key])
metrics[k]['n'] += 1
for k in metrics.keys():
print(f"freq={k}")
n = metrics[k]["n"]
for key in metrics[k]:
print(f"Average {key}: {metrics[k][key]/n : 0.03f}")
print()
def create_gabor():
x = np.linspace(-1.0, 1.0, 512)[None,:].repeat(512, 0)
y = np.linspace(-1.0, 1.0, 512)[:,None].repeat(512, 1)
xy = np.stack([x,y], axis=-1)
print(xy.shape)
g = np.exp(-(xy[:,:,0]*xy[:,:,0] + xy[:,:,1]*xy[:,:,1])/0.1)
s = np.sin(16*(xy[:,:,0]*np.cos(2.1)+xy[:,:,1]*np.sin(2.1)))
gab = g * s
plt.imshow(gab, cmap="bwr")
plt.show()
#generate_charts()
avg_metrics_F_test()
#avg_metrics_k_test()
#avg_metrics_max_freq_test()
#create_gabor()