-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_AFM3DTW_SFD_densenet121.py
368 lines (303 loc) · 15.3 KB
/
client_AFM3DTW_SFD_densenet121.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
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
import torch
import numpy as np
from torch import nn
#from torch.nn import functional as F
from torchvision import datasets, transforms
from copy import deepcopy
from torch.autograd import Variable
from torch.utils.data import DataLoader, Dataset
import pandas as pd
import time
import random
from torch.nn import functional as F
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import itertools
class DatasetSplit(Dataset):
"""An abstract Dataset class wrapped around Pytorch Dataset class.
"""
def __init__(self, dataset):
self.dataset = dataset
self.idxs = [int(i) for i in range(len(dataset))]
def __len__(self):
return len(self.idxs)
def __getitem__(self, item):
image, label = self.dataset[self.idxs[item]]
return torch.as_tensor(image), torch.as_tensor(label)
class Client(nn.Module):
def __init__(self,model,id,path,type_client,update_step,update_step_test,base_lr,meta_lr,device,mode,batch_size):
super(Client, self).__init__()
self.id = id
self.update_step = update_step ## task-level inner update steps
self.update_step_test = update_step_test
self.net = deepcopy(model)
self.base_lr = base_lr
self.meta_lr = meta_lr
self.batch_size = batch_size
self.data = torch.load(path)
np.random.shuffle(self.data)
self.mode = mode
self.time = 0
self.epoch = 0
if self.mode == "fed_train":
support_size = int(len(self.data)*1.0)
support_set = DatasetSplit(self.data[:support_size])
self.support_loader = DataLoader(
support_set, batch_size = self.batch_size, shuffle=True,drop_last=True)
elif self.mode == "reptile_train":
support_size = int(len(self.data)*1.0)
support_set = DatasetSplit(self.data[:support_size])
self.support_loader = DataLoader(
support_set, batch_size = int(0.2*len(support_set)), shuffle=True,drop_last=True)
else:
support_size = int(len(self.data)*0.5)
support_set = DatasetSplit(self.data[:support_size])
self.support_loader = DataLoader(
support_set, batch_size = self.batch_size, shuffle=True,drop_last=True)
query_size = int(len(self.data)*1.0)
if self.mode == "fed_train":
pass
elif self.mode == "reptile_train":
pass
else:
query_set = DatasetSplit(self.data[support_size:query_size])
self.query_loader = DataLoader(
query_set, batch_size = self.batch_size, shuffle=True,drop_last=True)
self.optim = torch.optim.Adam(self.net.parameters(), lr = self.base_lr)
self.outer_optim = torch.optim.Adam(self.net.parameters(), lr = self.meta_lr)
self.size = query_size
self.device = device
self.loss_function = torch.nn.CrossEntropyLoss().to(self.device)
def forward(self):
pass
def local_fed_train(self):
for _ in range(self.update_step):
for support in self.support_loader:
support_x, support_y = support
if torch.cuda.is_available():
support_x = support_x.cuda(self.device)
support_y = support_y.cuda(self.device)
self.optim.zero_grad()
output = self.net(support_x)
#output = torch.squeeze(output)
loss = self.loss_function(output,support_y)
loss.backward()
self.optim.step()
self.optim.zero_grad()
def local_meta_reptile_train(self):
for _ in range(self.update_step):
net_tem = deepcopy(self.net)
meta_optim_tem = torch.optim.Adam(net_tem.parameters(), lr = self.base_lr)
i = 1
for support in self.support_loader:
support_x, support_y = support
if torch.cuda.is_available():
support_x = support_x.cuda(self.device)
support_y = support_y.cuda(self.device)
meta_optim_tem.zero_grad()
output = net_tem(support_x,)
output = torch.squeeze(output)
loss = self.loss_function(output,support_y)
loss.backward()
meta_optim_tem.step()
i += 1
if i > 5:
break
self.outer_optim.zero_grad()
for w, w_t in zip(self.net.parameters(), net_tem.parameters()):
if w.grad is None:
w.grad = Variable(torch.zeros_like(w)).to(self.device)
w.grad.data.add_(w.data - w_t.data)
self.outer_optim.step()
def local_asymeta_fomaml_train(self):
for _ in range(self.update_step):
i = 0
for support,query in zip(self.support_loader,self.query_loader):
support_x, support_y = support[0].to(self.device), support[1].to(self.device)
query_x, query_y = query
if torch.cuda.is_available():
support_x = support_x.cuda(self.device)
support_y = support_y.cuda(self.device)
query_x = query_x.cuda(self.device)
query_y = query_y.cuda(self.device)
self.optim.zero_grad()
output = self.net(support_x)
#output = torch.squeeze(output)
loss = self.loss_function(output,support_y)
loss.backward()
self.optim.step()
self.optim.zero_grad()
self.outer_optim.zero_grad()
#for batch_idx, query_x in enumerate(query_x):
output = self.net(query_x)
#output = torch.squeeze(output)
loss = self.loss_function(output, query_y)
loss.backward()
self.outer_optim.step()
self.outer_optim.zero_grad()
i += 1
if i > 5:
break
#break
time_range_1, time_range_2 = 3, 20#------------------------------------
self.time = random.uniform(time_range_1, time_range_2)
def local_asymeta_reptile_train(self,):
for _ in range(self.update_step):
net_tem = deepcopy(self.net)
meta_optim_tem = torch.optim.Adam(net_tem.parameters(), lr = self.base_lr)
i = 1
for support in self.support_loader:
support_x, support_y = support
if torch.cuda.is_available():
support_x = support_x.cuda(self.device)
support_y = support_y.cuda(self.device)
meta_optim_tem.zero_grad()
output = net_tem(support_x,)
output = torch.squeeze(output)
loss = self.loss_function(output,support_y)
loss.backward()
meta_optim_tem.step()
i += 1
if i > 5:
break
self.outer_optim.zero_grad()
for w, w_t in zip(self.net.parameters(), net_tem.parameters()):
if w.grad is None:
w.grad = Variable(torch.zeros_like(w)).to(self.device)
w.grad.data.add_(w.data - w_t.data)
self.outer_optim.step()
time_range_1, time_range_2 = 3, 20
self.time = random.uniform(time_range_1, time_range_2)
def refresh(self,model):
for w,w_t in zip(self.net.parameters(),model.parameters()):
w.data.copy_(w_t.data)
def test(self):
fine_tuning_loss_list = torch.zeros([self.update_step_test])
test_loss_list = torch.zeros([self.update_step_test])
init_loss_list = torch.zeros([1])
#---
init_acc_list = torch.zeros([1])
fine_tuning_acc_list = torch.zeros([self.update_step_test])
test_acc_list = torch.zeros([self.update_step_test])
#---
init_pre_list = torch.zeros([1])
fine_tuning_pre_list = torch.zeros([self.update_step_test])
test_pre_list = torch.zeros([self.update_step_test])
#---
init_recall_list = torch.zeros([1])
fine_tuning_recall_list = torch.zeros([self.update_step_test])
test_recall_list = torch.zeros([self.update_step_test])
#---
init_f1_list = torch.zeros([1])
fine_tuning_f1_list = torch.zeros([self.update_step_test])
test_f1_list = torch.zeros([self.update_step_test])
loss_all, correct_all, total = 0.0, 0.0, 0.0
precision_all, recall_all, f1_all = 0.0, 0.0, 0.0
for query in self.query_loader:#---------------------------------------init
query_x, query_y = query
if torch.cuda.is_available():
query_x = query_x.cuda(self.device)
query_y = query_y.cuda(self.device)
output = self.net(query_x)
output = torch.squeeze(output)
loss = self.loss_function(output, query_y)
loss_all += loss.item()
total += len(query_y)
y_hat = self.net(query_x)
query_pred = F.softmax(y_hat, dim=1).argmax(dim=1) # size = (75)
correct = torch.eq(query_pred, query_y).sum().item()
correct_all += correct
#acc = correct/len(query_x)
#------------------------------------------------------------------
#acc_sk = accuracy_score(query_pred, query_y)
query_y_c = query_y.cpu().numpy()#----
query_pred_c = query_pred.cpu().numpy()#----
precision = precision_score(query_pred_c, query_y_c,average='macro')
recall = recall_score(query_pred_c, query_y_c,average='macro')
f1 = f1_score(query_pred_c, query_y_c,average='macro')
precision_all += precision
recall_all += recall
f1_all += f1
init_loss_list = loss_all/len(self.query_loader)
init_acc_list = correct_all/total
init_pre_list = precision_all/len(self.query_loader)
init_recall_list = recall_all/len(self.query_loader)
init_f1_list = f1_all/len(self.query_loader)
for epoch in range(self.update_step_test):
loss_all, correct_all, total = 0.0, 0.0, 0.0
precision_all, recall_all, f1_all = 0.0, 0.0, 0.0
for support in self.support_loader:
self.optim.zero_grad()
support_x, support_y = support
if torch.cuda.is_available():
support_x = support_x.cuda(self.device)
support_y = support_y.cuda(self.device)
output = self.net(support_x)
#print(output)
#output = torch.squeeze(output)
loss = self.loss_function(output, support_y)
loss.backward()
self.optim.step()
y_hat = self.net(support_x)
query_pred = F.softmax(y_hat, dim=1).argmax(dim=1) # size = (75)
correct = torch.eq(query_pred, support_y).sum().item()
correct_all += correct
#acc = correct/len(support_x)
support_y_c = support_y.cpu().numpy()#----
query_pred_c = query_pred.cpu().numpy()#----
precision = precision_score(query_pred_c, support_y_c,average='macro')
recall = recall_score(query_pred_c, support_y_c,average='macro')
f1 = f1_score(query_pred_c, support_y_c,average='macro')
loss_all += loss.item()
total += len(support_y)
precision_all += precision
recall_all += recall
f1_all += f1
fine_tuning_loss_list[epoch] = loss_all/len(self.support_loader)
fine_tuning_acc_list[epoch] = correct_all/total
fine_tuning_pre_list[epoch] = precision_all/len(self.support_loader)
fine_tuning_recall_list[epoch] = recall_all/len(self.support_loader)
fine_tuning_f1_list[epoch] = f1_all/len(self.support_loader)
loss_all, correct_all, total = 0.0, 0.0, 0.0
precision_all, recall_all, f1_all = 0.0, 0.0, 0.0
for query in self.query_loader:
query_x, query_y = query
if torch.cuda.is_available():
query_x = query_x.cuda(self.device)
query_y = query_y.cuda(self.device)
output = self.net(query_x)
output = torch.squeeze(output)
loss = self.loss_function(output, query_y)
loss_all += loss.item()
y_hat = self.net(query_x)
query_pred = F.softmax(y_hat, dim=1).argmax(dim=1) # size = (75)
correct = torch.eq(query_pred, query_y).sum().item()
correct_all += correct
total += len(query_y)
query_y_c = query_y.cpu().numpy()#----
query_pred_c = query_pred.cpu().numpy()#----
precision = precision_score(query_pred_c, query_y_c,average='macro')
recall = recall_score(query_pred_c, query_y_c,average='macro')
f1 = f1_score(query_pred_c, query_y_c,average='macro')
precision_all += precision
recall_all += recall
f1_all += f1
test_loss_list[epoch] = loss_all/len(self.query_loader)
test_acc_list[epoch] = correct_all/total
test_pre_list[epoch] = precision_all/len(self.query_loader)
test_recall_list[epoch] = recall_all/len(self.query_loader)
test_f1_list[epoch] = f1_all/len(self.query_loader)
fine_tuning_loss_list = fine_tuning_loss_list.detach().numpy()
test_loss_list = test_loss_list.detach().numpy()
fine_tuning_acc_list = fine_tuning_acc_list.detach().numpy()
test_acc_list = test_acc_list.detach().numpy()
fine_tuning_pre_list = fine_tuning_pre_list.detach().numpy()
test_pre_list = test_pre_list.detach().numpy()
fine_tuning_recall_list = fine_tuning_recall_list.detach().numpy()
test_recall_list = test_recall_list.detach().numpy()
fine_tuning_f1_list = fine_tuning_f1_list.detach().numpy()
test_f1_list = test_f1_list.detach().numpy()
return fine_tuning_loss_list,test_loss_list,init_loss_list\
,fine_tuning_acc_list,test_acc_list,init_acc_list\
,fine_tuning_pre_list,test_pre_list,init_pre_list\
,fine_tuning_recall_list,test_recall_list,init_recall_list\
,fine_tuning_f1_list,test_f1_list,init_f1_list