forked from lRomul/argus-freesound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixers.py
78 lines (58 loc) · 2.31 KB
/
mixers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import torch
import random
import numpy as np
def get_random_sample(dataset):
rnd_idx = random.randint(0, len(dataset) - 1)
rnd_image = dataset.images_lst[rnd_idx].copy()
rnd_target = dataset.targets_lst[rnd_idx].clone()
rnd_image = dataset.transform(rnd_image)
return rnd_image, rnd_target
class AddMixer:
def __init__(self, alpha_dist='uniform'):
assert alpha_dist in ['uniform', 'beta']
self.alpha_dist = alpha_dist
def sample_alpha(self):
if self.alpha_dist == 'uniform':
return random.uniform(0, 0.5)
elif self.alpha_dist == 'beta':
return np.random.beta(0.4, 0.4)
def __call__(self, dataset, image, target):
rnd_image, rnd_target = get_random_sample(dataset)
alpha = self.sample_alpha()
image = (1 - alpha) * image + alpha * rnd_image
target = (1 - alpha) * target + alpha * rnd_target
return image, target
class SigmoidConcatMixer:
def __init__(self, sigmoid_range=(3, 12)):
self.sigmoid_range = sigmoid_range
def sample_mask(self, size):
x_radius = random.randint(*self.sigmoid_range)
step = (x_radius * 2) / size[1]
x = np.arange(-x_radius, x_radius, step=step)
y = torch.sigmoid(torch.from_numpy(x)).numpy()
mix_mask = np.tile(y, (size[0], 1))
return torch.from_numpy(mix_mask.astype(np.float32))
def __call__(self, dataset, image, target):
rnd_image, rnd_target = get_random_sample(dataset)
mix_mask = self.sample_mask(image.shape[-2:])
rnd_mix_mask = 1 - mix_mask
image = mix_mask * image + rnd_mix_mask * rnd_image
target = target + rnd_target
target = np.clip(target, 0.0, 1.0)
return image, target
class RandomMixer:
def __init__(self, mixers, p=None):
self.mixers = mixers
self.p = p
def __call__(self, dataset, image, target):
mixer = np.random.choice(self.mixers, p=self.p)
image, target = mixer(dataset, image, target)
return image, target
class UseMixerWithProb:
def __init__(self, mixer, prob=.5):
self.mixer = mixer
self.prob = prob
def __call__(self, dataset, image, target):
if random.random() < self.prob:
return self.mixer(dataset, image, target)
return image, target