-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccuracy.py
More file actions
355 lines (295 loc) · 16.2 KB
/
accuracy.py
File metadata and controls
355 lines (295 loc) · 16.2 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import json
import os
import signal
from contextlib import redirect_stdout
from datetime import datetime
from multiprocessing import Process
from time import sleep, time
import numpy as np
from tqdm import tqdm
from agent.agent_representation import AgentRepresentation
from agent.constructor import get_agent, build_agent_from_repr
from api import create_app
from environment.constructor import get_controller
from environment.reward.abstract_reward import AbstractReward
from environment.settings import EVALUATION_CSV_FOLDER_PATH
from environment.state_handling import initialize_storage, cleanup_storage, set_prototype, get_num_configs, \
get_storage_path, set_simulation, get_instance_number, setup_child_instance, is_api_running, set_api_running
"""
Want to change evaluated prototype?
1) adjust initial EPSILON (setup) to the one used in evaluated prototype (usually in controller)
2) adjust the known best action (setup) if necessary (depending on AD and reward system)
3) adjust the filename of the logfile (setup) to match the prototype settings (in controller/agent/model)
4) set prototype to evaluated prototype number in setup below (start of try-block)
5) adjust simulation settings if required (start of try-block)
"""
def start_api(instance_number):
setup_child_instance(instance_number)
app = create_app()
print("==============================\nStart API\n==============================")
set_api_running()
app.run(host="0.0.0.0", port=5000)
def kill_process(proc):
print("kill Process", proc)
proc.terminate()
print("killed Process", proc)
timeout = 10
start = time()
while proc.is_alive() and time() - start < timeout:
sleep(1)
if proc.is_alive():
proc.kill()
print("...we had to put it down", proc)
sleep(2)
if proc.is_alive():
os.kill(proc.pid, signal.SIGKILL)
print("...die already", proc)
else:
print(proc, "now dead")
else:
print(proc, "now dead")
def evaluate_agent(agent, weights1, weights2, bias_weights1, bias_weights2, EPSILON):
accuracies_overall = {"total": 0}
accuracies_configs = {}
num_configs = get_num_configs()
for config in range(num_configs): # ensure all keys exist for accessing through KNOWN_BEST_ACTION
accuracies_overall[config] = 0
# eval normal
print("Normal")
normal_fp_dir = os.path.join(EVALUATION_CSV_FOLDER_PATH, "normal")
fp_files = os.listdir(normal_fp_dir)
accuracies_configs["normal"] = {"total": 0}
for fp_file in tqdm(fp_files):
# collect selected initial fingerprint
with open(os.path.join(normal_fp_dir, fp_file)) as file:
fp = file.readline()[1:-1].replace(" ", "")
state = transform_fp(fp)
# predict next action
curr_hidden, curr_q_values, selected_action = agent.predict(weights1, weights2, bias_weights1,
bias_weights2, EPSILON, state)
if selected_action not in accuracies_overall.keys():
accuracies_overall[selected_action] = 1
else:
accuracies_overall[selected_action] += 1
accuracies_overall["total"] += 1
if selected_action not in accuracies_configs["normal"].keys():
accuracies_configs["normal"][selected_action] = 1
else:
accuracies_configs["normal"][selected_action] += 1
accuracies_configs["normal"]["total"] += 1
# eval infected
for config in range(num_configs):
print("\nConfig", config)
config_fp_dir = os.path.join(EVALUATION_CSV_FOLDER_PATH, "infected-c{}".format(config))
fp_files = os.listdir(config_fp_dir)
accuracies_configs[config] = {"total": 0}
for fp_file in tqdm(fp_files):
# collect selected initial fingerprint
with open(os.path.join(config_fp_dir, fp_file)) as file:
fp = file.readline()[1:-1].replace(" ", "")
state = transform_fp(fp)
# predict next action
curr_hidden, curr_q_values, selected_action = agent.predict(weights1, weights2, bias_weights1,
bias_weights2, EPSILON, state)
if selected_action not in accuracies_overall.keys():
accuracies_overall[selected_action] = 1
else:
accuracies_overall[selected_action] += 1
accuracies_overall["total"] += 1
if selected_action not in accuracies_configs[config].keys():
accuracies_configs[config][selected_action] = 1
else:
accuracies_configs[config][selected_action] += 1
accuracies_configs[config]["total"] += 1
return accuracies_overall, accuracies_configs
def find_agent_file(timestamp):
storage_path = get_storage_path()
files = os.listdir(storage_path)
filtered = list(filter(lambda f: f.startswith("agent={}".format(timestamp)), files))
return os.path.join(storage_path, filtered.pop())
def transform_fp(fp):
split_to_floats = list(map(lambda feat: float(feat), fp.split(",")))
return np.asarray(split_to_floats).reshape(-1, 1) # shape (F, 1)
def print_accuracy_table(accuracies_overall, accuracies_configs, logs):
num_configs = get_num_configs()
print("----- Per Config -----")
logs.append("----- Per Config -----")
# from normal states
line = []
key_keys = sorted(list(filter(lambda k: not isinstance(k, str), list(accuracies_configs["normal"].keys()))))
for key in range(num_configs):
value = accuracies_configs["normal"][key] if key in key_keys else 0
line.append("c{} {}% ({}/{})\t".format(key, "%05.2f" % (value / accuracies_configs["normal"]["total"] * 100),
value, accuracies_configs["normal"]["total"]))
print("Normal:\t", *line, sep="\t")
logs.append("\t".join(["Normal:\t", *line]).strip())
# from infected states
config_keys = sorted(list(filter(lambda k: not isinstance(k, str), list(accuracies_configs.keys()))))
for config in config_keys:
line = []
key_keys = sorted(list(filter(lambda k: not isinstance(k, str), list(accuracies_configs[config].keys()))))
for key in range(num_configs):
value = accuracies_configs[config][key] if key in key_keys else 0
line.append("c{} {}% ({}/{})\t".format(key, "%05.2f" % (value / accuracies_configs[config]["total"] * 100),
value, accuracies_configs[config]["total"]))
print("Config {}:".format(config), *line, sep="\t")
logs.append("\t".join(["Config {}:".format(config), *line]).strip())
print("\n----- Overall -----")
logs.append("\n----- Overall -----")
overall_keys = sorted(list(filter(lambda k: not isinstance(k, str), list(accuracies_overall.keys()))))
for config in range(num_configs):
value = accuracies_overall[config] if config in overall_keys else 0
print("Config {}:\t{}% ({}/{})".format(config, "%05.2f" % (value / accuracies_overall["total"] * 100), value,
accuracies_overall["total"]))
logs.append("Config {}:\t{}% ({}/{})".format(config, "%05.2f" % (value / accuracies_overall["total"] * 100),
value, accuracies_overall["total"]))
return logs
if __name__ == "__main__":
# ==============================
# SETUP
# ==============================
total_start = time()
prototype_description = "p9-1000e=e0.4d0.01a0.0050y0.10=Log-SiLU=h40=he"
EPSILON = 0.4
KNOWN_BEST_ACTION = 3
log_file = os.path.join(os.path.curdir, "storage",
"accuracy-report={}={}.txt".format(datetime.now().strftime("%Y-%m-%d--%H-%M-%S"),
prototype_description))
logs = []
print("========== PREPARE ENVIRONMENT ==========\nAD evaluation is written to log file directly")
initialize_storage()
procs = []
try:
set_prototype("9")
simulated = True
set_simulation(simulated)
np.random.seed(42)
# ==============================
# WRITE AD EVALUATION TO LOG FILE
# ==============================
with open(log_file, "w+") as f:
with redirect_stdout(f):
print("========== PREPARE ENVIRONMENT ==========")
AbstractReward.prepare_reward_computation()
# ==============================
# EVAL UNTRAINED AGENT
# ==============================
print("\n========== MEASURE ACCURACY (INITIAL) ==========")
logs.append("\n========== MEASURE ACCURACY (INITIAL) ==========")
agent = get_agent()
print("Evaluating agent {} with settings {}.\n".format(agent, prototype_description))
logs.append("Evaluating agent {} with settings {}.\n".format(agent, prototype_description))
weights1, weights2, bias_weights1, bias_weights2 = agent.initialize_network()
logs.append("Agent representation")
logs.append("> prototype: {}".format(agent))
logs.append("> weights1: {}".format(weights1.tolist()))
logs.append("> weights2: {}".format(weights2.tolist()))
logs.append("> bias_weights1: {}".format(bias_weights1.tolist()))
logs.append("> bias_weights2: {}".format(bias_weights2.tolist()))
logs.append("> epsilon: {}, learn_rate: {}, num_input: {}, num_hidden: {}, num_output: {}".format(
EPSILON, agent.learn_rate, agent.num_input, agent.num_hidden, agent.num_output))
start = time()
accuracies_initial_overall, accuracies_initial_configs = evaluate_agent(agent, weights1, weights2,
bias_weights1,
bias_weights2, EPSILON)
duration = time() - start
print("\nEvaluation took {}s, roughly {}min.".format("%.3f" % duration, "%.1f" % (duration / 60)))
logs.append("\nEvaluation took {}s, roughly {}min.".format("%.3f" % duration, "%.1f" % (duration / 60)))
# ==============================
# TRAINING AGENT
# ==============================
if not simulated:
# Start API listener
proc_api = Process(target=start_api, args=(get_instance_number(),))
procs.append(proc_api)
proc_api.start()
print("\nWaiting for API...")
while not is_api_running():
sleep(1)
print("\n========== TRAIN AGENT ==========")
logs.append("\n========== TRAIN AGENT ==========")
controller = get_controller()
training_start = time()
timestamp = datetime.now().strftime("%Y-%m-%d--%H-%M-%S")
final_q_values, _ = controller.loop_episodes(agent)
training_duration = time() - training_start
logs.append("Agent and plots timestamp: {}".format(timestamp))
print("Training took {}s, roughly {}min.".format("%.3f" % training_duration, "%.1f" % (training_duration / 60)))
logs.append("Training took {}s, roughly {}min.".format("%.3f" % training_duration,
"%.1f" % (training_duration / 60)))
# ==============================
# EVAL TRAINED AGENT
# ==============================
print("\n========== MEASURE ACCURACY (TRAINED) ==========")
logs.append("\n========== MEASURE ACCURACY (TRAINED) ==========")
with open(find_agent_file(timestamp), "r") as agent_file:
repr_dict = json.load(agent_file)
representation = (
repr_dict["weights1"], repr_dict["weights2"], repr_dict["bias_weights1"], repr_dict["bias_weights2"],
repr_dict["epsilon"], repr_dict["learn_rate"], repr_dict["num_input"], repr_dict["num_hidden"],
repr_dict["num_output"]
)
agent = build_agent_from_repr(AgentRepresentation(*representation))
final_epsilon = repr_dict["epsilon"]
weights1, weights2, bias_weights1, bias_weights2 = agent.initialize_network()
logs.append("Agent representation")
logs.append("> prototype: {}".format(agent))
logs.append("> weights1: {}".format(weights1.tolist()))
logs.append("> weights2: {}".format(weights2.tolist()))
logs.append("> bias_weights1: {}".format(bias_weights1.tolist()))
logs.append("> bias_weights2: {}".format(bias_weights2.tolist()))
logs.append("> epsilon: {}, learn_rate: {}, num_input: {}, num_hidden: {}, num_output: {}".format(
final_epsilon, agent.learn_rate, agent.num_input, agent.num_hidden, agent.num_output))
start = time()
accuracies_trained_overall, accuracies_trained_configs = evaluate_agent(agent, weights1, weights2,
bias_weights1,
bias_weights2, final_epsilon)
duration = time() - start
print("\nEvaluation took {}s, roughly {}min.".format("%.3f" % duration, "%.1f" % (duration / 60)))
logs.append("\nEvaluation took {}s, roughly {}min.".format("%.3f" % duration, "%.1f" % (duration / 60)))
# ==============================
# COMPUTE ACCURACY TABLES
# ==============================
print("\n========== ACCURACY TABLE (INITIAL) ==========")
logs.append("\n========== ACCURACY TABLE (INITIAL) ==========")
logs = print_accuracy_table(accuracies_initial_overall, accuracies_initial_configs, logs)
print("\n========== ACCURACY TABLE (TRAINED) ==========")
logs.append("\n========== ACCURACY TABLE (TRAINED) ==========")
logs = print_accuracy_table(accuracies_trained_overall, accuracies_trained_configs, logs)
# ==============================
# SHOW RESULTS
# ==============================
print("\n========== RESULTS ==========")
logs.append("\n========== RESULTS ==========")
# highlight change through training
val_initial = accuracies_initial_overall[KNOWN_BEST_ACTION]
known_best_initial = "{}% ({}/{})".format("%05.2f" % (val_initial / accuracies_initial_overall["total"] * 100),
val_initial, accuracies_initial_overall["total"])
val_trained = accuracies_trained_overall[KNOWN_BEST_ACTION]
known_best_trained = "{}% ({}/{})".format("%05.2f" % (val_trained / accuracies_initial_overall["total"] * 100),
val_trained, accuracies_initial_overall["total"])
print("For known best action {}: from {} to {}.".format(KNOWN_BEST_ACTION, known_best_initial,
known_best_trained))
logs.append("For known best action {}: from {} to {}.".format(KNOWN_BEST_ACTION, known_best_initial,
known_best_trained))
# report final Q-values
print("Final Q-Values:\n{}".format(final_q_values))
logs.append("Final Q-Values:\n{}".format(final_q_values))
# show time required for entire accuracy computation
total_duration = time() - total_start
print("Accuracy computation took {}s in total, roughly {}min.".format("%.3f" % total_duration,
"%.1f" % (total_duration / 60)))
logs.append("Accuracy computation took {}s in total, roughly {}min.".format("%.3f" % total_duration,
"%.1f" % (total_duration / 60)))
# ==============================
# WRITE LOGS TO LOG FILE
# ==============================
with open(log_file, "a") as file:
log_lines = list(map(lambda l: l + "\n", logs))
file.writelines(log_lines)
finally:
for proc in procs:
kill_process(proc)
print("- Parallel processes killed.")
cleanup_storage()
print("- Storage cleaned up.\n==============================")