-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
71 lines (58 loc) · 2.27 KB
/
run.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
import argparse
import config
import logging
import os
import server
from datetime import datetime
import time
# Set up parser
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default='./config.json',
help='Federated learning configuration file.')
parser.add_argument('-l', '--log', type=str, default='INFO',
help='Log messages level.')
args = parser.parse_args()
# Set logging
logging.basicConfig(
format='[%(levelname)s][%(asctime)s]: %(message)s', level=getattr(logging, args.log.upper()), datefmt='%H:%M:%S')
def main():
"""Run a federated learning simulation."""
# Read configuration file
fl_config = config.Config(args.config)
# Initialize server
fl_server = {
"basic": server.Server(fl_config),
"accavg": server.AccAvgServer(fl_config),
"directed": server.DirectedServer(fl_config),
"kcenter": server.KCenterServer(fl_config),
"kmeans": server.KMeansServer(fl_config),
"magavg": server.MagAvgServer(fl_config),
# "dqn": server.DQNServer(fl_config), # DQN server disabled
# "dqntrain": server.DQNTrainServer(fl_config), # DQN server disabled
"sync": server.SyncServer(fl_config),
"async": server.AsyncServer(fl_config),
}[fl_config.server]
fl_server.boot()
# Run federated learning
fl_server.run()
# Save and plot accuracy-time curve
if fl_config.server == "sync" or fl_config.server == "async":
d_str = datetime.now().strftime("%m-%d-%H-%M-%S")
network_type = fl_config.network.type
total_clients = str(fl_config.clients.total)
per_round = str(fl_config.clients.per_round)
fl_server.records.save_record('{}_{}_{}_{}outOf{}.csv'.format(
fl_config.server, d_str, network_type, per_round, total_clients
))
fl_server.records.plot_record('{}_{}_{}_{}outOf{}.png'.format(
fl_config.server, d_str, network_type, per_round, total_clients
))
# Delete global model
#os.remove(fl_config.paths.model + '/global')
if __name__ == "__main__":
st = time.time()
main()
elapsed = time.time() - st
logging.info('The program takes {} s'.format(
time.strftime("%Hh%Mm%Ss", time.gmtime(elapsed))
))