-
Notifications
You must be signed in to change notification settings - Fork 2
/
fuse_utils.py
292 lines (246 loc) · 11.9 KB
/
fuse_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
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
import torch, torchvision
import torch.nn.functional as F
from PIL import Image
import numpy as np
import os, cv2
# Disp_type = '' # the ground-truth disparity format is disparity*1
Disp_type = 'x4' # the ground-truth disparity format is disparity*4
def train(model, optimizer, costs_input, img, disp, args, Test=False):
if not Test:
model.train()
optimizer.zero_grad()
else:
model.eval()
costs = []
if args.cuda:
img, disp = img.cuda(), disp.cuda()
for cost in costs_input:
costs.append(cost.cuda())
output = model(costs)
output = output.squeeze(1)
mask = (disp != 0)
loss = F.smooth_l1_loss(output[mask], disp[mask], reduction='mean')
if not Test:
loss.backward()
optimizer.step()
cv2.imwrite(args.output_path + 'train/img.png',
255*img[0].permute([1,2,0]).detach().cpu().numpy())
cv2.imwrite(args.output_path + 'train/outdisp.png',
output[0].detach().cpu().numpy())
return loss.data.item()
def test(model, args, epoch):
test_record = []
for scene in os.listdir(args.testpath):
left_path = args.testpath + scene + '/' + 'left.bin'
right_path = args.testpath + scene + '/' + 'right.bin'
img_path = args.testpath + scene + '/' + 'view1.png'
disp_path = args.testpath + scene + '/' + 'disp1' + Disp_type + '.png'
d = args.maxdisp
img = Image.open(img_path)
w, h = img.size
img = np.array(img).transpose(2, 0, 1)
disp = Image.open(disp_path)
disp = np.expand_dims(np.array(disp), 0)
left_mem = np.memmap(left_path, dtype=np.float32, shape=(1, d, h, w))
right_mem = np.memmap(right_path, dtype=np.float32, shape=(1, d, h, w))
costL = np.squeeze(np.array(left_mem))
costR = np.squeeze(np.array(right_mem))
costL[np.isnan(costL)]=20
costR[np.isnan(costR)]=20
costL = torch.from_numpy(costL).unsqueeze(0).cuda()
costR = torch.from_numpy(costR).unsqueeze(0).cuda()
# pad to 16
pad_h = (h / 16 + (1 if h % 16 != 0 else 0)) * 16 - h
pad_w = (w / 16 + (1 if h % 16 != 0 else 0)) * 16 - w
costL = F.pad(costL, (0, pad_w, 0, pad_h))
costR = F.pad(costR, (0, pad_w, 0, pad_h))
# for large image, process piece by piece
SIZE = 512
Edge = 8
wseg = w // SIZE + 1 if( w % SIZE != 0) else 0
hseg = h // SIZE + 1 if( h % SIZE != 0) else 0
outdisp = torch.ones(1, h+pad_h, w+pad_w).float().cuda()
with torch.no_grad():
for i in range(hseg):
for j in range(wseg):
y1 = max(i*SIZE-Edge, 0)
y2 = (i+1)*SIZE + Edge
x1 = max(j*SIZE-Edge, 0)
x2 = (j+1)*SIZE + Edge
outdisp[:, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE] = \
model([costL[:, :, y1:y2, x1:x2], costR[:, :, y1:y2, x1:x2]]) \
[:,min(i,1)*Edge:min(i,1)*Edge+SIZE,min(j,1)*Edge:min(j,1)*Edge+SIZE]
outdisp = outdisp[:, :h, :w]
dispgt = torch.from_numpy(disp).float().cuda()
if(Disp_type == 'x4'): # the ground-truth is disparity*4
dispgt /= 4.0
mask = (dispgt != 0)
diff = torch.abs(outdisp[mask] - dispgt[mask])
avgerr = torch.mean(diff)
rms = torch.sqrt( (diff**2).mean() )
bad05 = len(diff[diff>0.5])/float(len(diff))
bad1 = len(diff[diff>1])/float(len(diff))
bad2 = len(diff[diff>2])/float(len(diff))
test_record.append([avgerr, rms, bad05, bad1, bad2])
cv2.imwrite(args.output_path + 'test/' + scene + "_outdisp.png",
outdisp.cpu().numpy().squeeze())
test_res = np.array(test_record).mean(0)
print('==== epoch %d test avgerr = %.3f, rms = %.3f, bad05 = %.3f, bad1 = %.3f, bad2 = %.3f ==='
% (epoch, test_res[0], test_res[1], test_res[2], test_res[3], test_res[4]) )
return test_res
def eval(model, args, epoch):
eval_record = []
for scene in os.listdir(args.evalpath):
left_path = args.evalpath + scene + '/' + 'left.bin'
right_path = args.evalpath + scene + '/' + 'right.bin'
img_path = args.evalpath + scene + '/' + 'view1.png'
d = args.maxdisp
img = Image.open(img_path)
w, h = img.size
left_mem = np.memmap(left_path, dtype=np.float32, shape=(1, d, h, w))
right_mem = np.memmap(right_path, dtype=np.float32, shape=(1, d, h, w))
costL = np.squeeze(np.array(left_mem))
costR = np.squeeze(np.array(right_mem))
costL[np.isnan(costL)]=20
costR[np.isnan(costR)]=20
costL = torch.from_numpy(costL).unsqueeze(0).cuda()
costR = torch.from_numpy(costR).unsqueeze(0).cuda()
# pad to 16
pad_h = (h / 16 + (1 if h % 16 != 0 else 0)) * 16 - h
pad_w = (w / 16 + (1 if h % 16 != 0 else 0)) * 16 - w
costL = F.pad(costL, (0, pad_w, 0, pad_h))
costR = F.pad(costR, (0, pad_w, 0, pad_h))
# for large image
SIZE = 512
wseg = w // SIZE + 1 if( w % SIZE != 0) else 0
hseg = w // SIZE + 1 if( h % SIZE != 0) else 0
outdisp = torch.ones(1, h+pad_h, w+pad_w).float().cuda()
with torch.no_grad():
for i in range(hseg):
for j in range(wseg):
outdisp[:, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE] = \
model([costL[:, :, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE],
costR[:, :, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE]])
outdisp = outdisp[:, :h, :w]
cv2.imwrite(args.output_path + 'eval/' + scene + "_outdisp.png",
outdisp.cpu().numpy().squeeze())
return eval_record
def test4(model, args, epoch):
# ====================== train data ========================
test_record = []
for scene in os.listdir(args.trainpath):
left_path = args.trainpath + scene + '/left.bin'
right_path = args.trainpath + scene + '/right.bin'
bottom_path = args.trainpath + scene + '/bottom.bin'
top_path = args.trainpath + scene + '/top.bin'
img_path = args.trainpath + scene + '/view1.png'
disp_path = args.trainpath + scene + '/disp1' + Disp_type + '.png'
d = args.maxdisp
img = Image.open(img_path)
w, h = img.size
img = np.array(img).transpose(2, 0, 1)
disp = Image.open(disp_path)
disp = np.expand_dims(np.array(disp), 0)
left_mem = np.memmap(left_path, dtype=np.float32, shape=(1, d, h, w))
right_mem = np.memmap(right_path, dtype=np.float32, shape=(1, d, h, w))
costL = np.squeeze(np.array(left_mem))
costR = np.squeeze(np.array(right_mem))
costL[np.isnan(costL)]=20
costR[np.isnan(costR)]=20
costL = torch.from_numpy(costL).unsqueeze(0).cuda()
costR = torch.from_numpy(costR).unsqueeze(0).cuda()
bottom = np.memmap(bottom_path, dtype=np.float32, shape=(1, d, w, h))
top = np.memmap(top_path, dtype=np.float32, shape=(1, d, w, h))
bottom=np.rot90(np.array(bottom), k=-1, axes=(2,3)).copy()
top=np.rot90(np.array(top), k=-1, axes=(2,3)).copy()
bottom[np.isnan(bottom)]=20
top[np.isnan(top)]=20
bottom = np.squeeze(bottom)
top = np.squeeze(top)
costB = torch.from_numpy(bottom).unsqueeze(0).cuda()
costT = torch.from_numpy(top).unsqueeze(0).cuda()
# for large image
SIZE = 512
wseg = w // SIZE + 1 if( w % SIZE != 0) else 0
hseg = w // SIZE + 1 if( h % SIZE != 0) else 0
outdisp = torch.ones(1, h, w).float().cuda()
with torch.no_grad():
for i in range(hseg):
for j in range(wseg):
outdisp[:, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE] = \
model([costL[:, :, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE],
costR[:, :, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE],
costB[:, :, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE],
costT[:, :, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE]])
outdisp = outdisp.squeeze(1)
dispgt = torch.from_numpy(disp).float().cuda()
if(Disp_type == 'x4'): # the ground-truth is disparity*4
dispgt /= 4.0
mask = (dispgt != 0)
diff = torch.abs(outdisp[mask] - dispgt[mask])
avgerr = torch.mean(diff)
rms = torch.sqrt( (diff**2).mean())
bad05 = len(diff[diff>0.5])/float(len(diff))
bad1 = len(diff[diff>1])/float(len(diff))
bad2 = len(diff[diff>2])/float(len(diff))
test_record.append([avgerr, rms, bad05, bad1, bad2])
cv2.imwrite(args.output_path + 'test/' + scene + "_outdisp.png",
outdisp.cpu().numpy().squeeze())
test_res = np.array(test_record).mean(0)
print('======= epoch %d avgerr = %.3f, rms = %.3f, bad05 = %.3f, bad1 = %.3f, bad2 = %.3f ======='
% (epoch, test_res[0], test_res[1], test_res[2], test_res[3], test_res[4]))
return test_res
def test1(model, args, epoch):
test_record = []
for scene in os.listdir(args.testpath):
right_path = args.testpath + scene + '/' + 'right.bin'
img_path = args.testpath + scene + '/' + 'view1.png'
disp_path = args.testpath + scene + '/' + 'disp1' + Disp_type + '.png'
d = args.maxdisp
img = Image.open(img_path)
w, h = img.size
img = np.array(img).transpose(2, 0, 1)
disp = Image.open(disp_path)
disp = np.expand_dims(np.array(disp), 0)
right_mem = np.memmap(right_path, dtype=np.float32, shape=(1, d, h, w))
costR = np.squeeze(np.array(right_mem))
costR[np.isnan(costR)]=20
costR = torch.from_numpy(costR).unsqueeze(0).cuda()
# pad to 16
pad_h = (h / 16 + (1 if h % 16 != 0 else 0)) * 16 - h
pad_w = (w / 16 + (1 if h % 16 != 0 else 0)) * 16 - w
costR = F.pad(costR, (0, pad_w, 0, pad_h))
# for large image
SIZE = 512
Edge = 8
wseg = w // SIZE + 1 if( w % SIZE != 0) else 0
hseg = h // SIZE + 1 if( h % SIZE != 0) else 0
outdisp = torch.ones(1, h+pad_h, w+pad_w).float().cuda()
with torch.no_grad():
for i in range(hseg):
for j in range(wseg):
y1 = max(i*SIZE-Edge, 0)
y2 = (i+1)*SIZE + Edge
x1 = max(j*SIZE-Edge, 0)
x2 = (j+1)*SIZE + Edge
outdisp[:, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE] = \
model([costR[:, :, y1:y2, x1:x2]]) \
[:,min(i,1)*Edge:min(i,1)*Edge+SIZE,min(j,1)*Edge:min(j,1)*Edge+SIZE]
outdisp = outdisp[:, :h, :w]
dispgt = torch.from_numpy(disp).float().cuda()
if(Disp_type == 'x4'): # the ground-truth is disparity*4
dispgt /= 4.0
mask = (dispgt != 0)
diff = torch.abs(outdisp[mask] - dispgt[mask])
avgerr = torch.mean(diff)
rms = torch.sqrt( (diff**2).mean() )
bad05 = len(diff[diff>0.5])/float(len(diff))
bad1 = len(diff[diff>1])/float(len(diff))
bad2 = len(diff[diff>2])/float(len(diff))
test_record.append([avgerr, rms, bad05, bad1, bad2])
cv2.imwrite(args.output_path + 'test/' + scene + "_outdisp.png",
outdisp.cpu().numpy().squeeze())
test_res = np.array(test_record).mean(0)
print('==== epoch %d test avgerr = %.3f, rms = %.3f, bad05 = %.3f, bad1 = %.3f, bad2 = %.3f ==='
% (epoch, test_res[0], test_res[1], test_res[2], test_res[3], test_res[4]) )
return test_res