-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtrain_celeb.py
163 lines (127 loc) · 5.02 KB
/
train_celeb.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
'''
TaICML incremental learning
Copyright (c) Jathushan Rajasegaran, 2019
'''
from __future__ import print_function
import argparse
import os
import shutil
import time
import random
import pickle
import torch
import pdb
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data as data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from utils import Bar, Logger, AverageMeter, accuracy, mkdir_p, savefig
import numpy as np
import copy
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from torch.autograd import gradcheck
import sys
import random
import collections
from basic_net import *
# from learner_task_reptile import Learner
# from learner_task_FOMAML import Learner
# from learner_task_joint import Learner
from learner_task_itaml import Learner
import incremental_dataloader as data
class args:
checkpoint = "results/cifar100/meta2_celeb_T10_7"
savepoint = "models/" + "/".join(checkpoint.split("/")[1:])
data_path = "../Datasets/MS1M/imgs/"
num_class = 10000
class_per_task = 1000
num_task = 10
test_samples_per_class = 100
dataset = "celeb"
optimizer = "radam"
epochs = 70
lr = 0.01
train_batch = 256
test_batch = 100
workers = 16
sess = 0
schedule = [20,40,60]
gamma = 0.2
random_classes = False
validation = 0
memory = 50000
mu = 1
beta = 1
r = 1
state = {key:value for key, value in args.__dict__.items() if not key.startswith('__') and not callable(key)}
print(state)
use_cuda = torch.cuda.is_available()
seed = random.randint(1, 10000)
# seed = 7572
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if use_cuda:
torch.cuda.manual_seed_all(seed)
def main():
model = BasicNet1(args, 0).cuda()
# model = nn.DataParallel(model).cuda()
print(' Total params: %.2fM ' % (sum(p.numel() for p in model.parameters())/1000000.0))
if not os.path.isdir(args.checkpoint):
mkdir_p(args.checkpoint)
if not os.path.isdir(args.savepoint):
mkdir_p(args.savepoint)
np.save(args.checkpoint + "/seed.npy", seed)
inc_dataset = data.IncrementalDataset(
dataset_name=args.dataset,
args = args,
random_order=args.random_classes,
shuffle=True,
seed=1,
batch_size=args.train_batch,
workers=args.workers,
validation_split=args.validation,
increment=args.class_per_task,
)
start_sess = int(sys.argv[1])
memory = None
for ses in range(start_sess, args.num_task):
args.sess=ses
if(ses==0):
torch.save(model.state_dict(), os.path.join(args.savepoint, 'base_model.pth.tar'))
if(start_sess==ses and start_sess!=0):
inc_dataset._current_task = ses
with open(args.savepoint + "/sample_per_task_testing_"+str(args.sess-1)+".pickle", 'rb') as handle:
sample_per_task_testing = pickle.load(handle)
inc_dataset.sample_per_task_testing = sample_per_task_testing
args.sample_per_task_testing = sample_per_task_testing
if ses>0:
path_model=os.path.join(args.savepoint, 'session_'+str(ses-1) + '_model_best.pth.tar')
prev_best=torch.load(path_model)
model.load_state_dict(prev_best)
with open(args.savepoint + "/memory_"+str(args.sess-1)+".pickle", 'rb') as handle:
memory = pickle.load(handle)
task_info, train_loader, val_loader, test_loader, for_memory = inc_dataset.new_task(memory)
print(task_info)
print(inc_dataset.sample_per_task_testing)
args.sample_per_task_testing = inc_dataset.sample_per_task_testing
main_learner=Learner(model=model,args=args,trainloader=train_loader, testloader=test_loader, use_cuda=use_cuda)
main_learner.learn()
memory = inc_dataset.get_memory(memory, for_memory)
acc_task = {} #main_learner.meta_test(main_learner.best_model, memory, inc_dataset)
with open(args.savepoint + "/memory_"+str(args.sess)+".pickle", 'wb') as handle:
pickle.dump(memory, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open(args.savepoint + "/acc_task_"+str(args.sess)+".pickle", 'wb') as handle:
pickle.dump(acc_task, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open(args.savepoint + "/sample_per_task_testing_"+str(args.sess)+".pickle", 'wb') as handle:
pickle.dump(args.sample_per_task_testing, handle, protocol=pickle.HIGHEST_PROTOCOL)
time.sleep(10)
if __name__ == '__main__':
main()