MCPcopy Create free account
hub / github.com/drinkingcoder/NeuralMarker / pytorch_foward_interpolate

Function pytorch_foward_interpolate

core/utils/utils.py:26–108  ·  view source on GitHub ↗
(flow)

Source from the content-addressed store, hash-verified

24 return x[..., c[0]:c[1], c[2]:c[3]]
25
26def pytorch_foward_interpolate(flow):
27 def get_gaussian_weights(x, y, x1, x2, y1, y2):
28 w11 = torch.exp(-((x - x1)**2 + (y - y1)**2))
29 w12 = torch.exp(-((x - x1)**2 + (y - y2)**2))
30 w21 = torch.exp(-((x - x2)**2 + (y - y1)**2))
31 w22 = torch.exp(-((x - x2)**2 + (y - y2)**2))
32
33 return w11, w12, w21, w22
34
35 def sample_one(img, shiftx, shifty, weight):
36 """
37 Input:
38 -img (N, C, H, W) (flow)
39 -shiftx, shifty (N, c, H, W)
40 """
41
42 N, C, H, W = img.size()
43
44 # flatten all (all restored as Tensors)
45 flat_shiftx = shiftx.view(-1)
46 flat_shifty = shifty.view(-1)
47 flat_basex = torch.arange(0, H, requires_grad=False).view(-1, 1)[None, None].cuda().long().repeat(N, C, 1, W).view(-1)
48 flat_basey = torch.arange(0, W, requires_grad=False).view(1, -1)[None, None].cuda().long().repeat(N, C, H, 1).view(-1)
49 flat_weight = weight.view(-1)
50 flat_img = img.view(-1)
51
52 # The corresponding positions in I1
53 idxn = torch.arange(0, N, requires_grad=False).view(N, 1, 1, 1).long().cuda().repeat(1, C, H, W).view(-1)
54 idxc = torch.arange(0, C, requires_grad=False).view(1, C, 1, 1).long().cuda().repeat(N, 1, H, W).view(-1)
55 # ttype = flat_basex.type()
56 idxx = flat_shiftx.long() + flat_basex
57 idxy = flat_shifty.long() + flat_basey
58
59
60 # recording the inside part the shifted
61 mask = idxx.ge(0) & idxx.lt(H) & idxy.ge(0) & idxy.lt(W)
62
63 # Mask off points out of boundaries
64 ids = (idxn*C*H*W + idxc*H*W + idxx*W + idxy)
65 ids_mask = torch.masked_select(ids, mask).clone().cuda()
66
67 #(zero part - gt) -> difference
68 # difference back propagate -> No influence! Whether we do need mask? mask?
69 # put (add) them together
70 # Note here! accmulate fla must be true for proper bp
71 img_warp = torch.zeros([N*C*H*W, ]).cuda()
72 img_warp.put_(ids_mask, torch.masked_select(flat_img*flat_weight, mask), accumulate=True)
73
74 return img_warp.view(N, C, H, W)
75
76 """
77 flow should be a pytorch tensor
78 flow - Bx2xWxH
79 """
80 flow = flow.detach()
81 img = flow
82
83 N, C, ht, wd = flow.shape

Callers

nothing calls this directly

Calls 2

get_gaussian_weightsFunction · 0.85
sample_oneFunction · 0.85

Tested by

no test coverage detected