-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizations.py
338 lines (276 loc) · 9.67 KB
/
visualizations.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import argparse
import os
import re
from collections import OrderedDict
import pandas as pd
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
from scipy.stats import mode
from tensorflow.python.framework.errors_impl import OutOfRangeError
if False:
from model import model_fn
from tfinput import test_input_fn
else:
from oxford_model import model_fn
from oxtfinput import test_input_fn
def cli_interface():
parser = argparse.ArgumentParser()
parser.add_argument('--csv', '-c', help='name of csv')
parser.add_argument('--dir', '-d', help='location of models')
parser.add_argument('--print', action='store_true', help='print results as well')
parser.add_argument('--plot', '-p', action='store_true', help='plot results')
return parser.parse_args()
def main():
args = cli_interface()
directory = args.dir
csv = args.csv
plot = args.plot
df = read_ensemble_series(directory)
df.to_csv(csv)
def read_ensemble_series(directory):
regex = re.compile(r'ensemble_num[0-9]*_iter([0-9]*)')
matches = [regex.match(check) for check in os.listdir(directory)]
idx = {}
for item in matches:
grp = int(item.groups()[0])
idx[grp] = idx.get(grp, []) + [directory + '/' + item.string]
acc = {}
for key in sorted(idx.keys()):
idx[key] = sorted(idx[key], key=numeric_sort)
acc[key] = read_ensemble(idx[key])
df = pd.DataFrame.from_dict(acc, orient='index')
df['iterations'] = df.index
return df
def read_ensemble(modeldirs, params='cifar10'):
# return a dictionary of ensemble accuracies at iter
if params == 'cifar10':
params = {
'img_dim': [32, 32, 3],
'y_size': 10,
'learning_rate': .0001
}
elif params == 'oxford':
params = {
'img_dim': [500, 500, 3],
'y_size': 17,
'learning_rate': .0001
}
models = []
for i in range(len(modeldirs)):
models.append(['worker' + str(i), tf.estimator.Estimator(
model_fn=model_fn, params=params, model_dir=modeldirs[i]
)])
accuracy = ensemble_accuracy(
input_fn=test_input_fn, models=models,
print_results=True, operation=(by_mean, by_mode, by_certainty))
return accuracy
def ensemble_accuracy(input_fn, models=None, print_results=True, operation=None, output_cls=False):
# get true classes:
sess = tf.Session()
_, label_batch = input_fn()
labels = None
while True:
try:
bincls = sess.run(label_batch)
except OutOfRangeError:
break
lab = np.argmax(bincls, axis=1)
if labels is None:
labels = lab
else:
labels = np.hstack((labels, lab))
collection = []
accuracies = {}
best_of = None
best = 0.0
for model in models:
name = model[0]
model = model[1]
assert (isinstance(model, tf.estimator.Estimator))
prediction = model.predict(input_fn=input_fn)
softmax = [p for p in prediction]
collection.append(softmax)
mdl_pred = np.argmax(softmax, axis=1)
val = np.where(labels == mdl_pred, 1, 0)
accuracies[name] = np.sum(val) / len(val)
if print_results:
print(name + ' accuracy: %s' % accuracies[name])
if operation == 'best_worker':
if best < accuracies[name]:
best = accuracies[name]
best_of = mdl_pred
best_of_name = name
if operation == 'best_worker':
return best_of, labels
collection = np.array(collection)
if not hasattr(operation, '__iter__'):
operation = (operation,)
for op in operation:
results = op(collection)
val = np.where(results == labels, 1, 0)
accuracies[op.__name__] = np.sum(val)/len(val)
if print_results:
print('metric: %s' % op.__name__)
print('ensemble accuracy %s' % accuracies[op.__name__])
if output_cls:
return results, labels
else:
return accuracies
def by_mean(softmaxes):
# how to evaluate ensemble
means = np.mean(softmaxes, axis=0)
argmaxes = np.argmax(means, axis=1)
return argmaxes
def by_certainty(softmaxes):
modes = np.max(softmaxes, axis=0)
argmaxes = np.argmax(modes, axis=1)
return argmaxes
def by_mode(softmaxes):
preds = np.argmax(softmaxes, axis=2)
modes = mode(preds, axis=0).mode.flatten()
return modes
def numeric_sort(x):
regex = re.compile('(\d+)')
nums = regex.split(x)
return [int(y) if y.isdigit() else y for y in nums]
def plot_helper(df, ratio=1, name=None, yrange=None):
if isinstance(df, str):
df = pd.read_csv(df, index_col=0)
colors = ['r', 'b', 'g']
amnt = int(len(df['iterations'])*(1 - ratio))
for item in df.columns:
if item == 'iterations':
continue
if 'worker' in item:
plt.plot(df['iterations'][amnt:], df[item][amnt:], 'k--')
else:
plt.plot(df['iterations'][amnt:], df[item][amnt:], colors.pop() + '-')
ax = plt.gca()
ax.minorticks_on()
ax.tick_params(labelright=True)
ax.yaxis.set_ticks_position('both')
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(zip(labels, handles))
i = 1
while by_label.pop('worker%s' % i, False):
i += 1
by_label['workers'] = by_label.pop('worker0')
if name:
plt.title(name)
if yrange:
axes = plt.gca()
axes.set_ylim(yrange)
plt.legend(list(by_label.values()), list(by_label.keys()))
plt.xlabel('iterations')
plt.ylabel('test accuracy')
plt.show()
def best_worker(df, ratio=1, name=None, yrange=None):
if isinstance(df, str):
df = pd.read_csv(df, index_col=0)
colors = ['r', 'b', 'g']
workers = df.select(lambda col: col.startswith('worker'), axis=1)
df['best_worker'] = workers.max(axis=1)
amnt = int(len(df['iterations'])*(1 - ratio))
for item in df.columns:
if item == 'iterations':
continue
if item == 'best_worker':
plt.plot(df['iterations'][amnt:], df[item][amnt:], 'k--')
elif 'worker' in item:
pass
else:
plt.plot(df['iterations'][amnt:], df[item][amnt:], colors.pop() + '-')
ax = plt.gca()
ax.minorticks_on()
ax.tick_params(labelright=True)
ax.yaxis.set_ticks_position('both')
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(zip(labels, handles))
i = 0
while by_label.pop('worker%s' % i, False):
i += 1
if name:
plt.title(name)
if yrange:
axes = plt.gca()
axes.set_ylim(yrange)
plt.legend(list(by_label.values()), list(by_label.keys()))
plt.xlabel('iterations')
plt.ylabel('test accuracy')
plt.show()
def comparison(df_dict, ratio=1, measure='mean', name=None, yrange=None):
for key in df_dict.keys():
if isinstance(df_dict[key], str):
df_dict[key] = pd.read_csv(df_dict[key], index_col=0)
workers = df_dict[key].select(lambda col: col.startswith('worker'), axis=1)
df_dict[key]['best individual'] = workers.max(axis=1)
amnt = int(len(df_dict[key]['iterations']) * (1 - ratio))
for item in df_dict[key].columns:
if item == 'iterations':
continue
elif 'worker' in item:
pass
elif measure in item:
tmpdf = df_dict[key].rename(columns={item: key})
plt.plot(df_dict[key]['iterations'][amnt:], tmpdf[key][amnt:])
ax = plt.gca()
ax.minorticks_on()
ax.tick_params(labelright=True)
ax.yaxis.set_ticks_position('both')
plt.legend()
if name:
plt.title(name)
else:
plt.title(measure)
if yrange:
axes = plt.gca()
axes.set_ylim(yrange)
plt.xlabel('iterations')
plt.ylabel('test accuracy')
plt.show()
if __name__ == '__main__':
if False:
main()
if True:
name='4 workers performance'
fig = plt.figure(0)
plot_helper('csvs/bigbatchmodel4.csv', 1, name=name, yrange=[.65, .825])
fig.savefig('outputs/' + name + '.png')
if True:
name='4 worker ceiling'
fig = plt.figure(1)
best_worker('csvs/bigbatchmodel4.csv', 1, name=name, yrange=[.65, .825])
fig.savefig('outputs/' + name + '.png')
if False:
df_dict = {
'4 workers': 'csvs/bigbatchmodel4.csv',
'8 workers': 'csvs/bigbatchmodel8.csv',
'12 workers': 'csvs/bigbatchmodel12.csv',
'16 workers': 'csvs/bigbatchmodel16.csv',
'20 workers': 'csvs/bigbatchmodel20.csv',
'24 workers': 'csvs/bigbatchmodel24.csv'
}
fig = plt.figure(2)
comparison(df_dict, ratio=.75, measure='mean', name='ensemble mean', yrange=[.78, .82])
fig.savefig('outputs/' + 'mean75pct.png')
fig = plt.figure(3)
comparison(df_dict, ratio=.75, measure='mode', name='ensemble mode', yrange=[.78, .82])
fig.savefig('outputs/' + 'mode75pct.png')
fig = plt.figure(4)
comparison(df_dict, ratio=.75, measure='certainty', name='ensemble max', yrange=[.78, .82])
fig.savefig('outputs/' + 'max75pct.png')
if True:
pass
#cifar_params = {
# 'img_dim': [32, 32, 3],
# 'y_size': 10,
# 'learning_rate': .001
#}
#models = []
#for i in range(6):
# models.append(['task' + str(i), tf.estimator.Estimator(
# model_fn=model_fn, params=cifar_params, model_dir='ensemble%s' % i
# )])
#
#ensemble_accuracy(test_input_fn, models, operation=(by_mean, by_mode, by_certainty))