Skip to content

Commit 4a317e8

Browse files
authored
Merge pull request #2186 from Foxglove144/new_branch
Update get_started_fasterrcnn.py, application_object_detection.py and AdversarialPatchPyTorch
2 parents a338a0e + d37811f commit 4a317e8

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

art/attacks/evasion/adversarial_patch/adversarial_patch_pytorch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def __init__(
123123

124124
torch_version = list(map(int, torch.__version__.lower().split("+", maxsplit=1)[0].split(".")))
125125
torchvision_version = list(map(int, torchvision.__version__.lower().split("+", maxsplit=1)[0].split(".")))
126-
assert torch_version[0] >= 1 and torch_version[1] >= 7, "AdversarialPatchPyTorch requires torch>=1.7.0"
126+
assert (
127+
torch_version[0] >= 1 and torch_version[1] >= 7 or (torch_version[0] >= 2)
128+
), "AdversarialPatchPyTorch requires torch>=1.7.0"
127129
assert (
128130
torchvision_version[0] >= 0 and torchvision_version[1] >= 8
129131
), "AdversarialPatchPyTorch requires torchvision>=0.8.0"

examples/application_object_detection.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,24 @@ def plot_image_with_boxes(img, boxes, pred_cls):
129129

130130
for i in range(len(boxes)):
131131
# Draw Rectangle with the coordinates
132-
cv2.rectangle(img, boxes[i][0], boxes[i][1], color=(0, 255, 0), thickness=rect_th)
133132

133+
cv2.rectangle(
134+
img,
135+
(int(boxes[i][0][0]), int(boxes[i][0][1])),
136+
(int(boxes[i][1][0]), int(boxes[i][1][1])),
137+
color=(0, 255, 0),
138+
thickness=rect_th,
139+
)
134140
# Write the prediction class
135-
cv2.putText(img, pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0, 255, 0), thickness=text_th)
136-
141+
cv2.putText(
142+
img,
143+
pred_cls[i],
144+
(int(boxes[i][0][0]), int(boxes[i][0][1])),
145+
cv2.FONT_HERSHEY_SIMPLEX,
146+
text_size,
147+
(0, 255, 0),
148+
thickness=text_th,
149+
)
137150
plt.axis("off")
138151
plt.imshow(img.astype(np.uint8), interpolation="nearest")
139152
plt.show()

examples/get_started_fasterrcnn.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,24 @@ def plot_image_with_boxes(img, boxes, pred_cls):
158158

159159
for i in range(len(boxes)):
160160
# Draw Rectangle with the coordinates
161-
cv2.rectangle(img, boxes[i][0], boxes[i][1], color=(0, 255, 0), thickness=rect_th)
162161

162+
cv2.rectangle(
163+
img,
164+
(int(boxes[i][0][0]), int(boxes[i][0][1])),
165+
(int(boxes[i][1][0]), int(boxes[i][1][1])),
166+
color=(0, 255, 0),
167+
thickness=rect_th,
168+
)
163169
# Write the prediction class
164-
cv2.putText(img, pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0, 255, 0), thickness=text_th)
165-
170+
cv2.putText(
171+
img,
172+
pred_cls[i],
173+
(int(boxes[i][0][0]), int(boxes[i][0][1])),
174+
cv2.FONT_HERSHEY_SIMPLEX,
175+
text_size,
176+
(0, 255, 0),
177+
thickness=text_th,
178+
)
166179
plt.axis("off")
167180
plt.imshow(img.astype(np.uint8), interpolation="nearest")
168181
plt.show()
@@ -215,7 +228,7 @@ def append_loss_history(loss_history, output):
215228
"batch_size": 1,
216229
"image_file": "banner-diverse-group-of-people-2.jpg",
217230
"resume": False,
218-
"path": "xp/",
231+
"path": "",
219232
}
220233

221234
pp = pprint.PrettyPrinter(indent=4)
@@ -280,3 +293,14 @@ def append_loss_history(loss_history, output):
280293
file.write(json.dumps(loss_history))
281294

282295
np.save(os.path.join(config["path"], "patch"), attack._patch)
296+
297+
predictions_adv = frcnn.predict(x=x_patch)
298+
299+
for i in range(image.shape[0]):
300+
print("\nPredictions adversarial image {}:".format(i))
301+
302+
# Process predictions
303+
predictions_adv_class, predictions_adv_boxes, predictions_adv_class = extract_predictions(predictions_adv[i])
304+
305+
# Plot predictions
306+
plot_image_with_boxes(img=x_patch[i].copy(), boxes=predictions_adv_boxes, pred_cls=predictions_adv_class)

0 commit comments

Comments
 (0)