-
Notifications
You must be signed in to change notification settings - Fork 12
/
cifar.py
219 lines (166 loc) · 6.96 KB
/
cifar.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
212
213
214
215
216
217
218
219
'''
RPS network training on CIFAR100
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
from rps_net import RPS_net_cifar
from learner import Learner
from util import *
from cifar_dataset import CIFAR100
class args:
checkpoint = "results/cifar100/RPS_CIFAR_M8_J1"
labels_data = "prepare/cifar100_10.pkl"
savepoint = ""
num_class = 100
class_per_task = 10
M = 8
jump = 2
rigidness_coff = 2.5
dataset = "CIFAR"
epochs = 100
L = 9
N = 1
lr = 0.001
train_batch = 128
test_batch = 128
workers = 16
resume = False
arch = "res-18"
start_epoch = 0
evaluate = False
sess = 0
test_case = 0
schedule = [20, 40, 60, 80]
gamma = 0.5
state = {key:value for key, value in args.__dict__.items() if not key.startswith('__') and not callable(key)}
print(state)
# Use CUDA
use_cuda = torch.cuda.is_available()
seed = random.randint(1, 10000)
random.seed(seed)
torch.manual_seed(seed)
if use_cuda:
torch.cuda.manual_seed_all(seed)
def main():
model = RPS_net_cifar(args).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("models/CIFAR100/"+args.checkpoint.split("/")[-1]):
mkdir_p("models/CIFAR100/"+args.checkpoint.split("/")[-1])
args.savepoint = "models/CIFAR100/"+args.checkpoint.split("/")[-1]
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
dataloader = CIFAR100
start_sess = int(sys.argv[2])
test_case = sys.argv[1]
args.test_case = test_case
inds_all_sessions=pickle.load(open(args.labels_data,'rb'))
for ses in range(start_sess, start_sess+1):
if(ses==0):
path = get_path(args.L,args.M,args.N)*0
path[:,0] = 1
fixed_path = get_path(args.L,args.M,args.N)*0
train_path = path.copy()
infer_path = path.copy()
else:
load_test_case = get_best_model(ses-1, args.checkpoint)
if(ses%args.jump==0): #get a new path
fixed_path = np.load(args.checkpoint+"/fixed_path_"+str(ses-1)+"_"+str(load_test_case)+".npy")
path = get_path(args.L,args.M,args.N)
train_path = get_path(args.L,args.M,args.N)*0
else:
if((ses//args.jump)==0):
fixed_path = get_path(args.L,args.M,args.N)*0
else:
load_test_case_x = get_best_model((ses//args.jump)*args.jump-1, args.checkpoint)
fixed_path = np.load(args.checkpoint+"/fixed_path_"+str((ses//args.jump)*args.jump-1)+"_"+str(load_test_case_x)+".npy")
path = np.load(args.checkpoint+"/path_"+str(ses-1)+"_"+str(load_test_case)+".npy")
train_path = get_path(args.L,args.M,args.N)*0
infer_path = get_path(args.L,args.M,args.N)*0
for j in range(args.L):
for i in range(args.M):
if(fixed_path[j,i]==0 and path[j,i]==1):
train_path[j,i]=1
if(fixed_path[j,i]==1 or path[j,i]==1):
infer_path[j,i]=1
np.save(args.checkpoint+"/path_"+str(ses)+"_"+str(test_case)+".npy", path)
print('Starting with session {:d}'.format(ses))
print('test case : ' + str(test_case))
print('#################################################################################')
print("path\n",path)
print("fixed_path\n",fixed_path)
print("train_path\n", train_path)
ind_this_session=inds_all_sessions[ses]
ind_trn= ind_this_session['curent']
if ses > 0: ind_trn = np.concatenate([ind_trn, np.tile(inds_all_sessions[ses-1]['exmp'],int(1))]).ravel()
ind_tst=inds_all_sessions[ses]['test']
trainset = dataloader(root='./data', train=True, download=True, transform=transform_train,ind=ind_trn)
trainloader = data.DataLoader(trainset, batch_size=args.train_batch, shuffle=True,num_workers=args.workers)
testset = dataloader(root='./data', train=False, download=False, transform=transform_test,ind=ind_tst)
testloader = data.DataLoader(testset, batch_size=args.test_batch, shuffle=False, num_workers=args.workers)
args.sess=ses
if ses>0:
path_model=os.path.join(args.savepoint, 'session_'+str(ses-1)+'_'+str(load_test_case)+'_model_best.pth.tar')
prev_best=torch.load(path_model)
model.load_state_dict(prev_best['state_dict'])
main_learner=Learner(model=model,args=args,trainloader=trainloader,
testloader=testloader,old_model=copy.deepcopy(model),
use_cuda=use_cuda, path=path,
fixed_path=fixed_path, train_path=train_path, infer_path=infer_path)
main_learner.learn()
if(ses==0):
fixed_path = path.copy()
else:
for j in range(args.L):
for i in range(args.M):
if(fixed_path[j,i]==0 and path[j,i]==1):
fixed_path[j,i]=1
np.save(args.checkpoint+"/fixed_path_"+str(ses)+"_"+str(test_case)+".npy", fixed_path)
best_model = get_best_model(ses, args.checkpoint)
cfmat = main_learner.get_confusion_matrix(infer_path)
np.save(args.checkpoint+"/confusion_matrix_"+str(ses)+"_"+str(test_case)+".npy", cfmat)
print('done with session {:d}'.format(ses))
print('#################################################################################')
while(1):
if(is_all_done(ses, args.epochs, args.checkpoint)):
break
else:
time.sleep(10)
if __name__ == '__main__':
main()