forked from mk-minchul/attend-and-compare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
84 lines (63 loc) · 2.16 KB
/
model.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
from __future__ import print_function, division
import torch
import torch.nn as nn
import torch.optim as optim
import os
import net
def _forward_task(model, x):
x = getattr(model, 'classifier_conv')(x)
x = getattr(model, 'classifier_maxpool')(x)
x = x.squeeze()
x = nn.Sigmoid()(x)
return x
def load_model(N_LABELS, opt):
if opt.arch.startswith("resnet"):
model = net.resnet(opt.arch, pretrained=True, num_classes=1000,
zero_init_residual=True, module=opt.module, opt=opt)
print(model)
num_ftrs = model.fc.in_features
# remove unnecessary modules to save gpu mem
modules_rm = ['fc', "avgpool"]
for module_rm in modules_rm:
delattr(model, module_rm)
setattr(model, 'classifier_conv', nn.Conv2d(num_ftrs, N_LABELS, kernel_size=1))
setattr(model, 'classifier_maxpool', nn.AdaptiveMaxPool2d((1, 1)))
setattr(model, '_forward_task', _forward_task)
else:
raise ValueError('wrong type of architecture', opt.arch)
# add final layer with # outputs in same dimension of labels with sigmoid activation
# put model on GPU
model = model.cuda(opt.gpu_ids[0])
model = nn.DataParallel(model, device_ids=opt.gpu_ids)
return model
def checkpoint(model, best_loss, epoch, LR, opt):
"""
Saves checkpoint of torchvision model during training.
Args:
model: torchvision model to be saved
best_loss: best val loss achieved so far in training
epoch: current epoch of training
LR: current learning rate in training
Returns:
None
"""
print('saving')
state_dict = model.state_dict()
state = {
'state_dict': state_dict,
'best_loss': best_loss,
'epoch': epoch,
'rng_state': torch.get_rng_state(),
'LR': LR
}
torch.save(state, os.path.join(opt.run_path, 'checkpoint'))
def create_optimizer(model, LR, WEIGHT_DECAY, opt):
params = [
{'params': [p for p in model.parameters()]},
]
optimizer = optim.SGD(
params,
lr=LR,
momentum=0.9,
weight_decay=WEIGHT_DECAY)
return optimizer