-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
206 lines (149 loc) · 6.94 KB
/
utils.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
import sys
import logging
import numpy as np
import torch
import torchvision
CIFAR10_TRAIN_MEAN = np.array((0.4914, 0.4822, 0.4465))[None, :, None, None]
CIFAR10_TRAIN_STD = np.array((0.2470, 0.2435, 0.2616))[None, :, None, None]
def get_logger(filename):
# Logging configuration: set the basic configuration of the logging system
log_formatter = logging.Formatter(fmt='%(asctime)s [%(levelname)-5.5s] %(message)s',
datefmt='%Y-%b-%d %H:%M')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# File logger
file_handler = logging.FileHandler(filename, mode='w') # default is 'a' to append
file_handler.setFormatter(log_formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
# Stdout logger
std_handler = logging.StreamHandler(sys.stdout)
std_handler.setFormatter(log_formatter)
std_handler.setLevel(logging.DEBUG)
logger.addHandler(std_handler)
return logger
def get_data(args):
if args.dataset == 'mnist' or args.dataset == 'fashion-mnist':
data_file = f"{args.data_path}/{args.dataset}.npz"
dataset = np.load(data_file)
train_X, train_y = dataset['x_train'], dataset['y_train'].astype(np.int64)
test_X, test_y = dataset['x_test'], dataset['y_test'].astype(np.int64)
if args.dataset == 'fashion-mnist':
train_X = np.reshape(train_X, (-1, 1, 28, 28))
test_X = np.reshape(test_X, (-1, 1, 28, 28))
else:
train_X = np.expand_dims(train_X, 1)
test_X = np.expand_dims(test_X, 1)
elif args.dataset == 'cifar10':
# Only load data, transformation done later
trainset = torchvision.datasets.CIFAR10(root=f"{args.data_path}/{args.dataset}/",
train=True)
# download = True,
train_X = trainset.data.transpose([0, 3, 1, 2])
train_y = np.array(trainset.targets)
testset = torchvision.datasets.CIFAR10(root=f"{args.data_path}/{args.dataset}/",
train=False)
test_X = testset.data.transpose([0, 3, 1, 2])
test_y = np.array(testset.targets)
else:
raise ValueError("Unknown dataset")
return train_X, train_y, test_X, test_y
def data_loader(dataset, inputs, targets, batch_size, is_train=True):
def cifar10_norm(x):
x -= CIFAR10_TRAIN_MEAN
x /= CIFAR10_TRAIN_STD
return x
def no_norm(x):
return x
if dataset == 'cifar10':
norm_func = cifar10_norm
else:
norm_func = no_norm
assert inputs.shape[0] == targets.shape[0]
n_examples = inputs.shape[0]
sample_rate = batch_size / n_examples
num_blocks = int(n_examples / batch_size)
if is_train:
for i in range(num_blocks):
mask = np.random.rand(n_examples) < sample_rate
if np.sum(mask) != 0:
yield (norm_func(inputs[mask].astype(np.float32) / 255.),
targets[mask])
else:
for i in range(num_blocks):
yield (norm_func(inputs[i * batch_size: (i+1) * batch_size].astype(np.float32) / 255.),
targets[i * batch_size: (i+1) * batch_size])
if num_blocks * batch_size != n_examples:
yield (norm_func(inputs[num_blocks * batch_size:].astype(np.float32) / 255.),
targets[num_blocks * batch_size:])
def partition_data(train_X, train_y, args):
idx = np.arange(0, len(train_X))
np.random.shuffle(idx)
# Preparation
if args.partition_type == 'class':
avail_idx = np.arange(len(train_X))
train_labels = train_y
elif args.partition_type == 'iid':
idx = np.arange(0, len(train_X))
np.random.shuffle(idx)
# Get data
client_data_list = []
for i in range(args.n_clients):
if args.partition_type == 'class':
client_major_class = np.random.randint(args.n_class)
avail_X = train_X[avail_idx]
avail_labels = train_labels[avail_idx]
major_mask = avail_labels == client_major_class
major_idx = np.where(major_mask)[0]
np.random.shuffle(major_idx)
assert int(args.n_client_data * args.major_percent) <= len(major_idx)
major_idx = major_idx[:int(args.n_client_data * args.major_percent)]
minor_idx = np.where(~major_mask)[0]
np.random.shuffle(minor_idx)
assert args.n_client_data - int(args.n_client_data * args.major_percent) <= len(minor_idx)
minor_idx = minor_idx[:args.n_client_data - int(args.n_client_data * args.major_percent)]
client_data_idx = np.concatenate((major_idx, minor_idx))
np.random.shuffle(client_data_idx)
client_data = avail_X[client_data_idx], train_y[avail_idx][client_data_idx]
client_data_list.append(client_data)
remaining_idx = set(range(len(avail_idx))) - set(client_data_idx)
avail_idx = avail_idx[list(remaining_idx)]
elif args.partition_type == 'class-rep':
client_major_class = np.random.randint(args.n_class)
major_mask = train_y == client_major_class
major_idx = np.where(major_mask)[0]
np.random.shuffle(major_idx)
major_idx = major_idx[:int(args.n_client_data * args.major_percent)]
minor_idx = np.where(~major_mask)[0]
np.random.shuffle(minor_idx)
minor_idx = minor_idx[:args.n_client_data - int(args.n_client_data * args.major_percent)]
client_data_idx = np.concatenate((major_idx, minor_idx))
np.random.shuffle(client_data_idx)
client_data = train_X[client_data_idx], train_y[client_data_idx]
client_data_list.append(client_data)
elif args.partition_type == 'iid':
client_data_idx = idx[i * args.n_client_data:(i + 1) * args.n_client_data]
client_data = train_X[client_data_idx], train_y[client_data_idx]
client_data_list.append(client_data)
return client_data_list
def evaluate_model(model, data, args):
model.eval()
x, y = data
loader = data_loader(args.dataset, x, y, batch_size=1000, is_train=False)
acc = 0.
for xt, yt in loader:
xt = torch.tensor(xt, requires_grad=False, dtype=torch.float32).to(args.device)
yt = torch.tensor(yt, requires_grad=False, dtype=torch.int64).to(args.device)
preds_labels = torch.squeeze(torch.max(model(xt), 1)[1])
acc += torch.sum(preds_labels == yt).item()
return acc / x.shape[0]
def extract_numpy_weights(model):
tensor_weights = model.state_dict()
numpy_weights = {}
for k in tensor_weights.keys():
numpy_weights[k] = tensor_weights[k].detach().cpu().numpy()
return numpy_weights
def convert_np_weights_to_tensor(weights):
for k in weights.keys():
weights[k] = torch.from_numpy(weights[k])
return weights