-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
135 lines (102 loc) · 5.21 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
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
# -*- coding: utf-8 -*-
# import sys
import numpy as np
import os
from transmission_model.transmission_model import Transmission_Model
from communication_strategy.communication_strategy import Communication_Strategy
from server.server import Server
class FL(Server):
def __init__(self, n_rounds, total_number_clients, min_fit_clients, rb_number, load_client_data_constructor,
path_server, path_clients, shape, model_type, fixed_user_power,
parallel_processing=False, tm=None):
self.tm = Transmission_Model(rb_number=rb_number, user_number=total_number_clients,
shape=shape,
model_type=model_type,
lower_limit_distance=100, upper_limit_distance=500,
fixed_user_power=fixed_user_power)
super().__init__(n_rounds, total_number_clients, min_fit_clients, load_client_data_constructor,
path_server, path_clients, shape, model_type, parallel_processing, tm=self.tm)
self.strategy = Communication_Strategy(
transmission_model=self.tm,
min_fit_clients=min_fit_clients,
clients_number_data_samples=self.clients_number_data_samples,
clients_value_based_emd=self.clients_value_based_emd,
emd_mean=self.emd_mean,
clients_emd=self.clients_emd,
delay_requirement=0.4, energy_requirement=0.005, error_rate_requirement=0.3, lmbda=230) # 230
# delay_requirement=0.2, energy_requirement=0.0025 - NIID R-MNIST com MLP
# delay_requirement=0.4, energy_requirement=0.005 - NIID R-FMNIST com CNN
def print_result(self):
print("###############################")
print(f"centralized_accuracy: ")
print(self.evaluate_list["centralized"]["accuracy"])
print(f"centralized_loss: ")
print(self.evaluate_list["centralized"]["loss"])
print("###############################")
for item, value in self.strategy.round_costs_list.items():
print(item)
print(value)
print(np.cumsum(value).tolist())
print("\ncount_of_client_selected")
print(self.count_of_client_selected)
print("\ncount_of_client_uploads")
print(self.count_of_client_uploads)
def configure_fit(self):
# FedAvg
# self.strategy.random_user_selection(k=self.min_fit_clients)
# self.strategy.random_rb_allocation()
# self.strategy.fixed_user_power_allocation()
# POC
# self.strategy.greater_loss_user_selection(clients_loss_list=fl.clients_loss, factor=2, k=int(self.min_fit_clients))
# self.strategy.random_rb_allocation()
# self.strategy.fixed_user_power_allocation()
# FedAvg-wOpt
# self.strategy.random_user_selection(k=int(self.min_fit_clients))
# self.strategy.optimization()
# POC-wOpt
# self.strategy.greater_loss_user_selection(clients_loss_list=fl.clients_loss, factor=2, k=int(self.min_fit_clients))
# self.strategy.optimization()
# exit()
# DFed-wOpt
self.strategy.smaller_emd(factor=2, k=int(self.min_fit_clients))
self.strategy.optimization()
################
self.strategy.upload_status()
self.strategy.round_costs()
self.selected_clients = fl.strategy.success_uploads.copy()
if __name__ == "__main__":
os.system('clear')
for i in range(5):
fl = FL(n_rounds=200,
min_fit_clients=10,
rb_number=15,
total_number_clients=100,
path_server="../datasets/mnist/mnist",
path_clients="../datasets/mnist/non-iid-0.9",
shape=(28, 28, 1),
model_type="CNN",
fixed_user_power=0, # the allocation is dynamic when the value equals zero
# fixed_user_power=0.01,
load_client_data_constructor=False)
evaluate_loss, evaluate_accuracy = None, None
for fl.server_round in range(fl.n_rounds):
# Select customers who will participate in the next round of communication
fl.configure_fit()
fl.strategy.print_values()
print(f"success_uploads: {fl.strategy.success_uploads} - error_uploads: {fl.strategy.error_uploads}")
fl.strategy.print_round_costs()
for cid in fl.strategy.selected_clients:
fl.count_of_client_selected[cid] = fl.count_of_client_selected[cid] + 1
if len(fl.selected_clients) > 0:
for cid in fl.selected_clients:
fl.count_of_client_uploads[cid] = fl.count_of_client_uploads[cid] + 1
weight_list, sample_sizes, info = fl.fit()
# Aggregation
fl.aggregate_fit(weight_list, sample_sizes)
print(f"***************************")
# Centralized evaluate
print(f"Centralized evaluate: R: {fl.server_round + 1} ")
evaluate_loss, evaluate_accuracy = fl.centralized_evaluation()
print(f"evaluate_accuracy: {evaluate_accuracy}")
print(f"***************************")
fl.print_result()