-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_conf_dist_wrt_T.py
295 lines (238 loc) · 12 KB
/
generate_conf_dist_wrt_T.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
# import os
import sys
import time
import numpy as np
import pandas as pd
from datetime import datetime
# import torch.nn.functional as F
# import tools.aux_funcs as af
from tools.logger import Logger
from tools.logistics import *
# from round_features_sdn import get_trigger_type_aux_value
# from notebooks.methods import encode_architecture
"""
Available keys for npz files when num_ics = 4:
'num_ics',
'logit_ic_0',
'logit_ic_1',
'logit_ic_2',
'logit_ic_3', # if num_ics=3, then this key won't exist
'logit_net_out',
'conf_mat_ic0',
'conf_mat_ic1',
'conf_mat_ic2',
'conf_mat_ic3', # if num_ics=3, then this key won't exist
'conf_dist_original'
"""
# this method can also be found in notebooks/methods
def encode_architecture(model_architecture):
arch_codes = ['densenet', 'googlenet', 'inception', 'mobilenet', 'resnet', 'shufflenet', 'squeezenet', 'vgg']
for index, arch in enumerate(arch_codes):
if arch in model_architecture:
return index
return None
# this method can also be found in notebooks/methods
def encode_backdoor(trigger_type_aux):
code = None
if trigger_type_aux == 'none':
code = 0
elif 'polygon' in trigger_type_aux:
code = 1
elif 'gotham' in trigger_type_aux:
code = 2
elif 'kelvin' in trigger_type_aux:
code = 3
elif 'lomo' in trigger_type_aux:
code = 4
elif 'nashville' in trigger_type_aux:
code = 5
elif 'toaster' in trigger_type_aux:
code = 6
return code
# this method can also be found in round_features_sdn
def get_trigger_type_aux_value(triggers_0_type, triggers_0_instagram_filter_type, triggers_1_type, triggers_1_instagram_filter_type):
triggers_0_type = triggers_0_type.lower()
triggers_1_type = triggers_1_type.lower()
triggers_0_instagram_filter_type = triggers_0_instagram_filter_type.lower().replace('filter', '').replace('xform', '')
triggers_1_instagram_filter_type = triggers_1_instagram_filter_type.lower().replace('filter', '').replace('xform', '')
if triggers_0_type == 'instagram':
backd_0_str = f'instagram-{triggers_0_instagram_filter_type}'
backd_0_code = encode_backdoor(triggers_0_instagram_filter_type)
else:
backd_0_str = triggers_0_type
backd_0_code = encode_backdoor(triggers_0_type)
if triggers_1_type == 'instagram':
backd_1_str = f'instagram-{triggers_1_instagram_filter_type}'
backd_1_code = encode_backdoor(triggers_1_instagram_filter_type)
else:
backd_1_str = triggers_1_type
backd_1_code = encode_backdoor(triggers_1_type)
return f'{backd_0_str}_{backd_1_str}', backd_0_code, backd_1_code
def main():
T = 0.0
device = af.get_pytorch_device()
list_limits = {
'windows10': (0, 1007),
'opensub03.umiacs.umd.edu': (0, 1007),
'openlab08.umiacs.umd.edu': (0, 1007),
# 'openlab30.umiacs.umd.edu': (0, 249),
# 'openlab31.umiacs.umd.edu': (250, 499),
# 'openlab32.umiacs.umd.edu': (500, 749),
# 'openlab33.umiacs.umd.edu': (750, 1007),
}
if len(sys.argv) != 3:
lim_left, lim_right = list_limits[socket.gethostname()]
else:
lim_left, lim_right = int(sys.argv[1]), int(sys.argv[2])
print(f'lim_left={lim_left}, lim_right={lim_right}')
experiment_name = f'fc_synthetic_polygon-all-gray_filters_T={T:.1f}_{lim_left}-{lim_right}'
path_root_project = get_project_root_path()
path_root = os.path.join(path_root_project, 'TrojAI-data', 'round4-train-dataset')
path_report_conf_dist = os.path.join(path_root, 'ics_fc', f'{os.path.basename(path_root)}_{experiment_name}.csv')
synthetic_stats_root = os.path.join(path_root, 'synthetic_stats')
metadata_root = os.path.join(path_root, 'METADATA.csv')
metadata = pd.read_csv(metadata_root)
path_logger = os.path.join(path_root, 'ics_fc', f'{os.path.basename(path_root)}_{experiment_name}.log')
Logger.open(path_logger)
# continue training from where we left off last run
if os.path.isfile(path_report_conf_dist):
df_report_conf_dist = pd.read_csv(path_report_conf_dist)
n_report_conf_dist = len(df_report_conf_dist)
last_model_name_in_report_conf_dist = df_report_conf_dist.iloc[-1]['model_name']
print(f'Continue training (last id is {last_model_name_in_report_conf_dist})')
else:
print('Training from scratch')
last_model_name_in_report_conf_dist = None
n_report_conf_dist = 0
df_report_conf_dist = pd.DataFrame(columns=[
# preliminary info about the model
'model_name', 'model_architecture', 'architecture_code', 'backdoor_code_0', 'backdoor_code_1', 'model_label', 'backdoor_string',
'polygon_mean_diff', 'polygon_std_diff',
'gotham_mean_diff', 'gotham_std_diff',
'kelvin_mean_diff', 'kelvin_std_diff',
'lomo_mean_diff', 'lomo_std_diff',
'nashville_mean_diff', 'nashville_std_diff',
'toaster_mean_diff', 'toaster_std_diff',
'clean_mean', 'clean_std',
'polygon_mean', 'polygon_std',
'gotham_mean', 'gotham_std',
'kelvin_mean', 'kelvin_std',
'lomo_mean', 'lomo_std',
'nashville_mean', 'nashville_std',
'toaster_mean', 'toaster_std',
# other data
'num_classes',
'counts'
])
for index, row in metadata.iterrows():
start_time = datetime.now()
model_name = row['model_name']
model_id = int(model_name[3:])
if lim_left <= model_id <= lim_right and ((last_model_name_in_report_conf_dist is None) or (last_model_name_in_report_conf_dist is not None and model_name > last_model_name_in_report_conf_dist)):
# wait for the round_features_sdn process to create the npz files for each model
lookup_folder = os.path.join(synthetic_stats_root, model_name)
while True:
if not os.path.isdir(lookup_folder):
Logger.log(f'Waiting for folder synthetic_stats/{model_name} to be created...')
time.sleep(60)
continue
files_count = len(os.listdir(lookup_folder))
if files_count == 7:
time.sleep(10)
break
Logger.log(f'Waiting for folder synthetic_stats/{model_name} to have 7 files...')
time.sleep(60)
backd_str, backd_0_code, backd_1_code = get_trigger_type_aux_value(row['triggers_0_type'], row['triggers_0_instagram_filter_type'], row['triggers_1_type'], row['triggers_1_instagram_filter_type'])
model_label = 'backdoor' if row['poisoned'] else 'clean'
model_architecture = row['model_architecture']
num_classes = row['number_classes']
architecture_code = encode_architecture(model_architecture)
counts = {}
conf_dist_all = {}
conf_dist_T = {}
for trigger_name in ['clean', 'polygon_all', 'gotham', 'kelvin', 'lomo', 'nashville', 'toaster']:
stats = np.load(os.path.join(synthetic_stats_root, model_name, f'{model_name}_{trigger_name}.npz'))
num_ics = stats['num_ics'][0]
confusion_all, confusion_T = [], []
counts[trigger_name] = {ic: 0 for ic in range(num_ics+1)}
for index_image in range(1000): # we have 1000 synthetic images
score_all, score_T, count_ics_T = 0.0, 0.0, 0
logit_net_out = torch.tensor(stats['logit_net_out'][index_image], device=device).unsqueeze(0)
softmax_net_out = F.softmax(logit_net_out)
for index_ic in range(num_ics):
logit_ic = torch.tensor(stats[f'logit_ic_{index_ic}'][index_image], device=device).unsqueeze(0)
softmax_ic = F.softmax(logit_ic)
dist = F.pairwise_distance(softmax_ic, softmax_net_out, p=1).item()
max_confidence = softmax_ic.max(1)[0].item()
score_all += dist
if max_confidence > T:
score_T += dist
count_ics_T += 1
counts[trigger_name][count_ics_T] += 1
confusion_all.append(score_all)
confusion_T.append(score_T)
# print(f'{trigger_name}\t#{index_image}\tall:{score_all:.3f}\tT:{score_T:.3f}\tcount:{count_ics_T} of {num_ics}')
conf_dist_all[trigger_name] = np.array(confusion_all)
conf_dist_T[trigger_name] = np.array(confusion_T)
# compute mean and stds for confusion distributions
clean_mean = np.mean(conf_dist_T['clean'])
clean_std = np.std(conf_dist_T['clean'])
polygon_mean = np.mean(conf_dist_T['polygon_all'])
polygon_std = np.std(conf_dist_T['polygon_all'])
gotham_mean = np.mean(conf_dist_T['gotham'])
gotham_std = np.std(conf_dist_T['gotham'])
kelvin_mean = np.mean(conf_dist_T['kelvin'])
kelvin_std = np.std(conf_dist_T['kelvin'])
lomo_mean = np.mean(conf_dist_T['lomo'])
lomo_std = np.std(conf_dist_T['lomo'])
nashville_mean = np.mean(conf_dist_T['nashville'])
nashville_std = np.std(conf_dist_T['nashville'])
toaster_mean = np.mean(conf_dist_T['toaster'])
toaster_std = np.std(conf_dist_T['toaster'])
############ compute differences for mean and stds between backdoored and clean
polygon_mean_diff = polygon_mean - clean_mean
polygon_std_diff = polygon_std - clean_std
gotham_mean_diff = gotham_mean - clean_mean
gotham_std_diff = gotham_std - clean_std
kelvin_mean_diff = kelvin_mean - clean_mean
kelvin_std_diff = kelvin_std - clean_std
lomo_mean_diff = lomo_mean - clean_mean
lomo_std_diff = lomo_std - clean_std
nashville_mean_diff = nashville_mean - clean_mean
nashville_std_diff = nashville_std - clean_std
toaster_mean_diff = toaster_mean - clean_mean
toaster_std_diff = toaster_std - clean_std
df_report_conf_dist.loc[n_report_conf_dist] = [
# preliminary info about the model
model_name, model_architecture, architecture_code, backd_0_code, backd_1_code, model_label, backd_str,
polygon_mean_diff, polygon_std_diff,
gotham_mean_diff, gotham_std_diff,
kelvin_mean_diff, kelvin_std_diff,
lomo_mean_diff, lomo_std_diff,
nashville_mean_diff, nashville_std_diff,
toaster_mean_diff, toaster_std_diff,
## place effective metrics from confusion distribution
clean_mean, clean_std,
polygon_mean, polygon_std,
gotham_mean, gotham_std,
kelvin_mean, kelvin_std,
lomo_mean, lomo_std,
nashville_mean, nashville_std,
toaster_mean, toaster_std,
# other data
num_classes,
str(counts)
]
n_report_conf_dist += 1
df_report_conf_dist.to_csv(path_report_conf_dist, index=False)
end_time = datetime.now()
del conf_dist_T
torch.cuda.empty_cache()
Logger.log(f'model {model_name} took {end_time - start_time}')
# for trigger_name in ['clean', 'polygon_all', 'gotham', 'kelvin', 'lomo', 'nashville', 'toaster']:
# mean_all, std_all = np.mean(conf_dist_all[trigger_name]), np.std(conf_dist_all[trigger_name])
# mean_T, std_T = np.mean(conf_dist_T[trigger_name]), np.std(conf_dist_T[trigger_name])
# print(f'{trigger_name} all={mean_all:.4f},{std_all:.4f}, T={mean_T:.4f},{std_T:.4f}')
# print(counts)
if __name__ == '__main__':
main()