-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.py
294 lines (233 loc) · 10.4 KB
/
modules.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import torch
import functools
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data.sampler import Sampler
from unet import UNet
from utils import print_msg, one_hot
def generate_model(device, pretrained=True, checkpoint=None, args={}):
if pretrained:
model = torch.hub.load('milesial/Pytorch-UNet', 'unet_carvana')
else:
model = UNet(n_channels=3, n_classes=3, bilinear=False)
discriminator = NLayerDiscriminator(3)
if checkpoint:
weights = torch.load(checkpoint)
model.load_state_dict(weights, strict=True)
print_msg('Load weights form {}'.format(checkpoint))
if device == 'cuda':
model = model.cuda()
discriminator = discriminator.cuda()
if device == 'cuda' and torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
# discriminator = nn.DataParallel(discriminator)
return model, discriminator
# from patchGAN implementation
class NLayerDiscriminator(nn.Module):
"""Defines a PatchGAN discriminator"""
def __init__(self, input_nc, ndf=64, n_layers=3, norm_layer=nn.BatchNorm2d):
"""Construct a PatchGAN discriminator
Parameters:
input_nc (int) -- the number of channels in input images
ndf (int) -- the number of filters in the last conv layer
n_layers (int) -- the number of conv layers in the discriminator
norm_layer -- normalization layer
"""
super(NLayerDiscriminator, self).__init__()
if type(norm_layer) == functools.partial: # no need to use bias as BatchNorm2d has affine parameters
use_bias = norm_layer.func == nn.InstanceNorm2d
else:
use_bias = norm_layer == nn.InstanceNorm2d
kw = 4
padw = 1
sequence = [nn.Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw), nn.LeakyReLU(0.2, True)]
nf_mult = 1
nf_mult_prev = 1
for n in range(1, n_layers): # gradually increase the number of filters
nf_mult_prev = nf_mult
nf_mult = min(2 ** n, 8)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=2, padding=padw, bias=use_bias),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True)
]
nf_mult_prev = nf_mult
nf_mult = min(2 ** n_layers, 8)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=1, padding=padw, bias=use_bias),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True)
]
sequence += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)] # output 1 channel prediction map
self.model = nn.Sequential(*sequence)
def forward(self, input):
"""Standard forward."""
out = self.model(input)
# out = torch.sigmoid(out)
return out
# https://github.com/kornia/kornia
class FocalLoss(nn.Module):
def __init__(self, alpha, gamma=2.0, reduction='none'):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.reduction = reduction
self.eps = 1e-6
def forward(self, input, target):
return focal_loss(input, target, self.alpha, self.gamma, self.reduction, self.eps)
def focal_loss(input, target, alpha, gamma=2.0, reduction='none', eps=1e-8):
if not torch.is_tensor(input):
raise TypeError("Input type is not a torch.Tensor. Got {}"
.format(type(input)))
if not len(input.shape) >= 2:
raise ValueError("Invalid input shape, we expect BxCx*. Got: {}"
.format(input.shape))
if input.size(0) != target.size(0):
raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
.format(input.size(0), target.size(0)))
n = input.size(0)
out_size = (n,) + input.size()[2:]
if target.size()[1:] != input.size()[2:]:
raise ValueError('Expected target size {}, got {}'.format(
out_size, target.size()))
if not input.device == target.device:
raise ValueError(
"input and target must be in the same device. Got: {} and {}" .format(
input.device, target.device))
# compute softmax over the classes axis
input_soft: torch.Tensor = F.softmax(input, dim=1) + eps
# create the labels one hot tensor
target_one_hot: torch.Tensor = one_hot(
target, num_classes=input.shape[1],
device=input.device, dtype=input.dtype)
# compute the actual focal loss
weight = torch.pow(-input_soft + 1., gamma)
focal = -alpha * weight * torch.log(input_soft)
loss_tmp = torch.sum(target_one_hot * focal, dim=1)
if reduction == 'none':
loss = loss_tmp
elif reduction == 'mean':
loss = torch.mean(loss_tmp)
elif reduction == 'sum':
loss = torch.sum(loss_tmp)
else:
raise NotImplementedError("Invalid reduction mode: {}"
.format(reduction))
return loss
class WarmupLRScheduler():
def __init__(self, optimizer, warmup_epochs, initial_lr):
self.epoch = 0
self.optimizer = optimizer
self.warmup_epochs = warmup_epochs
self.initial_lr = initial_lr
def step(self):
if self.epoch <= self.warmup_epochs:
self.epoch += 1
curr_lr = (self.epoch / self.warmup_epochs) * self.initial_lr
for param_group in self.optimizer.param_groups:
param_group['lr'] = curr_lr
def is_finish(self):
return self.epoch >= self.warmup_epochs
class ScheduledWeightedSampler(Sampler):
def __init__(self, dataset, decay_rate):
self.dataset = dataset
self.decay_rate = decay_rate
self.num_samples = len(dataset)
self.targets = [sample[1] for sample in dataset.imgs]
self.class_weights = self.cal_class_weights(self.targets)
self.epoch = 0
self.w0 = torch.as_tensor(self.class_weights, dtype=torch.double)
self.wf = torch.as_tensor([1] * len(self.dataset.classes), dtype=torch.double)
self.sample_weight = torch.zeros(self.num_samples, dtype=torch.double)
for i, _class in enumerate(self.targets):
self.sample_weight[i] = self.w0[_class]
def step(self):
if self.decay_rate < 1:
self.epoch += 1
factor = self.decay_rate**(self.epoch - 1)
self.weights = factor * self.w0 + (1 - factor) * self.wf
for i, _class in enumerate(self.targets):
self.sample_weight[i] = self.weights[_class]
def __iter__(self):
return iter(torch.multinomial(self.sample_weight, self.num_samples, replacement=True).tolist())
def __len__(self):
return self.num_samples
def cal_class_weights(self, train_targets):
num_classes = len(self.dataset.classes)
classes_idx = list(range(num_classes))
class_count = [self.targets.count(i) for i in classes_idx]
weights = [self.num_samples / class_count[i] for i in classes_idx]
min_weight = min(weights)
class_weights = [weights[i] / min_weight for i in classes_idx]
return class_weights
class LossWeightsScheduler():
def __init__(self, dataset, decay_rate):
self.dataset = dataset
self.decay_rate = decay_rate
self.num_samples = len(dataset)
self.targets = [sample[1] for sample in dataset.imgs]
self.class_weights = self.cal_class_weights(self.targets)
self.epoch = 0
self.w0 = torch.as_tensor(self.class_weights, dtype=torch.float32)
self.wf = torch.as_tensor([1] * len(self.dataset.classes), dtype=torch.float32)
def step(self):
weights = self.w0
if self.decay_rate < 1:
self.epoch += 1
factor = self.decay_rate**(self.epoch - 1)
weights = factor * self.w0 + (1 - factor) * self.wf
return weights
def __len__(self):
return self.num_samples
def cal_class_weights(self, train_targets):
num_classes = len(self.dataset.classes)
classes_idx = list(range(num_classes))
class_count = [self.targets.count(i) for i in classes_idx]
weights = [self.num_samples / class_count[i] for i in classes_idx]
min_weight = min(weights)
class_weights = [weights[i] / min_weight for i in classes_idx]
return class_weights
class ClippedCosineAnnealingLR():
def __init__(self, optimizer, T_max, min_lr):
self.optimizer = optimizer
self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=T_max)
self.min_lr = min_lr
self.finish = False
def step(self):
if not self.finish:
self.scheduler.step()
curr_lr = self.optimizer.param_groups[0]['lr']
if curr_lr < self.min_lr:
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.min_lr
self.finish = True
def is_finish(self):
return self.finish
# http://defauw.ai/diabetic-retinopathy-detection/
class KappaLoss(nn.Module):
def __init__(self, num_classes, y_pow=2, eps=1e-10):
super(KappaLoss, self).__init__()
self.num_classes = num_classes
self.y_pow = y_pow
self.eps = eps
def kappa_loss(self, y_pred, y_true):
num_classes = self.num_classes
y = torch.eye(num_classes).cuda()
y_true = y[y_true]
y_true = y_true.float()
repeat_op = torch.Tensor(list(range(num_classes))).unsqueeze(1).repeat((1, num_classes)).cuda()
repeat_op_sq = torch.square((repeat_op - repeat_op.T))
weights = repeat_op_sq / ((num_classes - 1) ** 2)
pred_ = y_pred ** self.y_pow
pred_norm = pred_ / (self.eps + torch.reshape(torch.sum(pred_, 1), [-1, 1]))
hist_rater_a = torch.sum(pred_norm, 0)
hist_rater_b = torch.sum(y_true, 0)
conf_mat = torch.matmul(pred_norm.T, y_true)
bsize = y_pred.size(0)
nom = torch.sum(weights * conf_mat)
expected_probs = torch.matmul(torch.reshape(hist_rater_a, [num_classes, 1]), torch.reshape(hist_rater_b, [1, num_classes]))
denom = torch.sum(weights * expected_probs / bsize)
return nom / (denom + self.eps)
def forward(self, y_pred, y_true):
return self.kappa_loss(y_pred, y_true)