-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
168 lines (147 loc) · 5.12 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
import copy
import logging
import numpy as np
import os
import pandas as pd
import sys
import torch
from utils.data_manager import DataManager
from utils.toolkit import count_parameters
from SAFE import Learner
def train(args):
seed_list = copy.deepcopy(args["seed"])
device = copy.deepcopy(args["device"])
ave_accs=[]
for seed in seed_list:
args["seed"] = seed
args["device"] = device
ave_acc=_train(args)
ave_accs.append(ave_acc)
return ave_accs
def _train(args):
init_cls = 0 if args ["init_cls"] == args["increment"] else args["init_cls"]
logs_name = "logs/{}/{}/{}/{}".format(args["model_name"],args["dataset"], init_cls, args['increment'])
if not os.path.exists(logs_name):
os.makedirs(logs_name)
logfilename = "logs/{}/{}/{}/{}/{}_{}_{}".format(
args["model_name"],
args["dataset"],
init_cls,
args["increment"],
' ',
args["seed"],
args["convnet_type"],
)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(filename)s] => %(message)s",
handlers=[
logging.FileHandler(filename=logfilename + ".log"),
logging.StreamHandler(sys.stdout),
],
)
logging.info('Starting new run')
_set_random()
_set_device(args)
print_args(args)
model = Learner(args)
model.dil_init=False
if args['dataset']=='core50':
ds='core50_s1'
dil_tasks=['s1','s2','s4','s5','s6','s8','s9','s11']
num_tasks=len(dil_tasks)
model.is_dil=True
elif args['dataset']=='cddb':
ds='cddb_gaugan'
dil_tasks=['gaugan','biggan','wild','whichfaceisreal','san']
num_tasks=len(dil_tasks)
model.topk=2
model.is_dil=True
elif args['dataset']=='domainnet':
ds='domainnet_real'
dil_tasks=['real','quickdraw','painting','sketch','infograph','clipart']
num_tasks=len(dil_tasks)
model.is_dil=True
else:
#cil datasets
model.is_dil=False
data_manager = DataManager(
args['dataset'],
args["shuffle"],
args["seed"],
args["init_cls"],
args["increment"],
use_input_norm=args["use_input_norm"]
)
num_tasks=data_manager.nb_tasks
acc_curve = []
for i in range(10):
acc_curve.append({"top1_total": [],"ave_acc": []})
classes_df=None
logging.info("Pre-trained network parameters: {}".format(count_parameters(model._network)))
cnn_matrix=[]
for task in range(num_tasks):
if model.is_dil:
#reset the data manager to the next domain
data_manager = DataManager(
args["dataset"]+'_'+dil_tasks[task],
args["shuffle"],
args["seed"],
args["init_cls"],
args["increment"],
use_input_norm=args["use_input_norm"]
)
model._cur_task=-1
model._known_classes = 0
model._classes_seen_so_far = 0
if classes_df is None:
classes_df=pd.DataFrame()
classes_df['init']=-1*np.ones(data_manager._test_data.shape[0])
model.incremental_train(data_manager)
acc_total,acc_grouped,predicted_classes,true_classes = model.eval_task()
col1='pred_task_'+str(task)
col2='true_task_'+str(task)
model.after_task()
l = 0
for d in acc_grouped:
cur = d
n = cur.values()
m = np.round(np.mean(list(n)),2)
acc_curve[l]["top1_total"].append(acc_total)
acc_curve[l]["ave_acc"].append(m)
logging.info("Group Accuracies after this task: {}".format(cur))
l += 1
logging.info("Ave Acc curve: {}".format(acc_curve[0]["ave_acc"]))
logging.info("Top1 curve: {}".format(acc_curve[0]["top1_total"]))
logging.info('Finishing run')
logging.info('')
return acc_curve[0]["top1_total"][-1]
def save_results(args,top1_total,ave_acc,model,classes_df):
if not os.path.exists('./results/'):
os.makedirs('./results/')
output_df=pd.DataFrame()
output_df['top1_total']=top1_total
output_df['ave_acc']=ave_acc
output_df.to_csv('./results/'+args['dataset']+'_publish_'+str(args['ID'])+'.csv')
if not os.path.exists('./results/class_preds/'):
os.makedirs('./results/class_preds/')
classes_df.to_csv('./results/class_preds/'+args['dataset']+'_class_preds_publish_'+str(args['ID'])+'.csv')
def _set_device(args):
device_type = args["device"]
gpus = []
for device in device_type:
if device_type == -1:
device = torch.device("cpu")
else:
device = torch.device("cuda:{}".format(device))
gpus.append(device)
args["device"] = gpus
def _set_random():
torch.manual_seed(1)
torch.cuda.manual_seed(1)
torch.cuda.manual_seed_all(1)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def print_args(args):
for key, value in args.items():
logging.info("{}: {}".format(key, value))