@@ -183,7 +183,10 @@ def __init__(
183
183
self ._optimizer = torch .optim .Adam ([self ._patch ], lr = self .learning_rate )
184
184
185
185
def _train_step (
186
- self , images : "torch.Tensor" , target : "torch.Tensor" , mask : "torch.Tensor" | None = None
186
+ self ,
187
+ images : "torch.Tensor" ,
188
+ target : "torch.Tensor" | list [dict [str , "torch.Tensor" ]],
189
+ mask : "torch.Tensor" | None = None ,
187
190
) -> "torch.Tensor" :
188
191
import torch
189
192
@@ -227,7 +230,12 @@ def _predictions(
227
230
228
231
return predictions , target
229
232
230
- def _loss (self , images : "torch.Tensor" , target : "torch.Tensor" , mask : "torch.Tensor" | None ) -> "torch.Tensor" :
233
+ def _loss (
234
+ self ,
235
+ images : "torch.Tensor" ,
236
+ target : "torch.Tensor" | list [dict [str , "torch.Tensor" ]],
237
+ mask : "torch.Tensor" | None ,
238
+ ) -> "torch.Tensor" :
231
239
import torch
232
240
233
241
if isinstance (target , torch .Tensor ):
@@ -475,13 +483,13 @@ def _random_overlay(
475
483
return patched_images
476
484
477
485
def generate ( # type: ignore
478
- self , x : np .ndarray , y : np .ndarray | None = None , ** kwargs
486
+ self , x : np .ndarray , y : np .ndarray | list [ dict [ str , np . ndarray | "torch.Tensor" ]] | None = None , ** kwargs
479
487
) -> tuple [np .ndarray , np .ndarray ]:
480
488
"""
481
489
Generate an adversarial patch and return the patch and its mask in arrays.
482
490
483
491
:param x: An array with the original input images of shape NCHW or input videos of shape NFCHW.
484
- :param y: An array with the original true labels.
492
+ :param y: The true or target labels.
485
493
:param mask: A boolean array of shape equal to the shape of a single samples (1, H, W) or the shape of `x`
486
494
(N, H, W) without their channel dimensions. Any features for which the mask is True can be the
487
495
center location of the patch during sampling.
@@ -499,11 +507,12 @@ def generate( # type: ignore
499
507
if self .patch_location is not None and mask is not None :
500
508
raise ValueError ("Masks can only be used if the `patch_location` is `None`." )
501
509
502
- if y is None : # pragma: no cover
503
- logger .info ("Setting labels to estimator predictions and running untargeted attack because `y=None`." )
504
- y = to_categorical (np .argmax (self .estimator .predict (x = x ), axis = 1 ), nb_classes = self .estimator .nb_classes )
505
-
506
510
if hasattr (self .estimator , "nb_classes" ):
511
+
512
+ if y is None : # pragma: no cover
513
+ logger .info ("Setting labels to estimator classification predictions." )
514
+ y = to_categorical (np .argmax (self .estimator .predict (x = x ), axis = 1 ), nb_classes = self .estimator .nb_classes )
515
+
507
516
y = check_and_transform_label_format (labels = y , nb_classes = self .estimator .nb_classes )
508
517
509
518
# check if logits or probabilities
@@ -513,6 +522,10 @@ def generate( # type: ignore
513
522
self .use_logits = False
514
523
else :
515
524
self .use_logits = True
525
+ else :
526
+ if y is None : # pragma: no cover
527
+ logger .info ("Setting labels to estimator object detection predictions." )
528
+ y = self .estimator .predict (x = x )
516
529
517
530
if isinstance (y , np .ndarray ):
518
531
x_tensor = torch .Tensor (x )
0 commit comments