|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import time |
|
from flcore.clients.clientbn import clientBN |
|
from flcore.servers.serverbase import Server |
|
from utils.data_utils import read_client_data |
|
from threading import Thread |
|
|
|
|
|
class FedBN(Server): |
|
def __init__(self, args, times): |
|
super().__init__(args, times) |
|
|
|
|
|
self.set_slow_clients() |
|
self.set_clients(clientBN) |
|
|
|
print(f"\nJoin ratio / total clients: {self.join_ratio} / {self.num_clients}") |
|
print("Finished creating server and clients.") |
|
|
|
|
|
self.Budget = [] |
|
|
|
|
|
def train(self): |
|
for i in range(self.global_rounds+1): |
|
s_t = time.time() |
|
self.selected_clients = self.select_clients() |
|
self.send_models() |
|
|
|
if i%self.eval_gap == 0: |
|
print(f"\n-------------Round number: {i}-------------") |
|
print("\nEvaluate personalized models") |
|
self.evaluate() |
|
|
|
for client in self.selected_clients: |
|
client.train() |
|
|
|
|
|
|
|
|
|
|
|
|
|
self.receive_models() |
|
self.aggregate_parameters() |
|
|
|
self.Budget.append(time.time() - s_t) |
|
print('-'*25, 'time cost', '-'*25, self.Budget[-1]) |
|
|
|
if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): |
|
break |
|
|
|
print("\nBest accuracy.") |
|
|
|
|
|
print(max(self.rs_test_acc)) |
|
print("\nAverage time cost per round.") |
|
print(sum(self.Budget[1:])/len(self.Budget[1:])) |
|
|
|
self.save_results() |
|
|
|
if self.num_new_clients > 0: |
|
self.eval_new_clients = True |
|
self.set_new_clients(clientBN) |
|
print(f"\n-------------Fine tuning round-------------") |
|
print("\nEvaluate new clients") |
|
self.evaluate() |
|
|