-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_utils.py
407 lines (334 loc) · 15.1 KB
/
model_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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import copy
import re
import time
from collections import OrderedDict
import numpy as np
import torch
import torch.nn as nn
import resnet_cifar
from composite_batchnorm import CompositeBatchNorm2d
from losses import GenericLoss
from pc_batchnorm import PCBatchNorm2d
from torchvision import models
def get_model(model_string, **kwargs):
"""Retrieves model from library.
"""
if model_string == 'resnet18':
return models.resnet18(**kwargs)
elif model_string == 'resnet34':
return models.resnet34(**kwargs)
elif model_string == 'resnet50':
return models.resnet50(**kwargs)
elif model_string == 'resnet101':
return models.resnet101(**kwargs)
elif model_string == 'resnet152':
return models.resnet152(**kwargs)
elif model_string == 'resnet20':
return resnet_cifar.resnet20(**kwargs)
elif model_string == 'resnet32':
return resnet_cifar.resnet32(**kwargs)
elif model_string == 'resnet44':
return resnet_cifar.resnet44(**kwargs)
elif model_string == 'resnet56':
return resnet_cifar.resnet56(**kwargs)
elif model_string == 'resnet110':
return resnet_cifar.resnet110(**kwargs)
elif model_string == 'resnet1202':
return resnet_cifar.resnet1202(**kwargs)
else:
raise NameError('{} is not recognized.'.format(model_string))
def get_model_trainable_parameters(model):
"""Retrieves all trainable parameters of a model to pass to an optimizer.
"""
param_list = []
for param in model.parameters():
if param.requires_grad:
param_list.append(param)
return param_list
def freeze_model_parameters_(model):
"""Sets requires_grad attribute in all model parameters to False.
"""
for param in model.parameters():
param.requires_grad = False
def set_module_trainable_(model, target_module):
"""Sets all specified modules of model to trainable.
"""
for module in model.modules():
if isinstance(module, target_module):
for param in module.parameters():
param.requires_grad = True
def part_load_state_dict_(model, state_dict, prototype_module):
"""Loads only parameters of prototype module to model.
"""
valid_names = []
for name, module in model.named_modules():
if isinstance(module, prototype_module):
valid_names.append(name)
valid_keys = []
for key in state_dict:
if any([name in key for name in valid_names]):
valid_keys.append(key)
new_state_dict = OrderedDict((key, state_dict[key]) for key in valid_keys)
model.load_state_dict(new_state_dict, strict=False)
def replace_bn_with_combn_(model, state_dict_paths, mode='naive', init='auto',
manual_params=None):
"""
Replace all BatchNorm2d modules in model with CompositeBatchNorm2d,
initialised using a combination of BN layer weights retrieved from
state_dict_paths.
state_dict_paths is a collection of paths to model weights.
"""
num_composition = len(state_dict_paths)
batch_norms = [{} for i in range(num_composition)]
# Collect BatchNorm2d modules from all state_dict(s)
original_weights = model.state_dict()
for i, path in enumerate(state_dict_paths):
model.load_state_dict(torch.load(path))
for name, module in model.named_modules():
if isinstance(module, nn.BatchNorm2d):
batch_norms[i][name] = copy.deepcopy(module)
model.load_state_dict(original_weights)
# Replace model's BatchNorm2d modules with CompositeBatchNorm2d
regex = re.compile(r'\.(\d+)')
for name, _ in model.named_modules():
# Retrieve module dynamically instead of relying on generator
# This is done to prevent undefined behaviour with iterating over a
# mutating iterable
stat = regex.sub(r'[\1]', name)
transfer_dict = {'model': model}
if stat != '':
exec('module = model.' + stat, transfer_dict)
else:
exec('module = model', transfer_dict)
module = transfer_dict['module']
# Copy and replace Sequential module if the Sequential module contains
# BatchNorm2d
if isinstance(module, nn.Sequential):
# Check that there is BatchNorm2d in the Sequential module
has_bn = False
for submodule in module.children():
if isinstance(submodule, nn.BatchNorm2d):
has_bn = True
break
# Replace module if check passes
if has_bn:
seq_copy = OrderedDict(module.named_children())
for subname, submodule in seq_copy.items():
if isinstance(submodule, nn.BatchNorm2d):
bn_list = ([batch_norms[i][name + '.' + subname]
for i in range(num_composition)])
seq_copy[subname] = CompositeBatchNorm2d(bn_list,
mode=mode,
init=init,
manual_params=manual_params)
stat = regex.sub(r'[\1]', name)
exec('model.' + stat
+ ' = nn.Sequential(seq_copy)')
# Otherwise replace BatchNorm2d directly
elif isinstance(module, nn.BatchNorm2d):
bn_list = [batch_norms[i][name] for i in range(num_composition)]
stat = regex.sub(r'[\1]', name)
exec('model.' + stat
+ ' = CompositeBatchNorm2d(bn_list, mode=mode, init=init, manual_params=manual_params)')
def replace_bn_with_pcbn_(model, state_dict_paths, num_pc=0, init='equal'):
"""
Replace all BatchNorm2d modules in model with PCBatchNorm2d,
initialised using a combination of BN layer weights retrieved from
state_dict_paths.
state_dict_paths is a collection of paths to model weights.
If init='manual', PCBatchNorm2d parameters will be initialized to
BatchNorm2d parameters found in model, transformed to PC space.
"""
num_composition = len(state_dict_paths)
batch_norms = [{} for i in range(num_composition)]
original_params = None
# Collect BatchNorm2d modules from all state_dict(s)
original_weights = model.state_dict()
for i, path in enumerate(state_dict_paths):
model.load_state_dict(torch.load(path))
for name, module in model.named_modules():
if isinstance(module, nn.BatchNorm2d):
batch_norms[i][name] = copy.deepcopy(module)
model.load_state_dict(original_weights)
# Replace model's BatchNorm2d modules with CompositeBatchNorm2d
regex = re.compile(r'\.(\d+)')
for name, _ in model.named_modules():
# Retrieve module dynamically instead of relying on generator
# This is done to prevent undefined behaviour with iterating over a
# mutating iterable
stat = regex.sub(r'[\1]', name)
transfer_dict = {'model': model}
if stat != '':
exec('module = model.' + stat, transfer_dict)
else:
exec('module = model', transfer_dict)
module = transfer_dict['module']
# Copy and replace Sequential module if the Sequential module contains
# BatchNorm2d
if isinstance(module, nn.Sequential):
# Check that there is BatchNorm2d in the Sequential module
has_bn = False
for submodule in module.children():
if isinstance(submodule, nn.BatchNorm2d):
has_bn = True
break
# Replace module if check passes
if has_bn:
seq_copy = OrderedDict(module.named_children())
for subname, submodule in seq_copy.items():
if isinstance(submodule, nn.BatchNorm2d):
bn_list = ([batch_norms[i][name + '.' + subname]
for i in range(num_composition)])
if init == 'manual':
original_params = {'weight': submodule.weight.data,
'bias': submodule.bias.data}
seq_copy[subname] = PCBatchNorm2d(
bn_list,
num_pc=num_pc,
init=init,
original_params=original_params)
stat = regex.sub(r'[\1]', name)
exec('model.' + stat
+ ' = nn.Sequential(seq_copy)')
# Otherwise replace BatchNorm2d directly
elif isinstance(module, nn.BatchNorm2d):
bn_list = [batch_norms[i][name] for i in range(num_composition)]
if init == 'manual':
original_params = {'weight': module.weight.data,
'bias': module.bias.data}
stat = regex.sub(r'[\1]', name)
exec('model.' + stat
+ ' = PCBatchNorm2d(bn_list, num_pc=num_pc, init=init,'
+ 'original_params=original_params)')
def train_model(model, criterion, optimizer, dataloader, dataset_sizes,
scheduler=None, num_epochs=30, device='cuda', verbose=True,
log_dir=None):
"""Trainer function.
"""
device = torch.device(device)
use_tensorboardx = log_dir is not None
summary = {x: [] for x in ('train_loss',
'train_acc',
'val_loss',
'val_acc',
'wall_time')}
if 'lr' in optimizer.param_groups[0]:
summary['lr'] = []
if use_tensorboardx:
from tensorboardX import SummaryWriter
writer = SummaryWriter(log_dir=log_dir)
best_model_wts = model.state_dict()
best_acc = 0.0
start_time = time.time()
for epoch in range(num_epochs):
for phase in ('train', 'val'):
if phase == 'train':
model.train()
if scheduler is not None:
scheduler.step()
else:
model.eval()
running_loss = 0.0
running_corrects = 0
for inputs, labels in dataloader[phase]:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
if isinstance(criterion, GenericLoss):
loss, outputs = criterion(model, inputs, labels)
else:
outputs = model(inputs)
if isinstance(outputs, tuple):
outputs = outputs[0]
loss = criterion(outputs, labels)
if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
outputs_d, labels_d = outputs.detach(), labels.detach()
if isinstance(criterion, (nn.CrossEntropyLoss, GenericLoss)):
_, preds = torch.max(outputs_d, 1)
running_corrects += torch.sum(preds == labels_d).item()
elif isinstance(criterion, nn.MultiLabelSoftMarginLoss):
preds = (outputs_d > 0.5).to(torch.long)
running_corrects += torch.sum(
torch.sum(preds == labels_d.to(torch.long), 1) == labels.size(1)).item()
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects / dataset_sizes[phase]
# Update training summaries
summary['{}_loss'.format(phase)].append(epoch_loss)
summary['{}_acc'.format(phase)].append(epoch_acc)
# Deep copy best performing model
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
# Update end-of-epoch summaries
summary['wall_time'].append(time.time() - start_time)
if 'lr' in summary:
summary['lr'].append(optimizer.param_groups[0]['lr'])
if use_tensorboardx:
writer.add_scalars('summary/loss',
{'train': summary['train_loss'][-1],
'val': summary['val_loss'][-1]},
epoch)
writer.add_scalars('summary/acc',
{'train': summary['train_acc'][-1],
'val': summary['val_acc'][-1]},
epoch)
if verbose:
print(('Epoch {}/{} [{:.0f}m {:.0f}s] - train_loss: {:.4f} train_acc: {:.4f} '
'val_loss: {:.4f} val_acc: {:.4f}').format(
epoch + 1, num_epochs,
summary['wall_time'][-1] // 60, summary['wall_time'][-1] % 60,
summary['train_loss'][-1], summary['train_acc'][-1],
summary['val_loss'][-1], summary['val_acc'][-1]))
# Clean up
if use_tensorboardx:
writer.close()
print('Best validation accuracy: {:.4f}'.format(best_acc))
model.load_state_dict(best_model_wts)
return model, summary
def eval_model(model, dataloader, dataset_size, criterion=nn.CrossEntropyLoss(),
device='cuda', verbose=True, return_preds=False, return_loss=False):
"""Evaluator function.
"""
device = torch.device(device)
criterion = criterion.to(device)
model.eval()
start_time = time.time()
running_loss = 0.0
running_corrects = 0
all_preds = []
with torch.no_grad():
for inputs, labels in dataloader:
inputs, labels = inputs.to(device), labels.to(device)
if isinstance(criterion, GenericLoss):
this_loss, outputs = criterion(model, inputs, labels)
else:
outputs = model(inputs)
if isinstance(outputs, tuple):
outputs = outputs[0]
this_loss = criterion(outputs, labels)
running_loss += this_loss.item() * inputs.size(0)
if isinstance(criterion, (nn.CrossEntropyLoss, GenericLoss)):
_, preds = torch.max(outputs, 1)
running_corrects += torch.sum(preds == labels).item()
elif isinstance(criterion, nn.MultiLabelSoftMarginLoss):
preds = (outputs > 0.5).to(torch.long)
running_corrects += torch.sum(
torch.sum(preds == labels.to(torch.long), 1) == labels.size(1)).item()
all_preds += [preds.cpu().numpy()]
all_preds = np.concatenate(all_preds, axis=0)
loss = running_loss / dataset_size
accuracy = running_corrects / dataset_size
time_elapsed = time.time() - start_time
if verbose:
print('Evaluation complete in {:.0f}m {:.3f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Accuracy: {:.4f}'.format(accuracy))
if return_loss:
print('Loss: {:.4f}'.format(loss))
if return_preds:
return accuracy, all_preds
elif return_loss:
return accuracy, loss
return accuracy