-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumpy_to_JSON_utils.py
59 lines (47 loc) · 1.71 KB
/
Numpy_to_JSON_utils.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
import numpy as np
import codecs, json
from json import JSONEncoder
class Numpy2JSONEncoder(json.JSONEncoder):
'''
This class is to convert the Numpy format Tensorflow Model Weights into JSON format
to send it to the server for Federated Averaging
'''
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NumpyEncoder, self).default(obj)
def json2NumpyWeights(data):
'''
This function is to decode the JSON format Model weights to Tensorflow model suitable
Numpy for mat so that , model.set_weights(name_variable) can be used properly to set model weights
'''
decodedGlobalWeights = list()
decodedGlobalWeights = json.loads(data)
FinalWeight= list()
for i in range(len(decodedGlobalWeights)):
FinalWeight.append(np.array(decodedGlobalWeights[i]))
return FinalWeight
def EagerTensor2Numpy(data):
'''
This Function is to convert Eager Tensor to Numpy ndarray
After that this is used for JSON serializable to send it to the Global and local Server
'''
npWeightsMat = list()
for i in range(len(data)):
val = data[i].numpy()
npWeightsMat.append(val)
return npWeightsMat
def global_weights_mul_lr(data, learning_rate):
'''
This function multiplies the averaged weights with the learning rate after FedAvg of locall models weights
'''
tmp_global_weights = list()
for i in range(len(data)):
val = data[i]*learning_rate
tmp_global_weights.append(val)
return tmp_global_weights