-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
211 lines (185 loc) · 9.44 KB
/
trainer.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
import os
import torch
from pars import PARS
from utils import *
from models import setup_net
from get_data import *
import time
import json
def main(pars):
if pars.loadnet == None:
timestr = time.strftime("%Y%m%d-%H%M%S")
expdir = os.path.join(pars.savepath, timestr)
if not os.path.exists(expdir):
os.makedirs(expdir)
else:
expdir = pars.loadnet.rsplit('/',1)[0]
pars.expdir = expdir
print(pars.expdir)
pars.train_unsupervised = pars.unsupervised
start_epoch = 0
dtype = torch.float32
if pars.dataset == 'stl10_unlabeled':
if pars.gaze_shift:
base_train_loader, base_test_loader = get_stl10_unlabeled_patches(pars.datapath, pars.batch_size, pars.num_train)
else:
if pars.distort == 0:
base_train_loader, base_test_loader = get_stl10_unlabeled_deform(pars.datapath, pars.batch_size, pars.num_train)
else:
base_train_loader, base_test_loader = get_stl10_unlabeled_vanilla_deform(pars.datapath, pars.batch_size, pars.num_train)
clf_train_loader, clf_test_loader = get_stl10_labeled(pars.datapath, pars.batch_size, pars)
elif pars.dataset == 'cifar100':
base_train_loader, base_test_loader = get_cifar100(pars.datapath, pars.batch_size, pars.num_train)
clf_train_loader, clf_test_loader = get_cifar10(pars.datapath, pars.batch_size, pars.num_train)
elif pars.dataset == 'cifar10':
base_train_loader, base_test_loader = get_cifar10(pars.datapath, pars.batch_size, pars.num_train)
clf_train_loader, clf_test_loader = get_cifar10(pars.datapath, pars.batch_size, pars.num_train)
test_acc_all = []
for rep in range(pars.repeat):
print("\nRep {}".format(rep+1))
net, classifier, head = setup_net(pars)
val_loss = []
val_acc = []
lw_test_acc = []
if pars.unsupervised:
head_loss = []
if pars.process == 'RLL':
pars.train_unsupervised = True
train_model_rand(base_train_loader, base_test_loader, net, head, pars, head_loss, None)
print('Train classifier')
pars.train_unsupervised = False
for i in range(pars.NUM_LAYER):
train_model(clf_train_loader, clf_test_loader, net[:(i+1)], classifier[i], pars, val_loss, val_acc, pars.expdir)
test_acc = check_accuracy(clf_test_loader, net[:(i+1)], classifier[i], pars)
print('Rep: %d, Layer: %d, te.acc = %.4f' % (rep+1, i, test_acc))
lw_test_acc.append(test_acc)
elif pars.process == 'GLL':
# start from where the previous check point was saved
if pars.loadnet:
if pars.loadnet.startswith('.'):
start_layer = pars.loadnet.rsplit('.')[1].rsplit('_')[-1]
else:
start_layer = pars.loadnet.rsplit('.')[0].rsplit('_')[-1]
else:
start_layer = 0
for i in range(int(start_layer), pars.NUM_LAYER):
print('LAYER:%d'%i)
fix = net[:i]
print("Fixed part:\n", fix)
if pars.loss != 'CLAPP':
model = nn.Sequential(
net[i],
head[i]
)
else:
model = net[i]
if i != int(start_layer):
pars.loadnet = None
if pars.loadnet:
checkpoint = torch.load(pars.loadnet)
model.load_state_dict(checkpoint['state_dict'])
timestr = pars.loadnet.split('/')[-2].strip()
expdir = os.path.join(pars.savepath, timestr)
for pre in range(i):
print('Loading from fixed layer ', pre)
fix_path = os.path.join(expdir, f'basenet_epoch_{pars.epochs}_layer_{pre}.pth')
fix_checkpoint = torch.load(fix_path)
loaded_fix_weights = fix_checkpoint['state_dict']
new_fix_weights = fix[pre].state_dict()
for k in new_fix_weights.keys():
if pars.loss != 'CLAPP':
new_fix_weights[k] = loaded_fix_weights['0.'+k]
else:
new_fix_weights[k] = loaded_fix_weights[k]
fix[pre].load_state_dict(new_fix_weights)
if os.path.exists(expdir):
start_epoch = checkpoint['epoch']
print(f'Checkpoint loaded, resuming from {start_epoch}')
print(f"Resuming from layer {start_layer}")
print(f'Saving to existing path: {expdir}')
pars.expdir = expdir
if pars.loadclf:
classifier.load_state_dict(pars.loadclf)
print("Part to train:\n", model)
print('Classifier:\n', classifier[i])
pars.train_unsupervised = True
train_model(base_train_loader, base_test_loader, fix, model, pars, head_loss, None, pars.expdir, current_layer=i)
pars.train_unsupervised = False
if not pars.classify_whole_net:
print('Train Classifier on current layer')
train_model(clf_train_loader, clf_test_loader, net[:(i+1)], classifier[i], pars, val_loss, val_acc, pars.expdir)
test_acc = check_accuracy(clf_test_loader, net[:(i+1)], classifier[i], pars)
print('Rep: %d, Layer: %d, te.acc = %.4f' % (rep+1, i, test_acc))
lw_test_acc.append(test_acc)
print()
if pars.classify_whole_net:
print('Train Classifier for the whole model')
pars.train_unsupervised = False
train_model(clf_train_loader, clf_test_loader, net, classifier[-1], pars, val_loss, val_acc, pars.expdir)
test_acc = check_accuracy(clf_test_loader, net, classifier[-1], pars)
print('Rep: %d, te.acc = %.4f' % (rep+1, test_acc))
lw_test_acc.append(test_acc)
else: # 'E2E'
fix = nn.Sequential()
model = nn.Sequential(
net,
head
)
if pars.loadnet:
checkpoint = torch.load(pars.loadnet)
model.load_state_dict(checkpoint['state_dict'])
timestr = pars.loadnet.split('/')[-2].strip()
expdir = os.path.join(pars.savepath, timestr)
if os.path.exists(expdir):
start_epoch = checkpoint['epoch']
print(f'Checkpoint loaded, resuming from {start_epoch}')
print(f'Saving to existing path: {expdir}')
pars.expdir = expdir
if pars.loadclf:
classifier.load_state_dict(pars.loadclf)
print("Part to train:\n", model)
print('Classifier:\n', classifier)
pars.train_unsupervised = True
train_model(base_train_loader, base_test_loader, fix, model, pars, head_loss, None, pars.expdir)
print('Train Classifier')
pars.train_unsupervised = False
train_model(clf_train_loader, clf_test_loader, net, classifier, pars, val_loss, val_acc, pars.expdir)
test_acc = check_accuracy(clf_test_loader, net, classifier, pars)
print('Rep: %d, te.acc = %.4f' % (rep+1, test_acc))
lw_test_acc.append(test_acc)
torch.save(net.state_dict(), os.path.join(pars.expdir, "final_basenet.mdlp"))
torch.save(classifier.state_dict(), os.path.join(pars.expdir, "final_classifier.mdlp"))
if pars.unsupervised:
np.save(pars.expdir+'/head_loss_rep_{}_after_epoch{}'.format(rep+1, start_epoch), head_loss)
np.save(pars.expdir+'/loss_rep_{}_'.format(rep+1), val_loss)
np.save(pars.expdir+'/val.acc_rep_{}_'.format(rep+1), val_acc)
np.save(pars.expdir+'/te.acc_rep_{}_'.format(rep+1), lw_test_acc)
test_acc_all.append(lw_test_acc)
print('\nAll reps test.acc:')
for acc in test_acc_all:
print(acc)
np.save(pars.expdir+'/te.acc.all_', test_acc_all)
if __name__ == '__main__':
device = "cuda:0" if torch.cuda.is_available() else "cpu"
print(device)
datapath = './data'
savepath = './save'
pars = PARS(device, datapath, savepath)
pars.process = 'E2E'
pars.update = 'BP'
pars.architecture = 'CONV6'
pars.gaze_shift = False
pars.loss = 'HingeNNFewerNegs'
pars.thr1 = 2.
pars.thr2 = 1.
pars.batch_size = 500
pars.dataset = 'cifar100'
pars.clf_dataset = 'cifar10'
pars.distort = 3
pars.epochs = 400
pars.clf_epochs = 200
pars.n_negs = 5
print(pars)
main(pars)
with open(os.path.join(pars.expdir, 'configs.json'), 'w') as fp:
json.dump(pars.__dict__, fp)