-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
72 lines (55 loc) · 1.85 KB
/
utils.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
import re
import numpy as np
import config
def compute_npx_error(prediction, gt, n):
# computing n-px error
true_disp = gt
index = np.argwhere(true_disp > 0).T
gt[index[0][:], index[1][:], index[2][:]] = np.abs(
true_disp[index[0][:], index[1][:], index[2][:]] - prediction[index[0][:], index[1][:], index[2][:]])
correct = (gt[index[0][:], index[1][:], index[2][:]] < n) | \
(gt[index[0][:], index[1][:], index[2][:]] < true_disp[index[0][:], index[1][:], index[2][:]] * 0.05)
return 1 - (float(np.sum(correct)) / float(len(index[0])))
def readPFM(file):
if not isinstance(file, str):
file = file.numpy().decode()
file = open(file, 'rb')
color = None
width = None
height = None
scale = None
endian = None
header = file.readline().rstrip().decode()
if header == 'PF':
color = True
elif header == 'Pf':
color = False
else:
raise Exception('Not a PFM file.')
dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline().decode())
if dim_match:
width, height = map(int, dim_match.groups())
else:
raise Exception('Malformed PFM header.')
scale = float(file.readline().rstrip().decode())
if scale < 0: # little-endian
endian = '<'
scale = -scale
else:
endian = '>' # big-endian
data = np.fromfile(file, endian + 'f')
shape = (height, width, 3) if color else (height, width)
data = np.reshape(data, shape)
data = np.flipud(data)
# 加入maxdisp限制
data[data > (config.MAX_DISP - 1)] = config.MAX_DISP - 1
return data
def mean_std(inputs):
inputs = np.float32(inputs) / 255.
inputs[:, :, 0] -= 0.485
inputs[:, :, 0] /= 0.229
inputs[:, :, 1] -= 0.456
inputs[:, :, 1] /= 0.224
inputs[:, :, 2] -= 0.406
inputs[:, :, 2] /= 0.225
return inputs