-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.py
186 lines (167 loc) · 7.5 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python version: 3.6
import random
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import copy
import numpy as np
from torchvision import datasets, transforms
import torch
import os
from utils.sampling import mnist_iid, mnist_noniid, cifar_iid,cifar_noniid
from utils.options import args_parser
from models.Update import LocalUpdateDP, LocalUpdateDPSerial
from models.Nets import MLP, CNNMnist, CNNCifar, CNNFemnist, CharLSTM
from models.Fed import FedAvg, FedWeightAvg
from models.test import test_img
from utils.dataset import FEMNIST, ShakeSpeare
from opacus.grad_sample import GradSampleModule
if __name__ == '__main__':
# parse args
random.seed(123)
np.random.seed(123)
torch.manual_seed(123)
torch.cuda.manual_seed_all(123)
torch.cuda.manual_seed(123)
args = args_parser()
args.device = torch.device('cuda:{}'.format(args.gpu) if torch.cuda.is_available() and args.gpu != -1 else 'cpu')
dict_users = {}
dataset_train, dataset_test = None, None
# load dataset and split users
if args.dataset == 'mnist':
trans_mnist = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
dataset_train = datasets.MNIST('./data/mnist/', train=True, download=True, transform=trans_mnist)
dataset_test = datasets.MNIST('./data/mnist/', train=False, download=True, transform=trans_mnist)
args.num_channels = 1
# sample users
if args.iid:
dict_users = mnist_iid(dataset_train, args.num_users)
else:
dict_users = mnist_noniid(dataset_train, args.num_users)
elif args.dataset == 'cifar':
#trans_cifar = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
args.num_channels = 3
trans_cifar_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trans_cifar_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
dataset_train = datasets.CIFAR10('./data/cifar', train=True, download=True, transform=trans_cifar_train)
dataset_test = datasets.CIFAR10('./data/cifar', train=False, download=True, transform=trans_cifar_test)
if args.iid:
dict_users = cifar_iid(dataset_train, args.num_users)
else:
dict_users = cifar_noniid(dataset_train, args.num_users)
elif args.dataset == 'fashion-mnist':
args.num_channels = 1
trans_fashion_mnist = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
dataset_train = datasets.FashionMNIST('./data/fashion-mnist', train=True, download=True,
transform=trans_fashion_mnist)
dataset_test = datasets.FashionMNIST('./data/fashion-mnist', train=False, download=True,
transform=trans_fashion_mnist)
if args.iid:
dict_users = mnist_iid(dataset_train, args.num_users)
else:
dict_users = mnist_noniid(dataset_train, args.num_users)
elif args.dataset == 'femnist':
args.num_channels = 1
dataset_train = FEMNIST(train=True)
dataset_test = FEMNIST(train=False)
dict_users = dataset_train.get_client_dic()
args.num_users = len(dict_users)
if args.iid:
exit('Error: femnist dataset is naturally non-iid')
else:
print("Warning: The femnist dataset is naturally non-iid, you do not need to specify iid or non-iid")
elif args.dataset == 'shakespeare':
dataset_train = ShakeSpeare(train=True)
dataset_test = ShakeSpeare(train=False)
dict_users = dataset_train.get_client_dic()
args.num_users = len(dict_users)
if args.iid:
exit('Error: ShakeSpeare dataset is naturally non-iid')
else:
print("Warning: The ShakeSpeare dataset is naturally non-iid, you do not need to specify iid or non-iid")
else:
exit('Error: unrecognized dataset')
img_size = dataset_train[0][0].shape
net_glob = None
# build model
if args.model == 'cnn' and args.dataset == 'cifar':
net_glob = CNNCifar(args=args).to(args.device)
elif args.model == 'cnn' and (args.dataset == 'mnist' or args.dataset == 'fashion-mnist'):
net_glob = CNNMnist(args=args).to(args.device)
elif args.dataset == 'femnist' and args.model == 'cnn':
net_glob = CNNFemnist(args=args).to(args.device)
elif args.dataset == 'shakespeare' and args.model == 'lstm':
net_glob = CharLSTM().to(args.device)
elif args.model == 'mlp':
len_in = 1
for x in img_size:
len_in *= x
net_glob = MLP(dim_in=len_in, dim_hidden=64, dim_out=args.num_classes).to(args.device)
else:
exit('Error: unrecognized model')
# use opacus to wrap model to clip per sample gradient
if args.dp_mechanism != 'no_dp':
net_glob = GradSampleModule(net_glob)
print(net_glob)
net_glob.train()
# copy weights
w_glob = net_glob.state_dict()
all_clients = list(range(args.num_users))
# training
acc_test = []
if args.serial:
clients = [LocalUpdateDPSerial(args=args, dataset=dataset_train, idxs=dict_users[i]) for i in range(args.num_users)]
else:
clients = [LocalUpdateDP(args=args, dataset=dataset_train, idxs=dict_users[i]) for i in range(args.num_users)]
m, loop_index = max(int(args.frac * args.num_users), 1), int(1 / args.frac)
for iter in range(args.epochs):
t_start = time.time()
w_locals, loss_locals, weight_locols = [], [], []
# round-robin selection
begin_index = (iter % loop_index) * m
end_index = begin_index + m
idxs_users = all_clients[begin_index:end_index]
for idx in idxs_users:
local = clients[idx]
w, loss = local.train(net=copy.deepcopy(net_glob).to(args.device))
w_locals.append(copy.deepcopy(w))
loss_locals.append(copy.deepcopy(loss))
weight_locols.append(len(dict_users[idx]))
# update global weights
w_glob = FedWeightAvg(w_locals, weight_locols)
# copy weight to net_glob
net_glob.load_state_dict(w_glob)
# print accuracy
net_glob.eval()
acc_t, loss_t = test_img(net_glob, dataset_test, args)
t_end = time.time()
print("Round {:3d},Testing accuracy: {:.2f},Time: {:.2f}s".format(iter, acc_t, t_end - t_start))
acc_test.append(acc_t.item())
rootpath = './log'
if not os.path.exists(rootpath):
os.makedirs(rootpath)
accfile = open(rootpath + '/accfile_fed_{}_{}_{}_iid{}_dp_{}_epsilon_{}.dat'.
format(args.dataset, args.model, args.epochs, args.iid,
args.dp_mechanism, args.dp_epsilon), "w")
for ac in acc_test:
sac = str(ac)
accfile.write(sac)
accfile.write('\n')
accfile.close()
# plot loss curve
plt.figure()
plt.plot(range(len(acc_test)), acc_test)
plt.ylabel('test accuracy')
plt.savefig(rootpath + '/fed_{}_{}_{}_C{}_iid{}_dp_{}_epsilon_{}_acc.png'.format(
args.dataset, args.model, args.epochs, args.frac, args.iid, args.dp_mechanism, args.dp_epsilon))