@@ -123,9 +123,9 @@ def extract_predictions(predictions_):
123
123
124
124
125
125
def plot_image_with_boxes (img , boxes , pred_cls ):
126
- text_size = 5
127
- text_th = 5
128
- rect_th = 6
126
+ text_size = 2
127
+ text_th = 2
128
+ rect_th = 2
129
129
130
130
for i in range (len (boxes )):
131
131
# Draw Rectangle with the coordinates
@@ -155,23 +155,22 @@ def plot_image_with_boxes(img, boxes, pred_cls):
155
155
def main ():
156
156
# Create ART object detector
157
157
frcnn = PyTorchFasterRCNN (
158
- clip_values = (0 , 255 ), attack_losses = ["loss_classifier" , "loss_box_reg" , "loss_objectness" , "loss_rpn_box_reg" ]
158
+ clip_values = (0 , 255 ),
159
+ channels_first = True ,
160
+ attack_losses = ["loss_classifier" , "loss_box_reg" , "loss_objectness" , "loss_rpn_box_reg" ],
159
161
)
160
162
161
163
# Load image 1
162
164
image_0 = cv2 .imread ("./10best-cars-group-cropped-1542126037.jpg" )
163
165
image_0 = cv2 .cvtColor (image_0 , cv2 .COLOR_BGR2RGB ) # Convert to RGB
164
- print ("image_0.shape:" , image_0 .shape )
165
166
166
167
# Load image 2
167
168
image_1 = cv2 .imread ("./banner-diverse-group-of-people-2.jpg" )
168
169
image_1 = cv2 .cvtColor (image_1 , cv2 .COLOR_BGR2RGB ) # Convert to RGB
169
170
image_1 = cv2 .resize (image_1 , dsize = (image_0 .shape [1 ], image_0 .shape [0 ]), interpolation = cv2 .INTER_CUBIC )
170
- print ("image_1.shape:" , image_1 .shape )
171
171
172
172
# Stack images
173
173
image = np .stack ([image_0 , image_1 ], axis = 0 ).astype (np .float32 )
174
- print ("image.shape:" , image .shape )
175
174
176
175
for i in range (image .shape [0 ]):
177
176
plt .axis ("off" )
@@ -180,7 +179,8 @@ def main():
180
179
plt .show ()
181
180
182
181
# Make prediction on benign samples
183
- predictions = frcnn .predict (x = image )
182
+ image_chw = np .transpose (image , (0 , 3 , 1 , 2 ))
183
+ predictions = frcnn .predict (x = image_chw )
184
184
185
185
for i in range (image .shape [0 ]):
186
186
print ("\n Predictions image {}:" .format (i ))
@@ -194,7 +194,8 @@ def main():
194
194
# Create and run attack
195
195
eps = 32
196
196
attack = ProjectedGradientDescent (estimator = frcnn , eps = eps , eps_step = 2 , max_iter = 10 )
197
- image_adv = attack .generate (x = image , y = None )
197
+ image_adv_chw = attack .generate (x = image_chw , y = None )
198
+ image_adv = np .transpose (image_adv_chw , (0 , 2 , 3 , 1 ))
198
199
199
200
print ("\n The attack budget eps is {}" .format (eps ))
200
201
print ("The resulting maximal difference in pixel values is {}." .format (np .amax (np .abs (image - image_adv ))))
@@ -205,7 +206,7 @@ def main():
205
206
plt .imshow (image_adv [i ].astype (np .uint8 ), interpolation = "nearest" )
206
207
plt .show ()
207
208
208
- predictions_adv = frcnn .predict (x = image_adv )
209
+ predictions_adv = frcnn .predict (x = image_adv_chw )
209
210
210
211
for i in range (image .shape [0 ]):
211
212
print ("\n Predictions adversarial image {}:" .format (i ))
0 commit comments