|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import time |
|
from flcore.clients.clientdistill import clientDistill |
|
from flcore.servers.serverbase import Server |
|
from threading import Thread |
|
from collections import defaultdict |
|
|
|
|
|
class FedDistill(Server): |
|
def __init__(self, args, times): |
|
super().__init__(args, times) |
|
|
|
|
|
self.set_slow_clients() |
|
self.set_clients(clientDistill) |
|
|
|
print(f"\nJoin ratio / total clients: {self.join_ratio} / {self.num_clients}") |
|
print("Finished creating server and clients.") |
|
|
|
|
|
self.Budget = [] |
|
self.num_classes = args.num_classes |
|
self.global_logits = [None for _ in range(args.num_classes)] |
|
|
|
|
|
def train(self): |
|
for i in range(self.global_rounds+1): |
|
s_t = time.time() |
|
self.selected_clients = self.select_clients() |
|
|
|
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_logits() |
|
self.global_logits = logit_aggregation(self.uploaded_logits) |
|
self.send_logits() |
|
|
|
self.Budget.append(time.time() - s_t) |
|
print('-'*50, 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(sum(self.Budget[1:])/len(self.Budget[1:])) |
|
|
|
self.save_results() |
|
|
|
|
|
def send_logits(self): |
|
assert (len(self.clients) > 0) |
|
|
|
for client in self.clients: |
|
start_time = time.time() |
|
|
|
client.set_logits(self.global_logits) |
|
|
|
client.send_time_cost['num_rounds'] += 1 |
|
client.send_time_cost['total_cost'] += 2 * (time.time() - start_time) |
|
|
|
def receive_logits(self): |
|
assert (len(self.selected_clients) > 0) |
|
|
|
self.uploaded_ids = [] |
|
self.uploaded_logits = [] |
|
for client in self.selected_clients: |
|
self.uploaded_ids.append(client.id) |
|
self.uploaded_logits.append(client.logits) |
|
|
|
|
|
|
|
def logit_aggregation(local_logits_list): |
|
agg_logits_label = defaultdict(list) |
|
for local_logits in local_logits_list: |
|
for label in local_logits.keys(): |
|
agg_logits_label[label].append(local_logits[label]) |
|
|
|
for [label, logit_list] in agg_logits_label.items(): |
|
if len(logit_list) > 1: |
|
logit = 0 * logit_list[0].data |
|
for i in logit_list: |
|
logit += i.data |
|
agg_logits_label[label] = logit / len(logit_list) |
|
else: |
|
agg_logits_label[label] = logit_list[0].data |
|
|
|
return agg_logits_label |