Merge pull request #113 from magehrig/device-optim

Create tensors on device
This commit is contained in:
Zach Teed 2021-10-13 13:19:19 -05:00 committed by GitHub
commit aac9dd5472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 7 deletions

View File

@ -34,9 +34,9 @@ class CorrBlock:
out_pyramid = [] out_pyramid = []
for i in range(self.num_levels): for i in range(self.num_levels):
corr = self.corr_pyramid[i] corr = self.corr_pyramid[i]
dx = torch.linspace(-r, r, 2*r+1) dx = torch.linspace(-r, r, 2*r+1, device=coords.device)
dy = torch.linspace(-r, r, 2*r+1) dy = torch.linspace(-r, r, 2*r+1, device=coords.device)
delta = torch.stack(torch.meshgrid(dy, dx), axis=-1).to(coords.device) delta = torch.stack(torch.meshgrid(dy, dx), axis=-1)
centroid_lvl = coords.reshape(batch*h1*w1, 1, 1, 2) / 2**i centroid_lvl = coords.reshape(batch*h1*w1, 1, 1, 2) / 2**i
delta_lvl = delta.view(1, 2*r+1, 2*r+1, 2) delta_lvl = delta.view(1, 2*r+1, 2*r+1, 2)

View File

@ -63,8 +63,8 @@ class RAFT(nn.Module):
def initialize_flow(self, img): def initialize_flow(self, img):
""" Flow is represented as difference between two coordinate grids flow = coords1 - coords0""" """ Flow is represented as difference between two coordinate grids flow = coords1 - coords0"""
N, C, H, W = img.shape N, C, H, W = img.shape
coords0 = coords_grid(N, H//8, W//8).to(img.device) coords0 = coords_grid(N, H//8, W//8, device=img.device)
coords1 = coords_grid(N, H//8, W//8).to(img.device) coords1 = coords_grid(N, H//8, W//8, device=img.device)
# optical flow computed as difference: flow = coords1 - coords0 # optical flow computed as difference: flow = coords1 - coords0
return coords0, coords1 return coords0, coords1

View File

@ -71,8 +71,8 @@ def bilinear_sampler(img, coords, mode='bilinear', mask=False):
return img return img
def coords_grid(batch, ht, wd): def coords_grid(batch, ht, wd, device):
coords = torch.meshgrid(torch.arange(ht), torch.arange(wd)) coords = torch.meshgrid(torch.arange(ht, device=device), torch.arange(wd, device=device))
coords = torch.stack(coords[::-1], dim=0).float() coords = torch.stack(coords[::-1], dim=0).float()
return coords[None].repeat(batch, 1, 1, 1) return coords[None].repeat(batch, 1, 1, 1)