-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_from_generated_mobnet.py
More file actions
596 lines (544 loc) · 20.8 KB
/
Copy pathevaluate_from_generated_mobnet.py
File metadata and controls
596 lines (544 loc) · 20.8 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import torch
import cv2
from pathlib import Path
from loguru import logger
import pandas as pd
import argparse
import numpy as np
import tqdm
import sys
from advisg.utils.confusion_matrix import get_confusion_matrix
class DataSet(torch.utils.data.Dataset):
def __init__(
self,
input_dir: Path,
labels: list[str],
data_split: str,
adversarial_type: str,
max_samples: int = -1000,
):
self.input_dir = input_dir
self.labels = labels
self.data_split = data_split
self.adversarial_type = adversarial_type
self.npz_files = list(input_dir.rglob(f"{adversarial_type}/{data_split}/*.npz"))
self.max_samples = max_samples
if not self.npz_files:
raise FileNotFoundError(
f"No .npz files found in {input_dir / adversarial_type / data_split}"
)
def __len__(self):
return (
len(self.npz_files)
if self.max_samples < 0
else min(len(self.npz_files), self.max_samples)
)
def __getitem__(self, idx):
selected_file = self.npz_files[idx]
npz_data = np.load(selected_file)
input_image = npz_data["inputs"]
adversarial_image = npz_data["adversarial"]
label = npz_data["label_str"].item()
cv2.imwrite(
"/home/hpc/iwi7/iwi7101h/advisg/res.png",
adversarial_image,
)
label_index = self.labels.index(label)
img_tensor = torch.tensor(input_image, dtype=torch.float32).unsqueeze(0) / 255
adv_tensor = (
torch.tensor(adversarial_image, dtype=torch.float32).unsqueeze(0) / 255
)
self.curr_label = label
return img_tensor, adv_tensor, label_index
parser = argparse.ArgumentParser(
description="Evaluate adversarial examples against a classifier and an autoencoder."
)
parser.add_argument(
"--data_dir",
type=str,
required=True,
help="Directory containing the input images.",
)
parser.add_argument(
"--batch_size",
type=int,
default=32,
help="Batch size for processing images.",
)
parser.add_argument(
"--output_dir",
type=str,
default="/home/hpc/iwi7/iwi7101h/advisg/results/adversarial_blocking_mobnet",
help="Directory to save the output results.",
)
parser.add_argument(
"--results_dir",
type=str,
default="/home/hpc/iwi7/iwi7101h/advisg/results",
help="Directory containing the project structure with models.",
)
parser.add_argument(
"--data_type",
type=str,
default="normal",
choices=["normal", "normalized"],
)
parser.add_argument(
"--max_samples",
type=int,
default=-1000,
help="Maximum number of samples to process. Use -1000 for all samples.",
)
parser.add_argument(
"--adv_gen_model",
type=str,
default="resnet18_nosampling",
help="Adversarial generated model.",
)
parser.add_argument("--data_split", type=str, default="val")
args = parser.parse_args()
data_dir = Path(args.data_dir)
output_dir = Path(args.output_dir)
data_split = args.data_split
data_type = args.data_type
batch_size = args.batch_size
max_samples = args.max_samples
adv_gen_model = args.adv_gen_model
if not output_dir.exists():
output_dir.mkdir(parents=True, exist_ok=True)
output_dir = output_dir / adv_gen_model
# Set the project directory
results_dir = Path(args.results_dir)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
adversarial_names = [a.name for a in data_dir.iterdir()]
logger.info(f"Found {len(adversarial_names)} adversarial types.")
logger.info(f"\n{adversarial_names}")
def get_predictions(
clf_model,
atc_model,
ae_model,
input_batch,
adversarial_batch,
):
input_tensor = input_batch
adversarial_tensor = adversarial_batch
with torch.no_grad():
# for clean data
clean_clf_logit, proba = clf_model(input_tensor)
clean_atc_logit, adv_proba = atc_model(input_tensor)
# for adversarial data
adv_clf_logit, adv_proba = clf_model(adversarial_tensor)
adv_atc_logit, adv_adv_proba = atc_model(adversarial_tensor)
# reconstructed input and adversarial images
recon_input = ae_model(input_tensor)
recon_adversarial = ae_model(adversarial_tensor)
# block clean data
recon_clean_clf_logit, _ = clf_model(recon_input)
recon_clean_atc_logit, _ = atc_model(recon_input)
# block adversarial data
recon_adv_clf_logit, _ = clf_model(recon_adversarial)
recon_adv_atc_logit, _ = atc_model(recon_adversarial)
# Get predictions
clean_clf_pred = clean_clf_logit.cpu().numpy().argmax(axis=1)
clean_atc_pred = clean_atc_logit.cpu().numpy().argmax(axis=1)
adv_clf_pred = adv_clf_logit.cpu().numpy().argmax(axis=1)
adv_atc_pred = adv_atc_logit.cpu().numpy().argmax(axis=1)
recon_input = recon_input
recon_adversarial = recon_adversarial
recon_clean_clf_preds = recon_clean_clf_logit.cpu().numpy().argmax(axis=1)
recon_clean_atc_preds = recon_clean_atc_logit.cpu().numpy().argmax(axis=1)
recon_adv_clf_preds = recon_adv_clf_logit.cpu().numpy().argmax(axis=1)
recon_adv_atc_preds = recon_adv_atc_logit.cpu().numpy().argmax(axis=1)
return (
clean_clf_pred,
clean_atc_pred,
adv_clf_pred,
adv_atc_pred,
recon_input,
recon_adversarial,
recon_clean_clf_preds,
recon_clean_atc_preds,
recon_adv_clf_preds,
recon_adv_atc_preds,
)
labels = [
"REPLAY",
"DNP3_INFO",
"DNP3_ENUMERATE",
"STOP_APP",
"NORMAL",
"INIT_DATA",
"COLD_RESTART",
"WARM_RESTART",
"DISABLE_UNSOLICITED",
]
if "no_clf" in output_dir.name:
clf_adv_ae_models = [
# [
# Path("image_classification/resnet18_nosampling"),
# Path("image_classification/resnet18_nosampling_adv"),
# Path("autoencoder/rdunet_normal"),
# ],
# [
# Path("image_classification/resnet18_normalized_nosampling"),
# Path("image_classification/resnet18_normalized_nosampling_adv"),
# Path("autoencoder/rdunet_normalized"),
# ],
[
Path("image_classification/mobilenet_v3_large_nosampling"),
Path("image_classification/mobilenet_v3_large_nosampling_adv"),
Path("autoencoder/rdunet_normal_no_clf"),
],
[
Path("image_classification/mobilenet_v3_large_normalized_nosampling"),
Path("image_classification/mobilenet_v3_large_normalized_nosampling_adv"),
Path("autoencoder/rdunet_normalized_no_clf"),
],
# [
# Path("image_classification/resnet18_nosampling"),
# Path("image_classification/resnet18_nosampling_adv"),
# Path("autoencoder/unet_custom_normal"),
# ],
# [
# Path("image_classification/resnet18_normalized_nosampling"),
# Path("image_classification/resnet18_normalized_nosampling_adv"),
# Path("autoencoder/unet_custom_normalized"),
# ],
[
Path("image_classification/mobilenet_v3_large_nosampling"),
Path("image_classification/mobilenet_v3_large_nosampling_adv"),
Path("autoencoder/unet_custom_normal_no_clf"),
],
[
Path("image_classification/mobilenet_v3_large_normalized_nosampling"),
Path("image_classification/mobilenet_v3_large_normalized_nosampling_adv"),
Path("autoencoder/unet_custom_normalized_no_clf"),
],
]
else:
clf_adv_ae_models = [
# [
# Path("image_classification/resnet18_nosampling"),
# Path("image_classification/resnet18_nosampling_adv"),
# Path("autoencoder/rdunet_normal"),
# ],
# [
# Path("image_classification/resnet18_normalized_nosampling"),
# Path("image_classification/resnet18_normalized_nosampling_adv"),
# Path("autoencoder/rdunet_normalized"),
# ],
[
Path("image_classification/mobilenet_v3_large_nosampling"),
Path("image_classification/mobilenet_v3_large_nosampling_adv"),
Path("autoencoder/rdunet_normal"),
],
[
Path("image_classification/mobilenet_v3_large_normalized_nosampling"),
Path("image_classification/mobilenet_v3_large_normalized_nosampling_adv"),
Path("autoencoder/rdunet_normalized"),
],
# [
# Path("image_classification/resnet18_nosampling"),
# Path("image_classification/resnet18_nosampling_adv"),
# Path("autoencoder/unet_custom_normal"),
# ],
# [
# Path("image_classification/resnet18_normalized_nosampling"),
# Path("image_classification/resnet18_normalized_nosampling_adv"),
# Path("autoencoder/unet_custom_normalized"),
# ],
[
Path("image_classification/mobilenet_v3_large_nosampling"),
Path("image_classification/mobilenet_v3_large_nosampling_adv"),
Path("autoencoder/unet_custom_normal"),
],
[
Path("image_classification/mobilenet_v3_large_normalized_nosampling"),
Path("image_classification/mobilenet_v3_large_normalized_nosampling_adv"),
Path("autoencoder/unet_custom_normalized"),
],
]
skip_adversarial_names = ["Carlini", "DeepFool"]
results = []
for adversarial_name in adversarial_names:
skip_this = False
for skip_name in skip_adversarial_names:
if skip_name.lower() in adversarial_name.lower():
skip_this = True
logger.info(f"Skipping {adversarial_name} due to skip list.")
break
if skip_this:
continue
if not (data_dir / adversarial_name).exists():
logger.warning(f"Adversarial type {adversarial_name} does not exist.")
for clf_path, adv_path, ae_path in clf_adv_ae_models:
is_normalized = "normalized" in clf_path.name
curr_dtype = "normalized" if is_normalized else "normal"
if curr_dtype != data_type:
logger.info(
f"Skipping {adversarial_name} for {clf_path.name}, {adv_path.name}, {ae_path.name} due to data type mismatch."
)
continue
sample_images_dir = output_dir / data_split / f"{adversarial_name}"
if not sample_images_dir.exists():
sample_images_dir.mkdir(parents=True, exist_ok=True)
clf_model = torch.load(
results_dir / clf_path / "best_model_full.pth",
map_location=device,
weights_only=False,
)
clf_model.eval()
adv_model = torch.load(
results_dir / adv_path / "best_model_full.pth",
map_location=device,
weights_only=False,
)
adv_model.eval()
ae_model = torch.load(
results_dir / ae_path / "best_model_full.pth",
map_location=device,
weights_only=False,
)
ae_model.eval()
logger.info(
f"Evaluating {adversarial_name} with models: {clf_path.name}, {adv_path.name}, {ae_path.name}"
)
dataset = DataSet(
input_dir=data_dir,
labels=labels,
data_split=data_split,
adversarial_type=adversarial_name,
max_samples=max_samples,
)
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=False, num_workers=4
)
logger.info(f"Dataset length: {len(dataset)}")
true_targets = []
# clean data
clean_clf_predictions = []
clean_atc_predictions = []
# adv data
adv_clf_predictions = []
adv_atc_predictions = []
# blocked clean data
recon_clean_clf_predictions = []
recon_clean_atc_predictions = []
# blocked adv data
recon_adv_clf_predictions = []
recon_adv_atc_predictions = []
clean_recon_mae_values = []
adv_recon_mae_values = []
adv_recon_inp_mae_values = []
# label, clean, adversarial, reconstructed
sample_images = {}
completed_non_adversarial = False
for batch_idx, (input_batch, adversarial_batch, label_indices) in tqdm.tqdm(
enumerate(dataloader),
total=len(dataloader),
desc=f"{adversarial_name}",
disable=not sys.stdout.isatty(),
):
input_batch = input_batch.to(device)
adversarial_batch = adversarial_batch.to(device)
label_indices = label_indices.cpu().numpy()
(
clean_clf_pred,
clean_atc_pred,
adv_clf_pred,
adv_atc_pred,
recon_input,
recon_adversarial,
recon_clean_clf_preds,
recon_clean_atc_preds,
recon_adv_clf_preds,
recon_adv_atc_preds,
) = get_predictions(
clf_model,
adv_model,
ae_model,
input_batch,
adversarial_batch,
)
# = get_predictions(
# clf_model,
# adv_model,
# ae_model,
# input_batch,
# adversarial_batch,
# )
if not completed_non_adversarial:
for idx, label in enumerate(label_indices):
label_str = dataset.labels[label]
if label_str not in sample_images:
# as uint8
input_image = (
input_batch[idx].cpu().squeeze(0).numpy() * 255
).astype(np.uint8)
adversarial_image = (
adversarial_batch[idx].cpu().squeeze(0).numpy() * 255
).astype(np.uint8)
recon_input_image = (
recon_input[idx].cpu().squeeze(0).numpy() * 255
).astype(np.uint8)
recon_adversarial_image = (
recon_adversarial[idx].cpu().squeeze(0).numpy() * 255
).astype(np.uint8)
sample_images[label_str] = {
"input": input_image,
"adversarial": adversarial_image,
"recon_input": recon_input_image,
"recon_adversarial": recon_adversarial_image,
}
sample_completed_lbls = set(sample_images.keys())
if len(sample_completed_lbls) == len(dataset.labels):
completed_non_adversarial = True
true_targets.extend(label_indices.tolist())
clean_clf_predictions.extend(clean_clf_pred.tolist())
clean_atc_predictions.extend(clean_atc_pred.tolist())
adv_clf_predictions.extend(adv_clf_pred.tolist())
adv_atc_predictions.extend(adv_atc_pred.tolist())
recon_clean_clf_predictions.extend(recon_clean_clf_preds.tolist())
recon_clean_atc_predictions.extend(recon_clean_atc_preds.tolist())
recon_adv_clf_predictions.extend(recon_adv_clf_preds.tolist())
recon_adv_atc_predictions.extend(recon_adv_atc_preds.tolist())
clean_recon_mae = (
torch.mean(
torch.abs(input_batch - recon_input).view(input_batch.size(0), -1),
dim=1,
)
.cpu()
.numpy()
)
adv_recon_mae = (
torch.mean(
torch.abs(adversarial_batch - recon_adversarial).view(
adversarial_batch.size(0), -1
),
dim=1,
)
.cpu()
.numpy()
)
adv_recon_inp_mae = (
torch.mean(
torch.abs(input_batch - recon_adversarial).view(
input_batch.size(0), -1
),
dim=1,
)
.cpu()
.numpy()
)
clean_recon_mae_values.extend(clean_recon_mae.tolist())
adv_recon_mae_values.extend(adv_recon_mae.tolist())
adv_recon_inp_mae_values.extend(adv_recon_inp_mae.tolist())
true_targets = np.array(true_targets)
# Calculate confusion matrix and F1 score
# f1 from clean data passed to clf and atc clf
clean_clf_f1, clean_clf_cm = get_confusion_matrix(
clean_clf_predictions,
true_targets,
labels,
)
clean_atc_f1, clean_atc_cm = get_confusion_matrix(
clean_atc_predictions,
true_targets,
labels,
)
# f1 from adv data passed to clf and atc clf
adv_clf_f1, adv_clf_cm = get_confusion_matrix(
adv_clf_predictions,
true_targets,
labels,
)
adv_atc_f1, adv_atc_cm = get_confusion_matrix(
adv_atc_predictions,
true_targets,
labels,
)
# f1 from recon clean data passed to clf and atc clf
recon_clean_clf_f1, recon_clean_clf_cm = get_confusion_matrix(
recon_clean_clf_predictions,
true_targets,
labels,
)
recon_clean_atc_f1, recon_clean_atc_cm = get_confusion_matrix(
recon_clean_atc_predictions,
true_targets,
labels,
)
# f1 from recon adv data passed to clf and adv clf
recon_adv_clf_f1, recon_adv_clf_cm = get_confusion_matrix(
recon_adv_clf_predictions,
true_targets,
labels,
)
recon_adv_atc_f1, recon_adv_atc_cm = get_confusion_matrix(
recon_adv_atc_predictions,
true_targets,
labels,
)
# Calculate MAE values
clean_recon_mae_values = np.mean(clean_recon_mae_values)
adv_recon_mae_values = np.mean(adv_recon_mae_values)
adv_recon_inp_mae_values = np.mean(adv_recon_inp_mae_values)
logger.info(
f"Results for {adversarial_name} "
f"Clean CLF F1: {clean_clf_f1:.4f}, "
f"Clean ATC F1: {clean_atc_f1:.4f}, "
f"Adv CLF F1: {adv_clf_f1:.4f}, "
f"Adv ATC F1: {adv_atc_f1:.4f}, "
f"Blocked Clean CLF F1: {recon_clean_clf_f1:.4f}, "
f"Blocked Adv CLF F1: {recon_clean_atc_f1:.4f}, "
f"Adv Blocked CLF F1: {recon_adv_clf_f1:.4f}, "
f"Adv Blocked ATC F1: {recon_adv_atc_f1:.4f}, "
f"Clean Recon MAE: {clean_recon_mae_values:.4f}, "
f"Adv Recon MAE: {adv_recon_mae_values:.4f}, "
f"Adv Recon Input MAE: {adv_recon_inp_mae_values:.4f}",
)
results.append(
{
"clf_model": clf_path.name,
"adv_model": adv_path.name,
"ae_model": ae_path.name,
"data_type": data_type,
"data_split": data_split,
"adversarial_name": adversarial_name,
"clean_clf_f1": clean_clf_f1,
"clean_atc_f1": clean_atc_f1,
"adv_clf_f1": adv_clf_f1,
"adv_atc_f1": adv_atc_f1,
"recon_clean_clf_f1": recon_clean_clf_f1,
"recon_clean_atc_f1": recon_clean_atc_f1,
"recon_adv_clf_f1": recon_adv_clf_f1,
"recon_adv_atc_f1": recon_adv_atc_f1,
"clean_recon_mae": clean_recon_mae_values,
"adv_recon_mae": adv_recon_mae_values,
"adv_recon_inp_mae": adv_recon_inp_mae_values,
"clean_clf_cm": clean_clf_cm.tolist(),
"clean_atc_cm": clean_atc_cm.tolist(),
"adv_clf_cm": adv_clf_cm.tolist(),
"adv_atc_cm": adv_atc_cm.tolist(),
"recon_clean_clf_cm": recon_clean_clf_cm.tolist(),
"recon_clean_atc_cm": recon_clean_atc_cm.tolist(),
"recon_adv_clf_cm": recon_adv_clf_cm.tolist(),
"recon_adv_atc_cm": recon_adv_atc_cm.tolist(),
}
)
# Save sample images
for label, images in sample_images.items():
image_prefix = f"{label}_{ae_path.name}"
for img_name, image in images.items():
input_path = sample_images_dir / f"{image_prefix}_{img_name}.png"
cv2.imwrite(str(input_path), image)
logger.info(f"Saved {img_name} image for {label} to {input_path}")
# Save results to CSV
results_df = pd.DataFrame(results)
results_df.to_csv(
output_dir / f"evaluation_results_{data_type}_{data_split}.csv", index=False
)
logger.info(
f"Evaluation results saved to {output_dir / f'evaluation_results_{data_type}_{data_split}.csv'}"
)
logger.info("Evaluation completed successfully.")