|
4 | 4 | import time |
5 | 5 |
|
6 | 6 | import torch |
| 7 | +import torch.nn.functional as F |
7 | 8 | import torch.nn.parallel |
8 | 9 | import torch.backends.cudnn as cudnn |
9 | 10 | import torch.optim |
|
19 | 20 |
|
20 | 21 | model_names = sorted(name for name in models.__dict__ |
21 | 22 | if name.islower() and not name.startswith("__")) |
22 | | - |
23 | 23 | dataset_names = sorted(name for name in datasets.__all__) |
24 | 24 |
|
25 | 25 |
|
|
82 | 82 |
|
83 | 83 | best_EPE = -1 |
84 | 84 | n_iter = 0 |
| 85 | +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
85 | 86 |
|
86 | 87 |
|
87 | 88 | def main(): |
@@ -193,7 +194,8 @@ def main(): |
193 | 194 |
|
194 | 195 | # evaluate on validation set |
195 | 196 |
|
196 | | - EPE = validate(val_loader, model, epoch, output_writers) |
| 197 | + with torch.no_grad(): |
| 198 | + EPE = validate(val_loader, model, epoch, output_writers) |
197 | 199 | test_writer.add_scalar('mean EPE', EPE, epoch) |
198 | 200 |
|
199 | 201 | if best_EPE < 0: |
@@ -227,25 +229,23 @@ def train(train_loader, model, optimizer, epoch, train_writer): |
227 | 229 | for i, (input, target) in enumerate(train_loader): |
228 | 230 | # measure data loading time |
229 | 231 | data_time.update(time.time() - end) |
230 | | - target = target.cuda(async=True) |
231 | | - input = [j.cuda() for j in input] |
232 | | - input_var = torch.autograd.Variable(torch.cat(input,1)) |
233 | | - target_var = torch.autograd.Variable(target) |
| 232 | + target = target.to(device) |
| 233 | + input = torch.cat(input,1).to(device) |
234 | 234 |
|
235 | 235 | # compute output |
236 | | - output = model(input_var) |
| 236 | + output = model(input) |
237 | 237 | if args.sparse: |
238 | 238 | # Since Target pooling is not very precise when sparse, |
239 | 239 | # take the highest resolution prediction and upsample it instead of downsampling target |
240 | | - h, w = target_var.size()[-2:] |
241 | | - output = [torch.nn.functional.upsample(output[0], (h,w)), *output[1:]] |
| 240 | + h, w = target.size()[-2:] |
| 241 | + output = [F.interpolate(output[0], (h,w)), *output[1:]] |
242 | 242 |
|
243 | | - loss = multiscaleEPE(output, target_var, weights=args.multiscale_weights, sparse=args.sparse) |
244 | | - flow2_EPE = args.div_flow * realEPE(output[0], target_var, sparse=args.sparse) |
| 243 | + loss = multiscaleEPE(output, target, weights=args.multiscale_weights, sparse=args.sparse) |
| 244 | + flow2_EPE = args.div_flow * realEPE(output[0], target, sparse=args.sparse) |
245 | 245 | # record loss and EPE |
246 | | - losses.update(loss.data[0], target.size(0)) |
247 | | - train_writer.add_scalar('train_loss', loss.data[0], n_iter) |
248 | | - flow2_EPEs.update(flow2_EPE.data[0], target.size(0)) |
| 246 | + losses.update(loss.item(), target.size(0)) |
| 247 | + train_writer.add_scalar('train_loss', loss.item(), n_iter) |
| 248 | + flow2_EPEs.update(flow2_EPE.item(), target.size(0)) |
249 | 249 |
|
250 | 250 | # compute gradient and do optimization step |
251 | 251 | optimizer.zero_grad() |
@@ -278,26 +278,26 @@ def validate(val_loader, model, epoch, output_writers): |
278 | 278 |
|
279 | 279 | end = time.time() |
280 | 280 | for i, (input, target) in enumerate(val_loader): |
281 | | - target = target.cuda(async=True) |
282 | | - input_var = torch.autograd.Variable(torch.cat(input,1).cuda(), volatile=True) |
283 | | - target_var = torch.autograd.Variable(target, volatile=True) |
| 281 | + target = target.to(device) |
| 282 | + input = torch.cat(input,1).to(device) |
284 | 283 |
|
285 | 284 | # compute output |
286 | | - output = model(input_var) |
287 | | - flow2_EPE = args.div_flow*realEPE(output, target_var, sparse=args.sparse) |
| 285 | + output = model(input) |
| 286 | + flow2_EPE = args.div_flow*realEPE(output, target, sparse=args.sparse) |
288 | 287 | # record EPE |
289 | | - flow2_EPEs.update(flow2_EPE.data[0], target.size(0)) |
| 288 | + flow2_EPEs.update(flow2_EPE.item(), target.size(0)) |
290 | 289 |
|
291 | 290 | # measure elapsed time |
292 | 291 | batch_time.update(time.time() - end) |
293 | 292 | end = time.time() |
294 | 293 |
|
295 | 294 | if i < len(output_writers): # log first output of first batches |
296 | 295 | if epoch == 0: |
297 | | - output_writers[i].add_image('GroundTruth', flow2rgb(args.div_flow * target[0].cpu().numpy(), max_value=10), 0) |
298 | | - output_writers[i].add_image('Inputs', input[0][0].numpy().transpose(1, 2, 0) + np.array([0.411,0.432,0.45]), 0) |
299 | | - output_writers[i].add_image('Inputs', input[1][0].numpy().transpose(1, 2, 0) + np.array([0.411,0.432,0.45]), 1) |
300 | | - output_writers[i].add_image('FlowNet Outputs', flow2rgb(args.div_flow * output.data[0].cpu().numpy(), max_value=10), epoch) |
| 296 | + mean_values = torch.tensor([0.411,0.432,0.45], dtype=input.dtype).view(3,1,1) |
| 297 | + output_writers[i].add_image('GroundTruth', flow2rgb(args.div_flow * target[0], max_value=10), 0) |
| 298 | + output_writers[i].add_image('Inputs', (input[0,:3].cpu() + mean_values).clamp(0,1), 0) |
| 299 | + output_writers[i].add_image('Inputs', (input[0,3:].cpu() + mean_values).clamp(0,1), 1) |
| 300 | + output_writers[i].add_image('FlowNet Outputs', flow2rgb(args.div_flow * output[0], max_value=10), epoch) |
301 | 301 |
|
302 | 302 | if i % args.print_freq == 0: |
303 | 303 | print('Test: [{0}/{1}]\t Time {2}\t EPE {3}' |
@@ -337,17 +337,17 @@ def __repr__(self): |
337 | 337 |
|
338 | 338 |
|
339 | 339 | def flow2rgb(flow_map, max_value): |
340 | | - global args |
341 | | - _, h, w = flow_map.shape |
342 | | - flow_map[:,(flow_map[0] == 0) & (flow_map[1] == 0)] = float('nan') |
343 | | - rgb_map = np.ones((h,w,3)).astype(np.float32) |
| 340 | + flow_map_np = flow_map.detach().cpu().numpy() |
| 341 | + _, h, w = flow_map_np.shape |
| 342 | + flow_map_np[:,(flow_map_np[0] == 0) & (flow_map_np[1] == 0)] = float('nan') |
| 343 | + rgb_map = np.ones((3,h,w)).astype(np.float32) |
344 | 344 | if max_value is not None: |
345 | | - normalized_flow_map = flow_map / max_value |
| 345 | + normalized_flow_map = flow_map_np / max_value |
346 | 346 | else: |
347 | | - normalized_flow_map = flow_map / (np.abs(flow_map).max()) |
348 | | - rgb_map[:,:,0] += normalized_flow_map[0] |
349 | | - rgb_map[:,:,1] -= 0.5*(normalized_flow_map[0] + normalized_flow_map[1]) |
350 | | - rgb_map[:,:,2] += normalized_flow_map[1] |
| 347 | + normalized_flow_map = flow_map_np / (np.abs(flow_map_np).max()) |
| 348 | + rgb_map[0] += normalized_flow_map[0] |
| 349 | + rgb_map[1] -= 0.5*(normalized_flow_map[0] + normalized_flow_map[1]) |
| 350 | + rgb_map[2] += normalized_flow_map[1] |
351 | 351 | return rgb_map.clip(0,1) |
352 | 352 |
|
353 | 353 |
|
|
0 commit comments