@@ -97,12 +97,11 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> n
9797
9898 return x_adv
9999
100- def _generate_batch (self , x_batch : np .ndarray , y_batch : Optional [ np . ndarray ] = None ) -> np .ndarray :
100+ def _generate_batch (self , x_batch : np .ndarray ) -> np .ndarray :
101101 """
102102 Run the attack on a batch of images.
103103
104104 :param x_batch: A batch of original examples.
105- :param y_batch: Not Used.
106105 :return: A batch of adversarial examples.
107106 """
108107
@@ -144,7 +143,7 @@ def _attack(self, x_adv: "torch.Tensor", x: "torch.Tensor") -> "torch.Tensor":
144143 x_adv .requires_grad_ (False )
145144 return x_adv
146145
147- def _loss (self , x : "torch.tensor " ) -> Tuple ["torch.tensor " , "torch.tensor " ]:
146+ def _loss (self , x : "torch.Tensor " ) -> Tuple ["torch.Tensor " , "torch.Tensor " ]:
148147 """
149148 Compute the weight of each pixel and the overload loss for a given image.
150149
@@ -155,13 +154,13 @@ def _loss(self, x: "torch.tensor") -> Tuple["torch.tensor", "torch.tensor"]:
155154 import torch
156155
157156 adv_logits = self .estimator .model .model (x )
158- if type (adv_logits ) is tuple :
157+ if isinstance (adv_logits , tuple ) :
159158 adv_logits = adv_logits [0 ]
160159
161- THRESHOLD = self .estimator .model .conf
160+ threshold = self .estimator .model .conf
162161 conf = adv_logits [..., 4 ]
163162 prob = adv_logits [..., 5 :]
164- prob = torch .where (conf [:, :, None ] * prob > THRESHOLD , torch .ones_like (prob ), prob )
163+ prob = torch .where (conf [:, :, None ] * prob > threshold , torch .ones_like (prob ), prob )
165164 prob = torch .sum (prob , dim = 2 )
166165 conf = conf * prob
167166
@@ -174,19 +173,19 @@ def _loss(self, x: "torch.tensor") -> Tuple["torch.tensor", "torch.tensor"]:
174173 stride_x = x .shape [- 2 ] // self .num_grid
175174 stride_y = x .shape [- 1 ] // self .num_grid
176175 grid_box = torch .zeros ((0 , 4 ), device = x .device )
177- for ii in range (self .num_grid ):
178- for jj in range (self .num_grid ):
179- x1 = ii * stride_x
180- y1 = jj * stride_y
181- x2 = min (x1 + stride_x , x .shape [- 2 ])
182- y2 = min (y1 + stride_y , x .shape [- 1 ])
183- bb = torch .as_tensor ([x1 , y1 , x2 , y2 ], device = x .device )[None , :]
184- grid_box = torch .cat ([grid_box , bb ], dim = 0 )
185-
186- for xi in range (x .shape [0 ]):
187- xyhw = adv_logits [xi , :, :4 ]
188- prob = torch .max (adv_logits [xi , :, 5 :], dim = 1 ).values
189- box_idx = adv_logits [xi , :, 4 ] * prob > THRESHOLD
176+ for i_i in range (self .num_grid ):
177+ for j_j in range (self .num_grid ):
178+ x_1 = i_i * stride_x
179+ y_1 = j_j * stride_y
180+ x_2 = min (x_1 + stride_x , x .shape [- 2 ])
181+ y_2 = min (y_1 + stride_y , x .shape [- 1 ])
182+ b_b = torch .as_tensor ([x_1 , y_1 , x_2 , y_2 ], device = x .device )[None , :]
183+ grid_box = torch .cat ([grid_box , b_b ], dim = 0 )
184+
185+ for x_i in range (x .shape [0 ]):
186+ xyhw = adv_logits [x_i , :, :4 ]
187+ prob = torch .max (adv_logits [x_i , :, 5 :], dim = 1 ).values
188+ box_idx = adv_logits [x_i , :, 4 ] * prob > threshold
190189 xyhw = xyhw [box_idx ]
191190 c_xyxy = self .xywh2xyxy (xyhw )
192191 scores = box_iou (grid_box , c_xyxy )
@@ -197,20 +196,21 @@ def _loss(self, x: "torch.tensor") -> Tuple["torch.tensor", "torch.tensor"]:
197196 # Increase the weight of the grid with fewer objects
198197 idx_min = torch .argmin (scores )
199198 grid_min = grid_box [idx_min ]
200- x1 , y1 , x2 , y2 = grid_min .int ()
201- pixel_weight [xi , :, y1 : y2 , x1 : x2 ] = pixel_weight [xi , :, y1 : y2 , x1 : x2 ] * 2
202- pixel_weight = pixel_weight / torch .max (pixel_weight [xi , :]) / 255.0
199+ x_1 , y_1 , x_2 , y_2 = grid_min .int ()
200+ pixel_weight [x_i , :, y_1 : y_2 , x_1 : x_2 ] = pixel_weight [x_i , :, y_1 : y_2 , x_1 : x_2 ] * 2
201+ pixel_weight = pixel_weight / torch .max (pixel_weight [x_i , :]) / 255.0
203202
204203 return ind_loss , pixel_weight
205204
206- def xywh2xyxy (self , xywh : "torch.tensor" ) -> "torch.tensor" :
205+ @staticmethod
206+ def xywh2xyxy (xywh : "torch.Tensor" ) -> "torch.Tensor" :
207207 """
208208 Convert the representation from xywh format yo xyxy format.
209209
210- : param xyhw: A n by 4 boxes store the information in xyhw format
211- where [x ,y, w h] is [center_x, center_y, width, height]
212- : return: The n by 4 boxex in xyxy format
213- where [x1, y1, x2, y2] is [top_left_x, top_left_y, bottom_right_x, bottom_right_y]
210+ :param xyhw: A n by 4 boxes store the information in xyhw format
211+ where [x ,y, w h] is [center_x, center_y, width, height]
212+ :return: The n by 4 boxes in xyxy format
213+ where [x1, y1, x2, y2] is [top_left_x, top_left_y, bottom_right_x, bottom_right_y]
214214 """
215215 xyxy = xywh .clone ()
216216 xyxy [:, 0 ] = xywh [:, 0 ] - xywh [:, 2 ] / 2
0 commit comments