-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
213 lines (193 loc) · 9.11 KB
/
client.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # or any {'0', '1', '2'}
import argparse
import flwr as fl
import random
from model import create_model
from model_ascent import create_model_ascent
from cinic10_ds import get_test_val_ds
import numpy as np
#:)
class FLClient(fl.client.NumPyClient):
def __init__(self, x_train, y_train, x_test, y_test, is_poisoned, is_noniid, data):
self.x_train = x_train
self.y_train = y_train
self.x_test = x_test
self.y_test = y_test
self.is_poisoned = is_poisoned
self.is_noniid = is_noniid
self.data = data
self.train_count = len(x_train)
self.test_count = len(x_test)
self.model = create_model(data)
self.model_ascent = create_model_ascent(data)
self.lazy_poisoning = False
self.pga_poisoning = False
self.pga_poisoning_split = False
self.epochs = 10
if is_noniid:
#self.x_train, self.y_train = self.removeLabels(self.x_train, self.y_train, 7, 7)
pass
else:
#self.x_train, self.y_train = self.removeLabels(self.x_train, self.y_train, 4, 5)
pass
if is_poisoned:
if True:
#self.y_train = self.poisonRandomLabel(y_train = self.y_train, no_labels=3000)
#self.x_train = self.poisonRandomPixels(x_train = self.x_train)
#self.y_train = self.poisonSpecificLabel(y_train=self.y_train, part_of_labels=1.0,label=2,to_label='random')
#self.lazy_poisoning = True
#self.pga_poisoning_split = True
#self.pga_poisoning = True
#self.pga_scaler = 0.1
#self.epochs = 30
pass
else:
#heterogen split below
#self.x_train, self.y_train = self.removeLabels(self.x_train, self.y_train, 4, 5)
pass
def removeLabels(self,x_train,y_train,label1,label2):
print(f"removing labels {label1} , {label2} !!!!!!")
print(len(x_train))
x_train = list(x_train)
y_train = list(y_train)
x_train_new = []
y_train_new = []
for i in range(len(y_train)-1):
if np.argmax(y_train[i]) == label1 or np.argmax(y_train[i]) == label2:
pass
else:
x_train_new.append(x_train[i])
y_train_new.append(y_train[i])
x_train = np.array(x_train_new)
y_train = np.array(y_train_new)
print(len(x_train))
return x_train, y_train
def poisonRandomLabel(self,y_train,no_labels=800):
print("Poisoning labels!!!!!!!")
print(len(y_train))
y_train = list(y_train)
for i in range(no_labels):
x = [0.0 for j in range(10)]
x[random.randint(0,9)] = 1.0
y_train[random.randint(0,int(len(y_train)/2))-1] = np.array(x)
y_train[random.randint(int(len(y_train)/2),len(y_train)-1)] = np.array(x)
y_train = np.array(y_train)
return y_train
def poison_specific_label(self,y_train,part_of_labels=1.0, label=4, to_label=5):
print("Poisoning labels!!!!!!!")
print(len(y_train))
y_train = list(y_train)
for i in range(len(y_train)):
if np.argmax(y_train[i]) == label:
if random.uniform(0,1) <= part_of_labels:
x = [0.0 for j in range(10)]
if to_label == 'random':
to_label = random.randint(0,9)
x[to_label] = 1.0
y_train[i] = x
y_train = np.array(y_train)
return y_train
def poisonRandomPixels(self, x_train, perc_img=1.0, nr_pixels = 600, th = 0.5):
print("Poisoning pixels!!!!!!!")
x_train = list(x_train)
nr_pictures = int(perc_img*len(x_train))
index_value = random.sample(list(enumerate(x_train)), nr_pictures)
for idx, _ in index_value:
for i in range(nr_pixels):
position = random.randint(0,len(x_train[idx])-1)
row_position = random.randint(0,len(x_train[idx][position])-1)
x_train[idx][position][row_position]
for j in range(len(x_train[idx][position][row_position])):
current = x_train[idx][position][row_position][j]
new = np.float32(round(random.uniform(current*(1-th),current*(1+th))))
x_train[idx][position][row_position][j] = new
x_train = np.array(x_train)
return x_train
def get_parameters(self, config):
return self.model.get_weights()
def get_properties(self):
return {"is_poisoned" : self.is_poisoned}
def find_worst_labels(self, parameters):
spec_label_acc = self.ev_labels(parameters)
l1 = np.argmin(spec_label_acc)
spec_label_acc[l1] = 100
l2 = np.argmin(spec_label_acc)
spec_label_acc[l2] = 100
l3 = np.argmin(spec_label_acc)
spec_label_acc[l3] = 100
return [l1, l2, l3]
def split_data(self, l_list, x, y):
x_improve = []
y_improve = []
x_worsen = []
y_worsen = []
for i in range(len(y)):
if np.argmax(y[i]) in l_list:
x_improve.append(x[i])
y_improve.append(y[i])
else:
x_worsen.append(x[i])
y_worsen.append(y[i])
return np.array(x_improve), np.array(y_improve), np.array(x_worsen), np.array(y_worsen)
def pga_poison(self,parameters, x, y):
self.model_ascent.set_weights(parameters)
last_weights = self.model_ascent.get_weights()
self.model_ascent.fit(x, y, epochs=self.epochs, batch_size=128, validation_split=0.1)
new_weights = self.model_ascent.get_weights()
for i in range(len(last_weights)):
scaled_norm = np.subtract(new_weights[i],last_weights[i])*self.pga_scaler
last_weights[i] = np.add(last_weights[i],scaled_norm)
return last_weights, self.train_count, {"is_poisoned" : self.is_poisoned}
def pga_poison_split(self,parameters, x, y):
l_list = self.find_worst_labels(parameters)
x_improve, y_improve, x_worsen, y_worsen = self.split_data(l_list, x, y)
self.model_ascent.set_weights(parameters)
self.model.set_weights(parameters)
last_weights = self.model_ascent.get_weights()
self.model_ascent.fit(x_worsen, y_worsen, epochs=self.epochs, batch_size=128, validation_split=0.1)
self.model.fit(x_improve, y_improve, epochs=self.epochs, batch_size=128, validation_split=0.1)
new_weights = self.model_ascent.get_weights()
new_weights_good = self.model.get_weights()
for i in range(len(last_weights)):
scaled_norm = np.subtract(new_weights[i],last_weights[i])*self.pga_scaler
good_norm = np.subtract(new_weights_good[i], last_weights[i])
last_weights[i] = np.add(last_weights[i],scaled_norm)
last_weights[i] = np.add(last_weights[i],good_norm)
return last_weights, self.train_count, {"is_poisoned" : self.is_poisoned}
def fit(self, parameters, config):
partition = config.get('current_round') % config.get('nr_of_split_per_round')
part_size = len(self.x_train)/config.get('nr_of_split_per_round')
x = self.x_train[int(partition*part_size) : int(((partition+1)*part_size)-1) ]
y = self.y_train[ int(partition*part_size) : int(((partition+1)*part_size)-1) ]
if self.pga_poisoning:
return self.pga_poison(parameters, x, y)
if self.pga_poisoning_split:
return self.pga_poison_split(parameters, x, y)
self.model.set_weights(parameters)
if self.lazy_poisoning:
return self.model.get_weights(), self.train_count, {"is_poisoned" : self.is_poisoned}
self.model.fit(x, y, epochs=self.epochs, batch_size=128, validation_split=0.1)
return self.model.get_weights(), self.train_count, {"is_poisoned" : self.is_poisoned}
def ev_labels(self, parameters):
x_test, y_test = get_test_val_ds(self.data)
self.model.set_weights(parameters)
preds = self.model.predict(x_test)
spec_label_correct_count = [0.0 for i in range(len(y_test[0]))]
spec_label_all_count = [0.0 for i in range(len(y_test[0]))]
for i in range(len(preds)):
pred = np.argmax(preds[i])
true = np.argmax(y_test[i])
spec_label_all_count[true] = spec_label_all_count[true] +1
if true == pred:
spec_label_correct_count[true] = spec_label_correct_count[true] +1
spec_label_accuracy = []
for i in range(len(spec_label_all_count)):
spec_label_accuracy.append(spec_label_correct_count[i]/spec_label_all_count[i])
return spec_label_accuracy
def evaluate(self, parameters, config):
self.model.set_weights(parameters)
loss, accuracy = self.model.evaluate(self.x_test, self.y_test)
return loss, self.test_count, {"accuracy": accuracy}
def start(self, server_address):
fl.client.start_numpy_client(server_address = "127.0.0.1" + ":8080", client=self)