-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (46 loc) · 1.74 KB
/
Copy pathutils.py
File metadata and controls
58 lines (46 loc) · 1.74 KB
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
import os
import torch
import torch.nn as nn
import time
import numpy as np
TAG_CHAR = np.array([202021.25], np.float32)
def load_flow(path):
with open(path, 'rb') as f:
magic = float(np.fromfile(f, np.float32, count = 1)[0])
if magic == 202021.25:
w, h = np.fromfile(f, np.int32, count = 1)[0], np.fromfile(f, np.int32, count = 1)[0]
data = np.fromfile(f, np.float32, count = h*w*2)
data.resize((h, w, 2))
return data
return None
def tic():
global startTime_for_tictoc
startTime_for_tictoc = time.time()
def toc():
if 'startTime_for_tictoc' in globals():
print("Elapsed time is "+ str(time.time() - startTime_for_tictoc)+" seconds")
#str(time.time() - startTime_for tictoc)
else:
print("Toc: start time not set")
def warp(x,flo, return_mask=True):
B, C, H, W = x.size()
# mesh grid
xx = torch.arange(0, W).view(1, 1, 1, W).expand(B, 1, H, W)
yy = torch.arange(0, H).view(1, 1, H, 1).expand(B, 1, H, W)
grid = torch.cat((xx, yy), 1).float()
if x.is_cuda:
grid = grid.to(x.device)
vgrid = torch.autograd.Variable(grid) + flo
# scale grid to [-1,1]
vgrid[:, 0, :, :] = 2.0 * vgrid[:, 0, :, :] / max(W - 1, 1) - 1.0
vgrid[:, 1, :, :] = 2.0 * vgrid[:, 1, :, :] / max(H - 1, 1) - 1.0
vgrid = vgrid.permute(0, 2, 3, 1)
output = nn.functional.grid_sample(x, vgrid, align_corners=True)
mask = torch.autograd.Variable(torch.ones(x.size())).to(x.device)
mask = nn.functional.grid_sample(mask, vgrid, align_corners=True)
mask = mask.masked_fill_(mask < 0.999, 0)
mask = mask.masked_fill_(mask > 0, 1)
if return_mask:
return output * mask, mask
else:
return output * mask