-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
executable file
·358 lines (319 loc) · 12.6 KB
/
metrics.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import os
import shutil
import torch
import torchvision
from pytorch_fid import fid_score
from torch import distributed
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from tqdm.autonotebook import tqdm, trange
from renderer import *
from config import *
from diffusion import Sampler
from dist_utils import *
import lpips
from ssim import ssim
def make_subset_loader(conf: TrainConfig,
dataset: Dataset,
batch_size: int,
shuffle: bool,
parallel: bool,
drop_last=True):
dataset = SubsetDataset(dataset, size=conf.eval_num_images)
if parallel and distributed.is_initialized():
sampler = DistributedSampler(dataset, shuffle=shuffle)
else:
sampler = None
return DataLoader(
dataset,
batch_size=batch_size,
sampler=sampler,
# with sampler, use the sample instead of this option
shuffle=False if sampler else shuffle,
num_workers=conf.num_workers,
pin_memory=True,
drop_last=drop_last,
multiprocessing_context=get_context('fork'),
)
def evaluate_lpips(
sampler: Sampler,
model: Model,
conf: TrainConfig,
device,
val_data: Dataset,
latent_sampler: Sampler = None,
use_inverted_noise: bool = False,
):
"""
compare the generated images from autoencoder on validation dataset
Args:
use_inversed_noise: the noise is also inverted from DDIM
"""
lpips_fn = lpips.LPIPS(net='alex').to(device)
val_loader = make_subset_loader(conf,
dataset=val_data,
batch_size=conf.batch_size_eval,
shuffle=False,
parallel=True)
model.eval()
with torch.no_grad():
scores = {
'lpips': [],
'mse': [],
'ssim': [],
'psnr': [],
}
for batch in tqdm(val_loader, desc='lpips'):
imgs = batch['img'].to(device)
if use_inverted_noise:
# inverse the noise
# with condition from the encoder
model_kwargs = {}
if conf.model_type.has_autoenc():
with torch.no_grad():
model_kwargs = model.encode(imgs)
x_T = sampler.ddim_reverse_sample_loop(
model=model,
x=imgs,
clip_denoised=True,
model_kwargs=model_kwargs)
x_T = x_T['sample']
else:
x_T = torch.randn((len(imgs), 3, conf.img_size, conf.img_size),
device=device)
if conf.model_type == ModelType.ddpm:
# the case where you want to calculate the inversion capability of the DDIM model
assert use_inverted_noise
pred_imgs = render_uncondition(
conf=conf,
model=model,
x_T=x_T,
sampler=sampler,
latent_sampler=latent_sampler,
)
else:
pred_imgs = render_condition(conf=conf,
model=model,
x_T=x_T,
x_start=imgs,
cond=None,
sampler=sampler)
# # returns {'cond', 'cond2'}
# conds = model.encode(imgs)
# pred_imgs = sampler.sample(model=model,
# noise=x_T,
# model_kwargs=conds)
# (n, 1, 1, 1) => (n, )
scores['lpips'].append(lpips_fn.forward(imgs, pred_imgs).view(-1))
# need to normalize into [0, 1]
norm_imgs = (imgs + 1) / 2
norm_pred_imgs = (pred_imgs + 1) / 2
# (n, )
scores['ssim'].append(
ssim(norm_imgs, norm_pred_imgs, size_average=False))
# (n, )
scores['mse'].append(
(norm_imgs - norm_pred_imgs).pow(2).mean(dim=[1, 2, 3]))
# (n, )
scores['psnr'].append(psnr(norm_imgs, norm_pred_imgs))
# (N, )
for key in scores.keys():
scores[key] = torch.cat(scores[key]).float()
model.train()
barrier()
# support multi-gpu
outs = {
key: [
torch.zeros(len(scores[key]), device=device)
for i in range(get_world_size())
]
for key in scores.keys()
}
for key in scores.keys():
all_gather(outs[key], scores[key])
# final scores
for key in scores.keys():
scores[key] = torch.cat(outs[key]).mean().item()
# {'lpips', 'mse', 'ssim'}
return scores
def psnr(img1, img2):
"""
Args:
img1: (n, c, h, w)
"""
v_max = 1.
# (n,)
mse = torch.mean((img1 - img2)**2, dim=[1, 2, 3])
return 20 * torch.log10(v_max / torch.sqrt(mse))
def evaluate_fid(
sampler: Sampler,
model: Model,
conf: TrainConfig,
device,
train_data: Dataset,
val_data: Dataset,
latent_sampler: Sampler = None,
conds_mean=None,
conds_std=None,
remove_cache: bool = True,
clip_latent_noise: bool = False,
):
assert conf.fid_cache is not None
if get_rank() == 0:
# no parallel
# validation data for a comparing FID
val_loader = make_subset_loader(conf,
dataset=val_data,
batch_size=conf.batch_size_eval,
shuffle=False,
parallel=False)
# put the val images to a directory
cache_dir = f'{conf.fid_cache}_{conf.eval_num_images}'
if (os.path.exists(cache_dir)
and len(os.listdir(cache_dir)) < conf.eval_num_images):
shutil.rmtree(cache_dir)
if not os.path.exists(cache_dir):
# write files to the cache
# the images are normalized, hence need to denormalize first
loader_to_path(val_loader, cache_dir, denormalize=True)
# create the generate dir
if os.path.exists(conf.generate_dir):
shutil.rmtree(conf.generate_dir)
os.makedirs(conf.generate_dir)
barrier()
world_size = get_world_size()
rank = get_rank()
batch_size = chunk_size(conf.batch_size_eval, rank, world_size)
def filename(idx):
return world_size * idx + rank
model.eval()
with torch.no_grad():
if conf.model_type.can_sample():
eval_num_images = chunk_size(conf.eval_num_images, rank,
world_size)
desc = "generating images"
for i in trange(0, eval_num_images, batch_size, desc=desc):
batch_size = min(batch_size, eval_num_images - i)
x_T = torch.randn(
(batch_size, 3, conf.img_size, conf.img_size),
device=device)
batch_images = render_uncondition(
conf=conf,
model=model,
x_T=x_T,
sampler=sampler,
latent_sampler=latent_sampler,
conds_mean=conds_mean,
conds_std=conds_std).cpu()
batch_images = (batch_images + 1) / 2
# keep the generated images
for j in range(len(batch_images)):
img_name = filename(i + j)
torchvision.utils.save_image(
batch_images[j],
os.path.join(conf.generate_dir, f'{img_name}.png'))
elif conf.model_type == ModelType.autoencoder:
if conf.train_mode.is_latent_diffusion():
# evaluate autoencoder + latent diffusion (doesn't give the images)
model: BeatGANsAutoencModel
eval_num_images = chunk_size(conf.eval_num_images, rank,
world_size)
desc = "generating images"
for i in trange(0, eval_num_images, batch_size, desc=desc):
batch_size = min(batch_size, eval_num_images - i)
x_T = torch.randn(
(batch_size, 3, conf.img_size, conf.img_size),
device=device)
batch_images = render_uncondition(
conf=conf,
model=model,
x_T=x_T,
sampler=sampler,
latent_sampler=latent_sampler,
conds_mean=conds_mean,
conds_std=conds_std,
clip_latent_noise=clip_latent_noise,
).cpu()
batch_images = (batch_images + 1) / 2
# keep the generated images
for j in range(len(batch_images)):
img_name = filename(i + j)
torchvision.utils.save_image(
batch_images[j],
os.path.join(conf.generate_dir, f'{img_name}.png'))
else:
# evaulate autoencoder (given the images)
# to make the FID fair, autoencoder must not see the validation dataset
# also shuffle to make it closer to unconditional generation
train_loader = make_subset_loader(conf,
dataset=train_data,
batch_size=batch_size,
shuffle=True,
parallel=True)
i = 0
for batch in tqdm(train_loader, desc='generating images'):
imgs = batch['img'].to(device)
x_T = torch.randn(
(len(imgs), 3, conf.img_size, conf.img_size),
device=device)
batch_images = render_condition(
conf=conf,
model=model,
x_T=x_T,
x_start=imgs,
cond=None,
sampler=sampler,
latent_sampler=latent_sampler).cpu()
# model: BeatGANsAutoencModel
# # returns {'cond', 'cond2'}
# conds = model.encode(imgs)
# batch_images = sampler.sample(model=model,
# noise=x_T,
# model_kwargs=conds).cpu()
# denormalize the images
batch_images = (batch_images + 1) / 2
# keep the generated images
for j in range(len(batch_images)):
img_name = filename(i + j)
torchvision.utils.save_image(
batch_images[j],
os.path.join(conf.generate_dir, f'{img_name}.png'))
i += len(imgs)
else:
raise NotImplementedError()
model.train()
barrier()
if get_rank() == 0:
fid = fid_score.calculate_fid_given_paths(
[cache_dir, conf.generate_dir],
batch_size,
device=device,
dims=2048)
# remove the cache
if remove_cache and os.path.exists(conf.generate_dir):
shutil.rmtree(conf.generate_dir)
barrier()
if get_rank() == 0:
# need to float it! unless the broadcasted value is wrong
fid = torch.tensor(float(fid), device=device)
broadcast(fid, 0)
else:
fid = torch.tensor(0., device=device)
broadcast(fid, 0)
fid = fid.item()
print(f'fid ({get_rank()}):', fid)
return fid
def loader_to_path(loader: DataLoader, path: str, denormalize: bool):
# not process safe!
if not os.path.exists(path):
os.makedirs(path)
# write the loader to files
i = 0
for batch in tqdm(loader, desc='copy images'):
imgs = batch['img']
if denormalize:
imgs = (imgs + 1) / 2
for j in range(len(imgs)):
torchvision.utils.save_image(imgs[j],
os.path.join(path, f'{i+j}.png'))
i += len(imgs)