text stringlengths 1 93.6k |
|---|
"""
|
Samples videos on test set & calculates some metrics such as PSNR & VLB.
|
PSNR for diffusion is found by sampling x_0 to T//2 and then finding a prediction of x_0
|
:param testing_dataset_loader: The cycle(dataloader) object for looping through test set
|
:param diffusion: Gaussian Diffusion model instance
|
:param args: parameters of the model
|
:param ema: exponential moving average unet for sampling
|
:param model: original unet for VLB calc
|
:return: outputs:
|
total VLB mu +- sigma,
|
prior VLB mu +- sigma,
|
vb -> T mu +- sigma,
|
x_0 mse -> T mu +- sigma,
|
mse -> T mu +- sigma,
|
PSNR mu +- sigma
|
"""
|
import os
|
try:
|
os.makedirs(f'./diffusion-videos/ARGS={args["arg_num"]}/test-set/')
|
except OSError:
|
pass
|
ema.eval()
|
model.eval()
|
plt.rcParams['figure.dpi'] = 200
|
for i in [*range(100, args['sample_distance'], 100)]:
|
data = next(testing_dataset_loader)
|
if args["dataset"] == "cifar" or args["dataset"] == "carpet":
|
# cifar outputs [data,class]
|
x = data[0].to(device)
|
else:
|
x = data["image"]
|
x = x.to(device)
|
row_size = min(5, args['Batch_Size'])
|
fig, ax = plt.subplots()
|
out = diffusion.forward_backward(ema, x, see_whole_sequence="half", t_distance=i)
|
imgs = [[ax.imshow(gridify_output(x, row_size), animated=True)] for x in out]
|
ani = animation.ArtistAnimation(
|
fig, imgs, interval=200, blit=True,
|
repeat_delay=1000
|
)
|
files = os.listdir(f'./diffusion-videos/ARGS={args["arg_num"]}/test-set/')
|
ani.save(f'./diffusion-videos/ARGS={args["arg_num"]}/test-set/t={i}-attempts={len(files) + 1}.mp4')
|
test_iters = 40
|
vlb = []
|
for epoch in range(test_iters // args["Batch_Size"] + 5):
|
data = next(testing_dataset_loader)
|
if args["dataset"] != "cifar":
|
x = data["image"]
|
x = x.to(device)
|
else:
|
# cifar outputs [data,class]
|
x = data[0].to(device)
|
vlb_terms = diffusion.calc_total_vlb(x, model, args)
|
vlb.append(vlb_terms)
|
psnr = []
|
for epoch in range(test_iters // args["Batch_Size"] + 5):
|
data = next(testing_dataset_loader)
|
if args["dataset"] != "cifar":
|
x = data["image"]
|
x = x.to(device)
|
else:
|
# cifar outputs [data,class]
|
x = data[0].to(device)
|
out = diffusion.forward_backward(ema, x, see_whole_sequence=None, t_distance=args["T"] // 2)
|
psnr.append(PSNR(out, x))
|
print(
|
f"Test set total VLB: {np.mean([i['total_vlb'].mean(dim=-1).cpu().item() for i in vlb])} +- {np.std([i['total_vlb'].mean(dim=-1).cpu().item() for i in vlb])}"
|
)
|
print(
|
f"Test set prior VLB: {np.mean([i['prior_vlb'].mean(dim=-1).cpu().item() for i in vlb])} +-"
|
f" {np.std([i['prior_vlb'].mean(dim=-1).cpu().item() for i in vlb])}"
|
)
|
print(
|
f"Test set vb @ t=200: {np.mean([i['vb'][0][199].cpu().item() for i in vlb])} "
|
f"+- {np.std([i['vb'][0][199].cpu().item() for i in vlb])}"
|
)
|
print(
|
f"Test set x_0_mse @ t=200: {np.mean([i['x_0_mse'][0][199].cpu().item() for i in vlb])} "
|
f"+- {np.std([i['x_0_mse'][0][199].cpu().item() for i in vlb])}"
|
)
|
print(
|
f"Test set mse @ t=200: {np.mean([i['mse'][0][199].cpu().item() for i in vlb])}"
|
f" +- {np.std([i['mse'][0][199].cpu().item() for i in vlb])}"
|
)
|
print(f"Test set PSNR: {np.mean(psnr)} +- {np.std(psnr)}")
|
def main():
|
args, output = load_parameters(device)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.