-
Notifications
You must be signed in to change notification settings - Fork 3
/
client3.py
217 lines (180 loc) · 5.75 KB
/
client3.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
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
import flwr as fl
import tensorflow as tf
from tensorflow import keras
import sys
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from keras import layers
import keras
from sklearn.utils import resample
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from trans import *
sweep_config = {
'method': 'grid',
'metric': {
'goal': 'maximize',
'name': 'val_accuracy'
},
'parameters': {
'epochs': {
'value': 50
},
'num_layers': {
'value': 3
},
'embed_layer_size': {
'value': 128
},
'fc_layer_size': {
'value': 256
},
'num_heads': {
'value': 6
},
'dropout': {
'value': 0.1
},
'attention_dropout': {
'value': 0.1
},
'optimizer': {
'value': 'adam'
},
'amsgrad': {
'value': False
},
'label_smoothing': {
'value': 0.1
},
'learning_rate': {
'value': 1e-3
},
#'weight_decay': {
# 'values': [2.5e-4, 1e-4, 5e-5, 1e-5]
#},
'warmup_steps': {
'value': 10
},
'batch_size': {
'value': 64
},
'global_clipnorm': {
'value': 3.0
},
}
}
sweep_id = wandb.sweep(sweep_config, project="imu-transformer")
tf.random.set_seed(42)
df = pd.read_csv('data/mhealth_raw_data.csv')
df_majority = df[df.Activity==0]
df_minorities = df[df.Activity!=0]
df_majority_downsampled = resample(df_majority,n_samples=30000, random_state=42)
df = pd.concat([df_majority_downsampled, df_minorities])
#Dropping feature have data outside 98% confidence interval
df1 = df.copy()
for feature in df1.columns[:-2]:
lower_range = np.quantile(df[feature],0.01)
upper_range = np.quantile(df[feature],0.99)
print(feature,'range:',lower_range,'to',upper_range)
df1 = df1.drop(df1[(df1[feature]>upper_range) | (df1[feature]<lower_range)].index, axis=0)
label_map = {
0: 'Nothing',
1: 'Standing still',
2: 'Sitting and relaxing',
3: 'Lying down',
4: 'Walking',
5: 'Climbing stairs',
6: 'Waist bends forward',
7: 'Frontal elevation of arms',
8: 'Knees bending (crouching)',
9: 'Cycling',
10: 'Jogging',
11: 'Running',
12: 'Jump front & back'
}
df1 = df1[(df1['subject'] != 'subject1') & (df1['subject'] != 'subject2') & (df1['subject'] != 'subject3') & (df1['subject'] != 'subject4') & (df1['subject'] != 'subject7') & (df1['subject'] != 'subject8')]
#y = df1.Activity
#X = df1.drop(['Activity','subject'], axis=1)
#X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2)
#spliting data into train and test set
train = df1[(df1['subject'] != 'subject9') & (df1['subject'] != 'subject10')]
test = df1.drop(train.index, axis=0)
allmeans=[]
allstds=[]
#I HAVE TAKEN STANDARD MEAN AND STANDARD DEVIATION OF EACH DATA (d1,d2,d3,d4,d5,d6,d7,d8) STORED IN ONE LIST
#THEN I HAVE TAKEN THE MEAN OF THOSE TWO
def get_data(data):
X_train = data.drop(['Activity','subject'],axis=1)
y_train = data['Activity']
scaler=StandardScaler()
X_train=pd.DataFrame(scaler.fit_transform(X_train),columns=X_train.columns)
allmeans.append(scaler.mean_)
allstds.append(scaler.scale_)
return X_train, y_train
X_train, y_train = get_data(df1)
meant=np.mean(allmeans,axis=0)
stdt=np.mean(allstds,axis=0)
X_test = test.drop(['Activity','subject'],axis=1)
X_test1=np.array(X_test)
X_test1=(X_test1-meant)/stdt
X_test=pd.DataFrame(X_test1,columns=X_test.columns)
y_test = test['Activity']
#X_train = train.drop(['Activity','subject'],axis=1)
#y_train = train['Activity']
#X_test = test.drop(['Activity','subject'],axis=1)
#y_test = test['Activity']
#function to create time series datset for seuence modeling
def create_dataset(X, y, time_steps, step=1):
Xs, ys = [], []
for i in range(0, len(X) - time_steps, step):
x = X.iloc[i:(i + time_steps)].values
labels = y.iloc[i: i + time_steps]
Xs.append(x)
ys.append(stats.mode(labels)[0])
return np.array(Xs), np.array(ys).reshape(-1, 1)
# Load dataset
X_train,y_train = create_dataset(X_train, y_train, 1, step=50)
X_test,y_test = create_dataset(X_test, y_test, 1, step=50)
# Generate new model
model = Transformer(
num_layers=3,
embed_dim=128,
mlp_dim=256,
num_heads=6,
num_classes=13,
dropout_rate=0.1,
attention_dropout_rate=0.1,
)
# adapt on training dataset - must be before model.compile !!!
#model.input_norm.adapt(X_train, batch_size=config.batch_size)
#print(model.input_norm.variables)
model.compile(
loss=smoothed_sparse_categorical_crossentropy(label_smoothing=0.1),
optimizer='adam',
metrics=["accuracy"],
)
# Define Flower client
class FlowerClient(fl.client.NumPyClient):
def get_parameters(self,config):
return model.get_weights()
def fit(self, parameters, config):
model.set_weights(parameters)
r = model.fit(X_train, y_train, epochs=3, validation_data=(X_test, y_test), verbose=0)
hist = r.history
print("Fit history : " ,hist)
return model.get_weights(), len(X_train), {}
def evaluate(self, parameters, config):
model.set_weights(parameters)
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print("Eval accuracy : ", accuracy)
return loss, len(X_test), {"accuracy": accuracy}
# Start Flower client
fl.client.start_numpy_client(
server_address="127.0.0.1:8080",
client=FlowerClient(),
grpc_max_message_length = 1024*1024*1024
)