-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_metric_all.py
185 lines (166 loc) · 8.08 KB
/
get_metric_all.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
from dataclasses import replace
import os
from posixpath import split
import numpy as np
import argparse
import glob
from parse_log_to_result import *
import matplotlib.pyplot as plt
import numpy as np
import os
def plot_2d_matrix(matrix, label_ticks, title, save_name,save_path='../plot_avalanche',min_acc=None, max_acc=None, normalize_by_column=False, plotting_trick=False):
if plotting_trick:
matrix_score = matrix.copy()
matrix = matrix / matrix.mean(axis=0).reshape(1,-1)
cbar_str = "Test Accuracy"
format_score = lambda s : f"{s:.2f}"
elif normalize_by_column:
matrix = matrix / matrix.mean(axis=0).reshape(1,-1)
cbar_str = "Test Accuracy / Average Test accuracy per column"
format_score = lambda s : f"{s:.2f}"
matrix_score = matrix
else:
cbar_str = "Test Accuracy"
format_score = lambda s : f"{s:.2%}"
matrix_score = matrix
plt.figure(figsize=(10,10))
x = ["Test " + n for n in label_ticks]
y = ['Train ' + n for n in label_ticks]
p = plt.imshow(matrix, interpolation='none', cmap=f'Blues', vmin=min_acc, vmax=max_acc)
cbar = plt.colorbar()
cbar.ax.set_ylabel(f"{cbar_str}", rotation=-90, va="bottom")
cbar.ax.set_ylim(bottom=min_acc, top=max_acc)
cbar.ax.invert_yaxis()
plt.xticks(range(len(x)), x, fontsize=11, rotation = -90)
plt.yticks(range(len(y)), y, fontsize=11)
plt.title(title, fontsize=15)
for i in range(len(x)):
for j in range(len(y)):
text = plt.text(j, i, format_score(matrix_score[i, j]),
ha="center", va="center", color="black")
os.makedirs(save_path,exist_ok=True)
plt.savefig(os.path.join(save_path,'{}.pdf'.format(save_name)))
print("Image has been saved on {}".format(os.path.join(save_path,'{}.pdf'.format(save_name))))
argparser = argparse.ArgumentParser()
argparser.add_argument("--timestamp",type=int,default=10)
argparser.add_argument("--plot",type=int,default=0) # 1 for generating plot
argparser.add_argument("--verbose",type=int,default=0) # 1 for print out detailed metric
argparser.add_argument("--train_eval",type=int,default=0) # whether the code also include the evaluation of training set as well
args = argparser.parse_args()
logpath='clear10_moco_res50_public_private/metric'
log_list=sorted(os.listdir(logpath))
# unique_name=sorted(list(set(list(map(lambda log_name: log_name[:log_name.index('line')+4], log_list)))))
unique_name=sorted(list(set(list(map(lambda log_name: log_name.split(".")[0], log_list)))))
save_dict = {}
# each unique prefix
for name in unique_name:
all_name=glob.glob(os.path.join(logpath,name+"*"))
stat_list=[]
plot_list=[]
# for all element in same prefix
for elemt in all_name:
result_list=[]
log_file_name=os.path.join(logpath,elemt)
file=open(log_file_name, 'r')
while(True):
try:
line=file.readline()
except:
break
if('Top1_Acc_Stream/eval_phase/test_stream/Task0' in line):
try:
tmp_float_num = line.split()[-1]
if ',' in tmp_float_num:
tmp_float_num = tmp_float_num.replace(",", "")
result_list.append(float(tmp_float_num))
except:
import pdb
pdb.set_trace()
if not line:
break
file.close()
if(args.train_eval==1):
try:
lowerIndex=[i for i in range(10,200,20)]
upperIndex=[i for i in range(21,210,20)]
eval_index=[k for i in range(len(lowerIndex)) for k in range(lowerIndex[i],upperIndex[i]-1) ]
result_list=np.array(result_list)[eval_index]
except:
print('#################################################')
print('Skipping {} for removing train result'.format(name))
print('#################################################')
if(len(result_list)!=int(args.timestamp*args.timestamp)):
if('online' in name):
# assert np.max(result_list[:args.timestamp])<0.3
result_list=result_list[args.timestamp:]
stat_list.append(np.mean(result_list))
# print("{} count of {}, with mean of {}".format(name,len(result_list), np.mean(result_list)))
else:
result_list=np.array(result_list)
if('online' in name):
# assert np.max(result_list[:args.timestamp])<0.3
index_list=get_online_protocol_index(class_=args.timestamp)
else:
index_list=get_offline_protocol_index(class_=args.timestamp)
plot_list.append(result_list.reshape((args.timestamp,args.timestamp)))
result_list=[str(np.mean(result_list[np.array(item[1])])) for item in index_list.items()]
key_list=[item[0] for item in index_list.items()]
stat_list.append(result_list)
try:
stat_list=np.array(stat_list).astype(float)
stat_list=np.unique(stat_list,axis=0)
if(args.verbose==1):
print(stat_list)
print("{} with {} of mean of {} std of {} with {} elem".format(name,", ".join(key_list),np.mean(stat_list,axis=0).tolist(),np.std(stat_list,axis=0),len(all_name)))
except:
print('---------------------------------------')
print('skip {}'.format(name))
print('---------------------------------------')
if(args.plot==1 and 'online' not in name and 'Joint' not in name):
try:
plot_array=np.mean(np.array(plot_list),axis=0)
plot_2d_matrix(plot_array, [str(i) for i in range(1,11)], '',name, normalize_by_column=False, plotting_trick=False)
save_dict[name] = plot_array
except:
print('---------------------------------------')
print('Plot skipped {}'.format(name))
print('---------------------------------------')
totall_column = 10
for column in range(totall_column):
X1 = [1,2,3,4,5,6,7,8,9,10]
RWM = save_dict["test_metric_RWM"][:,column]
EWC = save_dict["test_metric_EWC"][:,column]
LwF = save_dict["test_metric_LwF"][:,column]
Naive = save_dict["test_metric_Naive"][:,column]
GDF = save_dict["test_metric_GDumbFinetune"][:,column]
CWRStar = save_dict["test_metric_CWRStar"][:,column]
GEM = save_dict["test_metric_GEM"][:,column]
AGEM = save_dict["test_metric_AGEMFixed"][:,column]
SI = save_dict["test_metric_SynapticIntelligence"][:,column]
Res = save_dict["test_metric_Reservoir"][:,column]
BRes = save_dict["test_metric_BiasReservoir_Dynamic_1"][:,column]
OWM = save_dict["test_metric_OWM"][:,column]
Replay = save_dict["test_metric_Replay"][:,column]
plt.figure()
plt.plot(X1, RWM, label="RWM", color="#0000FF", marker='*', linestyle="-")
plt.plot(X1, EWC, label="EWC", color="#3CB371", marker='v', linestyle="-")
plt.plot(X1, LwF, label="LwF", color="#B22222", marker='1', linestyle="-")
plt.plot(X1, Naive, label="Finetune", color="#808000", marker='2', linestyle="-")
plt.plot(X1, GDF, label="GDF", color="#FFA500", marker='3', linestyle="-")
plt.plot(X1, CWRStar, label="CWR", color="#98FB98", marker='4', linestyle="-")
plt.plot(X1, GEM, label="GEM", color="#A0522D", marker='+', linestyle="-")
plt.plot(X1, AGEM, label="AGEM", color="#EE82EE", marker='x', linestyle="-")
plt.plot(X1, SI, label="SI", color="#87CEEB", marker='>', linestyle="-")
plt.plot(X1, Res, label="RF", color="#BC8F8F", marker='<', linestyle="-")
plt.plot(X1, BRes, label="BRF", color="#B0E0E6", marker='s', linestyle="-")
plt.plot(X1, OWM, label="OWM", color="#FF6347", marker='p', linestyle="-")
plt.plot(X1, Replay, label="Replay", color="#FF69B4", marker='8', linestyle="-")
X_labels = [1,2,3,4,5,6,7,8,9,10]
plt.xticks(X1,X_labels,rotation=0)
plt.legend()
plt.title("Acc(%) on Experience {}".format(column + 1))
plt.xlabel("Experiences")
plt.ylabel("Acc(%)")
# plt.xlim([5,50])
plt.show()
plt.savefig("./rwm_clear_exp_{}.pdf".format(column + 1))