forked from cvlab-yonsei/DASS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_builder.py
110 lines (96 loc) · 3.67 KB
/
model_builder.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
from collections import OrderedDict
from .sync_batchnorm import convert_model
import torch
from .DeeplabV2 import *
def freeze_bn(net):
for module in net.modules():
if isinstance(module, torch.nn.modules.BatchNorm2d):
for i in module.parameters():
i.requires_grad = False
def release_bn(net):
for module in net.modules():
if isinstance(module, torch.nn.modules.BatchNorm2d):
for i in module.parameters():
i.requires_grad = True
def init_model_so(cfg):
model = Res_Deeplab(num_classes = cfg.num_classes).cuda()
if cfg.fixbn:
freeze_bn(model)
else:
release_bn(model)
if cfg.model=='deeplab' and cfg.init_weight != 'None':
params = torch.load(cfg.init_weight)
print('Model restored with weights from : {}'.format(cfg.init_weight))
if 'init' in cfg.init_weight and cfg.model=='deeplab':
new_params = model.state_dict().copy()
for i in params:
i_parts = i.split('.')
if not i_parts[1] == 'layer5':
new_params['.'.join(i_parts[1:])] = params[i]
model.load_state_dict(new_params, strict=True)
else:
new_params = model.state_dict().copy()
for i in params:
if 'module' in i:
i_ = i.replace('module.', '')
new_params[i_] = params[i]
else:
new_params[i] = params[i]
# i_parts = i.split('.')[0]
model.load_state_dict(new_params, strict=True)
# model.load_state_dict(params, strict=True)
if cfg.restore_from != 'None':
params = torch.load(cfg.restore_from)
model.load_state_dict(params)
print('Model initialize with weights from : {}'.format(cfg.restore_from))
if cfg.multigpu:
model = convert_model(model)
model = nn.DataParallel(model)
if cfg.train:
model.train().cuda()
print('Mode --> Train')
else:
model.eval().cuda()
print('Mode --> Eval')
return model
def init_model(cfg):
model = ResPair_Deeplab(num_classes=cfg.num_classes, cfg=cfg).cuda()
if cfg.fixbn:
freeze_bn(model)
else:
release_bn(model)
if cfg.model=='deeplab' and cfg.init_weight != 'None':
params = torch.load(cfg.init_weight)
print('Model restored with weights from : {}'.format(cfg.init_weight))
if 'init' in cfg.init_weight and cfg.model=='deeplab':
new_params = model.state_dict().copy()
for i in params:
i_parts = i.split('.')
if not i_parts[1] == 'layer5':
new_params['.'.join(i_parts[1:])] = params[i]
model.load_state_dict(new_params, strict=True)
else:
new_params = model.state_dict().copy()
for i in params:
if 'module' in i:
i_ = i.replace('module.', '')
new_params[i_] = params[i]
else:
new_params[i] = params[i]
# i_parts = i.split('.')[0]
model.load_state_dict(new_params, strict=True)
# model.load_state_dict(params, strict=True)
if cfg.restore_from != 'None':
params = torch.load(cfg.restore_from)
model.load_state_dict(params)
print('Model initialize with weights from : {}'.format(cfg.restore_from))
if cfg.multigpu:
model = convert_model(model)
model = nn.DataParallel(model)
if cfg.train:
model.train().cuda()
print('Mode --> Train')
else:
model.eval().cuda()
print('Mode --> Eval')
return model