-
Notifications
You must be signed in to change notification settings - Fork 65
/
image_helper.py
240 lines (200 loc) · 9.69 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
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
from models.resnet import ResNet18
from models.word_model import RNNModel
from utils.text_load import *
from utils.utils import SubsetSampler
logger = logging.getLogger("logger")
POISONED_PARTICIPANT_POS = 0
class ImageHelper(Helper):
def poison(self):
return
def create_model(self):
local_model = ResNet18(name='Local',
created_time=self.params['current_time'])
local_model.cuda()
target_model = ResNet18(name='Target',
created_time=self.params['current_time'])
target_model.cuda()
if self.params['resumed_model']:
loaded_params = torch.load(f"saved_models/{self.params['resumed_model']}")
target_model.load_state_dict(loaded_params['state_dict'])
self.start_epoch = loaded_params['epoch']
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 sample_dirichlet_train_data(self, no_participants, alpha=0.9):
"""
Input: Number of participants and alpha (param for distribution)
Output: A list of indices denoting data in CIFAR training set.
Requires: cifar_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.
"""
cifar_classes = {}
for ind, x in enumerate(self.train_dataset):
_, label = x
if ind in self.params['poison_images'] or ind in self.params['poison_images_test']:
continue
if label in cifar_classes:
cifar_classes[label].append(ind)
else:
cifar_classes[label] = [ind]
class_size = len(cifar_classes[0])
per_participant_list = defaultdict(list)
no_classes = len(cifar_classes.keys())
for n in range(no_classes):
random.shuffle(cifar_classes[n])
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]))
sampled_list = cifar_classes[n][:min(len(cifar_classes[n]), no_imgs)]
per_participant_list[user].extend(sampled_list)
cifar_classes[n] = cifar_classes[n][min(len(cifar_classes[n]), no_imgs):]
return per_participant_list
def poison_dataset(self):
#
# return [(self.train_dataset[self.params['poison_image_id']][0],
# torch.IntTensor(self.params['poison_label_swap']))]
cifar_classes = {}
for ind, x in enumerate(self.train_dataset):
_, label = x
if ind in self.params['poison_images'] or ind in self.params['poison_images_test']:
continue
if label in cifar_classes:
cifar_classes[label].append(ind)
else:
cifar_classes[label] = [ind]
indices = list()
# create array that starts with poisoned images
#create candidates:
# range_no_id = cifar_classes[1]
# range_no_id.extend(cifar_classes[1])
range_no_id = list(range(50000))
for image in self.params['poison_images'] + self.params['poison_images_test']:
if image in range_no_id:
range_no_id.remove(image)
# add random images to other parts of the batch
for batches in range(0, self.params['size_of_secret_dataset']):
range_iter = random.sample(range_no_id,
self.params['batch_size'])
# range_iter[0] = self.params['poison_images'][0]
indices.extend(range_iter)
# range_iter = random.sample(range_no_id,
# self.params['batch_size']
# -len(self.params['poison_images'])*self.params['poisoning_per_batch'])
# for i in range(0, self.params['poisoning_per_batch']):
# indices.extend(self.params['poison_images'])
# indices.extend(range_iter)
return torch.utils.data.DataLoader(self.train_dataset,
batch_size=self.params['batch_size'],
sampler=torch.utils.data.sampler.SubsetRandomSampler(indices))
def poison_test_dataset(self):
#
# return [(self.train_dataset[self.params['poison_image_id']][0],
# torch.IntTensor(self.params['poison_label_swap']))]
return torch.utils.data.DataLoader(self.train_dataset,
batch_size=self.params['batch_size'],
sampler=torch.utils.data.sampler.SubsetRandomSampler(
range(1000)
))
def load_data(self):
logger.info('Loading data')
### 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('./data', train=True, download=True,
transform=transform_train)
self.test_dataset = datasets.CIFAR10('./data', train=False, transform=transform_test)
if self.params['sampling_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
# splitted to 500 images per participant
all_range = list(range(len(self.train_dataset)))
random.shuffle(all_range)
train_loaders = [(pos, self.get_train_old(all_range, pos))
for pos in range(self.params['number_of_total_participants'])]
self.train_data = train_loaders
self.test_data = self.get_test()
self.poisoned_data_for_train = self.poison_dataset()
self.test_data_poison = self.poison_test_dataset()
# self.params['adversary_list'] = [POISONED_PARTICIPANT_POS] + \
# random.sample(range(len(train_loaders)),
# self.params['number_of_adversaries'] - 1)
# logger.info(f"Poisoned following participants: {self.params['adversary_list']}")
def get_train(self, indices):
"""
This method is used along with Dirichlet distribution
:param params:
:param indices:
:return:
"""
train_loader = torch.utils.data.DataLoader(self.train_dataset,
batch_size=self.params['batch_size'],
sampler=torch.utils.data.sampler.SubsetRandomSampler(
indices))
return train_loader
def get_train_old(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]
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_secret_loader(self):
"""
For poisoning we can use a larger data set. I don't sample randomly, though.
"""
indices = list(range(len(self.train_dataset)))
random.shuffle(indices)
shuffled_indices = indices[:self.params['size_of_secret_dataset']]
train_loader = torch.utils.data.DataLoader(self.train_dataset,
batch_size=self.params['batch_size'],
sampler=SubsetSampler(shuffled_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=True)
return test_loader
def get_batch(self, train_data, bptt, evaluation=False):
data, target = bptt
data = data.cuda()
target = target.cuda()
if evaluation:
data.requires_grad_(False)
target.requires_grad_(False)
return data, target