-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplot_results.py
248 lines (207 loc) · 8.43 KB
/
plot_results.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import argparse
import json
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
args = argparse.ArgumentParser()
args.add_argument('--results', type=str, default='vad_results.json')
args.add_argument('--keyword', type=str, default='val_auc')
args.add_argument('--keyword2', type=str, default='val_f1score')
args.add_argument('--stage', type=str, default=None)
args.add_argument('--n_stage', type=int, default=3)
args.add_argument('--count1d', action='store_true')
args.add_argument('--black_list', type=str, default='')
args.add_argument('--min_samples', type=int, default=1)
args.add_argument('--test', action='store_true')
stages_1d = ['bidirectional_GRU_stage',
'transformer_encoder_stage',
'simple_dense_stage',
'conformer_encoder_stage']
stages_2d = ['simple_conv_stage',
'another_conv_stage',
'res_basic_stage',
'res_bottleneck_stage',
'dense_net_stage',
# 'sepformer_stage',
'xception_basic_stage']
# 'identity_block']
stages_total = stages_1d + stages_2d
def is_1d(block):
return block in stages_1d
def get_block_keys(config):
return sorted([key for key in config.keys()
if key.startswith('BLOCK') and not key.endswith('ARGS')])
def count_blocks(config, criteria=is_1d, include_seddoa=False):
keys = get_block_keys(config)
if include_seddoa:
keys.extend(['SED', 'DOA'])
return sum([criteria(config[key]) for key in keys])
def filter_fn(pairs, fn):
return [pair for pair in pairs if fn(pair)]
def plot_pairs(pairs, keyword='test_f', label=None, plot=None):
if plot is None:
plot = plt
plot.plot([x['perf'][keyword] for x in pairs],
np.linspace(0, 1, len(pairs)),
label=label)
def sort_pairs(pairs, keyword='test_f'):
return sorted(pairs, key=lambda x: x['perf'][keyword], reverse=True)
if __name__ == '__main__':
config = args.parse_args()
pairs = []
for j_file in config.results.split(','):
if not j_file.endswith('.json'):
j_file = f'{j_file}.json'
with open(j_file, 'r') as f:
results = json.load(f)
for key in results.keys():
if key.isdigit():
pairs.append(results[key])
# add f1score
for pair in pairs:
precision = pair['perf']['val_precision'][0]
recall = pair['perf']['val_recall'][0]
f1 = 2 * precision * recall / (precision + recall + 1e-8)
pair['perf']['val_f1score'] = f1
keyword = config.keyword
# black list
for stage in config.black_list.split(','):
pairs = filter_fn(
pairs,
lambda x: count_blocks(x['config'], lambda x: x == stage) == 0)
'''
pairs = filter_fn(
pairs,
lambda x: x['config']['BLOCK0'] != 'res_bottleneck_stage')
pairs = filter_fn(
pairs,
lambda x: x['config']['BLOCK1'] == 'res_bottleneck_stage')
'''
perfs = np.array([x['perf'][keyword][0] for x in pairs])
print('total', np.mean(perfs), np.std(perfs), len(perfs))
# plot total
# pairs = sort_pairs(pairs, keyword=keyword)
# plot_pairs(pairs, keyword=keyword, label=f'total({len(pairs)})')
# print(pairs[:5])
if config.test:
import stage_complexity
def extract_feats(config):
config = config['config']
keys = get_block_keys(config)
'''
strides = [
config[f'{key}_ARGS'].get('strides',
config[f'{key}_ARGS'].get('pool_size', [1, 1]))
for key in keys]
result = [0, 0] # , 0, 0]
for i, s in enumerate(strides):
result[i] = s[-1] # s[-1]-1] += 1
return result
'''
# WIDTH
shape = [7, 80, 1]
feats = []
cxs = []
for key in keys:
cx, shape = getattr(stage_complexity,
f'{config[f"{key}"]}_complexity')(
config[f'{key}_ARGS'], shape)
feats.append(shape[-1])
cxs.append(cx)
mapper = {
'simple_conv_stage': 1,
'another_conv_stage': 2,
'res_basic_stage': 2,
'res_bottleneck_stage': 3,
'dense_net_stage': 2,
'xception_basic_stage': 3,
}
feats = [config[f'{key}_ARGS']['depth'] * 1 # mapper[config[key]]
for key in keys]
if feats[0] > feats[1]:
result = -1
elif feats[0] < feats[1]:
result = 1
else:
result = 0
return result
def extract_feats3(config):
config = config['config']
keys = get_block_keys(config)
feats = [config[f'{key}_ARGS']['groups'] for key in keys]
feats = config['BLOCK1_ARGS']['bottleneck_ratio']
result = 0
feats = [config['BLOCK0_ARGS']['groups'],
config['BLOCK1_ARGS']['groups']]
return feats
combinations = [[j, i] for i in [0, 0.5, 1] for j in [0, 0.5, 1]]
# combinations = [0.25, 0.35, 0.5, 0.7, 1, 1.41, 2, 2.83, 4]
combinations = [[i, j] for i in range(1, 4) for j in range(1, 4)]
combinations = [[i, j] for i in [0, 0.5, 1] for j in [0, 0.5, 1]]
# combinations = [1, 2]
combinations = [-1, 0, 1]
perfs = [[np.log(1-x['perf'][keyword][0]) for x in pairs]]
labels = ['total']
for i, s in enumerate(combinations):
new_pairs = filter_fn(
pairs,
lambda x: extract_feats(x) == s)
new_pairs = sort_pairs(new_pairs, keyword=keyword)
labels.append(f'{s}({len(new_pairs)})')
if len(new_pairs) > 0:
perfs.append([np.log(1-x['perf'][keyword][0]) for x in new_pairs])
else:
perfs.append([np.mean(perfs[0])] * 3)
'''
labels = ['filter0', 'filter1', 'depth0', 'depth1',
'groups0', 'groups1', 'strides', 'bottleneck_ratio']
perfs = [
# [p['config']['BLOCK0_ARGS']['filters'] for p in pairs],
# [p['config']['BLOCK1_ARGS']['filters'] for p in pairs],
[p['config']['BLOCK0_ARGS']['depth'] for p in pairs],
[p['config']['BLOCK1_ARGS']['depth'] for p in pairs],
# [p['config']['BLOCK0_ARGS']['groups'] for p in pairs],
# [p['config']['BLOCK1_ARGS']['groups'] for p in pairs],
# [p['config']['BLOCK1_ARGS']['strides'][-1] for p in pairs],
# [p['config']['BLOCK1_ARGS']['bottleneck_ratio'] for p in pairs],
]
plt.plot(perfs)
'''
fig, ax = plt.subplots()
ax.set_xticks(np.arange(1, len(labels)+1))
ax.set_xticklabels(labels)
ax.violinplot(perfs, showmeans=True, showmedians=True)
elif config.stage:
for i in range(config.n_stage + 1):
new_pairs = filter_fn(
pairs,
lambda x: count_blocks(x['config'],
lambda x: x == config.stage) == i)
if len(new_pairs) >= config.min_samples:
plot_pairs(sort_pairs(new_pairs, keyword=keyword),
keyword=keyword,
label=f'{i} {config.stage}({len(new_pairs)})')
elif config.count1d:
for i in range(config.n_stage + 1):
new_pairs = filter_fn(pairs,
lambda x: count_blocks(x['config']) == i)
if len(new_pairs) >= config.min_samples:
plot_pairs(sort_pairs(new_pairs, keyword=keyword),
keyword=keyword,
label=f'{i} 1d stages({len(new_pairs)})')
else:
for i, stage in enumerate(stages_total):
new_pairs = filter_fn(
pairs,
# lambda x: x['config']['BLOCK1'] == stage)
lambda x: count_blocks(x['config'],
lambda x: x == stage) > 0)
if len(new_pairs) >= config.min_samples:
plot_pairs(sort_pairs(new_pairs, keyword=keyword),
keyword=keyword,
label=f'>0 {stage}({len(new_pairs)})')
plt.title(keyword)
plt.legend()
plt.show()
print(len(pairs))