-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
utils.py
134 lines (116 loc) · 4.56 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
import os
import torch
import yaml
from model import two_view_net, three_view_net
def make_weights_for_balanced_classes(images, nclasses):
count = [0] * nclasses
for item in images:
count[item[1]] += 1 # count the image number in every class
weight_per_class = [0.] * nclasses
N = float(sum(count))
for i in range(nclasses):
weight_per_class[i] = N/float(count[i])
weight = [0] * len(images)
for idx, val in enumerate(images):
weight[idx] = weight_per_class[val[1]]
return weight
# Get model list for resume
def get_model_list(dirname, key):
if os.path.exists(dirname) is False:
print('no dir: %s'%dirname)
return None
gen_models = [os.path.join(dirname, f) for f in os.listdir(dirname) if
os.path.isfile(os.path.join(dirname, f)) and key in f and ".pth" in f]
if gen_models is None:
return None
gen_models.sort()
last_model_name = gen_models[-1]
return last_model_name
######################################################################
# Save model
#---------------------------
def save_network(network, dirname, epoch_label):
if not os.path.isdir('./model/'+dirname):
os.mkdir('./model/'+dirname)
if isinstance(epoch_label, int):
save_filename = 'net_%03d.pth'% epoch_label
else:
save_filename = 'net_%s.pth'% epoch_label
save_path = os.path.join('./model',dirname,save_filename)
torch.save(network.cpu().state_dict(), save_path)
if torch.cuda.is_available:
network.cuda()
######################################################################
# Load model for resume
#---------------------------
def load_network(name, opt):
# Load config
dirname = os.path.join('./model',name)
last_model_name = os.path.basename(get_model_list(dirname, 'net'))
epoch = last_model_name.split('_')[1]
epoch = epoch.split('.')[0]
if not epoch=='last':
epoch = int(epoch)
config_path = os.path.join(dirname,'opts.yaml')
with open(config_path, 'r') as stream:
config = yaml.load(stream, Loader=yaml.FullLoader)
opt.name = config['name']
opt.data_dir = config['data_dir']
opt.train_all = config['train_all']
opt.droprate = config['droprate']
opt.color_jitter = config['color_jitter']
opt.batchsize = config['batchsize']
opt.h = config['h']
opt.w = config['w']
opt.share = config['share']
opt.stride = config['stride']
if 'pool' in config:
opt.pool = config['pool']
if 'h' in config:
opt.h = config['h']
opt.w = config['w']
if 'gpu_ids' in config:
opt.gpu_ids = config['gpu_ids']
opt.erasing_p = config['erasing_p']
opt.lr = config['lr']
opt.nclasses = config['nclasses']
opt.erasing_p = config['erasing_p']
opt.use_dense = config['use_dense']
opt.fp16 = config['fp16']
opt.views = config['views']
if opt.use_dense:
model = ft_net_dense(opt.nclasses, opt.droprate, opt.stride, None, opt.pool)
if opt.PCB:
model = PCB(opt.nclasses)
if opt.views == 2:
model = two_view_net(opt.nclasses, opt.droprate, stride = opt.stride, pool = opt.pool, share_weight = opt.share)
elif opt.views == 3:
model = three_view_net(opt.nclasses, opt.droprate, stride = opt.stride, pool = opt.pool, share_weight = opt.share)
if 'use_vgg16' in config:
opt.use_vgg16 = config['use_vgg16']
if opt.views == 2:
model = two_view_net(opt.nclasses, opt.droprate, stride = opt.stride, pool = opt.pool, share_weight = opt.share, VGG16 = opt.use_vgg16)
elif opt.views == 3:
model = three_view_net(opt.nclasses, opt.droprate, stride = opt.stride, pool = opt.pool, share_weight = opt.share, VGG16 = opt.use_vgg16)
# load model
if isinstance(epoch, int):
save_filename = 'net_%03d.pth'% epoch
else:
save_filename = 'net_%s.pth'% epoch
save_path = os.path.join('./model',name,save_filename)
print('Load the model from %s'%save_path)
network = model
network.load_state_dict(torch.load(save_path))
return network, opt, epoch
def toogle_grad(model, requires_grad):
for p in model.parameters():
p.requires_grad_(requires_grad)
def update_average(model_tgt, model_src, beta):
toogle_grad(model_src, False)
toogle_grad(model_tgt, False)
param_dict_src = dict(model_src.named_parameters())
for p_name, p_tgt in model_tgt.named_parameters():
p_src = param_dict_src[p_name]
assert(p_src is not p_tgt)
p_tgt.copy_(beta*p_tgt + (1. - beta)*p_src)
toogle_grad(model_src, True)