-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
182 lines (151 loc) · 7.58 KB
/
run.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import argparse
import torch
import numpy as np
import yaml
import json
import random
from trainer import Trainer
def create_args():
# This function prepares the variables shared across demo.py
parser = argparse.ArgumentParser()
# Standard Args
parser.add_argument('--gpuid', nargs="+", type=int, default=[0],
help="The list o f gpuid, ex:--gpuid 3 1. Negative value means cpu-only")
parser.add_argument('--log_dir', type=str, default="outputs/out",
help="Save experiments results in dir for future plotting!")
parser.add_argument('--learner_type', type=str, default='default', help="The type (filename) of learner")
parser.add_argument('--learner_name', type=str, default='NormalNN', help="The class name of learner")
parser.add_argument('--query', type=str, default='vit', help="choose one of [poolformer]")
parser.add_argument('--debug_mode', type=int, default=0, metavar='N',
help="activate learner specific settings for debug_mode")
parser.add_argument('--repeat', type=int, default=1, help="Repeat the experiment N times")
parser.add_argument('--overwrite', type=int, default=0, metavar='N', help='Train regardless of whether saved model exists')
# CL Args
parser.add_argument('--oracle_flag', default=False, action='store_true', help='Upper bound for oracle')
parser.add_argument('--upper_bound_flag', default=False, action='store_true', help='Upper bound')
parser.add_argument('--memory', type=int, default=0, help="size of memory for replay")
parser.add_argument('--temp', type=float, default=2., dest='temp', help="temperature for distillation")
parser.add_argument('--DW', default=False, action='store_true', help='dataset balancing')
parser.add_argument('--prompt_param', nargs="+", type=float, default=[1, 1, 1],
help="e prompt pool size, e prompt length, g prompt length")
# Config Arg
parser.add_argument('--config', type=str, default="configs/config.yaml",
help="yaml experiment config input")
return parser
def get_args(argv):
parser=create_args()
args = parser.parse_args(argv)
config = yaml.load(open(args.config, 'r'), Loader=yaml.Loader)
config.update(vars(args))
return argparse.Namespace(**config)
# want to save everything printed to outfile
class Logger(object):
def __init__(self, name):
self.terminal = sys.stdout
self.log = open(name, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
self.log.flush()
if __name__ == '__main__':
args = get_args(sys.argv[1:])
# determinstic backend
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic=True
# duplicate output stream to output file
if not os.path.exists(args.log_dir): os.makedirs(args.log_dir)
log_out = args.log_dir + '/output.log'
sys.stdout = Logger(log_out)
# save args
with open(args.log_dir + '/args.yaml', 'w') as yaml_file:
yaml.dump(vars(args), yaml_file, default_flow_style=False)
metric_keys = ['acc','time',]
save_keys = ['global', 'pt', 'pt-local']
global_only = ['time']
avg_metrics = {}
for mkey in metric_keys:
avg_metrics[mkey] = {}
for skey in save_keys: avg_metrics[mkey][skey] = []
# load results
if args.overwrite:
start_r = 0
else:
try:
for mkey in metric_keys:
for skey in save_keys:
if (not (mkey in global_only)) or (skey == 'global'):
save_file = args.log_dir+'/results-'+mkey+'/'+skey+'.yaml'
if os.path.exists(save_file):
with open(save_file, 'r') as yaml_file:
yaml_result = yaml.safe_load(yaml_file)
avg_metrics[mkey][skey] = np.asarray(yaml_result['history'])
# next repeat needed
start_r = avg_metrics[metric_keys[0]][save_keys[0]].shape[-1]
# extend if more repeats left
if start_r < args.repeat:
max_task = avg_metrics['acc']['global'].shape[0]
for mkey in metric_keys:
avg_metrics[mkey]['global'] = np.append(avg_metrics[mkey]['global'], np.zeros((max_task,args.repeat-start_r)), axis=-1)
if (not (mkey in global_only)):
avg_metrics[mkey]['pt'] = np.append(avg_metrics[mkey]['pt'], np.zeros((max_task,max_task,args.repeat-start_r)), axis=-1)
avg_metrics[mkey]['pt-local'] = np.append(avg_metrics[mkey]['pt-local'], np.zeros((max_task,max_task,args.repeat-start_r)), axis=-1)
except:
start_r = 0
# start_r = 0
f_score_array = []
for r in range(start_r, args.repeat):
print('************************************')
print('* STARTING TRIAL ' + str(r+1))
print('************************************')
# set random seeds
seed = r
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# set up a trainer
trainer = Trainer(args, seed, metric_keys, save_keys)
# init total run metrics storage
max_task = trainer.max_task
if r == 0:
for mkey in metric_keys:
avg_metrics[mkey]['global'] = np.zeros((max_task,args.repeat))
if (not (mkey in global_only)):
avg_metrics[mkey]['pt'] = np.zeros((max_task,max_task,args.repeat))
avg_metrics[mkey]['pt-local'] = np.zeros((max_task,max_task,args.repeat))
# train model
avg_metrics = trainer.train(avg_metrics)
# evaluate model
avg_metrics, f_score = trainer.evaluate(avg_metrics)
f_score_array.append(f_score)
# save results
# for mkey in metric_keys:
# m_dir = args.log_dir+'/results-'+mkey+'/'
# if not os.path.exists(m_dir): os.makedirs(m_dir)
# for skey in save_keys:
# if (not (mkey in global_only)) or (skey == 'global'):
# save_file = m_dir+skey+'.yaml'
# result=avg_metrics[mkey][skey]
# yaml_results = {}
# if len(result.shape) > 2:
# yaml_results['mean'] = result[:,:,:r+1].mean(axis=2).tolist()
# if r>1: yaml_results['std'] = result[:,:,:r+1].std(axis=2).tolist()
# yaml_results['history'] = result[:,:,:r+1].tolist()
# else:
# yaml_results['mean'] = result[:,:r+1].mean(axis=1).tolist()
# if r>1: yaml_results['std'] = result[:,:r+1].std(axis=1).tolist()
# yaml_results['history'] = result[:,:r+1].tolist()
# with open(save_file, 'w') as yaml_file:
# yaml.dump(yaml_results, yaml_file, default_flow_style=False)
# Print the summary so far
print('===Summary of experiment repeats:',r+1,'/',args.repeat,'===')
for mkey in metric_keys:
print(mkey, ' | mean:', avg_metrics[mkey]['global'][-1,:r+1].mean(), 'std:', avg_metrics[mkey]['global'][-1,:r+1].std())
print ('F-score: mean {} std {}'.format(np.mean(f_score_array), np.std(f_score_array)))