-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedgeserver.py
46 lines (36 loc) · 1.76 KB
/
edgeserver.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
from average import average_weights
from models.initialize_model import create
import numpy as np
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
class Edgeserver:
def __init__(self,id_name,cnames,dataset,model,loss,metrics,lr,image_shape,num_labels):
n='edgeserver'
self.name=f'{n}_{id_name+1}'
self.cnames=cnames
self.buffer={}
self.participated_sample={}
self.model=create(dataset,model,loss,metrics,lr,image_shape,num_labels)
self.test_avg_acc=[]
def aggregate(self,comm_r,num_agg):
sample_number=[]
weight=[]
for i in self.participated_sample.values():
sample_number.append(i)
for w in self.buffer.values():
weight.append(w)
self.model.set_weights(average_weights(w=weight,sample_num=sample_number))
self.model.save(fr".\results\edges_models\itr_{comm_r+1}\agg_{num_agg+1}_{self.name}.h5")
def send_to_client(self,client):
client.model.set_weights(self.model.get_weights())
def send_to_server(self,server):
server.buffer[self.name]=self.model.get_weights()
def receive_from_server(self,global_weight):
self.model.set_weights(global_weight)
def refresh_edgeserver(self):
self.buffer.clear()
self.participated_sample.clear()
def client_registering(self,client):
self.participated_sample[client.name]=client.train_num
def m_compile(self,loss,optimizer,metrics):
self.model.compile(loss=loss,optimizer=optimizer,metrics=metrics)