|
155 | 155 | "# Helper function to preprocess the image so that it can be inputted in MobileNetV2\n",
|
156 | 156 | "def preprocess(image):\n",
|
157 | 157 | " image = tf.cast(image, tf.float32)\n",
|
158 |
| - " image = image/255\n", |
159 | 158 | " image = tf.image.resize(image, (224, 224))\n",
|
| 159 | + " image = tf.keras.applications.mobilenet_v2.preprocess_input(image)\n", |
160 | 160 | " image = image[None, ...]\n",
|
161 | 161 | " return image\n",
|
162 | 162 | "\n",
|
|
215 | 215 | "outputs": [],
|
216 | 216 | "source": [
|
217 | 217 | "plt.figure()\n",
|
218 |
| - "plt.imshow(image[0])\n", |
| 218 | + "plt.imshow(image[0]*0.5+0.5) # To change [-1, 1] to [0,1]\n", |
219 | 219 | "_, image_class, class_confidence = get_imagenet_label(image_probs)\n",
|
220 | 220 | "plt.title('{} : {:.2f}% Confidence'.format(image_class, class_confidence*100))\n",
|
221 | 221 | "plt.show()"
|
|
285 | 285 | "label = tf.reshape(label, (1, image_probs.shape[-1]))\n",
|
286 | 286 | "\n",
|
287 | 287 | "perturbations = create_adversarial_pattern(image, label)\n",
|
288 |
| - "plt.imshow(perturbations[0])" |
| 288 | + "plt.imshow(perturbations[0]*0.5+0.5); # To change [-1, 1] to [0,1]" |
289 | 289 | ]
|
290 | 290 | },
|
291 | 291 | {
|
|
311 | 311 | "def display_images(image, description):\n",
|
312 | 312 | " _, label, confidence = get_imagenet_label(pretrained_model.predict(image))\n",
|
313 | 313 | " plt.figure()\n",
|
314 |
| - " plt.imshow(image[0])\n", |
| 314 | + " plt.imshow(image[0]*0.5+0.5)\n", |
315 | 315 | " plt.title('{} \\n {} : {:.2f}% Confidence'.format(description,\n",
|
316 | 316 | " label, confidence*100))\n",
|
317 | 317 | " plt.show()"
|
|
333 | 333 | "\n",
|
334 | 334 | "for i, eps in enumerate(epsilons):\n",
|
335 | 335 | " adv_x = image + eps*perturbations\n",
|
336 |
| - " adv_x = tf.clip_by_value(adv_x, 0, 1)\n", |
| 336 | + " adv_x = tf.clip_by_value(adv_x, -1, 1)\n", |
337 | 337 | " display_images(adv_x, descriptions[i])"
|
338 | 338 | ]
|
339 | 339 | },
|
|
0 commit comments