-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_poison.py
executable file
·336 lines (279 loc) · 10.5 KB
/
main_poison.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
import os
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models import resnet18, resnet50
from pytorch_lightning import seed_everything
from solo.args.setup import parse_args_linear
from solo.methods import METHODS
from solo.methods.base import BaseMethod
from solo.utils.backbones import (
swin_base,
swin_large,
swin_small,
swin_tiny,
vit_base,
vit_large,
vit_small,
vit_tiny,
)
from solo.utils.poison_dataloader import prepare_data_for_inject_poison
def get_backbone(args):
assert args.backbone in BaseMethod._SUPPORTED_BACKBONES
backbone_model = {
"resnet18": resnet18,
"resnet50": resnet50,
"vit_tiny": vit_tiny,
"vit_small": vit_small,
"vit_base": vit_base,
"vit_large": vit_large,
"swin_tiny": swin_tiny,
"swin_small": swin_small,
"swin_base": swin_base,
"swin_large": swin_large,
}[args.backbone]
# initialize backbone
kwargs = args.backbone_args
cifar = kwargs.pop("cifar", False)
# swin specific
if "swin" in args.backbone and cifar:
kwargs["window_size"] = 4
backbone = backbone_model(**kwargs)
if "resnet" in args.backbone:
# remove fc layer
# backbone.fc = nn.Linear(backbone.inplanes, args.num_classes)
backbone.fc = nn.Identity()
if cifar:
backbone.conv1 = nn.Conv2d(
3, 64, kernel_size=3, stride=1, padding=2, bias=False)
backbone.maxpool = nn.Identity()
assert (
args.pretrained_feature_extractor.endswith(".ckpt")
or args.pretrained_feature_extractor.endswith(".pth")
or args.pretrained_feature_extractor.endswith(".pt")
)
ckpt_path = args.pretrained_feature_extractor
state = torch.load(ckpt_path)["state_dict"]
for k in list(state.keys()):
if "encoder" in k:
raise Exception(
"You are using an older checkpoint."
"Either use a new one, or convert it by replacing"
"all 'encoder' occurances in state_dict with 'backbone'"
)
if "backbone" in k:
state[k.replace("backbone.", "")] = state[k]
if args.load_linear:
if "classifier" in k:
state[k.replace("classifier.", "fc.")] = state[k]
del state[k]
# prepare model
backbone.load_state_dict(state, strict=False)
backbone = backbone.cuda()
backbone.eval()
return backbone
def inference(model, loader, device=torch.device('cuda')):
feature_vector = []
labels_vector = []
for step, (x, y) in tqdm(enumerate(loader)):
x = x.cuda()
# get encoding
with torch.no_grad():
h = model(x)
if type(h) is tuple:
h = h[-1]
if type(h) is dict:
h = h['feats']
h = model.projector(h)
feature_vector.append(h.data.to(device))
labels_vector.append(y.to(device))
feature_vector = torch.cat(feature_vector)
labels_vector = torch.cat(labels_vector)
return feature_vector, labels_vector
def get_near_index(anchor_feature, train_features, num_poisons):
vals, indices = torch.topk(
train_features @ anchor_feature, k=num_poisons, dim=0)
return indices
# contrastive select
def select_con(train_features, num_poisons):
def get_anchor_con(train_features, num_poisons):
similarity = train_features @ train_features.T
w = torch.cat((torch.ones((num_poisons)),
-torch.ones((num_poisons))), dim=0)
top_sim = torch.topk(similarity, 2 * num_poisons, dim=1)[0]
mean_top_sim = torch.matmul(top_sim, w)
idx = torch.argmax(mean_top_sim)
return idx
anchor_idx = get_anchor_con(
train_features, num_poisons)
anchor_feature = train_features[anchor_idx]
poisoning_index = get_near_index(
anchor_feature, train_features, num_poisons)
poisoning_index = poisoning_index.cpu()
return poisoning_index
# contrastive select only positive
def select_conp(train_features, num_poisons):
def get_anchor_conp(train_features, num_poisons):
similarity = train_features @ train_features.T
mean_top_sim = torch.topk(similarity, num_poisons, dim=1)[
0].mean(dim=1)
idx = torch.argmax(mean_top_sim)
return idx
anchor_idx = get_anchor_conp(
train_features, num_poisons)
anchor_feature = train_features[anchor_idx]
poisoning_index = get_near_index(
anchor_feature, train_features, num_poisons)
poisoning_index = poisoning_index.cpu()
return poisoning_index
# contrastive select only negative
def select_conn(train_features, num_poisons):
def get_anchor_conn(train_features, num_poisons):
similarity = train_features @ train_features.T
w = torch.cat((torch.zeros((num_poisons)),
-torch.ones((num_poisons))), dim=0)
top_sim = torch.topk(similarity, 2 * num_poisons, dim=1)[0]
mean_top_sim = torch.matmul(top_sim, w)
idx = torch.argmax(mean_top_sim)
return idx
anchor_idx = get_anchor_conn(
train_features, num_poisons)
anchor_feature = train_features[anchor_idx]
poisoning_index = get_near_index(
anchor_feature, train_features, num_poisons)
poisoning_index = poisoning_index.cpu()
return poisoning_index
# K-means select
def select_kmean(train_features, num_poisons, n_clusters):
from sklearn.cluster import MiniBatchKMeans
kmeans = MiniBatchKMeans(n_clusters=n_clusters, n_init='auto')
preds = torch.from_numpy(kmeans.fit_predict(train_features))
cluster_labels, cluster_counts = preds.unique(return_counts=True)
min_counts_over_bar = min(
cluster_counts[cluster_counts >= num_poisons])
chosen_pseudo_label = cluster_counts.tolist().index(min_counts_over_bar)
print("cluster:", cluster_counts[chosen_pseudo_label])
poisoning_index = (preds == chosen_pseudo_label).nonzero().squeeze().cpu()
poisoning_index = poisoning_index[:num_poisons]
return poisoning_index
# select with label
def select_target(train_labels, num_poisons, target_class=None):
assert target_class is not None
poisoning_index = torch.arange(len(train_labels))[
train_labels == target_class]
shuffle_idx = torch.randperm(len(poisoning_index))
poisoning_index = poisoning_index[shuffle_idx]
poisoning_index = poisoning_index[:num_poisons].cpu()
return poisoning_index
# random select
def select_random(length, num_poisons):
import random
numbers = list(range(length))
random.shuffle(numbers)
poisoning_index = numbers[:num_poisons]
poisoning_index = torch.tensor(poisoning_index)
return poisoning_index
def main():
args = parse_args_linear()
seed_everything(args.random_seed)
# load backbone
if args.poison_method != 'clb' and args.poison_method != 'random':
backbone = get_backbone(args)
else:
backbone = None
args.pretrain_method = None
# load dataset
train_loader, train_dataset = prepare_data_for_inject_poison(
args.dataset,
data_dir=args.data_dir,
train_dir=args.train_dir,
batch_size=args.batch_size,
num_workers=args.num_workers,
)
data_size = len(train_dataset)
num_poisons = int(args.poison_rate * data_size / args.num_classes)
train_labels = torch.tensor(train_dataset.targets)
# get feature from backbone
feature_path = os.path.join(args.data_dir, "feature")
os.makedirs(feature_path, exist_ok=True)
feature_path = os.path.join(
feature_path, str(args.pretrain_method) + '.pt')
if backbone != None:
if 0 and os.path.isfile(feature_path):
print('loading..')
train_features, train_labels = torch.load(feature_path)
else:
print('computing..')
train_features, train_labels = inference(backbone, train_loader)
train_features, train_labels = train_features.cpu(), train_labels.cpu()
torch.save([train_features, train_labels], feature_path)
train_features = F.normalize(train_features, dim=1)
else:
train_features = None
# select poison subset
n_clusters = {
"cifar10": 10,
"cifar100": 100,
"imagenet": 1000,
"imagenet100": 100,
}[args.dataset]
target = args.target_class
poison_method = args.poison_method
dataset_size = len(train_features)
if poison_method == 'con':
poisoning_index = select_con(train_features, num_poisons)
elif poison_method == 'conp':
poisoning_index = select_conp(train_features, num_poisons)
elif poison_method == 'conn':
poisoning_index = select_conn(train_features, num_poisons)
elif poison_method == 'kmean':
poisoning_index = select_kmean(train_features, num_poisons, n_clusters)
elif poison_method == 'target':
poisoning_index = select_target(train_labels, num_poisons, target)
elif poison_method == 'rand':
poisoning_index = select_random(dataset_size, num_poisons)
else:
assert 0, f"poison_method {poison_method} is not supported"
# calc TPR
poisoning_labels = np.array(train_labels)[poisoning_index]
print(poisoning_labels)
anchor_label = np.bincount(poisoning_labels).argmax()
print(anchor_label)
tpr = (poisoning_labels == anchor_label).astype(float).mean()
print('class: %d , tpr: %.4f' % (anchor_label, tpr))
# save poison information file
poison_data_name = "%s-%s-%s-%s-%d-%.3f-%d-%.4f" % (
args.dataset,
args.backbone,
args.poison_method,
args.pretrain_method,
args.random_seed,
args.poison_rate,
anchor_label,
tpr,
)
poison_data = {
'dataset': args.dataset,
'backbone': args.backbone,
'poison_method': args.poison_method,
'pretrain_method': args.pretrain_method,
'rate': args.poison_rate,
'targets': train_labels,
'poisoning_index': poisoning_index,
'data_size': data_size,
'anchor_label': anchor_label,
'tpr': tpr,
'random_seed': args.random_seed,
'name': poison_data_name,
'args': args,
}
save_path = os.path.join(args.data_dir, "poison")
os.makedirs(save_path, exist_ok=True)
file_name = os.path.join(save_path, poison_data_name + '.pt')
print('saving to %s' % file_name)
poison_data['args'] = args
torch.save(poison_data, file_name)
if __name__ == "__main__":
main()