-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
242 lines (193 loc) · 10.2 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
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
import copy
import torch
from options import args_parser
import os
import time
import warnings
import numpy as np
import torchvision
import logging
from core.servers.serveravg import FedAvg
from core.servers.serverlocal import Local
from core.trainmodel.models import *
from core.trainmodel.bilstm import *
from core.trainmodel.resnet import *
from core.trainmodel.alexnet import *
from core.trainmodel.mobilenet_v2 import *
from core.trainmodel.transformer import *
from utils.result_utils import average_data
from utils.mem_utils import MemReporter
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
warnings.simplefilter("ignore")
torch.manual_seed(0)
# hyper-params for Text tasks
vocab_size = 98635
max_len = 200
emb_dim = 32
def run(args):
time_list = []
reporter = MemReporter()
model_str = args.model
for i in range(args.prev, args.times):
print(f"\n============= Running time: {i}th =============")
print("Creating server and clients ...")
start = time.time()
# Generate args.model
if model_str == "mlr": # convex
if "mnist" in args.dataset:
args.model = Mclr_Logistic(1 * 28 * 28, num_classes=args.num_classes).to(args.device)
elif "Cifar10" in args.dataset:
args.model = Mclr_Logistic(3 * 32 * 32, num_classes=args.num_classes).to(args.device)
else:
args.model = Mclr_Logistic(60, num_classes=args.num_classes).to(args.device)
elif model_str == "cnn": # non-convex
if "mnist" in args.dataset:
args.model = FedAvgCNN(in_features=1, num_classes=args.num_classes, dim=1024).to(args.device)
elif "Cifar10" in args.dataset:
args.model = FedAvgCNN(in_features=3, num_classes=args.num_classes, dim=1600).to(args.device)
elif "omniglot" in args.dataset:
args.model = FedAvgCNN(in_features=1, num_classes=args.num_classes, dim=33856).to(args.device)
# args.model = CifarNet(num_classes=args.num_classes).to(args.device)
elif "Digit5" in args.dataset:
args.model = Digit5CNN().to(args.device)
else:
args.model = FedAvgCNN(in_features=3, num_classes=args.num_classes, dim=10816).to(args.device)
elif model_str == "dnn": # non-convex
if "mnist" in args.dataset:
args.model = DNN(1 * 28 * 28, 100, num_classes=args.num_classes).to(args.device)
elif "Cifar10" in args.dataset:
args.model = DNN(3 * 32 * 32, 100, num_classes=args.num_classes).to(args.device)
else:
args.model = DNN(60, 20, num_classes=args.num_classes).to(args.device)
elif model_str == "resnet":
args.model = torchvision.models.resnet18(pretrained=False, num_classes=args.num_classes).to(args.device)
# args.model = torchvision.models.resnet18(pretrained=True).to(args.device)
# feature_dim = list(args.model.fc.parameters())[0].shape[1]
# args.model.fc = nn.Linear(feature_dim, args.num_classes).to(args.device)
# args.model = resnet18(num_classes=args.num_classes, has_bn=True, bn_block_num=4).to(args.device)
elif model_str == "resnet10":
args.model = resnet10(num_classes=args.num_classes).to(args.device)
elif model_str == "resnet34":
args.model = torchvision.models.resnet34(pretrained=False, num_classes=args.num_classes).to(args.device)
elif model_str == "alexnet":
args.model = alexnet(pretrained=False, num_classes=args.num_classes).to(args.device)
# args.model = alexnet(pretrained=True).to(args.device)
# feature_dim = list(args.model.fc.parameters())[0].shape[1]
# args.model.fc = nn.Linear(feature_dim, args.num_classes).to(args.device)
elif model_str == "googlenet":
args.model = torchvision.models.googlenet(pretrained=False, aux_logits=False,
num_classes=args.num_classes).to(args.device)
# args.model = torchvision.models.googlenet(pretrained=True, aux_logits=False).to(args.device)
# feature_dim = list(args.model.fc.parameters())[0].shape[1]
# args.model.fc = nn.Linear(feature_dim, args.num_classes).to(args.device)
elif model_str == "mobilenet_v2":
args.model = mobilenet_v2(pretrained=False, num_classes=args.num_classes).to(args.device)
# args.model = mobilenet_v2(pretrained=True).to(args.device)
# feature_dim = list(args.model.fc.parameters())[0].shape[1]
# args.model.fc = nn.Linear(feature_dim, args.num_classes).to(args.device)
elif model_str == "lstm":
args.model = LSTMNet(hidden_dim=emb_dim, vocab_size=vocab_size, num_classes=args.num_classes).to(
args.device)
elif model_str == "bilstm":
args.model = BiLSTM_TextClassification(input_size=vocab_size, hidden_size=emb_dim,
output_size=args.num_classes,
num_layers=1, embedding_dropout=0, lstm_dropout=0,
attention_dropout=0,
embedding_length=emb_dim).to(args.device)
elif model_str == "fastText":
args.model = fastText(hidden_dim=emb_dim, vocab_size=vocab_size, num_classes=args.num_classes).to(
args.device)
elif model_str == "TextCNN":
args.model = TextCNN(hidden_dim=emb_dim, max_len=max_len, vocab_size=vocab_size,
num_classes=args.num_classes).to(args.device)
elif model_str == "Transformer":
args.model = TransformerModel(ntoken=vocab_size, d_model=emb_dim, nhead=8, d_hid=emb_dim, nlayers=2,
num_classes=args.num_classes).to(args.device)
elif model_str == "AmazonMLP":
args.model = AmazonMLP().to(args.device)
elif model_str == "harcnn":
if args.dataset == 'har':
args.model = HARCNN(9, dim_hidden=1664, num_classes=args.num_classes, conv_kernel_size=(1, 9),
pool_kernel_size=(1, 2)).to(args.device)
elif args.dataset == 'pamap':
args.model = HARCNN(9, dim_hidden=3712, num_classes=args.num_classes, conv_kernel_size=(1, 9),
pool_kernel_size=(1, 2)).to(args.device)
else:
raise NotImplementedError
print(args.model)
# select algorithm
if args.algorithm == "FedAvg":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedAvg(args, i)
elif args.algorithm == "Local":
server = Local(args, i)
else:
raise NotImplementedError
server.train()
time_list.append(time.time() - start)
print(f"\nAverage time cost: {round(np.average(time_list), 2)}s.")
# Global average
average_data(dataset=args.dataset, algorithm=args.algorithm, goal=args.goal, times=args.times)
print("All done!")
reporter.report()
if __name__ == "__main__":
total_start = time.time()
args = args_parser()
os.environ["CUDA_VISIBLE_DEVICES"] = args.device_id
if args.device == "cuda" and not torch.cuda.is_available():
print("\ncuda is not avaiable.\n")
args.device = "cpu"
print("=" * 50)
print("Algorithm: {}".format(args.algorithm))
print("Local batch size: {}".format(args.batch_size))
print("Local epochs: {}".format(args.local_epochs))
print("Local learing rate: {}".format(args.local_learning_rate))
print("Local learing rate decay: {}".format(args.learning_rate_decay))
if args.learning_rate_decay:
print("Local learing rate decay gamma: {}".format(args.learning_rate_decay_gamma))
print("Total number of clients: {}".format(args.num_clients))
print("Clients join in each round: {}".format(args.join_ratio))
print("Clients randomly join: {}".format(args.random_join_ratio))
print("Client drop rate: {}".format(args.client_drop_rate))
print("Client select regarding time: {}".format(args.time_select))
if args.time_select:
print("Time threthold: {}".format(args.time_threthold))
print("Running times: {}".format(args.times))
print("Dataset: {}".format(args.dataset))
print("Number of classes: {}".format(args.num_classes))
print("Backbone: {}".format(args.model))
print("Using device: {}".format(args.device))
print("Using DP: {}".format(args.privacy))
if args.privacy:
print("Sigma for DP: {}".format(args.dp_sigma))
print("Auto break: {}".format(args.auto_break))
if not args.auto_break:
print("Global rounds: {}".format(args.global_rounds))
if args.device == "cuda":
print("Cuda device id: {}".format(os.environ["CUDA_VISIBLE_DEVICES"]))
print("DLG attack: {}".format(args.dlg_eval))
if args.dlg_eval:
print("DLG attack round gap: {}".format(args.dlg_gap))
print("Total number of new clients: {}".format(args.num_new_clients))
print("Fine tuning epoches on new clients: {}".format(args.fine_tuning_epoch_new))
print("=" * 50)
# if args.data == "mnist" or args.data == "fmnist":
# generate_mnist('../data/mnist/', args.num_clients, 10, args.niid)
# elif args.data == "Cifar10" or args.data == "Cifar100":
# generate_cifar10('../data/Cifar10/', args.num_clients, 10, args.niid)
# else:
# generate_synthetic('../data/synthetic/', args.num_clients, 10, args.niid)
# with torch.profiler.profile(
# activities=[
# torch.profiler.ProfilerActivity.CPU,
# torch.profiler.ProfilerActivity.CUDA],
# profile_memory=True,
# on_trace_ready=torch.profiler.tensorboard_trace_handler('./log')
# ) as prof:
# with torch.autograd.profiler.profile(profile_memory=True) as prof:
run(args)
# print(prof.key_averages().table(sort_by="cpu_time_total", row_limit=20))
# print(f"\nTotal time cost: {round(time.time()-total_start, 2)}s.")