forked from wangg12/intel-cervical-cancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
378 lines (326 loc) · 13.4 KB
/
utils.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
''' python 3.4+
author: Gu Wang
'''
from __future__ import division, absolute_import, print_function
import numpy as np
import torch
from torch import nn
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
import torch.nn.init as init
import torchvision
import random
from torchsample.transforms import *
from data_utils.my_folder import MyImageFolder
from torchvision import transforms
from lr_scheduler import cosine_anneal_schedule, warmup_scheduler
def convert_to_one_hot(y, num_class):
return torch.zeros(y.size()[0], num_class).scatter_(1, y.unsqueeze(1), 1.)
class KaggleLogLoss(nn.Module):
"""kaggle log loss function.
-1/N sum(y*log(y_pred))
the submitted probabilities are divided by row_sum,
and then max(min(p, 1-1e-15), 1e-15), can be achieved by torch.clamp
TODO: not sure the order of these two operations
"""
def __init__(self):
super(KaggleLogLoss, self).__init__()
def forward(self, y_pred, y_true_one_hot):
'''
y_pred: [B,C],
y_true_one_hot: [B,C], Variable torch.LongTensor
'''
## y_pred has passed through softmax
# do not average over batch size here
loss = - torch.mean(torch.sum(y_true_one_hot * torch.log(torch.clamp(y_pred, min=1e-15, max=1-1e-15)), 1))
# loss = - torch.mean(y_true_one_hot * F.log_softmax(y_pred)) # y_pred should not be passed through softmax
# loss = - torch.mean(y_true_one_hot * torch.log(y_pred + 1e-15)) # y_pred has passed through softmax
# print(loss.data[0])
return loss
class BCE_Loss(nn.Module):
"""docstring for BCE_Loss"""
def __init__(self, weight):
super(BCE_Loss, self).__init__()
self.loss = nn.BCELoss(weight=weight)
def forward(self, input, target):
# input: raw output of nn (without softmax)
# target: Variable of raw labels (not ont-hot)
target_tensor = target.data.cpu()
labels_one_hot = convert_to_one_hot(target_tensor, num_class=3)
labels_one_hot = Variable(labels_one_hot.cuda()) if input.is_cuda else Variable(labels_one_hot) # wrap it into variable
softmax = nn.Softmax()
return self.loss(softmax(input), labels_one_hot)
def optim_scheduler_ft(model, epoch, optimizer_name='rmsprop', slow_base=True,
init_lr=0.001, lr_decay_epoch=10, lr_decay_factor=0.9,
momentum=0.9, weight_decay=1e-4,
beta1=0.9,
warmup=False, warm_lr=1e-4, warm_epochs=5, warmup_type='constant',
cos_schedule=False, cos_schedule_params=None):
'''exponentially decrease the learning rate once every few epochs
beta1: beta1 for adam, default is 0.9
cos_schedule_params: for example: {'T': 100, 'M': 10, 'init_lr': 0.1}
----
optimizer: the re-scheduled optimizer
'''
if cos_schedule == False:
if warmup:
if epoch<=warm_epochs:
lr = warmup_scheduler(epoch, warm_lr=warm_lr, warm_epochs=warm_epochs, warmup_type=warmup_type, target_lr=init_lr)
else:
lr = init_lr * (lr_decay_factor**((epoch - 1 - warm_epochs) // lr_decay_epoch))
if (epoch - 1 - warm_epochs) % lr_decay_epoch == 0:
print('learning rate is set to {}'.format(lr))
else:
lr = init_lr * (lr_decay_factor**((epoch-1) // lr_decay_epoch))
if (epoch - 1) % lr_decay_epoch == 0:
print('learning rate is set to {}'.format(lr))
else: # cosine schedule
if warmup:
if epoch<=warm_epochs:
lr = warmup_scheduler(epoch, warm_lr=warm_lr, warm_epochs=warm_epochs, warmup_type=warmup_type, target_lr=init_lr)
else:
lr = cosine_anneal_schedule(epoch-1-warm_epochs, **cos_schedule_params)
else:
lr = cosine_anneal_schedule(epoch-1, **cos_schedule_params)
print('epoch:{}, lr:{}'.format(epoch, lr))
if slow_base:
if isinstance(model, torchvision.models.vgg.VGG):
ignored_params = model.classifier.parameters()
elif isinstance(model, torchvision.models.resnet.ResNet) or isinstance(model, torchvision.models.inception.Inception3):
ignored_params = model.fc.parameters()
else:
ignored_params = list()
ignored_params_id = list(map(id, ignored_params))
base_params = filter(lambda p: id(p) not in ignored_params_id, model.parameters())
optimizer_name = optimizer_name.lower()
if optimizer_name == 'adam':
if slow_base:
optimizer = optim.Adam([
{'params': base_params},
{'params': ignored_params, 'lr': lr}
], lr=lr * 0.1, betas=(beta1, 0.999), eps=1e-08, weight_decay=0)
else:
optimizer = optim.Adam(model.parameters(), lr=lr, betas=(beta1, 0.999), eps=1e-08, weight_decay=0)
elif optimizer_name == 'adadelta': # lr=1.0 is recommended?
if slow_base:
optimizer = optim.Adadelta([
{'params': base_params},
{'params': ignored_params, 'lr': lr}
], lr=lr * 0.1, rho=0.9, eps=1e-06, weight_decay=0)
else:
optimizer = optim.Adadelta(model.parameters(), lr=lr, rho=0.9, eps=1e-06, weight_decay=0)
elif optimizer_name == 'rmsprop':
if slow_base:
optimizer = optim.RMSprop([
{'params': base_params},
{'params': ignored_params, 'lr': lr}
], lr=lr * 0.1, alpha=0.99, eps=1e-08, weight_decay=0, momentum=0, centered=False)
else:
optimizer = optim.RMSprop(model.parameters(), lr=lr, alpha=0.99, eps=1e-08, weight_decay=0, momentum=0, centered=False)
elif optimizer_name == 'nag':
if slow_base:
optimizer = optim.SGD([
{'params': base_params},
{'params': ignored_params, 'lr': lr}
], lr=lr * 0.1, momentum=momentum, weight_decay=weight_decay, nesterov=True)
else:
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay, nesterov=True)
elif optimizer_name == 'sgd':
if slow_base:
optimizer = optim.SGD([
{'params': base_params},
{'params': ignored_params, 'lr': lr}
], lr=lr * 0.1, momentum=momentum, weight_decay=weight_decay)
else:
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
else:
optimizer = None
print("No optimizer: {}!".format(optimizer_name))
return optimizer, lr
def weights_init(m):
# classname = m.__class__.__name__
if isinstance(m, nn.Conv2d):
#print('init conv2d')
#init.xavier_uniform(m.weight.data, gain=np.sqrt(2.0))
init.kaiming_uniform(m.weight.data, mode='fan_in')
# m.weight.data.normal_(0.0, 0.02)
if isinstance(m, nn.Linear):
#print('init fc')
init.kaiming_uniform(m.weight.data, mode='fan_in')
# size = m.weight.size()
# fan_out = size[0] # number of rows
# fan_in = size[1] # number of columns
# variance = np.sqrt(2.0/(fan_in + fan_out))
# m.weight.data.uniform_(0.0, variance)
def imshow_tensor(inp):
"""Imshow for Tensor.
inp: (3,h,w), typically torch's image are stored in (c,h,w),
because common Tensors in neural networks in torch are in BCHW format
"""
inp = inp.numpy().transpose((1, 2, 0)) # (h,w,3)
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
plt.imshow(inp)
class RandomDiscreteRotate(object):
def __init__(self,
rotation_range,
interp='bilinear',
lazy=False):
"""
Randomly rotate an image between degrees in the given list. If the image
has multiple channels, the same rotation will be applied to each channel.
Arguments
---------
rotation_range : list
image will be rotated between degrees given in list
fill_mode : string in {'constant', 'nearest'}
how to fill the empty space caused by the transform
fill_value : float
the value to fill the empty space with if fill_mode='constant'
lazy : boolean
if false, perform the transform on the tensor and return the tensor
if true, only create the affine transform matrix and return that
"""
self.rotation_range = rotation_range
if not isinstance(interp, (tuple,list)):
interp = (interp, interp)
self.interp = interp
self.lazy = lazy
def __call__(self, x, y=None):
k = random.randint(0, len(self.rotation_range)-1)
degree = self.rotation_range[k]
if self.lazy:
return Rotate(degree, lazy=True)(x)
else:
if y is None:
x_transformed = Rotate(degree,
interp=self.interp)(x)
return x_transformed
else:
x_transformed, y_transformed = Rotate(degree,
interp=self.interp)(x,y)
return x_transformed, y_transformed
def get_augmented_test_set(data_root, idx_file,
scale_size, crop_size, aug_type='ten_crop',
seg_root=None, mixture=False):
dsets = []
if aug_type == 'ten_crop':
crop_types = [0, 1, 2, 3, 4]
# 0: center crop,
# 1: top left crop, 2: top right crop
# 3: bottom right crop, 4: bottom left crop
flips = [0, 1] # 0: no flip, 1: horizontal flip
for i in crop_types:
for j in flips:
data_transform = transforms.Compose([
transforms.Scale(scale_size),
# transforms.CenterCrop(crop_size),
transforms.ToTensor(),
RandomFlip(flips[j]),
SpecialCrop((crop_size, crop_size), crop_type=crop_types[i]),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
if mixture:
seg_transform = transforms.Compose([
transforms.Scale(crop_size),
# transforms.CenterCrop(crop_size),
transforms.ToTensor(),
RandomFlip(flips[j]),
# SpecialCrop(crop_size=(crop_size, crop_size), crop_type=crop_types[i]),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
dsets.append(MyImageFolder(root = data_root,
idx_file = idx_file,
transform = data_transform,
seg_transform = seg_transform,
seg_root = seg_root))
else:
dsets.append(MyImageFolder(root = data_root,
idx_file = idx_file,
transform = data_transform))
return dsets
def get_multi_scale_crop_test_set(data_root, idx_file,
scale_sizes, crop_size, aug_type='forty_crop',
seg_root=None, mixture=False):
dsets = []
if aug_type == 'forty_crop':
for scale_size in scale_sizes:
crop_types = [0, 1, 2, 3, 4]
# 0: center crop,
# 1: top left crop, 2: top right crop
# 3: bottom right crop, 4: bottom left crop
flips = [0, 1] # 0: no flip, 1: horizontal flip
for i in crop_types:
for j in flips:
data_transform = transforms.Compose([
transforms.Scale(scale_size),
# transforms.CenterCrop(crop_size),
transforms.ToTensor(),
RandomFlip(flips[j]),
SpecialCrop((crop_size, crop_size), crop_type=crop_types[i]),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
if mixture:
seg_transform = transforms.Compose([
transforms.Scale(crop_size),
# transforms.CenterCrop(crop_size),
transforms.ToTensor(),
RandomFlip(flips[j]),
# SpecialCrop(crop_size=(crop_size, crop_size), crop_type=crop_types[i]),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
dsets.append(MyImageFolder(root = data_root,
idx_file = idx_file,
transform = data_transform,
seg_transform = seg_transform,
seg_root = seg_root))
else:
dsets.append(MyImageFolder(root = data_root,
idx_file = idx_file,
transform = data_transform))
return dsets
def loss_weight(args, dset_loaders):
'''
get the weights of losses of different types
'''
total_type1 = 0
total_type2 = 0
total_type3 = 0
for data in dset_loaders['train']:
inputs, labels, _ = data
labels_one_hot = convert_to_one_hot(labels,num_class=3)
if args.cuda:
_, labels = Variable(inputs.cuda()), Variable(labels.cuda())
labels_one_hot = Variable(labels_one_hot.cuda())
else:
_, labels = Variable(inputs), Variable(labels)
labels_one_hot = Variable(labels_one_hot)
total_type1 += torch.sum(labels.data == 0)
total_type2 += torch.sum(labels.data == 1)
total_type3 += torch.sum(labels.data == 2)
weight_type1 = 1/total_type1
weight_type2 = 1/total_type2
weight_type3 = 1/total_type3
total = weight_type1 + weight_type2 + weight_type3
weight = torch.FloatTensor([weight_type1/total,weight_type2/total,weight_type3/total])
return weight
"""
def visualize_model(model, num_images=5):
for i, data in enumerate(dset_loaders['val']):
inputs, labels, _ = data
if args.cuda:
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
else:
inputs, labels = Variable(inputs), Variable(labels)
outputs = model(inputs)
_, preds = torch.max(outputs.data, 1)
plt.figure()
imshow(inputs.cpu().data[0])
plt.title('pred: {}'.format(dset_classes[labels.data[0]]))
plt.show()
if i == num_images - 1:
break
"""