-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_history.py
executable file
·175 lines (138 loc) · 6.76 KB
/
plot_history.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
#!/usr/bin/env python3
# coding: utf-8
__author__ = 'Rafael Teixeira'
__version__ = '0.1'
__email__ = 'rafaelgteixeira@ua.pt'
__status__ = 'Development'
import argparse
import json
import pathlib
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
from functools import partial
def plot_model_history(input_folder, file_name):
plt.rcParams.update({'font.size': 22})
output = pathlib.Path(input_folder)
model_history = json.load(open(output/ file_name))
for key in model_history:
if key != "times":
state = 0
points = []
values = list(np.arange(0.50,1,0.05))
for i in range(len(model_history[key])):
if model_history[key][i] >= values[state]:
while state < len(values) and model_history[key][i] >= values[state]:
state += 1
try:
points.append((i+1, model_history[key][i], model_history["times"]["global_times"][i]))
except:
points.append((i+1, model_history[key][i], model_history["times"][i]))
if state == len(values):
break
n_epochs = len(model_history[key])
fig = plt.figure(figsize=(12, 8))
plt.plot(range(1, n_epochs+1), model_history[key], label="Validation " +key)
for i in range(len(points)):
x = points[i][0]
y = points[i][1]
plt.plot(x, y, 'bo')
plt.text(x + 2.5, y-0.02, "%.2f" % (points[i][2] / 60), fontsize=18)
if key == "mcc_val" or key == "mcc":
print("%6.2f %6.2f" %(y, points[i][2] / 60))
plt.xlabel("Nº Epochs")
plt.ylabel(key)
plt.title("Evolution of the "+key+" of the model")
plt.legend()
fig.savefig(output /(key+".png"), dpi=300.0, bbox_inches='tight', format="png", orientation="landscape")
plt.close('all')
def get_average_times(folder, n_batches):
output = pathlib.Path(folder)
plt.rcParams.update({'font.size': 16})
results_list = []
for folder in output.glob("*"):
if folder.is_dir():
average_results = {
"train" : [],
"comm_send" : [],
"comm_recv" : [],
"conv_send" : [],
"conv_recv" : [],
"epochs" : []
}
for file in folder.glob("**/worker*"):
model_history = json.load(open(file))
for key in average_results:
if key != "epochs":
if "decentralized" in str(folder):
average_results[key].append(sum(model_history["times"][key])/len(model_history["times"][key]))
else:
temp_array = []
for i in range(0, len(model_history["times"][key]), n_batches):
temp_array.append(sum(model_history["times"][key][i:i+n_batches]))
average_results[key].append(sum(temp_array)/len(temp_array))
results = [np.log10(10000*sum(average_results[key])/len(average_results[key]) )for key in average_results if key != "epochs" ]
#results = [sum(average_results[key])/len(average_results[key]) for key in average_results if key != "epochs" ]
results_list.append([str(folder).split("/")[-1].replace("_", " ").capitalize()] + results)
df = pd.DataFrame(results_list,
columns=['FL approach', 'Training', 'Conv. send', 'Comm. send.', 'Comm. recv.', 'Conv. recv.'])
ax = df.plot(x='FL approach', kind='bar', stacked=True, figsize=(12, 8))
plt.xticks(rotation=0)
ax.get_yaxis().set_ticklabels([])
ax.get_figure().savefig(output /("times_comparison.pdf"), dpi=300.0, bbox_inches='tight', format="pdf", orientation="landscape")
plt.close('all')
def get_average_times_single(folder_fl, folder_single, n_batches):
output = pathlib.Path(folder_fl)
plt.rcParams.update({'font.size': 16})
results_list = []
average_results = {
"train" : [],
"comm_send" : [],
"comm_recv" : [],
"conv_send" : [],
"conv_recv" : [],
"epochs" : []
}
for file in output.glob("**/worker*"):
model_history = json.load(open(file))
for key in average_results:
if key != "epochs":
if "decentralized" in str(output):
average_results[key].append(sum(model_history["times"][key])/len(model_history["times"][key]))
else:
temp_array = []
for i in range(0, len(model_history["times"][key]), n_batches):
temp_array.append(sum(model_history["times"][key][i:i+n_batches]))
average_results[key].append(sum(temp_array)/len(temp_array))
results = [np.log10(10000*sum(average_results[key])/len(average_results[key]) )for key in average_results if key != "epochs" ]
results_list.append([str(output).split("/")[-1].replace("_", " ").capitalize()] + results)
single_folder = pathlib.Path(folder_single)
model_history = json.load(open(single_folder/ "train_history.json"))
times = [model_history["times"][0]]
for idx, time in enumerate(model_history["times"][1:]):
times.append(time - model_history["times"][idx-1])
results = [0]*len(results)
results[0] = np.log10(10000*sum(times)/len(times))
results_list.append(["Single host"] + results)
df = pd.DataFrame(results_list,
columns=['Training approach', 'Training', 'Conv. send', 'Comm. send.', 'Comm. recv.', 'Conv. recv.'])
ax = df.plot(x='Training approach', kind='bar', stacked=True, figsize=(12, 8))
plt.xticks(rotation=0)
ax.get_yaxis().set_ticklabels([])
ax.get_figure().savefig(output /("times_comparison.png"), dpi=300.0, bbox_inches='tight', format="png", orientation="landscape")
plt.close('all')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Train and test the model')
parser.add_argument('-f', type=str, help='Input/output folder', default='results/')
parser.add_argument('-s', type=str, help='Server file name', default='server.json')
parser.add_argument('-n', type=bool, help='Number of batches', default=8)
parser.add_argument('-g', type=str, help='Graphic type', default="bar")
args = parser.parse_args()
if args.g == "linear":
plot_model_history(args.f, args.s)
elif args.g == "bar":
# If IOT and 128 batch size n_batch = 299
# If slicing n_batch=292
get_average_times(args.f, args.n)
else:
get_average_times_single(args.f, args.s, args.n)