DL-Art-School/codes/trainer/eval/flow_gaussian_nll.py

37 lines
1.4 KiB
Python
Raw Normal View History

2020-12-02 21:09:54 +00:00
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
2020-12-02 21:09:54 +00:00
2020-12-18 16:18:34 +00:00
import trainer.eval.evaluator as evaluator
2020-12-02 21:09:54 +00:00
# Evaluate how close to true Gaussian a flow network predicts in a "normal" pass given a LQ/HQ image pair.
2022-03-16 18:05:56 +00:00
from data.images.image_folder_dataset import ImageFolderDataset
2022-03-16 18:04:00 +00:00
from models.image_generation.srflow import GaussianDiag
2020-12-02 21:09:54 +00:00
class FlowGaussianNll(evaluator.Evaluator):
def __init__(self, model, opt_eval, env):
2021-06-14 15:51:44 +00:00
super().__init__(model, opt_eval, env, uses_all_ddp=False)
2020-12-02 21:09:54 +00:00
self.batch_sz = opt_eval['batch_size']
self.dataset = ImageFolderDataset(opt_eval['dataset'])
self.dataloader = DataLoader(self.dataset, self.batch_sz)
def perform_eval(self):
total_zs = 0
z_loss = 0
self.model.eval()
2020-12-02 21:09:54 +00:00
with torch.no_grad():
print("Evaluating FlowGaussianNll..")
for batch in tqdm(self.dataloader):
dev = self.env['device']
z, _, _ = self.model(gt=batch['hq'].to(dev),
lr=batch['lq'].to(dev),
2020-12-02 21:09:54 +00:00
epses=[],
reverse=False,
add_gt_noise=False)
for z_ in z:
z_loss += GaussianDiag.logp(None, None, z_).mean()
total_zs += 1
self.model.train()
2020-12-02 21:09:54 +00:00
return {"gaussian_diff": z_loss / total_zs}