@@ -86,15 +86,15 @@ def add_pattern_bd(x: np.ndarray, distance: int = 2, pixel_value: int = 1) -> np
8686
8787
8888def insert_image (
89- x : np .ndarray ,
90- backdoor_path : str = "../utils/data/backdoors/alert.png" ,
91- channels_first : bool = False ,
92- random : bool = True ,
93- x_shift : int = 0 ,
94- y_shift : int = 0 ,
95- size : Optional [Tuple [int , int ]] = None ,
96- mode : str = "L" ,
97- blend = 0.8 ,
89+ x : np .ndarray ,
90+ backdoor_path : str = "../utils/data/backdoors/alert.png" ,
91+ channels_first : bool = False ,
92+ random : bool = True ,
93+ x_shift : int = 0 ,
94+ y_shift : int = 0 ,
95+ size : Optional [Tuple [int , int ]] = None ,
96+ mode : str = "L" ,
97+ blend = 0.8 ,
9898) -> np .ndarray :
9999 """
100100 Augments a matrix by setting a checkboard-like pattern of values some `distance` away from the bottom-right
@@ -115,8 +115,11 @@ def insert_image(
115115 n_dim = len (x .shape )
116116 if n_dim == 4 :
117117 return np .array (
118- [insert_image (single_img , backdoor_path , channels_first , random , x_shift , y_shift , size , mode , blend )
119- for single_img in x ])
118+ [
119+ insert_image (single_img , backdoor_path , channels_first , random , x_shift , y_shift , size , mode , blend )
120+ for single_img in x
121+ ]
122+ )
120123
121124 if n_dim != 3 :
122125 raise ValueError ("Invalid array shape " + str (x .shape ))
@@ -128,17 +131,17 @@ def insert_image(
128131 width , height , num_channels = x .shape
129132
130133 no_color = num_channels == 1
131- orig_img = Image .new (' RGBA' , (width , height ), 0 )
132- backdoored_img = Image .new (' RGBA' , (width , height ), 0 )
134+ orig_img = Image .new (" RGBA" , (width , height ), 0 )
135+ backdoored_img = Image .new (" RGBA" , (width , height ), 0 )
133136
134137 if no_color :
135- backdoored_input = Image .fromarray ((data * 255 ).astype (' uint8' ).squeeze (axis = 2 ), mode = mode )
138+ backdoored_input = Image .fromarray ((data * 255 ).astype (" uint8" ).squeeze (axis = 2 ), mode = mode )
136139 else :
137- backdoored_input = Image .fromarray ((data * 255 ).astype (' uint8' ), mode = mode )
140+ backdoored_input = Image .fromarray ((data * 255 ).astype (" uint8" ), mode = mode )
138141
139142 orig_img .paste (backdoored_input )
140143
141- trigger = Image .open (backdoor_path ).convert (' RGBA' )
144+ trigger = Image .open (backdoor_path ).convert (" RGBA" )
142145 if size :
143146 trigger = trigger .resize (size )
144147
0 commit comments