-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_cifar_ari.py
159 lines (124 loc) · 5.02 KB
/
train_cifar_ari.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
from __future__ import print_function
import argparse
import os
import shutil
import time
import pickle
import torch
import numpy as np
import copy
import sys
import random
import collections
from utils import mkdir_p, CELS
from basic_net_adv import *
from learner_task_ari import Learner
import incremental_dataloader as data
from attack_type import attackers
class args:
checkpoint = "path"
savepoint = "models/" + "/".join(checkpoint.split("/")[1:])
data_path = "path"
num_class = 100
class_per_task = 10
num_task = 10
test_samples_per_class = 100
dataset = "cifar100"
optimizer = "radam"
epochs = 70
lr = 0.01
train_batch = 512
test_batch = 100
workers = 16
sess = 0
schedule = [20,40,60]
gamma = 0.2
random_classes = False
validation = 0
memory = 2000
mu = 1
beta = 1.0
r = 2
attack_type='fgsm_delta'
adv_epoch = 0
eps = 1.0
label_smooth = 0
nb_iter = 3
initial_const = 0.1
eps_iter = 2/255
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():
# import pdb;pdb.set_trace()
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)
try:
shutil.copy2('train_cifar_ari.py', args.checkpoint)
shutil.copy2('learner_task_ari.py', args.checkpoint)
except:
pass
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'))
mask = {}
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
train_criterion = CELS.CrossEntropyLabelSmooth(args.num_class, args.label_smooth).cuda()
if args.attack_type:
attacker_train = attackers[args.attack_type](model, loss_fn=train_criterion, eps=args.eps, nb_iter=args.nb_iter, \
eps_iter=args.eps_iter, num_classes=args.num_class, initial_const=args.initial_const)
main_learner=Learner(model=model,args=args,trainloader=train_loader, testloader=test_loader, use_cuda=use_cuda, attacker=attacker_train)
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()