-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_mask.py
50 lines (41 loc) · 1.29 KB
/
random_mask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import torch
import torch as th
import torch.nn as nn
import torch.nn.functional as F
import random
import concurrent.futures
import sys, os
sys.path.append(os.path.dirname(__file__))
def random_mask(x: th.Tensor) -> th.Tensor:
'''
x: [channels, num_frames, time_frames]
'''
C, F, T = x.shape
masked = random.randint(0, C // 8)
for i in range(masked):
idx = random.randint(0, C - 1)
x[idx,...] = 0.0
return x
def random_mask_by_batch(x: th.Tensor) -> th.Tensor:
'''
x: [batch, channels, num_frames, time_frames]
'''
B, _, _, _ = x.shape
mask = th.ones_like(x)
ret = []
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:
future_to_mask = {executor.submit(random_mask, mask[i,...]): i for i in range(B)}
for future in concurrent.futures.as_completed(future_to_mask):
m = future_to_mask[future]
try:
data = future.result()
ret.append(data)
except Exception as exc:
print('%r generated an exception: %s' % (m, exc))
return th.stack(ret, dim=0) * x
def test(x):
print("after", random_mask_by_batch(x))
if __name__=="__main__":
x = th.rand([2, 8, 1, 1])
print("before", x)
test(x)