-
Notifications
You must be signed in to change notification settings - Fork 6
/
loop_df_fl.py
executable file
·331 lines (291 loc) · 13.7 KB
/
loop_df_fl.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python version: 3.6
import argparse
import copy
import os
import shutil
import sys
import warnings
import torchvision.models as models
import numpy as np
from tqdm import tqdm
import pdb
from helpers.datasets import partition_data
from helpers.synthesizers import AdvSynthesizer
from helpers.utils import get_dataset, average_weights, DatasetSplit, KLDiv, setup_seed, test
from models.generator import Generator
from models.nets import CNNCifar, CNNMnist, CNNCifar100
import torch
from torch.utils.data import DataLoader, Dataset
import torch.nn.functional as F
from models.resnet import resnet18
from models.vit import deit_tiny_patch16_224
import wandb
warnings.filterwarnings('ignore')
upsample = torch.nn.Upsample(mode='nearest', scale_factor=7)
class LocalUpdate(object):
def __init__(self, args, dataset, idxs):
self.args = args
self.train_loader = DataLoader(DatasetSplit(dataset, idxs),
batch_size=self.args.local_bs, shuffle=True, num_workers=4)
def update_weights(self, model, client_id):
model.train()
optimizer = torch.optim.SGD(model.parameters(), lr=self.args.lr,
momentum=0.9)
# label_list = [0] * 100
# for batch_idx, (images, labels) in enumerate(self.train_loader):
# for i in range(100):
# label_list[i] += torch.sum(labels == i).item()
# print(label_list)
local_acc_list = []
for iter in tqdm(range(self.args.local_ep)):
for batch_idx, (images, labels) in enumerate(self.train_loader):
images, labels = images.cuda(), labels.cuda()
model.zero_grad()
# ---------------------------------------
output = model(images)
loss = F.cross_entropy(output, labels)
# ---------------------------------------
loss.backward()
optimizer.step()
acc, test_loss = test(model, test_loader)
# if client_id == 0:
# wandb.log({'local_epoch': iter})
# wandb.log({'client_{}_accuracy'.format(client_id): acc})
local_acc_list.append(acc)
return model.state_dict(), np.array(local_acc_list)
def args_parser():
parser = argparse.ArgumentParser()
# federated arguments (Notation for the arguments followed from paper)
parser.add_argument('--epochs', type=int, default=10,
help="number of rounds of training")
parser.add_argument('--num_users', type=int, default=5,
help="number of users: K")
parser.add_argument('--frac', type=float, default=1,
help='the fraction of clients: C')
parser.add_argument('--local_ep', type=int, default=100,
help="the number of local epochs: E")
parser.add_argument('--local_bs', type=int, default=128,
help="local batch size: B")
parser.add_argument('--lr', type=float, default=0.01,
help='learning rate')
parser.add_argument('--momentum', type=float, default=0.9,
help='SGD momentum (default: 0.5)')
# other arguments
parser.add_argument('--dataset', type=str, default='cifar10', help="name \
of dataset")
parser.add_argument('--iid', type=int, default=1,
help='Default set to IID. Set to 0 for non-IID.')
# Data Free
parser.add_argument('--adv', default=0, type=float, help='scaling factor for adv loss')
parser.add_argument('--bn', default=0, type=float, help='scaling factor for BN regularization')
parser.add_argument('--oh', default=0, type=float, help='scaling factor for one hot loss (cross entropy)')
parser.add_argument('--act', default=0, type=float, help='scaling factor for activation loss used in DAFL')
parser.add_argument('--save_dir', default='run/synthesis', type=str)
parser.add_argument('--partition', default='dirichlet', type=str)
parser.add_argument('--beta', default=0.5, type=float,
help=' If beta is set to a smaller value, '
'then the partition is more unbalanced')
# Basic
parser.add_argument('--lr_g', default=1e-3, type=float,
help='initial learning rate for generation')
parser.add_argument('--T', default=1, type=float)
parser.add_argument('--g_steps', default=20, type=int, metavar='N',
help='number of iterations for generation')
parser.add_argument('--batch_size', default=256, type=int, metavar='N',
help='number of total iterations in each epoch')
parser.add_argument('--nz', default=256, type=int, metavar='N',
help='number of total iterations in each epoch')
parser.add_argument('--synthesis_batch_size', default=256, type=int)
# Misc
parser.add_argument('--seed', default=None, type=int,
help='seed for initializing training.')
parser.add_argument('--type', default="pretrain", type=str,
help='seed for initializing training.')
parser.add_argument('--model', default="", type=str,
help='seed for initializing training.')
parser.add_argument('--other', default="", type=str,
help='seed for initializing training.')
args = parser.parse_args()
return args
class Ensemble(torch.nn.Module):
def __init__(self, model_list):
super(Ensemble, self).__init__()
self.models = model_list
def forward(self, x):
logits_total = 0
for i in range(len(self.models)):
logits = self.models[i](x)
logits_total += logits
logits_e = logits_total / len(self.models)
return logits_e
def kd_train(synthesizer, model, criterion, optimizer):
student, teacher = model
student.train()
teacher.eval()
description = "loss={:.4f} acc={:.2f}%"
total_loss = 0.0
correct = 0.0
with tqdm(synthesizer.get_data()) as epochs:
for idx, (images) in enumerate(epochs):
optimizer.zero_grad()
images = images.cuda()
with torch.no_grad():
t_out = teacher(images)
s_out = student(images.detach())
loss_s = criterion(s_out, t_out.detach())
loss_s.backward()
optimizer.step()
total_loss += loss_s.detach().item()
avg_loss = total_loss / (idx + 1)
pred = s_out.argmax(dim=1)
target = t_out.argmax(dim=1)
correct += pred.eq(target.view_as(pred)).sum().item()
acc = correct / len(synthesizer.data_loader.dataset) * 100
epochs.set_description(description.format(avg_loss, acc))
def save_checkpoint(state, is_best, filename='checkpoint.pth'):
if is_best:
torch.save(state, filename)
def get_model(args):
if args.model == "mnist_cnn":
global_model = CNNMnist().cuda()
elif args.model == "fmnist_cnn":
global_model = CNNMnist().cuda()
elif args.model == "cnn":
global_model = CNNCifar().cuda()
elif args.model == "svhn_cnn":
global_model = CNNCifar().cuda()
elif args.model == "cifar100_cnn":
global_model = CNNCifar100().cuda()
elif args.model == "res":
# global_model = resnet18()
global_model = resnet18(num_classes=100).cuda()
elif args.model == "vit":
global_model = deit_tiny_patch16_224(num_classes=1000,
drop_rate=0.,
drop_path_rate=0.1)
global_model.head = torch.nn.Linear(global_model.head.in_features, 10)
global_model = global_model.cuda()
global_model = torch.nn.DataParallel(global_model)
return global_model
if __name__ == '__main__':
args = args_parser()
wandb.init(config=args,
project="ont-shot FL")
setup_seed(args.seed)
# pdb.set_trace()
train_dataset, test_dataset, user_groups, traindata_cls_counts = partition_data(
args.dataset, args.partition, beta=args.beta, num_users=args.num_users)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=256,
shuffle=False, num_workers=4)
# BUILD MODEL
global_model = get_model(args)
bst_acc = -1
description = "inference acc={:.4f}% loss={:.2f}, best_acc = {:.2f}%"
local_weights = []
global_model.train()
acc_list = []
users = []
if args.type == "pretrain":
# ===============================================
for idx in range(args.num_users):
print("client {}".format(idx))
users.append("client_{}".format(idx))
local_model = LocalUpdate(args=args, dataset=train_dataset,
idxs=user_groups[idx])
w, local_acc = local_model.update_weights(copy.deepcopy(global_model), idx)
acc_list.append(local_acc)
local_weights.append(copy.deepcopy(w))
# wandb
for i in range(args.local_ep):
wandb.log({"client_{}_acc".format(users[0]):acc_list[0][i],
"client_{}_acc".format(users[1]):acc_list[1][i],
"client_{}_acc".format(users[2]):acc_list[2][i],
"client_{}_acc".format(users[3]):acc_list[3][i],
"client_{}_acc".format(users[4]):acc_list[4][i],
})
# np.save("client_{}_acc.npy".format(args.num_users), acc_list)
wandb.log({"client_accuracy" : wandb.plot.line_series(
xs=[ i for i in range(args.local_ep) ],
ys=[ acc_list[i] for i in range(args.num_users) ],
keys=users,
title="Client Accuacy")})
# torch.save(local_weights, '{}_{}.pkl'.format(name, iid))
torch.save(local_weights, '{}_{}clients_{}.pkl'.format(args.dataset, args.num_users, args.beta))
# update global weights by FedAvg
global_weights = average_weights(local_weights)
global_model.load_state_dict(global_weights)
print("avg acc:")
test_acc, test_loss = test(global_model, test_loader)
model_list = []
for i in range(len(local_weights)):
net = copy.deepcopy(global_model)
net.load_state_dict(local_weights[i])
model_list.append(net)
ensemble_model = Ensemble(model_list)
print("ensemble acc:")
test(ensemble_model, test_loader)
# ===============================================
else:
# ===============================================
local_weights = torch.load('{}_{}clients_{}.pkl'.format(args.dataset, args.num_users, args.beta))
global_weights = average_weights(local_weights)
global_model.load_state_dict(global_weights)
print("avg acc:")
test_acc, test_loss = test(global_model, test_loader)
model_list = []
for i in range(len(local_weights)):
net = copy.deepcopy(global_model)
net.load_state_dict(local_weights[i])
model_list.append(net)
ensemble_model = Ensemble(model_list)
print("ensemble acc:")
test(ensemble_model, test_loader)
# ===============================================
global_model = get_model(args)
# ===============================================
# data generator
nz = args.nz
nc = 3 if "cifar" in args.dataset or args.dataset == "svhn" else 1
img_size = 32 if "cifar" in args.dataset or args.dataset == "svhn" else 28
generator = Generator(nz=nz, ngf=64, img_size=img_size, nc=nc).cuda()
args.cur_ep = 0
img_size2 = (3, 32, 32) if "cifar" in args.dataset or args.dataset == "svhn" else (1, 28, 28)
num_class = 100 if args.dataset == "cifar100" else 10
synthesizer = AdvSynthesizer(ensemble_model, model_list, global_model, generator,
nz=nz, num_classes=num_class, img_size=img_size2,
iterations=args.g_steps, lr_g=args.lr_g,
synthesis_batch_size=args.synthesis_batch_size,
sample_batch_size=args.batch_size,
adv=args.adv, bn=args.bn, oh=args.oh,
save_dir=args.save_dir, dataset=args.dataset)
# &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
criterion = KLDiv(T=args.T)
optimizer = torch.optim.SGD(global_model.parameters(), lr=args.lr,
momentum=0.9)
global_model.train()
distill_acc = []
for epoch in tqdm(range(args.epochs)):
# 1. Data synthesis
synthesizer.gen_data(args.cur_ep) # g_steps
args.cur_ep += 1
kd_train(synthesizer, [global_model, ensemble_model], criterion, optimizer) # # kd_steps
acc, test_loss = test(global_model, test_loader)
distill_acc.append(acc)
is_best = acc > bst_acc
bst_acc = max(acc, bst_acc)
_best_ckpt = 'df_ckpt/{}.pth'.format(args.other)
print("best acc:{}".format(bst_acc))
save_checkpoint({
'state_dict': global_model.state_dict(),
'best_acc': float(bst_acc),
}, is_best, _best_ckpt)
wandb.log({'accuracy': acc})
wandb.log({"global_accuracy" : wandb.plot.line_series(
xs=[ i for i in range(args.epochs) ],
ys=distill_acc,
keys="DENSE",
title="Accuacy of DENSE")})
np.save("distill_acc_{}.npy".format(args.dataset), np.array(distill_acc))
# ===============================================