42
42
logger = logging .getLogger (__name__ )
43
43
44
44
45
- # tiling
46
45
def _generate_tile_kernel (patch : list , mask : list , tile_size : int ) -> Tuple ["torch.Tensor" , "torch.Tensor" ]:
47
46
"""
48
- Generate specific size of pertuerbed tiles from randomly selected patches.
47
+ Generate specific size of perturbed tiles from randomly selected patches.
49
48
50
- :param patch: Candiate patches.
49
+ :param patch: Candidate patches.
51
50
:param mask: Masks for each patch.
52
51
:param tile_size: The size of each tile.
53
- :return: Pertuerbed tiles and corresponding maskes .
52
+ :return: Perturbed tiles and corresponding masks .
54
53
"""
55
54
import torch
56
55
import torchvision
@@ -79,7 +78,7 @@ def _generate_tile_kernel(patch: list, mask: list, tile_size: int) -> Tuple["tor
79
78
min_len = height
80
79
81
80
if max_len > tile_size :
82
- new_len = round (min_len * tile_size / max_len )
81
+ new_len = max ( round (min_len * tile_size / max_len ), 1 )
83
82
p_1 = torchvision .transforms .Resize ((tile_size , new_len ))(t_patch )
84
83
# fix for the case that (strides - new_len) > new_len
85
84
p_list = []
@@ -95,7 +94,7 @@ def _generate_tile_kernel(patch: list, mask: list, tile_size: int) -> Tuple["tor
95
94
n_mask = torch .where (n_patch == 0 , torch .zeros_like (n_patch ), torch .ones_like (n_patch ))
96
95
97
96
elif max_len >= tile_size / 2.0 :
98
- new_len = round (min_len * (tile_size / 2.0 ) / max_len )
97
+ new_len = max ( round (min_len * (tile_size / 2.0 ) / max_len ), 1 )
99
98
100
99
p_list = []
101
100
for _ in range (tile_size // new_len ):
@@ -150,13 +149,13 @@ def _generate_tile_kernel(patch: list, mask: list, tile_size: int) -> Tuple["tor
150
149
151
150
def generate_tile (patches : list , masks : list , tile_size : int , scale : list ) -> Tuple ["torch.Tensor" , "torch.Tensor" ]:
152
151
"""
153
- Generate different size of pertuerbed tiles from randomly selected patches.
152
+ Generate different size of perturbed tiles from randomly selected patches.
154
153
155
- :param patch: Candiate patches.
154
+ :param patch: Candidate patches.
156
155
:param mask: Masks for each patch.
157
156
:param tile_size: The size of each tile.
158
- :param scale: Scale factor for various tileing size.
159
- :return: Pertuerbed tiles and corresponding maskes .
157
+ :param scale: Scale factor for various tiling size.
158
+ :return: Perturbed tiles and corresponding masks .
160
159
"""
161
160
import torch
162
161
@@ -316,8 +315,8 @@ def __init__(
316
315
Create a SNAL attack instance.
317
316
318
317
:param estimator: A trained YOLOv8 model or other models with the same output format
319
- :param candidates: The collected pateches to generate perturbations.
320
- :param collector: A callbel uses to generate patches.
318
+ :param candidates: The collected patches to generate perturbations.
319
+ :param collector: A callable uses to generate patches.
321
320
:param eps: Maximum perturbation that the attacker can introduce.
322
321
:param max_iter: The maximum number of iterations.
323
322
:param num_grid: The number of grids for width and high dimension.
@@ -391,7 +390,7 @@ def _attack(self, x_adv: "torch.Tensor", x: "torch.Tensor") -> "torch.Tensor":
391
390
raise ValueError ("The size of the image must be divided by the number of grids" )
392
391
tile_size = x .shape [- 1 ] // self .num_grid
393
392
394
- # Prapare a 2D array to store the results of each grid
393
+ # Prepare a 2D array to store the results of each grid
395
394
buffer_depth = 5
396
395
tile_mat = {}
397
396
for idx_i in range (self .num_grid ):
@@ -503,10 +502,10 @@ def _attack(self, x_adv: "torch.Tensor", x: "torch.Tensor") -> "torch.Tensor":
503
502
504
503
def _get_loss (self , pert : "torch.Tensor" , epsilon : float ) -> "torch.Tensor" : # pylint: disable=R0201
505
504
"""
506
- Calculate accumulated distance of the perturbations outside the epslion ball.
505
+ Calculate accumulated distance of the perturbations outside the epsilon ball.
507
506
508
507
:param pert: Perturbations in the pixel space.
509
- :param epsilon: The radius of the eplion bass.
508
+ :param epsilon: The radius of the epsilon bass.
510
509
:return: loss.
511
510
"""
512
511
import torch
@@ -526,7 +525,7 @@ def _color_projection( # pylint: disable=R0201
526
525
527
526
:param tile: The target to convert.
528
527
:param x_ref: The source data.
529
- :param epsilon: The radius of the eplion bass.
528
+ :param epsilon: The radius of the epsilon bass.
530
529
:return: The converted tile.
531
530
"""
532
531
import torch
0 commit comments