-
Notifications
You must be signed in to change notification settings - Fork 0
/
mix.py
148 lines (110 loc) · 4.19 KB
/
mix.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import torch
import numpy as np
from .models import get_resnet18_modified
from abc import ABC, abstractmethod
class MixMethod(ABC):
def __init__(self, alpha : float, device: str):
self.alpha = alpha
self.device = device
@abstractmethod
def mix_data(self, x, y):
pass
def set_device(self, device):
self.device = device
class Mixup(MixMethod):
def mix_data(self, x, y):
'''Returns mixed inputs, pairs of targets, and lambda'''
if self.alpha > 0:
lam = np.random.beta(self.alpha, self.alpha)
else:
lam = 1
batch_size = x.size()[0]
index = torch.randperm(batch_size).to(self.device)
mixed_x = lam * x + (1 - lam) * x[index, :]
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lam
class CutMix(MixMethod):
def rand_bbox(self, size, lam):
W = size[2]
H = size[3]
cut_rat = np.sqrt(1. - lam)
cut_w = np.int(W * cut_rat)
cut_h = np.int(H * cut_rat)
cx = np.random.randint(W)
cy = np.random.randint(H)
bbx1 = np.clip(cx - cut_w // 2, 0, W)
bby1 = np.clip(cy - cut_h // 2, 0, H)
bbx2 = np.clip(cx + cut_w // 2, 0, W)
bby2 = np.clip(cy + cut_h // 2, 0, H)
return bbx1, bby1, bbx2, bby2
def mix_data(self, x, y):
if self.alpha > 0:
lam = np.random.beta(self.alpha, self.alpha)
else:
lam = 1
rand_index = torch.randperm(x.size()[0]).to(self.device)
target_a = y
target_b = y[rand_index]
bbx1, bby1, bbx2, bby2 = self.rand_bbox(x.size(), lam)
x[:, :, bbx1:bbx2, bby1:bby2] = x[rand_index, :, bbx1:bbx2, bby1:bby2]
# adjust lambda to exactly match pixel ratio
lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (x.size()[-1] * x.size()[-2]))
return x, target_a, target_b, lam
class Segmix(MixMethod):
def __init__(self, segmodel, alpha, device):
super().__init__(alpha, device)
self.segmodel = segmodel
@abstractmethod
def mix_data(self, x, y):
pass
class Cifar10Segmix(Segmix):
def attention(self, x):
return torch.sigmoid(torch.logsumexp(x, 1, keepdim=True))
def get_segmented_images(self, x):
preds = self.segmodel(x.to(self.device))
attn = self.attention(preds)
attn = torch.cat((attn, attn, attn), dim=1)
attn[attn < 0.3] = 0.0
attn[attn >= 0.3] = 1.0
x = x.to(self.device) * attn.to(self.device)
return x
def mix_data(self, x, y):
'''Returns mixed inputs, pairs of targets, and lambda'''
if self.alpha > 0:
lam = np.random.beta(self.alpha, self.alpha)
else:
lam = 1
batch_size = x.size()[0]
index = torch.randperm(batch_size).to(self.device)
x2 = self.get_segmented_images(x[index, :])
mixed_x = lam * x.to(self.device) + (1 - lam) * x2.to(self.device)
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lam
class Cifar10DoubleSegmix(Segmix):
def attention(self, x):
return torch.sigmoid(torch.logsumexp(x, 1, keepdim=True))
def get_segmented_images(self, x):
preds = self.segmodel(x.to(self.device))
attn = self.attention(preds)
attn = torch.cat((attn, attn, attn), dim=1)
attn[attn < 0.3] = 0.0
attn[attn >= 0.3] = 1.0
x = x.to(self.device) * attn.to(self.device)
return x
def mix_data(self, x, y):
'''Returns mixed inputs, pairs of targets, and lambda'''
if self.alpha > 0:
lam = np.random.beta(self.alpha, self.alpha)
else:
lam = 1.
batch_size = x.size()[0]
index = torch.randperm(batch_size).to(self.device)
x3 = self.get_segmented_images(x)
x2 = x3[index, :]
x4 = x[index]
mixed1 = lam * x.to(self.device) + (1 - lam) * x2.to(self.device)
mixed2 = lam * x3.to(self.device) + (1 - lam) * x4.to(self.device)
y_a, y_b = y, y[index]
return mixed1, mixed2, y_a, y_b, lam
def mixup_criterion(criterion, pred, y_a, y_b, lam):
return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)