-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.py
175 lines (148 loc) · 7.1 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
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
from pathlib import Path
import os
from PIL import Image
import torch
import torchvision.transforms.functional as tf
from utils.loss_utils import ssim
# from lpipsPyTorch import lpips
import lpips
import json
from tqdm import tqdm
from utils.image_utils import psnr
from utils.image_utils import rmse
from argparse import ArgumentParser
import numpy as np
def array2tensor(array, device="cuda", dtype=torch.float32):
return torch.tensor(array, dtype=dtype, device=device)
# Learned Perceptual Image Patch Similarity
class LPIPS(object):
"""
borrowed from https://github.com/huster-wgm/Pytorch-metrics/blob/master/metrics.py
"""
def __init__(self, device="cuda"):
self.model = lpips.LPIPS(net='alex').to(device)
def __call__(self, y_pred, y_true, normalized=True):
"""
args:
y_true : 4-d ndarray in [batch_size, channels, img_rows, img_cols]
y_pred : 4-d ndarray in [batch_size, channels, img_rows, img_cols]
normalized : change [0,1] => [-1,1] (default by LPIPS)
return LPIPS, smaller the better
"""
if normalized:
y_pred = y_pred * 2.0 - 1.0
y_true = y_true * 2.0 - 1.0
error = self.model.forward(y_pred, y_true)
return torch.mean(error)
lpips = LPIPS()
def cal_lpips(a, b, device="cuda", batch=2):
"""Compute lpips.
a, b: [batch, H, W, 3]"""
if not torch.is_tensor(a):
a = array2tensor(a, device)
if not torch.is_tensor(b):
b = array2tensor(b, device)
lpips_all = []
for a_split, b_split in zip(a.split(split_size=batch, dim=0), b.split(split_size=batch, dim=0)):
out = lpips(a_split, b_split)
lpips_all.append(out)
lpips_all = torch.stack(lpips_all)
lpips_mean = lpips_all.mean()
return lpips_mean
def readImages(renders_dir, gt_dir, depth_dir, gtdepth_dir, masks_dir):
renders = []
gts = []
image_names = []
depths = []
gt_depths = []
masks = []
for fname in os.listdir(renders_dir):
render = np.array(Image.open(renders_dir / fname))
gt = np.array(Image.open(gt_dir / fname))
depth = np.array(Image.open(depth_dir / fname))
gt_depth = np.array(Image.open(gtdepth_dir / fname))
mask = np.array(Image.open(masks_dir / fname))
renders.append(tf.to_tensor(render).unsqueeze(0)[:, :3, :, :].cuda())
gts.append(tf.to_tensor(gt).unsqueeze(0)[:, :3, :, :].cuda())
depths.append(torch.from_numpy(depth).unsqueeze(0).unsqueeze(1)[:, :, :, :].cuda())
gt_depths.append(torch.from_numpy(gt_depth).unsqueeze(0).unsqueeze(1)[:, :3, :, :].cuda())
masks.append(tf.to_tensor(mask).unsqueeze(0).cuda())
image_names.append(fname)
return renders, gts, depths, gt_depths, masks, image_names
def evaluate(model_paths):
full_dict = {}
per_view_dict = {}
full_dict_polytopeonly = {}
per_view_dict_polytopeonly = {}
print("")
with torch.no_grad():
for scene_dir in model_paths:
print("Scene:", scene_dir)
full_dict[scene_dir] = {}
per_view_dict[scene_dir] = {}
full_dict_polytopeonly[scene_dir] = {}
per_view_dict_polytopeonly[scene_dir] = {}
test_dir = Path(scene_dir) / args.phase
for method in os.listdir(test_dir):
print("Method:", method)
full_dict[scene_dir][method] = {}
per_view_dict[scene_dir][method] = {}
full_dict_polytopeonly[scene_dir][method] = {}
per_view_dict_polytopeonly[scene_dir][method] = {}
method_dir = test_dir / method
gt_dir = method_dir/ "gt"
renders_dir = method_dir / "renders"
depth_dir = method_dir / "depth"
gt_depth_dir = method_dir / "gt_depth"
masks_dir = method_dir / "masks"
renders, gts, depths, gt_depths, masks, image_names = readImages(renders_dir, gt_dir, depth_dir, gt_depth_dir, masks_dir)
ssims = []
psnrs = []
lpipss = []
rmses = []
for idx in tqdm(range(len(renders)), desc="Metric evaluation progress"):
render, gt, depth, gt_depth, mask = renders[idx], gts[idx], depths[idx], gt_depths[idx], masks[idx]
render = render * mask
gt = gt * mask
psnrs.append(psnr(render, gt))
ssims.append(ssim(render, gt))
lpipss.append(cal_lpips(render, gt))
if (gt_depth!=0).sum() < 10:
continue
rmses.append(rmse(depth, gt_depth, mask))
print("Scene: ", scene_dir, "SSIM : {:>12.7f}".format(torch.tensor(ssims).mean(), ".5"))
print("Scene: ", scene_dir, "PSNR : {:>12.7f}".format(torch.tensor(psnrs).mean(), ".5"))
print("Scene: ", scene_dir, "LPIPS: {:>12.7f}".format(torch.tensor(lpipss).mean(), ".5"))
print("Scene: ", scene_dir, "RMSE: {:>12.7f}".format(torch.tensor(rmses).mean(), ".5"))
print("")
full_dict[scene_dir][method].update({"SSIM": torch.tensor(ssims).mean().item(),
"PSNR": torch.tensor(psnrs).mean().item(),
"LPIPS": torch.tensor(lpipss).mean().item(),
"RMSE": torch.tensor(rmses).mean().item()})
per_view_dict[scene_dir][method].update({"SSIM": {name: ssim for ssim, name in zip(torch.tensor(ssims).tolist(), image_names)},
"PSNR": {name: psnr for psnr, name in zip(torch.tensor(psnrs).tolist(), image_names)},
"LPIPS": {name: lp for lp, name in zip(torch.tensor(lpipss).tolist(), image_names)},
"RMSES": {name: lp for lp, name in zip(torch.tensor(rmses).tolist(), image_names)}})
with open(scene_dir + "/results.json", 'w') as fp:
json.dump(full_dict[scene_dir], fp, indent=True)
with open(scene_dir + "/per_view.json", 'w') as fp:
json.dump(per_view_dict[scene_dir], fp, indent=True)
if __name__ == "__main__":
device = torch.device("cuda:0")
torch.cuda.set_device(device)
# Set up command line argument parser
parser = ArgumentParser(description="Training script parameters")
parser.add_argument('--model_paths', '-m', required=True, nargs="+", type=str, default=[])
parser.add_argument('--phase', '-p', type=str, default='test')
args = parser.parse_args()
evaluate(args.model_paths)