-
Notifications
You must be signed in to change notification settings - Fork 26
/
common.py
293 lines (247 loc) · 9.81 KB
/
common.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
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
#!/usr/bin/env python3
# Copyright (c) 2020-2022, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
import os
import sys
import shutil
import math
from pathlib import Path, PurePath, PurePosixPath
import numpy as np
import pyexr as exr
import csv
from scipy.ndimage.filters import convolve1d
import glob
import struct
import io
import PIL.Image
PIL.Image.MAX_IMAGE_PIXELS = 10000000000
import flip
import flip.utils
import code
PAPER_FOLDER = Path(__file__).resolve().parent.parent
SUPPL_FOLDER = PAPER_FOLDER/"supplemental"
SCRIPTS_FOLDER = PAPER_FOLDER/"scripts"
TEMPLATE_FOLDER = SCRIPTS_FOLDER/"template"
DATA_FOLDER = SCRIPTS_FOLDER/"data"
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
RESULTS_DIR = os.path.join(ROOT_DIR, "results")
NGP_DATA_FOLDER = os.environ.get("NGP_DATA_FOLDER") or os.path.join(ROOT_DIR, "data")
NERF_DATA_FOLDER = os.path.join(NGP_DATA_FOLDER, "nerf")
SDF_DATA_FOLDER = os.path.join(NGP_DATA_FOLDER, "sdf")
IMAGE_DATA_FOLDER = os.path.join(NGP_DATA_FOLDER, "image")
VOLUME_DATA_FOLDER = os.path.join(NGP_DATA_FOLDER, "volume")
# Search for pyngp in the build folder.
sys.path += [os.path.dirname(pyd) for pyd in glob.iglob(os.path.join(ROOT_DIR, "build*", "**/*.pyd"), recursive=True)]
sys.path += [os.path.dirname(pyd) for pyd in glob.iglob(os.path.join(ROOT_DIR, "build*", "**/*.so"), recursive=True)]
def repl(testbed):
print("-------------------\npress Ctrl-Z to return to gui\n---------------------------")
code.InteractiveConsole(locals=locals()).interact()
print("------- returning to gui...")
def mse2psnr(x): return -10.*np.log(x)/np.log(10.)
def sanitize_path(path):
return str(PurePosixPath(path.relative_to(PAPER_FOLDER)))
# from https://stackoverflow.com/questions/31638651/how-can-i-draw-lines-into-numpy-arrays
def trapez(y,y0,w):
return np.clip(np.minimum(y+1+w/2-y0, -y+1+w/2+y0),0,1)
def weighted_line(r0, c0, r1, c1, w, rmin=0, rmax=np.inf):
# The algorithm below works fine if c1 >= c0 and c1-c0 >= abs(r1-r0).
# If either of these cases are violated, do some switches.
if abs(c1-c0) < abs(r1-r0):
# Switch x and y, and switch again when returning.
xx, yy, val = weighted_line(c0, r0, c1, r1, w, rmin=rmin, rmax=rmax)
return (yy, xx, val)
# At this point we know that the distance in columns (x) is greater
# than that in rows (y). Possibly one more switch if c0 > c1.
if c0 > c1:
return weighted_line(r1, c1, r0, c0, w, rmin=rmin, rmax=rmax)
# The following is now always < 1 in abs
slope = (r1-r0) / (c1-c0)
# Adjust weight by the slope
w *= np.sqrt(1+np.abs(slope)) / 2
# We write y as a function of x, because the slope is always <= 1
# (in absolute value)
x = np.arange(c0, c1+1, dtype=float)
y = x * slope + (c1*r0-c0*r1) / (c1-c0)
# Now instead of 2 values for y, we have 2*np.ceil(w/2).
# All values are 1 except the upmost and bottommost.
thickness = np.ceil(w/2)
yy = (np.floor(y).reshape(-1,1) + np.arange(-thickness-1,thickness+2).reshape(1,-1))
xx = np.repeat(x, yy.shape[1])
vals = trapez(yy, y.reshape(-1,1), w).flatten()
yy = yy.flatten()
# Exclude useless parts and those outside of the interval
# to avoid parts outside of the picture
mask = np.logical_and.reduce((yy >= rmin, yy < rmax, vals > 0))
return (yy[mask].astype(int), xx[mask].astype(int), vals[mask])
def diagonally_truncated_mask(shape, x_threshold, angle):
result = np.zeros(shape, dtype=bool)
for x in range(shape[1]):
for y in range(shape[0]):
thres = x_threshold * shape[1] - (angle * shape[0] / 2) + y * angle
result[y, x, ...] = x < thres
return result
def diagonally_combine_two_images(img1, img2, x_threshold, angle, gap=0, color=1):
if img2.shape != img1.shape:
raise ValueError(f"img1 and img2 must have the same shape; {img1.shape} vs {img2.shape}")
mask = diagonally_truncated_mask(img1.shape, x_threshold, angle)
result = img2.copy()
result[mask] = img1[mask]
if gap > 0:
rr, cc, val = weighted_line(0, int(x_threshold * img1.shape[1] - (angle * img1.shape[0] / 2)), img1.shape[0]-1, int(x_threshold * img1.shape[1] + (angle * img1.shape[0] / 2)), gap)
result[rr, cc, :] = result[rr, cc, :] * (1 - val[...,np.newaxis]) + val[...,np.newaxis] * color
return result
def diagonally_combine_images(images, x_thresholds, angle, gap=0, color=1):
result = images[0]
for img, thres in zip(images[1:], x_thresholds):
result = diagonally_combine_two_images(result, img, thres, angle, gap, color)
return result
def write_image_pillow(img_file, img, quality):
img_array = (np.clip(img, 0.0, 1.0) * 255.0 + 0.5).astype(np.uint8)
im = PIL.Image.fromarray(img_array)
if os.path.splitext(img_file)[1] == ".jpg":
im = im.convert("RGB") # Bake the alpha channel
im.save(img_file, quality=quality, subsampling=0)
def read_image_pillow(img_file):
img = PIL.Image.open(img_file, "r")
if os.path.splitext(img_file)[1] == ".jpg":
img = img.convert("RGB")
else:
img = img.convert("RGBA")
img = np.asarray(img).astype(np.float32)
return img / 255.0
def srgb_to_linear(img):
limit = 0.04045
return np.where(img > limit, np.power((img + 0.055) / 1.055, 2.4), img / 12.92)
def linear_to_srgb(img):
limit = 0.0031308
return np.where(img > limit, 1.055 * (img ** (1.0 / 2.4)) - 0.055, 12.92 * img)
def read_image(file):
if os.path.splitext(file)[1] == ".exr":
img = exr.read(file).astype(np.float32)
elif os.path.splitext(file)[1] == ".bin":
with open(file, "rb") as f:
bytes = f.read()
h, w = struct.unpack("ii", bytes[:8])
img = np.frombuffer(bytes, dtype=np.float16, count=h*w*4, offset=8).astype(np.float32).reshape([h, w, 4])
else:
img = read_image_pillow(file)
if img.shape[2] == 4:
img[...,0:3] = srgb_to_linear(img[...,0:3])
# Premultiply alpha
img[...,0:3] *= img[...,3:4]
else:
img = srgb_to_linear(img)
return img
def write_image(file, img, quality=95):
if os.path.splitext(file)[1] == ".exr":
img = exr.write(file, img)
elif os.path.splitext(file)[1] == ".bin":
if img.shape[2] < 4:
img = np.dstack((img, np.ones([img.shape[0], img.shape[1], 4 - img.shape[2]])))
with open(file, "wb") as f:
f.write(struct.pack("ii", img.shape[0], img.shape[1]))
f.write(img.astype(np.float16).tobytes())
else:
if img.shape[2] == 4:
img = np.copy(img)
# Unmultiply alpha
img[...,0:3] = np.divide(img[...,0:3], img[...,3:4], out=np.zeros_like(img[...,0:3]), where=img[...,3:4] != 0)
img[...,0:3] = linear_to_srgb(img[...,0:3])
else:
img = linear_to_srgb(img)
write_image_pillow(file, img, quality)
def write_image_gamma(file, img, gamma, quality=95):
if os.path.splitext(file)[1] == ".exr":
img = exr.write(file, img)
else:
img = img**(1.0/gamma) # this will break alpha channels
write_image_pillow(file, img, quality)
def trim(error, skip=0.000001):
error = np.sort(error.flatten())
size = error.size
skip = int(skip * size)
return error[skip:size-skip].mean()
def luminance(a):
a = np.maximum(0, a)**0.4545454545
return 0.2126 * a[:,:,0] + 0.7152 * a[:,:,1] + 0.0722 * a[:,:,2]
def SSIM(a, b):
def blur(a):
k = np.array([0.120078, 0.233881, 0.292082, 0.233881, 0.120078])
x = convolve1d(a, k, axis=0)
return convolve1d(x, k, axis=1)
a = luminance(a)
b = luminance(b)
mA = blur(a)
mB = blur(b)
sA = blur(a*a) - mA**2
sB = blur(b*b) - mB**2
sAB = blur(a*b) - mA*mB
c1 = 0.01**2
c2 = 0.03**2
p1 = (2.0*mA*mB + c1)/(mA*mA + mB*mB + c1)
p2 = (2.0*sAB + c2)/(sA + sB + c2)
error = p1 * p2
return error
def L1(img, ref):
return np.abs(img - ref)
def APE(img, ref):
return L1(img, ref) / (1e-2 + ref)
def SAPE(img, ref):
return L1(img, ref) / (1e-2 + (ref + img) / 2.)
def L2(img, ref):
return (img - ref)**2
def RSE(img, ref):
return L2(img, ref) / (1e-2 + ref**2)
def rgb_mean(img):
return np.mean(img, axis=2)
def compute_error_img(metric, img, ref):
img[np.logical_not(np.isfinite(img))] = 0
img = np.maximum(img, 0.)
if metric == "MAE":
return L1(img, ref)
elif metric == "MAPE":
return APE(img, ref)
elif metric == "SMAPE":
return SAPE(img, ref)
elif metric == "MSE":
return L2(img, ref)
elif metric == "MScE":
return L2(np.clip(img, 0.0, 1.0), np.clip(ref, 0.0, 1.0))
elif metric == "MRSE":
return RSE(img, ref)
elif metric == "MtRSE":
return trim(RSE(img, ref))
elif metric == "MRScE":
return RSE(np.clip(img, 0, 100), np.clip(ref, 0, 100))
elif metric == "SSIM":
return SSIM(np.clip(img, 0.0, 1.0), np.clip(ref, 0.0, 1.0))
elif metric in ["FLIP", "\FLIP"]:
# Set viewing conditions
monitor_distance = 0.7
monitor_width = 0.7
monitor_resolution_x = 3840
# Compute number of pixels per degree of visual angle
pixels_per_degree = monitor_distance * (monitor_resolution_x / monitor_width) * (np.pi / 180)
ref_srgb = np.clip(flip.color_space_transform(ref, "linrgb2srgb"), 0, 1)
img_srgb = np.clip(flip.color_space_transform(img, "linrgb2srgb"), 0, 1)
result = flip.compute_flip(flip.utils.HWCtoCHW(ref_srgb), flip.utils.HWCtoCHW(img_srgb), pixels_per_degree)
assert np.isfinite(result).all()
return flip.utils.CHWtoHWC(result)
raise ValueError(f"Unknown metric: {metric}.")
def compute_error(metric, img, ref, metric_map_filename=None):
metric_map = compute_error_img(metric, img, ref)
metric_map[np.logical_not(np.isfinite(metric_map))] = 0
if len(metric_map.shape) == 3:
metric_map = np.mean(metric_map, axis=2)
mean = np.mean(metric_map)
if metric_map_filename:
if not metric_map_filename.suffix.lower() == ".exr":
index_map = np.clip(255.0 * np.squeeze(metric_map), 0, 255)
metric_map = flip.utils.CHWtoHWC(flip.utils.index2color(index_map, flip.utils.get_magma_map()))
exr.write(data=metric_map, filename=str(metric_map_filename))
return mean