Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new metrics #117

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dependencies:
- moviepy
- matplotlib
- scipy>=1.6.0
- lpips
- torchmetrics
- pytorch=1.11.0
- torchvision
- cudatoolkit
Expand Down
14 changes: 13 additions & 1 deletion opt/opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
from tqdm import tqdm
from typing import NamedTuple, Optional, Union

import lpips
from torchmetrics.functional import structural_similarity_index_measure

device = "cuda" if torch.cuda.is_available() else "cpu"

parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -365,6 +368,7 @@
if args.enable_random:
warn("Randomness is enabled for training (normal for LLFF & scenes with background)")

lpips_fn = lpips.LPIPS(net='alex').eval().to(device)
epoch_id = -1
while True:
dset.shuffle_rays()
Expand All @@ -376,7 +380,7 @@ def eval_step():
# Put in a function to avoid memory leak
print('Eval step')
with torch.no_grad():
stats_test = {'psnr' : 0.0, 'mse' : 0.0}
stats_test = {'psnr' : 0.0, 'ssim' : 0.0, 'lpips' : 0.0, 'mse' : 0.0}

# Standard set
N_IMGS_TO_EVAL = min(20 if epoch_id > 0 else 5, dset_test.n_images)
Expand Down Expand Up @@ -425,14 +429,20 @@ def eval_step():
depth_img,
global_step=gstep_id_base, dataformats='HWC')

rgb_pred_test_perm = rgb_pred_test.unsqueeze(0).permute(0, 3, 1, 2)
rgb_gt_test_perm = rgb_gt_test.unsqueeze(0).permute(0, 3, 1, 2)
rgb_pred_test = rgb_gt_test = None
mse_num : float = all_mses.mean().item()
psnr = -10.0 * math.log10(mse_num)
if math.isnan(psnr):
print('NAN PSNR', i, img_id, mse_num)
assert False
ssim = structural_similarity_index_measure(rgb_pred_test_perm, rgb_gt_test_perm).item()
lpips = lpips_fn(rgb_pred_test_perm, rgb_gt_test_perm, normalize=True).item()
stats_test['mse'] += mse_num
stats_test['psnr'] += psnr
stats_test['ssim'] += ssim
stats_test['lpips'] += lpips
n_images_gen += 1

if grid.basis_type == svox2.BASIS_TYPE_3D_TEXTURE or \
Expand Down Expand Up @@ -461,6 +471,8 @@ def eval_step():

stats_test['mse'] /= n_images_gen
stats_test['psnr'] /= n_images_gen
stats_test['ssim'] /= n_images_gen
stats_test['lpips'] /= n_images_gen
for stat_name in stats_test:
summary_writer.add_scalar('test/' + stat_name,
stats_test[stat_name], global_step=gstep_id_base)
Expand Down