-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathmain.py
More file actions
205 lines (159 loc) · 7.13 KB
/
Copy pathmain.py
File metadata and controls
205 lines (159 loc) · 7.13 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
"""Script to run the baselines."""
import argparse
import importlib
import numpy as np
import os
import sys
import random
import copy
import tensorflow as tf
import metrics.writer as metrics_writer
from baseline_constants import MAIN_PARAMS, MODEL_PARAMS
from client import Client
from server import Server
from model import ServerModel
from utils.args import parse_args
from utils.model_utils import read_data
STAT_METRICS_PATH = 'metrics/metrics_stat.csv'
SYS_METRICS_PATH = 'metrics/metrics_sys.csv'
def main():
args = parse_args()
# Set the random seed if provided (affects client sampling, and batching)
random.seed(1 + args.seed)
np.random.seed(12 + args.seed)
tf.set_random_seed(123 + args.seed)
model_path = '%s/%s.py' % (args.dataset, args.model)
if not os.path.exists(model_path):
print('Please specify a valid dataset and a valid model.')
model_path = '%s.%s' % (args.dataset, args.model)
print('############################## %s ##############################' % model_path)
# todo tdye
model_info = {
'model_path': model_path
}
# mod = importlib.import_module(model_path)
# ClientModel = getattr(mod, 'ClientModel')
tup = MAIN_PARAMS[args.dataset][args.t]
num_rounds = args.num_rounds if args.num_rounds != -1 else tup[0]
eval_every = args.eval_every if args.eval_every != -1 else tup[1]
clients_per_round = args.clients_per_round if args.clients_per_round != -1 else tup[2]
# Suppress tf warnings
tf.logging.set_verbosity(tf.logging.WARN)
# Create 2 models
# model_params = (0.0003, 62)
# 默认学习率
model_params = MODEL_PARAMS[model_path]
# 重置学习率
# 重置后的模型参数
if args.lr != -1:
model_params_list = list(model_params)
model_params_list[0] = args.lr
model_params = tuple(model_params_list)
# Create client model, and share params with server model
# 重置全局默认图
tf.reset_default_graph()
# model_params (0.06, 62)
# client_model = ClientModel(args.seed, *model_params)
model_info.update({
'seed': args.seed,
'model_params': model_params
})
# Create server
server = Server(model_info)
# Create clients
clients = setup_clients(args.dataset, model_info, args.use_val_set)
client_ids, client_groups, client_num_samples = server.get_clients_info(clients)
print('Clients in Total: %d' % len(clients))
# Initial status
print('--- Random Initialization ---')
stat_writer_fn = get_stat_writer_function(client_ids, client_groups, client_num_samples, args)
sys_writer_fn = get_sys_writer_function(args)
print_stats(0, server, clients, client_num_samples, args, stat_writer_fn, args.use_val_set)
# Simulate training
for i in range(num_rounds):
print('--- Round %d of %d: Training %d Clients ---' % (i + 1, num_rounds, clients_per_round))
# Select clients to train this round
server.select_clients(i, online(clients), num_clients=clients_per_round)
c_ids, c_groups, c_num_samples = server.get_clients_info(server.selected_clients)
# Simulate server model training on selected clients' data
sys_metrics = server.train_model(num_epochs=args.num_epochs, batch_size=args.batch_size,
minibatch=args.minibatch)
sys_writer_fn(i + 1, c_ids, sys_metrics, c_groups, c_num_samples)
# Update server model
server.update_model()
# Test model
if (i + 1) % eval_every == 0 or (i + 1) == num_rounds:
print_stats(i + 1, server, clients, client_num_samples, args, stat_writer_fn, args.use_val_set)
# Save server model
ckpt_path = os.path.join('checkpoints', args.dataset)
if not os.path.exists(ckpt_path):
os.makedirs(ckpt_path)
save_path = server.save_model(os.path.join(ckpt_path, '{}.ckpt'.format(args.model)))
print('Model saved in path: %s' % save_path)
# Close models
server.close_model()
def online(clients):
"""We assume all users are always online."""
return clients
def create_clients(users, groups, train_data, test_data, model_info):
if len(groups) == 0:
groups = [[] for _ in users]
clients = [Client(u, g, train_data[u], test_data[u], model_info) for u, g in zip(users, groups)]
# clients = []
# for u, g in zip(users, groups):
# model = copy.deepcopy(model)
# clients.append(Client(u, g, train_data[u], test_data[u], model))
return clients
def setup_clients(dataset, model_info=None, use_val_set=False):
"""Instantiates clients based on given train and test data directories.
Return:
all_clients: list of Client objects.
"""
eval_set = 'test' if not use_val_set else 'val'
train_data_dir = os.path.join('..', 'data', dataset, 'data', 'train')
test_data_dir = os.path.join('..', 'data', dataset, 'data', eval_set)
users, groups, train_data, test_data = read_data(train_data_dir, test_data_dir)
clients = create_clients(users, groups, train_data, test_data, model_info)
return clients
def get_stat_writer_function(ids, groups, num_samples, args):
def writer_fn(num_round, metrics, partition):
metrics_writer.print_metrics(
num_round, ids, metrics, groups, num_samples, partition, args.metrics_dir,
'{}_{}'.format(args.metrics_name, 'stat-fedsp'))
return writer_fn
def get_sys_writer_function(args):
def writer_fn(num_round, ids, metrics, groups, num_samples):
metrics_writer.print_metrics(
num_round, ids, metrics, groups, num_samples, 'train', args.metrics_dir,
'{}_{}'.format(args.metrics_name, 'sys-fedsp'))
return writer_fn
def print_stats(
num_round, server, clients, num_samples, args, writer, use_val_set):
train_stat_metrics = server.test_model(clients, set_to_use='train')
print_metrics(train_stat_metrics, num_samples, prefix='train_')
writer(num_round, train_stat_metrics, 'train')
eval_set = 'test' if not use_val_set else 'val'
test_stat_metrics = server.test_model(clients, set_to_use=eval_set)
print_metrics(test_stat_metrics, num_samples, prefix='{}_'.format(eval_set))
writer(num_round, test_stat_metrics, eval_set)
def print_metrics(metrics, weights, prefix=''):
"""Prints weighted averages of the given metrics.
Args:
metrics: dict with client ids as keys. Each entry is a dict
with the metrics of that client.
weights: dict with client ids as keys. Each entry is the weight
for that client.
"""
ordered_weights = [weights[c] for c in sorted(weights)]
metric_names = metrics_writer.get_metrics_names(metrics)
to_ret = None
for metric in metric_names:
ordered_metric = [metrics[c][metric] for c in sorted(metrics)]
print('%s: %g, 10th percentile: %g, 50th percentile: %g, 90th percentile %g' \
% (prefix + metric,
np.average(ordered_metric, weights=ordered_weights),
np.percentile(ordered_metric, 10),
np.percentile(ordered_metric, 50),
np.percentile(ordered_metric, 90)))
if __name__ == '__main__':
main()