forked from edouardelasalles/srvp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
421 lines (378 loc) · 19.7 KB
/
test.py
File metadata and controls
421 lines (378 loc) · 19.7 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Copyright 2020 Mickael Chen, Edouard Delasalles, Jean-Yves Franceschi, Patrick Gallinari, Sylvain Lamprier
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import configargparse
import os
import random
import torch
import matplotlib.pyplot as plt # :Bhavani
import matplotlib.image as mpimg # :Bhavani
import numpy as np
import torch.nn.functional as F
from collections import defaultdict
from torch.utils.data import DataLoader
from tqdm import tqdm
import func as helper
import data.base as data
import module.srvp as srvp
from metrics.ssim import ssim_loss
from metrics.lpips.loss import PerceptualLoss
from metrics.fvd.score import fvd as fvd_score
def _ssim_wrapper(sample, gt):
"""
Computes the pixel-averaged SSIM between two videos.
Parameters
----------
sample : torch.*.Tensor
Tensor representing a video, of shape (length, batch, channels, width, height) and with float values lying in
[0, 1].
gt : torch.*.Tensor
Tensor representing a video, of shape (length, batch, channels, width, height) and with float values lying in
[0, 1]. Its shape should be the same as sample.
Returns
-------
torch.*.Tensor
Tensor of pixel-averaged SSIM between the input videos, of shape (length, batch, channels).
"""
nt, bsz = sample.shape[0], sample.shape[1]
img_shape = sample.shape[2:]
ssim = ssim_loss(sample.view(nt * bsz, *img_shape), gt.view(nt * bsz, *img_shape), max_val=1., reduction='none')
return ssim.mean(dim=[2, 3]).view(nt, bsz, img_shape[0])
def _lpips_wrapper(sample, gt, lpips_model):
"""
Computes the frame-wise LPIPS between two videos.
Parameters
----------
sample : torch.*.Tensor
Tensor representing a video, of shape (length, batch, channels, width, height) and with float values lying in
[0, 1].
gt : torch.*.Tensor
Tensor representing a video, of shape (length, batch, channels, width, height) and with float values lying in
[0, 1]. Its shape should be the same as sample.
Returns
-------
torch.*.Tensor
Tensor of frame-wise LPIPS between the input videos, of shape (length, batch).
"""
nt, bsz = sample.shape[0], sample.shape[1]
img_shape = sample.shape[2:]
# Switch to three color channels for grayscale videos
if img_shape[0] == 1:
sample_ = sample.repeat(1, 1, 3, 1, 1)
gt_ = gt.repeat(1, 1, 3, 1, 1)
else:
sample_ = sample
gt_ = gt
lpips = lpips_model(sample_.view(nt * bsz, 3, *img_shape[1:]), gt_.view(nt * bsz, 3, *img_shape[1:]))
return lpips.view(nt, bsz)
def _get_idx_better(name, ref, hyp):
"""
Computes the batch indices for which the input metric value is better than the current metric value.
Parameters
----------
name : str
'mse', 'psnr', 'ssim', 'lpips', or 'fvd'. Metric to consider. For 'mse', 'fvd' and 'lpips', lower is better,
while for 'psnr' and 'ssim', higher is better.
ref : torch.*.Tensor
One-dimensional tensor containing a list of current metric values.
hyp : torch.*.Tensor
One-dimensional tensor containing a list of new metric values to be compared agains ref. Must be of the same
length as ref.
Returns
-------
torch.*.LongTensor
List of indices i for which the value hyp[i] is better than the value ref[i].
"""
if name in ('mse', 'fvd', 'lpips'):
return torch.nonzero(hyp < ref, as_tuple=False).flatten()
if name in ('psnr', 'ssim'):
return torch.nonzero(hyp > ref, as_tuple=False).flatten()
raise ValueError(f'Metric \'{name}\' not yet implemented')
def _get_idx_worst(name, ref, hyp):
"""
Computes the batch indices for which the input metric value is worse than the current metric value.
Parameters
----------
name : str
'mse', 'psnr', 'ssim', 'lpips', or 'fvd'. Metric to consider. For 'mse', 'fvd' and 'lpips', lower is better,
while for 'psnr' and 'ssim', higher is better.
ref : torch.*.Tensor
One-dimensional tensor containing a list of current metric values.
hyp : torch.*.Tensor
One-dimensional tensor containing a list of new metric values to be compared agains ref. Must be of the same
length as ref.
Returns
-------
torch.*.LongTensor
List of indices i for which the value hyp[i] is worse than the value ref[i].
"""
if name in ('mse', 'fvd', 'lpips'):
return torch.nonzero(hyp > ref, as_tuple=False).flatten()
if name in ('psnr', 'ssim'):
return torch.nonzero(hyp < ref, as_tuple=False).flatten()
raise ValueError(f'Metric \'{name}\' not yet implemented')
def main(opt):
"""
Tests SRVP by computing and printing its PSNR, SSIM, LPIPS and FVD scores.
Parameters
----------
opt : helper.DotDict
Contains the testing configuration.
"""
##################################################################################################################
# Setup
##################################################################################################################
# -- Device handling (CPU, GPU)
opt.train = False
#if opt.device is None:
opt.device = torch.device('cpu') # :Bhavani
device = torch.device('cpu') # :Bhavani
#else:
# os.environ["CUDA_VISIBLE_DEVICES"] = str(opt.device)
# device = torch.device('cuda:0')
# torch.cuda.set_device(0)
# Seed
random.seed(opt.test_seed)
np.random.seed(opt.test_seed)
torch.manual_seed(opt.test_seed)
# cuDNN
assert torch.backends.cudnn.enabled
# Load LPIPS model
lpips_model = PerceptualLoss(opt.lpips_dir)
##################################################################################################################
# Load XP config
##################################################################################################################
xp_config = helper.load_json(os.path.join(opt.xp_dir, 'config.json'))
nt_cond = opt.nt_cond if opt.nt_cond is not None else xp_config.nt_cond
print("nt_cond line 178 = ", nt_cond) # :Bhavani
nt_test = opt.nt_gen if opt.nt_gen is not None else xp_config.seq_len_test
print("nt_test (total_frames) line 180 = ", nt_test) # :Bhavani
if opt.n_euler_steps is None:
opt.n_euler_steps = xp_config.n_euler_steps
##################################################################################################################
# Load test data
##################################################################################################################
print('Loading data...')
xp_config.data_dir = opt.data_dir
xp_config.seq_len = nt_test
dataset = data.load_dataset(xp_config, False)
testset = dataset.get_fold('test')
test_loader = DataLoader(testset, batch_size=opt.batch_size, collate_fn=data.collate_fn, pin_memory=True)
##################################################################################################################
# Load model
##################################################################################################################
print('Loading model...')
model = srvp.StochasticLatentResidualVideoPredictor(xp_config.nx, xp_config.nc, xp_config.nf, xp_config.nhx,
xp_config.ny, xp_config.nz, xp_config.skipco,
xp_config.nt_inf, xp_config.nh_inf, xp_config.nlayers_inf,
xp_config.nh_res, xp_config.nlayers_res, xp_config.archi)
state_dict = torch.load(os.path.join(opt.xp_dir, opt.model_name), map_location='cpu')
model.load_state_dict(state_dict)
model.to(device)
model.eval()
##################################################################################################################
# Eval
##################################################################################################################
print('Evaluation...')
torch.set_grad_enabled(False)
best_samples = defaultdict(list)
worst_samples = defaultdict(list)
results = defaultdict(list)
results_worst = defaultdict(list)
cond = []
cond_rec = []
gt = []
# random samples = [[]] for n_samples = 1 :Bhavani
random_samples = [[] for _ in range(min(5, opt.n_samples))]
# Evaluation is done by batch
# test_loader has the testset :Bhavani
for batch in tqdm(test_loader, ncols=0):
# Data
x = batch.to(device)
# x = 25 :Bhavani
# print("line 225 length of the testset? = ",len(x)) :Bhavani
# x (line 229) = torch.Size([25, 16, 1, 64, 64]) :Bhavani
# x_cond (line 229) = torch.Size([5, 16, 1, 64, 64]) :Bhavani
# x_target (line 229) = torch.Size([20, 16, 1, 64, 64]) :Bhavani
# There are 25/5/20 frames in each video,16 such sequence of frames in each batch,1 channel (grayscale),64*64 is the frame size :Bhavani
assert nt_test <= len(x)
x = x[:nt_test]
# print("x (line 229) = ",x.shape) :Bhavani
x_cond = x[:nt_cond]
# print("x_cond (line 229) = ",x_cond.shape) :Bhavani
x_target = x[nt_cond:]
# print("x_target (line 229) = ",x_target.shape) :Bhavani
cond.append(x_cond.cpu().mul(255).byte().permute(1, 0, 3, 4, 2))
gt.append(x_target.cpu().mul(255).byte().permute(1, 0, 3, 4, 2))
# Predictions
metric_best = {}
sample_best = {}
metric_worst = {}
sample_worst = {}
# Encode conditional frames and extracts skip connections
# print("x_cond = ", x_cond.shape) # :Bhavani
skip = model.encode(x_cond)[1] if model.skipco != 'none' else None
# Generate opt.n_samples predictions
for i in range(opt.n_samples):
# Infer latent variables
x_rec, y, _, w, _, _, _, _ = model(x_cond, nt_cond, dt=1 / xp_config.n_euler_steps)
# print("x_rec size (line 252) = ",x_rec.shape) :Bhavani
y_0 = y[-1]
if i == 0:
cond_rec.append(x_rec.cpu().mul(255).byte().permute(1, 0, 3, 4, 2))
# Use the model in prediction mode starting from the last inferred state
y_os = model.generate(y_0, [], nt_test - nt_cond + 1, dt=1 / opt.n_euler_steps)[0]
y = y_os[1:].contiguous() # Remove the first state which is the last inferred state
x_pred = model.decode(w, y, skip).clamp(0, 1)
# print("x_pred size (line 259) = ", x_pred.shape)
# x_pred size (line 259) = torch.Size([20, 16, 1, 64, 64]) :Bhavani
# There are 20 frames in each sequence,16 such sequences of frames in each batch,1 channel (grayscale),64*64 is the frame size :Bhavani
new_video_Bhavani = x_pred
frames_Bhavani = nt_test - nt_cond
batch_Bhavani = opt.batch_size
video_Bhavani = new_video_Bhavani.permute(1,0,2,3,4)
new_save_number = 1
for bhav in range(batch_Bhavani):
for frame_Bhav in range(frames_Bhavani):
plt.imsave('/content/predicted/x_pred_image'+str(new_save_number)+'.png',video_Bhavani[bhav][frame_Bhav].numpy()[0],cmap='gray')
new_save_number = new_save_number + 1
#plt.imshow(video_Bhavani[bhav][frame_Bhav].numpy()[0])
#plt.show()
new_cond_Bhavani = x_rec
frames_Bhavani = nt_cond
batch_Bhavani = opt.batch_size
cond_Bhavani = new_cond_Bhavani.permute(1,0,2,3,4)
new_num = 1
for bhav in range(batch_Bhavani):
for frame_Bhav in range(frames_Bhavani):
plt.imsave('/content/conditional/x_cond_image'+str(new_num)+'.png',cond_Bhavani[bhav][frame_Bhav].numpy()[0],cmap='gray')
new_num = new_num + 1
#plt.imshow(video_Bhavani[bhav][frame_Bhav].numpy()[0])
#plt.show()
new_t_Bhavani = x_target
frames_Bhavani = nt_test - nt_cond
batch_Bhavani = opt.batch_size
t_Bhavani = new_t_Bhavani.permute(1,0,2,3,4)
new_e_number = 1
for bhav in range(batch_Bhavani):
for frame_Bhav in range(frames_Bhavani):
plt.imsave('/content/target/x_target_image'+str(new_e_number)+'.png',t_Bhavani[bhav][frame_Bhav].numpy()[0],cmap='gray')
new_e_number = new_e_number + 1
#plt.imshow(video_Bhavani[bhav][frame_Bhav].numpy()[0])
#plt.show()
# Metrics
mse = torch.mean(F.mse_loss(x_pred, x_target, reduction='none'), dim=[3, 4])
metrics_batch = {
'psnr': 10 * torch.log10(1 / mse).mean(2).mean(0).cpu(),
'ssim': _ssim_wrapper(x_pred, x_target).mean(2).mean(0).cpu(),
'lpips': _lpips_wrapper(x_pred, x_target, lpips_model).mean(0).cpu()
}
x_pred_byte = x_pred.cpu().mul(255).byte().permute(1, 0, 3, 4, 2)
# Random samples
if i < 5:
random_samples[i].append(x_pred_byte)
for name, values in metrics_batch.items():
# Iteratively compute the best and worse samples across all performed predictions for the batch of
# videos
if i == 0:
# Initial values given by the first prediction
metric_best[name] = values.clone() # Metric value for the current best prediction
sample_best[name] = x_pred_byte.clone() # Current best prediction
metric_worst[name] = values.clone() # Metric value for the current worse prediction
sample_worst[name] = x_pred_byte.clone() # Current worse prediction
continue
# Update best samples and metrics per batch element
idx_better = _get_idx_better(name, metric_best[name], values)
metric_best[name][idx_better] = values[idx_better]
sample_best[name][idx_better] = x_pred_byte[idx_better]
# Update worst samples and metrics per batch element
idx_worst = _get_idx_worst(name, metric_worst[name], values)
metric_worst[name][idx_worst] = values[idx_worst]
sample_worst[name][idx_worst] = x_pred_byte[idx_worst]
# Register best and worse predictions and best metrics
for name in sample_best.keys():
best_samples[name].append(sample_best[name])
worst_samples[name].append(sample_worst[name])
results[name].append(metric_best[name])
results_worst[name].append(metric_worst[name])
# Store best, worst and random samples
samples = {f'random_{i + 1}': torch.cat(random_sample).numpy() for i, random_sample in enumerate(random_samples)}
samples['cond_rec'] = torch.cat(cond_rec)
for name in best_samples.keys():
samples[f'{name}_best'] = torch.cat(best_samples[name]).numpy()
samples[f'{name}_worst'] = torch.cat(worst_samples[name]).numpy()
results[name] = torch.cat(results[name]).numpy()
results_worst[name] = torch.cat(results_worst[name]).numpy()
##################################################################################################################
# Compute FVD
##################################################################################################################
if opt.fvd:
print('Computing FVD...')
cond = torch.cat(cond, 0).permute(1, 0, 4, 2, 3).float().div(255)
gt = torch.cat(gt, 0).permute(1, 0, 4, 2, 3).float().div(255)
ref = torch.cat([cond, gt], 0)
hyp = torch.from_numpy(samples['random_1']).clone().permute(1, 0, 4, 2, 3).float().div(255)
hyp = torch.cat([cond, hyp], 0)
fvd = fvd_score(ref, hyp)
##################################################################################################################
# Print results
##################################################################################################################
print('\n')
print('Results:')
for name, res in results.items():
print(name, res.mean(), '+/-', 1.960 * res.std() / np.sqrt(len(res)))
if opt.fvd:
print('FVD', fvd)
##################################################################################################################
# Save samples
##################################################################################################################
# Saves the results of the metrics in results.npz (unzipped => lpips.npy, ssim.npy, psnr.npy) :Bhavani
np.savez_compressed(os.path.join(opt.xp_dir, 'results.npz'), **results)
np.savez_compressed(os.path.join(opt.xp_dir, 'results_worst.npz'), **results_worst)
for name, res in samples.items():
np.savez_compressed(os.path.join(opt.xp_dir, f'{name}.npz'), samples=res)
if __name__ == '__main__':
# Arguments
p = configargparse.ArgParser(
prog='Stochastic Latent Residual Video Prediction (testing)',
description='Evaluates a given SRVP model with respect to PSNR, SSIM, LPIPS and FVD scores. Prints numerical \
results and saves as npz files for each element of the test set its best and worst predictions \
with respect to PSNR, SSIM and LPIPS, as well as up to five random predictions.',
formatter_class=configargparse.ArgumentDefaultsHelpFormatter
)
p.add('--xp_dir', type=str, metavar='DIR', required=True,
help='Directory where the model and its configuration file are saved.')
p.add('--data_dir', type=str, metavar='DIR', required=True,
help='Directory where the dataset is saved.')
p.add('--lpips_dir', type=str, metavar='DIR', required=True,
help='Directory where the LPIPS weights are saved.')
p.add('--n_euler_steps', type=int, metavar='STEPS', default=None,
help='Number of Euler step per frame to perform for prediction. If None, takes the value of the \
corresponding training parameter.')
p.add('--nt_cond', type=int, metavar='COND', default=None,
help='Number of conditioning frames.')
p.add('--nt_gen', type=int, metavar='GEN', default=None,
help='Total number of frames (conditioning and predicted frames).')
p.add('--batch_size', type=int, metavar='BATCH', default=16,
help='Batch size used to compute metrics.')
p.add('--n_samples', type=int, metavar='NB_SAMPLES', default=100,
help='Number of predictions per sequence to produce in order to compute the best PSNR, SSIM and LPIPS.')
p.add('--model_name', type=str, metavar='FILE', default='model.pt',
help='File name of the PyTorch model to load, contained in xp_dirs.')
p.add('--device', type=int, metavar='DEVICE', default=None,
help='GPU where the model should be placed when testing (if None, put it on the CPU)')
p.add('--fvd', action='store_true',
help='Whether to compute FVD.')
p.add('--test_seed', type=int, metavar='SEED', default=1,
help='Manual seed.')
# Parse arguments
opt = helper.DotDict(vars(p.parse_args()))
# Main
main(opt)