-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattack_robustdpatch.py
More file actions
90 lines (69 loc) · 2.5 KB
/
attack_robustdpatch.py
File metadata and controls
90 lines (69 loc) · 2.5 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
import numpy as np
import torch
import cv2
import os
from pathlib import Path
from tqdm import tqdm
import yolov5
from art.estimators.object_detection.pytorch_yolo import PyTorchYolo
from art.attacks.evasion import RobustDPatch
from aap_utils.aap_utils import Yolo
from aap_utils.aap_utils import data_loader_test
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# -------------------------------------------------
# Load YOLOv5
# -------------------------------------------------
model = yolov5.load("./weights/shipv5m.pt")
model = Yolo(model, alpha=0.1, beta=1)
detector = PyTorchYolo(
model=model,
device_type="gpu",
input_shape=(3, 640, 640),
clip_values=(0, 255),
attack_losses=("loss_total",)
)
# -------------------------------------------------
# Config
# -------------------------------------------------
test_img_num = 1000
src_path = "./dataset/clean_val/"
save_path = "./adv_examples/robustdpatch7/"
max_iter = 5000
# -------------------------------------------------
# Attack
# -------------------------------------------------
def generate_robustdpatch(source_path, save_path, iterations=200):
attack = RobustDPatch(
detector,
patch_shape=(3,120,120),
patch_location=(150,150),
crop_range=(0,0),
brightness_range=(0.7,1.4),
rotation_weights=(0.7,0.1,0.1,0.1),
sample_size=1,
learning_rate=5.0,
max_iter=iterations,
batch_size=1,
targeted=False,
verbose=True
)
src_path = Path(source_path)
save_path = Path(save_path)
save_path.mkdir(parents=True, exist_ok=True)
img_list, txt_list, loc_list = data_loader_test(source_path, True, test_img_num)
for i in tqdm(range(test_img_num)):
stem = Path(txt_list[i]).stem
img_path = src_path / "images" / f"{stem}.jpg"
save_name = save_path / f"patched_{stem}.jpg"
img = cv2.imread(str(img_path))
img = cv2.resize(img, (640, 640))
img = img.transpose((2,0,1))
x = np.expand_dims(img, axis=0).astype(np.float32)
x_adv = attack.generate(x=x, y=None)
adv_img = attack.apply_patch(x, patch_external=x_adv, random_location=True)
adv_img = np.nan_to_num(adv_img, nan=0.0, posinf=255.0, neginf=0.0)
adv_img = np.clip(adv_img, 0, 255)
adv_img = adv_img[0].transpose(1,2,0).astype(np.uint8)
cv2.imwrite(str(save_name), adv_img)
# -------------------------------------------------
generate_robustdpatch(src_path, save_path, max_iter)