-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_helper.py
357 lines (299 loc) · 13.8 KB
/
image_helper.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
import os
import copy
from config import device
import config
from collections import defaultdict
import torch
import torch.utils.data
from helper import Helper
import random
import logging
from torchvision import datasets, transforms
import numpy as np
logger = logging.getLogger("logger")
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
class PartDataset(torch.utils.data.Dataset):
def __init__(self, dataset, lab_list):
self.dataset = dataset
self.used_ids = []
self.lab_list = lab_list
self.lab_map = dict()
for i in range(len(lab_list)):
self.lab_map[lab_list[i]] = i
print("lab_map", self.lab_map)
for i, (X, y) in enumerate(dataset):
if y in self.lab_list:
self.used_ids.append(i)
def __len__(self,):
return len(self.used_ids)
def __getitem__(self, i):
X, y = self.dataset[self.used_ids[i]]
y_new = self.lab_map[y]
return X, y_new
def get_resnet18(pretrained=True, num_of_classes=10):
from torchvision import models
import torch.nn as nn
model_ft = models.resnet18(pretrained=pretrained)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_of_classes)
return model_ft
def get_cifarcnn(num_of_classes=10):
import torch.nn as nn
from models.CifarNet import CifarNet100
model = CifarNet100().cuda()
loaded_params = torch.load('./saved_models/cifar100_model_best.pt.tar')
model.load_state_dict(loaded_params['state_dict'])
model.fc = nn.Linear(128, num_of_classes, bias=True).cuda()
return model
class ImageHelper(Helper):
def create_one_model(self):
local_model = None
if self.params['type'] == config.TYPE_CIFAR:
if self.params['binary_cls'] == True:
num_of_classes = 2
else:
num_of_classes = 10
local_model = get_cifarcnn(num_of_classes)
elif self.params['type'] == config.TYPE_MNIST:
from models.MnistNet import MnistNet
if self.params['binary_cls'] == True:
num_of_classes = 2
else:
num_of_classes = 10
local_model = MnistNet(name='Local',
created_time=self.params['current_time'], num_of_classes=num_of_classes)
local_model = local_model.to(device)
return local_model
def create_model(self):
local_model = None
target_model = None
if self.params['type'] == config.TYPE_CIFAR:
if self.params['binary_cls'] == True:
num_of_classes = 2
else:
num_of_classes = 10
local_model = get_cifarcnn(num_of_classes)
target_model = get_cifarcnn(num_of_classes)
elif self.params['type'] == config.TYPE_MNIST:
from models.MnistNet import MnistNet
if self.params['binary_cls'] == True:
num_of_classes = 2
else:
num_of_classes = 10
local_model = MnistNet(name='Local',
created_time=self.params['current_time'], num_of_classes=num_of_classes)
target_model = MnistNet(name='Target',
created_time=self.params['current_time'], num_of_classes=num_of_classes)
local_model = local_model.to(device)
target_model = target_model.to(device)
if self.params['resumed_model']:
if torch.cuda.is_available():
loaded_params = torch.load(
f"{self.params['resumed_model_name']}")
else:
loaded_params = torch.load(
f"{self.params['resumed_model_name']}", map_location='cpu')
target_model.load_state_dict(loaded_params['state_dict'])
self.start_epoch = loaded_params['epoch']+1
self.params['lr'] = loaded_params.get('lr', self.params['lr'])
logger.info(f"Loaded parameters from saved model: LR is"
f" {self.params['lr']} and current epoch is {self.start_epoch}")
else:
self.start_epoch = 1
self.local_model = local_model
self.target_model = target_model
def build_classes_dict(self):
classes = {}
for ind, x in enumerate(self.train_dataset):
_, label = x
if label in classes:
classes[label].append(ind)
else:
classes[label] = [ind]
return classes
def sample_dirichlet_train_data(self, no_participants, alpha=0.9, lst_sample=2):
"""
Input: Number of participants and alpha (param for distribution)
Output: A list of indices denoting data in CIFAR training set.
Requires: classes, a preprocessed class-indice dictionary.
Sample Method: take a uniformly sampled 10-dimension vector as parameters for
dirichlet distribution to sample number of images in each class.
"""
classes = self.classes_dict
per_participant_list = defaultdict(list)
num_classes = len(classes.keys()) # for cifar: 10
image_nums = []
for n in range(num_classes):
image_num = []
random.shuffle(classes[n])
class_size = len(classes[n]) - no_participants*lst_sample
sampled_probabilities = class_size * np.random.dirichlet(
np.array(no_participants * [alpha]))
for user in range(no_participants):
no_imgs = int(round(sampled_probabilities[user]))+lst_sample
sampled_list = classes[n][:min(len(classes[n]), no_imgs)]
image_num.append(len(sampled_list))
per_participant_list[user].extend(sampled_list)
classes[n] = classes[n][min(len(classes[n]), no_imgs):]
image_nums.append(image_num)
print(n, image_nums[n])
return per_participant_list
def poison_test_dataset(self):
logger.info('get poison test loader')
# delete the test data with target label
test_classes = {}
for ind, x in enumerate(self.test_dataset):
_, label = x
if label in test_classes:
test_classes[label].append(ind)
else:
test_classes[label] = [ind]
range_no_id = list(range(0, len(self.test_dataset)))
for image_ind in test_classes[self.params['poison_label_swap']]:
if image_ind in range_no_id:
range_no_id.remove(image_ind)
poison_label_inds = test_classes[self.params['poison_label_swap']]
return torch.utils.data.DataLoader(self.test_dataset,
batch_size=self.params['batch_size'],
sampler=torch.utils.data.sampler.SubsetRandomSampler(
range_no_id)), \
torch.utils.data.DataLoader(self.test_dataset,
batch_size=self.params['batch_size'],
sampler=torch.utils.data.sampler.SubsetRandomSampler(
poison_label_inds))
def load_data(self):
logger.info('Loading data')
dataPath = "./data"
if self.params['type'] == config.TYPE_CIFAR:
# data load
transform_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)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
self.train_dataset = datasets.CIFAR10(dataPath, train=True, download=True,
transform=transform_train)
self.test_dataset = datasets.CIFAR10(
dataPath, train=False, transform=transform_test)
if self.params['binary_cls'] == True:
self.LABEL_LIST = [0, 2]
self.train_dataset = PartDataset(
self.train_dataset, self.LABEL_LIST)
self.test_dataset = PartDataset(
self.test_dataset, self.LABEL_LIST)
print("cifar load data done")
elif self.params['type'] == config.TYPE_MNIST:
self.train_dataset = datasets.MNIST(dataPath, train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
]))
self.test_dataset = datasets.MNIST(dataPath, train=False, transform=transforms.Compose([
transforms.ToTensor(),
]))
if self.params['binary_cls'] == True:
self.LABEL_LIST = [0, 1]
self.train_dataset = PartDataset(
self.train_dataset, self.LABEL_LIST)
self.test_dataset = PartDataset(
self.test_dataset, self.LABEL_LIST)
self.classes_dict = self.build_classes_dict()
if self.params['sampling_dirichlet']:
logger.info('Dirichlet')
# sample indices for participants using Dirichlet distribution
indices_per_participant = self.sample_dirichlet_train_data(
self.params['number_of_total_participants'],
alpha=self.params['dirichlet_alpha'])
train_loaders = [(pos, self.get_train(indices)) for pos, indices in
indices_per_participant.items()]
else:
# sample indices for participants that are equally
logger.info('iid')
all_range = list(range(len(self.train_dataset)))
random.shuffle(all_range)
train_loaders = [(pos, self.get_train_iid(all_range, pos))
for pos in range(self.params['number_of_total_participants'])]
self.train_data = train_loaders
self.test_data = self.get_test()
if self.params['is_poison'] == True:
self.test_data_poison, self.test_targetlabel_data = self.poison_test_dataset()
def get_train_iid(self, all_range, model_no):
"""
This method equally splits the dataset.
:param params:
:param all_range:
:param model_no:
:return:
"""
data_len = int(len(self.train_dataset) /
self.params['number_of_total_participants'])
sub_indices = all_range[model_no * data_len: (model_no + 1) * data_len]
if self.params['batch_size'] == -1:
logger.info('new batch size='+str(len(sub_indices)))
self.params['batch_size'] = len(sub_indices)
train_loader = torch.utils.data.DataLoader(self.train_dataset,
batch_size=self.params['batch_size'],
sampler=torch.utils.data.sampler.SubsetRandomSampler(
sub_indices))
return train_loader
def get_test(self):
test_loader = torch.utils.data.DataLoader(self.test_dataset,
batch_size=self.params['test_batch_size'],
shuffle=False)
return test_loader
def get_batch(self, train_data, bptt, evaluation=False):
data, target = bptt
data = data.to(device)
target = target.to(device)
if evaluation:
data.requires_grad_(False)
target.requires_grad_(False)
return data, target
def get_poison_batch(self, bptt, adversarial_index=0, evaluation=False, agent_name=-1):
# adversarial_index =1 backdoor
# adversarial_index =0 label-flipping
images, targets = bptt
poison_count = 0
new_images = copy.deepcopy(images)
new_targets = copy.deepcopy(targets)
for index in range(0, len(images)):
if evaluation: # poison all data when testing
new_targets[index] = self.params['poison_label_swap']
if adversarial_index == 1:
new_images[index] = self.add_pixel_pattern(images[index])
poison_count += 1
else: # poison part of data when training
if index < self.params['poisoning_per_batch']:
new_targets[index] = self.params['poison_label_swap']
if adversarial_index == 1:
new_images[index] = self.add_pixel_pattern(
images[index], agent_name)
poison_count += 1
new_images = new_images.to(device)
new_targets = new_targets.to(device).long()
if evaluation:
new_images.requires_grad_(False)
new_targets.requires_grad_(False)
return new_images, new_targets, poison_count
def add_pixel_pattern(self, ori_image, agent_name=-1):
image = ori_image
poison_patterns = self.params['poison_pattern']
if agent_name != -1:
poison_patterns = self.params[str(agent_name)+'_poison_pattern']
if self.params['type'] == config.TYPE_CIFAR:
for i in range(0, len(poison_patterns)):
pos = poison_patterns[i]
image[0][pos[0]][pos[1]] = 1
image[1][pos[0]][pos[1]] = 1
image[2][pos[0]][pos[1]] = 1
elif self.params['type'] == config.TYPE_MNIST:
for i in range(0, len(poison_patterns)):
pos = poison_patterns[i]
image[0][pos[0]][pos[1]] = 1
return image