Created
December 21, 2019 14:37
-
-
Save alexandre01/7d4a03f2ba21e57a676a491782933dfc to your computer and use it in GitHub Desktop.
Tensor operation-only random mask generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by François Fleuret
Source: https://twitter.com/francoisfleuret/status/1208342223340875776?s=20