Skip to content

Instantly share code, notes, and snippets.

View alexandre01's full-sized avatar
🚀
Building

Alexandre Carlier alexandre01

🚀
Building
View GitHub Profile
@alexandre01
alexandre01 / random_mask_generation.py
Created December 21, 2019 14:37
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)