[e99403]: / master_node / master.py

Download this file

416 lines (306 with data), 13.3 kB

  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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# imports
import os
import time
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.linear_model import SGDClassifier
from collections import Counter
import pickle
import tenseal as ts
import urllib3
import boto3
from botocore.exceptions import ClientError
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
# normalization of dataframe data
def normalize_df(df):
for column in df.columns:
df[column] = (df[column] - df[column].min()) / \
(df[column].max() - df[column].min())
return df
# handling outlier data in dataframe
def outlier_detection(df, n, columns):
rows = []
will_drop_train = []
for col in columns:
Q1 = np.nanpercentile(df[col], 25)
Q3 = np.nanpercentile(df[col], 75)
IQR = Q3 - Q1
outlier_point = 1.5 * IQR
rows.extend(df[(df[col] < Q1 - outlier_point) |
(df[col] > Q3 + outlier_point)].index)
for r, c in Counter(rows).items():
if c >= n:
will_drop_train.append(r)
return will_drop_train
# function to perform data preprocessing
def preprocess_data(csv_file):
# passing address of csv file to create data frame
df = pd.read_csv(csv_file)
# renaming columns
df.rename(columns={'height(cm)': 'height', 'weight(kg)': 'weight', 'waist(cm)': 'waist',
'eyesight(left)': 'eyesight_left', 'eyesight(right)': 'eyesight_right',
'hearing(left)': 'hearing_left', 'hearing(right)': 'hearing_right',
'fasting blood sugar': 'fasting_blood_sugar', 'Cholesterol': 'cholesterol',
'HDL': 'hdl', 'LDL': 'ldl', 'Urine protein': 'urine_protein',
'serum creatinine': 'serum_creatinine', 'AST': 'ast', 'ALT': 'alt',
'Gtp': 'gtp', 'dental caries': 'dental_caries'}, inplace=True)
# converting non-numeric columns to numeric data type
df['gender'] = df['gender'].str.replace('F', '0')
df['gender'] = df['gender'].str.replace('M', '1')
df['gender'] = pd.to_numeric(df['gender'])
df['tartar'] = df['tartar'].str.replace('N', '0')
df['tartar'] = df['tartar'].str.replace('Y', '1')
df['tartar'] = pd.to_numeric(df['tartar'])
df['oral'] = df['oral'].str.replace('N', '0')
df['oral'] = df['oral'].str.replace('Y', '1')
df['oral'] = pd.to_numeric(df['oral'])
# cleaning data by observation
df = df.drop(['ID'], axis=1)
# removing oral column due to skewed data
df = df.drop("oral", axis='columns')
# handling outliers in df
will_drop_train = outlier_detection(
df, 3, df.select_dtypes(["float", "int"]).columns)
df.drop(will_drop_train, inplace=True, axis=0)
# creating x and y split where y is the resultant classification data
y = df['smoking'].copy()
x = df[['age', 'gender', 'height', 'weight', 'waist', 'hdl', 'ldl', 'serum_creatinine',
'alt', 'gtp', 'dental_caries', 'tartar', 'triglyceride', 'hemoglobin']].copy()
# normalizing x data to maintain the scale necessary for creation of model
x = normalize_df(x)
return x, y
# create base model from initial data
def create_master_model(x_train, y_train, print_flag=False):
sgd = SGDClassifier()
sgd.fit(x_train, y_train)
if print_flag:
x_train_prediction = sgd.predict(x_train)
training_data_accuracy = accuracy_score(x_train_prediction, y_train)
print('Training data accuracy: ', training_data_accuracy)
return sgd
# function to get keys used for asymmetric encryption
def get_encryption_keys():
# read keys from remote server
http = urllib3.PoolManager()
bytes_private_key = http.request(
'GET', 'https://personal.utdallas.edu/~pxn210006/keys/private_key.pem')
bytes_public_key = http.request(
'GET', 'https://personal.utdallas.edu/~pxn210006/keys/public_key.pem')
private_key = serialization.load_pem_private_key(
bytes_private_key.data,
password=None,
backend=default_backend()
)
public_key = serialization.load_pem_public_key(
bytes_public_key.data,
backend=default_backend()
)
return private_key, public_key
# function that encrypts the model file
def encrypt_model(model_pickle, encrypted_model):
# get keys
private_key, public_key = get_encryption_keys()
# encrypt the model
output = open(encrypted_model, 'ab')
# perform encryption
with open(model_pickle, 'rb') as input:
while True:
msg = input.read(100)
if not msg:
break
encrypted = public_key.encrypt(
msg,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
output.write(encrypted)
output.close()
# function that decrypts the model file
def decrypt_worker_model(received_file, decrypted_file):
# Decrypting the model
input = open(decrypted_file, 'ab')
# get keys
private_key, public_key = get_encryption_keys()
# perform decryption
with open(received_file, 'rb') as output:
while True:
encrypt = output.read(256)
if not encrypt:
break
original_message = private_key.decrypt(
encrypt,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
input.write(original_message)
input.close()
# decrypt all the received models
def decrypt_received_models():
decrypt_worker_model('worker_model_1', 'decrypted_model_1')
decrypt_worker_model('worker_model_2', 'decrypted_model_2')
decrypt_worker_model('worker_model_3', 'decrypted_model_3')
# function to perform model aggregation
def perform_model_aggregation(print_flag=False):
# define context for tenseal
def context():
context = ts.context(ts.SCHEME_TYPE.CKKS, 8192,
coeff_mod_bit_sizes=[60, 40, 40, 60])
context.global_scale = pow(2, 40)
context.generate_galois_keys()
return context
context = context()
# fetch encrypted model parameters
with open('worker_model_1', 'rb') as file:
content1 = file.read()
encrypted_params_model1 = ts.ckks_tensor_from(context, content1)
with open('worker_model_2', 'rb') as file:
content2 = file.read()
encrypted_params_model2 = ts.ckks_tensor_from(context, content2)
with open('worker_model_3', 'rb') as file:
content3 = file.read()
encrypted_params_model3 = ts.ckks_tensor_from(context, content3)
# perform aggregation of model params
mul = np.array([[0.33 for x in range(15)]])
mul_tensor = ts.plain_tensor(mul)
encrypted_params_aggregate = (
encrypted_params_model1 + encrypted_params_model2 + encrypted_params_model3) * mul
# decrypt the aggregated params
decrypted = encrypted_params_aggregate.decrypt().tolist()
sgd_params_aggregate = np.array(decrypted)
# fit the parameters to the new model
sgd_aggregate_intercept = np.array([sgd_params_aggregate[0][0]])
sgd_aggregate_coef = np.reshape(sgd_params_aggregate[0][1:], (1, 14))
x_train, y_train = preprocess_data(
'https://personal.utdallas.edu/~pxn210006/dataset/dataset_initial_model.csv')
aggregated_model = SGDClassifier()
aggregated_model.partial_fit(x_train, y_train, np.array([0, 1]))
aggregated_model.coef_ = sgd_aggregate_coef
aggregated_model.intercept_ = sgd_aggregate_intercept
aggregated_model.classes_ = np.array([0, 1])
if print_flag:
x_test, y_test = preprocess_data(
'https://personal.utdallas.edu/~pxn210006/dataset/dataset_test.csv')
score = aggregated_model.score(x_test, y_test)
print('Model accuracy on test data', score)
return aggregated_model
# function that return client object to interact with s3 bucket
def get_s3_client():
http = urllib3.PoolManager()
aws_access_key_id = http.request(
'GET', 'https://personal.utdallas.edu/~pxn210006/keys/aws_access_key_id')
aws_secret_access_key = http.request(
'GET', 'https://personal.utdallas.edu/~pxn210006/keys/aws_secret_access_key')
awsAccessKeyID = aws_access_key_id.data.decode()
awsSecretAccessKey = aws_secret_access_key.data.decode()
s3_client = boto3.client(
"s3", aws_access_key_id=awsAccessKeyID, aws_secret_access_key=awsSecretAccessKey)
return s3_client
# function that sends files to s3 bucket
def store_files_s3(local_file_name, dest_file_name):
s3_client = get_s3_client()
bucketName = 'team-20-sptopic-master'
dest1 = 'worker-node1/'+dest_file_name+'1'
dest2 = 'worker-node2/'+dest_file_name+'2'
dest3 = 'worker-node3/'+dest_file_name+'3'
dest_par = 'master-node/parent/master_model'
s3_client.upload_file(local_file_name, bucketName, dest1)
s3_client.upload_file(local_file_name, bucketName, dest2)
s3_client.upload_file(local_file_name, bucketName, dest3)
s3_client.upload_file(local_file_name, bucketName, dest_par)
# function that checks if file object present in s3 bucket
def s3_key_exists(filepath):
s3_client = get_s3_client()
bucketName = 'team-20-sptopic-master'
try:
s3_client.head_object(Bucket=bucketName, Key=filepath)
except ClientError as e:
return False
return True
# function that fetches files from s3 bucket
def get_files_s3(models, child_path):
s3_client = get_s3_client()
bucketName = 'team-20-sptopic-master'
file1 = child_path + models[0]
file2 = child_path + models[1]
file3 = child_path + models[2]
s3_client.download_file(bucketName, file1, models[0])
s3_client.download_file(bucketName, file2, models[1])
s3_client.download_file(bucketName, file3, models[2])
# function that deletes files from local directory and s3 bucket
def delete_files_local_s3(models, child_path):
s3_client = get_s3_client()
bucketName = 'team-20-sptopic-master'
file1 = child_path + models[0]
os.remove(models[0])
s3_client.delete_object(Bucket=bucketName, Key=file1)
file2 = child_path + models[1]
os.remove(models[1])
s3_client.delete_object(Bucket=bucketName, Key=file2)
file3 = child_path + models[2]
os.remove(models[2])
s3_client.delete_object(Bucket=bucketName, Key=file3)
# main function
def main():
# parent_path = 'master-node/parent/'
child_path = 'master-node/child/'
# STEP 1
# create the master base model
x_train, y_train = preprocess_data(
'https://personal.utdallas.edu/~pxn210006/dataset/dataset_initial_model.csv')
sgd_base_model = create_master_model(x_train, y_train, print_flag=True)
# create pickle of the model
pickle.dump(sgd_base_model, open('base_model_pickle', 'wb'))
# now encrypt the base model
encrypt_model('base_model_pickle', 'master_model')
os.remove('base_model_pickle')
# upload encrypted base model to s3 bucket
store_files_s3('master_model', 'active_worker_model_')
print('Base model trained and uploaded')
# STEP 2
# perform model aggregation
while (True):
# check for each model file if it exists
flag = True
worker_models_list = ['worker_model_1',
'worker_model_2', 'worker_model_3']
for model in worker_models_list:
filepath = child_path+model
flag = s3_key_exists(filepath)
if not flag:
print('Waiting to receive model from all active clients!!!!')
break
# model file exists
if flag:
print('\n********************************************\n')
print('Received model from all active clients!!!!')
# download and save models
get_files_s3(worker_models_list, child_path)
print('All models downloaded. Now aggregation will start!!!!')
# perform aggregation
aggregated_model = perform_model_aggregation(True)
print('Aggregation successfull')
# delete old models
delete_files_local_s3(worker_models_list, child_path)
print('Delete old files from child directory')
# create pickle of the model
pickle.dump(aggregated_model, open('aggregate_model_pickle', 'wb'))
# now encrypt the base model
os.remove('master_model')
encrypt_model('aggregate_model_pickle', 'master_model')
os.remove('aggregate_model_pickle')
# upload encrypted base model to s3 bucket
store_files_s3('master_model', 'passive_master_model')
print('New aggregated model trained and uploaded')
print('\n******************************************\n')
time.sleep(10)
main()