-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_train.py
244 lines (201 loc) · 11 KB
/
local_train.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
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
import torch
import torch.nn as nn
import test
from utils.utils import model_copy_params
import utils.csv_record as csv_record
import math
def model_dist_norm(model, target_params, _type):
squared_sum = 0
for name, layer in model.state_dict().items():
if 'num_batches_tracked' in name:
continue
squared_sum += torch.sum(torch.pow(layer.data -
target_params[name].data, 2))
return math.sqrt(squared_sum)
def FLTrain_UserDPFL(helper, logger, start_epoch, local_model, target_model, is_poison, agent_name_keys):
submit_params_update_dict = dict()
num_samples_dict = dict()
num_poisoned_samples_dict = dict()
target_params = dict()
for name, param in target_model.state_dict().items():
if 'num_batches_tracked' in name:
continue
target_params[name] = target_model.state_dict(
)[name].clone().detach().requires_grad_(False)
for model_id in range(helper.params['no_models']):
ADV_CLIENT = False
agent_name_key = agent_name_keys[model_id]
if is_poison and agent_name_key in helper.params['adversary_list']:
ADV_CLIENT = True
# Synchronize LR and models
model = local_model
model_copy_params(model, target_model.state_dict())
optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=helper.params['lr'],
momentum=helper.params['momentum'],
weight_decay=helper.params['decay'])
criterion = nn.CrossEntropyLoss().cuda()
model.train()
epoch = start_epoch
temp_local_epoch = (epoch - 1) * helper.params['internal_epochs']
for internal_epoch in range(1, helper.params['internal_epochs'] + 1):
temp_local_epoch += 1
_, data_iterator = helper.train_data[agent_name_key]
total_loss = 0.
correct = 0
dataset_size = 0
poison_data_count = 0
dis2global_list = []
model.train()
for batch_id, batch in enumerate(data_iterator):
optimizer.zero_grad()
# label -flipping
if ADV_CLIENT == True and helper.params['adv_method'] == 'labelflip':
data, targets, poison_num = helper.get_poison_batch(
batch, adversarial_index=0, evaluation=False)
poison_data_count += poison_num
# backdoor
elif ADV_CLIENT == True and helper.params['adv_method'] == 'backdoor':
if helper.params['dba'] == True:
data, targets, poison_num = helper.get_poison_batch(
batch, adversarial_index=1, evaluation=False, agent_name=agent_name_key)
else:
data, targets, poison_num = helper.get_poison_batch(
batch, adversarial_index=1, evaluation=False)
poison_data_count += poison_num
else:
data, targets = helper.get_batch(
data_iterator, batch, evaluation=False)
dataset_size += len(data)
output = model(data)
loss = criterion(output, targets)
loss.backward()
optimizer.step()
total_loss += loss.data
# get the index of the max log-probability
pred = output.data.max(1)[1]
correct += pred.eq(targets.data.view_as(pred)
).cpu().sum().item()
acc = 100.0 * (float(correct) / float(dataset_size))
total_l = total_loss / dataset_size
if internal_epoch == helper.params['internal_epochs'] and helper.params['record_local_train'] == True:
if ADV_CLIENT == True:
logger.info(
'___Train Local adv, epoch {:3d}, local model {}, local_epoch {:3d}, loss: {:.4f}, '
'Acc: {}/{} ({:.4f}%), num_adv_data: {}, dis: {:.3f}'.format(epoch, agent_name_key,
internal_epoch,
total_l, correct, dataset_size,
acc, poison_data_count,
model_dist_norm(model, target_params, helper.params['type'])))
else:
logger.info(
'___Train Local, epoch {:3d}, local model {}, local_epoch {:3d}, loss: {:.4f}, '
'Acc: {}/{} ({:.4f}%) dis: {:.3f}'.format(epoch, agent_name_key, internal_epoch,
total_l, correct, dataset_size,
acc, model_dist_norm(model, target_params, helper.params['type'])))
num_samples_dict[agent_name_key] = dataset_size
num_poisoned_samples_dict[agent_name_key] = poison_data_count
if ADV_CLIENT == True:
for name, layer in model.state_dict().items():
if 'num_batches_tracked' in name:
continue
data = model.state_dict()[name]
new_value = target_params[name] + \
(data - target_params[name]) * \
helper.params['scale_factor']
model.state_dict()[name].copy_(new_value)
if helper.params['record_p'] == True:
epoch_loss, epoch_acc_p, epoch_corret, epoch_total = test.poison_test(helper=helper,
epoch=epoch,
model=model,
is_poison=ADV_CLIENT,
visualize=True,
agent_name_key=agent_name_key)
csv_record.posiontest_result.append(
[agent_name_key, epoch, epoch_loss, epoch_acc_p])
# update the model params
client_pramas_update = dict()
for name, data in model.state_dict().items():
if 'num_batches_tracked' in name:
continue
client_pramas_update[name] = torch.zeros_like(data)
client_pramas_update[name] = (
data - target_model.state_dict()[name])
submit_params_update_dict[agent_name_key] = client_pramas_update
return submit_params_update_dict, num_samples_dict, num_poisoned_samples_dict
def FLTrain_InsDPFL(helper, logger, start_epoch, local_models, local_optimizers, local_privacy_engines, target_model, is_poison, agent_name_keys):
submit_params_update_dict = dict()
target_params = dict()
for name, param in target_model.named_parameters():
target_params[name] = target_model.state_dict(
)[name].clone().detach().requires_grad_(False)
for model_id in range(helper.params['no_models']):
# client_grad = []
agent_name_key = agent_name_keys[model_id]
ADV_CLIENT = False
if is_poison and agent_name_key in helper.params['adversary_list']:
ADV_CLIENT = True
# Synchronize LR and models
model = local_models[agent_name_key]
optimizer = local_optimizers[agent_name_key]
if helper.params['withDP'] == True:
local_privacy_engine = local_privacy_engines[agent_name_key]
model_copy_params(model, target_model.state_dict())
criterion = nn.CrossEntropyLoss().cuda()
model.train()
epoch = start_epoch
temp_local_epoch = (epoch - 1) * helper.params['internal_epochs']
for internal_epoch in range(1, helper.params['internal_epochs'] + 1):
temp_local_epoch += 1
_, data_iterator = helper.train_data[agent_name_key]
total_loss = 0.
correct = 0
dataset_size = 0
poison_data_count = 0
optimizer.zero_grad()
model.train()
for batch_id, batch in enumerate(data_iterator):
if ADV_CLIENT == True:
if batch_id == 0:
if helper.params['adv_method'] == 'labelflip': # label -flipping
data, targets, poison_num = helper.get_poison_batch(
batch, adversarial_index=0, evaluation=False)
poison_data_count += poison_num
elif helper.params['adv_method'] == 'backdoor': # backdoor
if helper.params['dba'] == True:
data, targets, poison_num = helper.get_poison_batch(
batch, adversarial_index=1, evaluation=False, agent_name=agent_name_key)
else:
data, targets, poison_num = helper.get_poison_batch(
batch, adversarial_index=1, evaluation=False)
poison_data_count += poison_num
else:
data, targets = helper.get_batch(
data_iterator, batch, evaluation=False)
dataset_size += len(data)
output = model(data)
loss = criterion(output, targets)
loss.backward()
optimizer.step()
optimizer.zero_grad()
total_loss += loss.data
# get the index of the max log-probability
pred = output.data.max(1)[1]
correct += pred.eq(targets.data.view_as(pred)
).cpu().sum().item()
acc = 100.0 * (float(correct) / float(dataset_size))
total_l = total_loss / dataset_size
if helper.params['record_local_train'] == True:
logger.info(
'___Train Local, epoch {:3d}, local model {}, local_epoch {:3d}, Avg loss: {:.4f}, '
'Acc: {}/{} ({:.4f}%) '.format(epoch, agent_name_key, internal_epoch,
total_l, correct, dataset_size,
acc))
# update the model params
client_pramas_update = dict()
for name, layer in model.named_parameters():
data = model.state_dict()[name]
client_pramas_update[name] = torch.zeros_like(data)
client_pramas_update[name].add_(
data - target_model.state_dict()[name])
submit_params_update_dict[agent_name_key] = client_pramas_update
return submit_params_update_dict