Skip to content

Instantly share code, notes, and snippets.

@alexandre01
Created December 21, 2019 14:37
Show Gist options
  • Save alexandre01/7d4a03f2ba21e57a676a491782933dfc to your computer and use it in GitHub Desktop.
Save alexandre01/7d4a03f2ba21e57a676a491782933dfc to your computer and use it in GitHub Desktop.
Tensor operation-only random mask generation
import torch
x = torch.randn(1, 3, 800, 1200)
n, _, h, w, = x.shape
rnd = torch.rand(2, n, 1, 2).sort(-1).values
r, c = torch.linspace(0, 1, h+2)[None, None], torch.linspace(0, 1, w+2)[None, None]
mask = (((r > rnd[0, :, :, :1]) & (r < rnd[0, :, :, 1:])).unsqueeze(-1) *
((c > rnd[1, :, :, :1]) & (c < rnd[1, :, :, 1:])).unsqueeze(-2))[:, :, 1:-1, 1:-1].expand_as(x)
@alexandre01
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment