Input: -img (N, C, H, W) (flow) -shiftx, shifty (N, c, H, W)
(img, shiftx, shifty, weight)
| 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 |
no outgoing calls
no test coverage detected