/
fuc_utils.py
237 lines (226 loc) · 11.1 KB
/
fuc_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
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
import json
from torch.multiprocessing import Pool, Process, set_start_method
import torch
import os
import pickle
import argparse
import time
import random
import pandas as pd
def classify(model, inp):
'''
classify clips.
return the top prob and class label.
'''
if inp.shape[0] != 1:
inp = torch.unsqueeze(inp, 0)
values, indices = torch.sort(-torch.nn.functional.softmax(model(inp)), dim=1)
confidence_prob, pre_label = -float(values[:,0]), int(indices[:,0])
return confidence_prob, pre_label
def get_attacked_targeted_label(model_name, data_name, attack_id):
df = pd.read_csv('./attacked_samples-{}-{}.csv'.format(model_name, data_name))
targeted_label = df[df['attack_id'] == attack_id]['targeted_label'].values.tolist()[0]
return targeted_label
def get_attacked_samples(model, test_data, nums_attack, model_name, data_name):
'''
Generate idxs of test dataset for attacking.
'''
if os.path.exists('./attacked_samples-{}-{}.pkl'.format(model_name, data_name)):
with open('./attacked_samples-{}-{}.pkl'.format(model_name, data_name), 'rb') as ipt:
attacked_ids = pickle.load(ipt)
else:
random.seed(1024)
idxs = random.sample(range(len(test_data)), len(test_data))
attacked_ids = []
for i in idxs:
clips, label = test_data[i]
video_id = label[0]
label = int(label[1])
_, pre = classify(model, clips)
if pre != label:
pass
else:
attacked_ids.append(i)
if len(attacked_ids) == nums_attack:
break
with open('./attacked_samples-{}-{}.pkl'.format(model_name, data_name), 'wb') as opt:
pickle.dump(attacked_ids, opt)
return attacked_ids
def get_idx_labels(model, train_data, model_name, data_name):
'''
keys : ids in the training dataset.
values: the label and the prediction of the model according to the ids.
'''
if os.path.exists('./train_dataset_ids_labels-{}-{}.pkl'.format(model_name, data_name)):
with open('./train_dataset_ids_labels-{}-{}.pkl'.format(model_name, data_name), 'rb') as ipt:
idx_to_labels = pickle.load(ipt)
else:
idx_to_labels = {}
for i in range(len(train_data)):
clips,label = train_data[i]
_, pre = classify(model, clips)
idx_to_labels[i] = [label, pre]
with open('./train_dataset_ids_labels-{}-{}.pkl'.format(model_name, data_name), 'wb') as opt:
pickle.dump(idx_to_labels, opt)
return idx_to_labels
def get_initialize_samples(model, train_data, attack_id, num_samples, targeted, idx_to_labels):
if targeted:
ids = []
for i in range(len(train_data)):
label, pre = idx_to_labels[i]
if pre == targeted:
ids.append(i)
random.seed(attack_id)
try:
init_samples = sorted(random.sample(ids, num_samples))
except:
init_samples = ids
return init_samples
else:
random.seed(attack_id)
init_samples = sorted(random.sample(range(len(train_data)), num_samples))
return init_samples
def untargetted_attack_one(model, train_data, attack_idx, x0, y0, init_samples, video_id, args):
'''
Attack one example.
'''
from untargeted_attack_utils import Attack_base
output_path = args.output_path
model_name = args.model_name
del_frame, bound, bound_threshold, salient_region, spatial_mode, spatial_ratio, targeted, dataset_name = args.del_frame, args.bound, args.bound_threshold, args.salient_region, args.spatial_mode, args.spatial_ratio, args.targeted, args.dataset_name
class_names = train_data.class_names
label = y0
one_class = Attack_base(model, train_data, attack_idx, x0, y0, output_path, model_name, dataset_name, init_samples, del_frame=del_frame, bound=bound, bound_threshold=bound_threshold, salient_region=salient_region, spatial_mode=spatial_mode, spatial_ratio=spatial_ratio)
start = time.time()
print ('Do attack_one {}'.format(attack_idx))
one_class.attack()
print ('Attack over {}'.format(attack_idx))
end = time.time()
print ('Success or not', one_class.success)
if one_class.success:
print ('Saving in path: {}.'.format(os.path.join(args.output_path, 'ori_adv_video_idx_{}'.format(attack_idx))))
with open(os.path.join(args.output_path, 'ori_adv_video_idx_{}'.format(attack_idx)), 'wb') as opt_write:
pickle.dump([one_class.image_ori, one_class.adv_image], opt_write)
try:
with open(os.path.join(args.output_path, 'MASK_video_idx_{}'.format(attack_idx)), 'wb') as opt_write:
pickle.dump(one_class.best_MASK, opt_write)
except AttributeError as e:
pass
with open(os.path.join(args.output_path, 'iteration_attack.log'), 'a') as the_file:
json_ = json.dumps({
'idx': attack_idx,
'video_id':video_id,
'targeted': targeted,
'original_label': class_names[label],
'original_confidence': one_class.ori_confi,
'adversarial_label': class_names[one_class.adv_indice],
'adversarial_confidence': one_class.adv_confi,
'query_counts': one_class.query_counts,
'opt_counts': one_class.opt_counts,
'all_counts': one_class.query_counts + one_class.opt_counts,
'P': '%.4f'%one_class.P.item(),
'success': one_class.success,
'time(mins)': (end-start)/60.0,
'g2': one_class.g_theta.item(),
'frame_indices': one_class.best_frame_indices,
'additional': 'attack:Yes, success: True'
})
the_file.write(json_)
else:
with open(os.path.join(args.output_path, 'iteration_attack.log'), 'a') as the_file:
json_ = json.dumps({
'idx': attack_idx,
'video_id':video_id,
'targeted': targeted,
'original_label': class_names[label],
'original_confidence': one_class.ori_confi,
'adversarial_label': 0,
'adversarial_confidence': 0,
'query_counts': one_class.query_counts,
'opt_counts': one_class.opt_counts,
'all_counts': one_class.query_counts + one_class.opt_counts,
'P': 0,
'success': False,
'time(mins)': (end-start)/60.0,
'g2': one_class.g_theta.item(),
'frame_indices': one_class.best_frame_indices,
'additional': 'attack: Yes, success: False'
})
the_file.write(json_)
def targeted_attack_one(model, train_data, attack_idx, x0, y0, init_samples, video_id, args):
'''
Attack one example.
'''
from targeted_attack_utils import targeted_Attack_base
output_path = args.output_path
model_name = args.model_name
del_frame, bound, bound_threshold, salient_region, spatial_mode, spatial_ratio, targeted, dataset_name = args.del_frame, args.bound, args.bound_threshold, args.salient_region, args.spatial_mode, args.spatial_ratio, args.targeted, args.dataset_name
class_names = train_data.class_names
label = y0
one_class = targeted_Attack_base(model, train_data, attack_idx, x0, y0, targeted, output_path, model_name, dataset_name, init_samples, del_frame=del_frame, bound=bound, bound_threshold=bound_threshold, salient_region=salient_region, spatial_mode=spatial_mode, spatial_ratio=spatial_ratio)
start = time.time()
print ('Do attack_one {}'.format(attack_idx))
one_class.attack()
print ('Attack over {}'.format(attack_idx))
end = time.time()
print ('Success or not', one_class.success)
if one_class.success:
print ('Saving in path: {}.'.format(os.path.join(args.output_path, 'ori_adv_video_idx_{}'.format(attack_idx))))
with open(os.path.join(args.output_path, 'ori_adv_video_idx_{}'.format(attack_idx)), 'wb') as opt_write:
pickle.dump([one_class.image_ori, one_class.adv_image], opt_write)
try:
with open(os.path.join(args.output_path, 'MASK_video_idx_{}'.format(attack_idx)), 'wb') as opt_write:
pickle.dump(one_class.best_MASK, opt_write)
except AttributeError as e:
pass
with open(os.path.join(args.output_path, 'iteration_attack.log'), 'a') as the_file:
json_ = json.dumps({
'idx': attack_idx,
'video_id':video_id,
'targeted': targeted,
'true_label': label,
'original_label': class_names[label],
'original_confidence': one_class.ori_confi,
'adversarial_label': class_names[one_class.adv_indice],
'adversarial_confidence': one_class.adv_confi,
'query_counts': one_class.query_counts,
'opt_counts': one_class.opt_counts,
'all_counts': one_class.query_counts + one_class.opt_counts,
'P': '%.4f'%one_class.P.item(),
'success': one_class.success,
'time(mins)': (end-start)/60.0,
'g2': one_class.g_theta.item(),
'frame_indices': one_class.best_frame_indices,
'additional': 'attack:Yes, success: True'
})
the_file.write(json_)
else:
with open(os.path.join(args.output_path, 'iteration_attack.log'), 'a') as the_file:
json_ = json.dumps({
'idx': attack_idx,
'video_id':video_id,
'targeted': targeted,
'true_label': label,
'original_label': class_names[label],
'original_confidence': one_class.ori_confi,
'adversarial_label': 0,
'adversarial_confidence': 0,
'query_counts': one_class.query_counts,
'opt_counts': one_class.opt_counts,
'all_counts': one_class.query_counts + one_class.opt_counts,
'P': 0,
'success': False,
'time(mins)': (end-start)/60.0,
'g2': one_class.g_theta,
'frame_indices': one_class.best_frame_indices,
'additional': 'attack: Yes, success: False'
})
the_file.write(json_)
def save_parameters(args):
def add_parameters(params, **kwargs):
params.update(kwargs)
params = {}
add_parameters(params, data=args.dataset_name, model=args.model_name, nums_attack=args.nums_attack, path=args.path, n_process=args.n_process, del_frame=args.del_frame, bound=args.bound, salient_region=args.salient_region, bound_threshold=args.bound_threshold, spatial_mode=args.spatial_mode, spatial_ratio=args.spatial_ratio, output_path=args.output_path, target=args.target)
json_ = json.dumps(params)
with open(os.path.join(args.output_path, 'parameters.info'), 'w') as opt:
opt.write(json_)