|
115 | 115 | ] |
116 | 116 |
|
117 | 117 |
|
118 | | -def extract_predictions(predictions_, conf_thresh): |
| 118 | +def extract_predictions(predictions_, top_k): |
119 | 119 | # Get the predicted class |
120 | 120 | predictions_class = [COCO_INSTANCE_CATEGORY_NAMES[i] for i in list(predictions_["labels"])] |
121 | | - # print("\npredicted classes:", predictions_class) |
122 | | - if len(predictions_class) < 1: |
123 | | - return [], [], [] |
| 121 | + |
124 | 122 | # Get the predicted bounding boxes |
125 | 123 | predictions_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(predictions_["boxes"])] |
126 | 124 |
|
127 | 125 | # Get the predicted prediction score |
128 | 126 | predictions_score = list(predictions_["scores"]) |
129 | | - # print("predicted score:", predictions_score) |
| 127 | + |
| 128 | + # sort all lists according to scores |
| 129 | + # Combine into a list of tuples |
| 130 | + combined = list(zip(predictions_score, predictions_boxes, predictions_class)) |
| 131 | + |
| 132 | + # Sort by score (first element of tuple), descending |
| 133 | + combined_sorted = sorted(combined, key=lambda x: x[0], reverse=True) |
| 134 | + |
| 135 | + # Unpack sorted tuples |
| 136 | + predictions_score, predictions_boxes, predictions_class = zip(*combined_sorted) |
| 137 | + |
| 138 | + # Convert back to lists |
| 139 | + predictions_score = list(predictions_score) |
| 140 | + predictions_boxes = list(predictions_boxes) |
| 141 | + predictions_class = list(predictions_class) # Combine into a list of tuples |
130 | 142 |
|
131 | 143 | # Get a list of index with score greater than threshold |
132 | | - threshold = conf_thresh |
133 | | - predictions_t = [predictions_score.index(x) for x in predictions_score if x > threshold] |
134 | | - if len(predictions_t) == 0: |
135 | | - return [], [], [] |
136 | | - |
137 | | - # predictions in score order |
138 | | - predictions_boxes = [predictions_boxes[i] for i in predictions_t] |
139 | | - predictions_class = [predictions_class[i] for i in predictions_t] |
140 | | - predictions_scores = [predictions_score[i] for i in predictions_t] |
| 144 | + predictions_t = top_k |
| 145 | + |
| 146 | + predictions_boxes = predictions_boxes[:predictions_t] |
| 147 | + predictions_class = predictions_class[:predictions_t] |
| 148 | + predictions_scores = predictions_score[:predictions_t] |
| 149 | + |
141 | 150 | return predictions_class, predictions_boxes, predictions_scores |
142 | 151 |
|
143 | 152 |
|
144 | 153 | def plot_image_with_boxes(img, boxes, pred_cls, title): |
145 | | - plt.style.use("ggplot") |
146 | | - text_size = 1 |
147 | | - text_th = 3 |
148 | | - rect_th = 1 |
| 154 | + text_size = 2 |
| 155 | + text_th = 2 |
| 156 | + rect_th = 2 |
149 | 157 |
|
150 | 158 | img = img.copy() |
151 | 159 |
|
@@ -175,18 +183,10 @@ def plot_image_with_boxes(img, boxes, pred_cls, title): |
175 | 183 | plt.show() |
176 | 184 |
|
177 | 185 |
|
178 | | -""" |
179 | | -################# Evasion settings ################# |
180 | | -""" |
181 | | -eps = 32 |
182 | | -eps_step = 2 |
183 | | -max_iter = 10 |
184 | | - |
185 | | - |
186 | 186 | """ |
187 | 187 | ################# Model definition ################# |
188 | 188 | """ |
189 | | -MODEL = "yolov3" # OR yolov5 |
| 189 | +MODEL = "yolov5" # OR yolov5 |
190 | 190 |
|
191 | 191 |
|
192 | 192 | if MODEL == "yolov3": |
@@ -265,35 +265,37 @@ def forward(self, x, targets=None): |
265 | 265 | """ |
266 | 266 | response = requests.get("https://ultralytics.com/images/zidane.jpg") |
267 | 267 | img = np.asarray(Image.open(BytesIO(response.content)).resize((640, 640))) |
268 | | -img_reshape = img.transpose((2, 0, 1)) |
269 | | -image = np.stack([img_reshape], axis=0).astype(np.float32) |
270 | | -x = image.copy() |
| 268 | +image = np.stack([img], axis=0).astype(np.float32) |
| 269 | +image_chw = np.transpose(image, (0, 3, 1, 2)) |
271 | 270 |
|
272 | 271 | """ |
273 | 272 | ################# Evasion attack ################# |
274 | 273 | """ |
275 | 274 |
|
276 | | -attack = ProjectedGradientDescent(estimator=detector, eps=eps, eps_step=eps_step, max_iter=max_iter) |
277 | | -image_adv = attack.generate(x=x, y=None) |
| 275 | +eps = 32 |
| 276 | +attack = ProjectedGradientDescent(estimator=detector, eps=eps, eps_step=2, max_iter=10) |
| 277 | +image_adv_chw = attack.generate(x=image_chw, y=None) |
| 278 | +image_adv = np.transpose(image_adv_chw, (0, 2, 3, 1)) |
278 | 279 |
|
279 | 280 | print("\nThe attack budget eps is {}".format(eps)) |
280 | | -print("The resulting maximal difference in pixel values is {}.".format(np.amax(np.abs(x - image_adv)))) |
| 281 | +print("The resulting maximal difference in pixel values is {}.".format(np.amax(np.abs(image_chw - image_adv_chw)))) |
281 | 282 |
|
282 | 283 | plt.axis("off") |
283 | 284 | plt.title("adversarial image") |
284 | | -plt.imshow(image_adv[0].transpose(1, 2, 0).astype(np.uint8), interpolation="nearest") |
| 285 | +plt.imshow(image_adv[0].astype(np.uint8), interpolation="nearest") |
285 | 286 | plt.show() |
286 | 287 |
|
287 | | -threshold = 0.85 # 0.5 |
288 | | -dets = detector.predict(x) |
289 | | -preds = extract_predictions(dets[0], threshold) |
290 | | -plot_image_with_boxes(img=img, boxes=preds[1], pred_cls=preds[0], title="Predictions on original image") |
| 288 | +predictions = detector.predict(x=image_chw) |
| 289 | +predictions_class, predictions_boxes, _ = extract_predictions(predictions[0], top_k=3) |
| 290 | +plot_image_with_boxes( |
| 291 | + img=image[0], boxes=predictions_boxes, pred_cls=predictions_class, title="Predictions on original image" |
| 292 | +) |
291 | 293 |
|
292 | | -dets = detector.predict(image_adv) |
293 | | -preds = extract_predictions(dets[0], threshold) |
| 294 | +predictions = detector.predict(image_adv_chw) |
| 295 | +predictions_class, predictions_boxes, d = extract_predictions(predictions[0], top_k=3) |
294 | 296 | plot_image_with_boxes( |
295 | | - img=image_adv[0].transpose(1, 2, 0).copy(), |
296 | | - boxes=preds[1], |
297 | | - pred_cls=preds[0], |
| 297 | + img=image_adv[0], |
| 298 | + boxes=predictions_boxes, |
| 299 | + pred_cls=predictions_class, |
298 | 300 | title="Predictions on adversarial image", |
299 | 301 | ) |
0 commit comments