|
19 | 19 | "\n", |
20 | 20 | "import numpy as np\n", |
21 | 21 | "import imageio\n", |
22 | | - "import visvis as vv\n", |
23 | 22 | "from matplotlib import pyplot as plt\n", |
24 | 23 | "from IPython.display import clear_output\n", |
25 | 24 | "import os \n", |
26 | 25 | "\n", |
27 | | - "from art import config\n", |
28 | 26 | "from art.estimators.classification import BlackBoxClassifier\n", |
29 | 27 | "from art.defences.preprocessor import JpegCompression\n", |
30 | 28 | "from art.attacks.evasion import HopSkipJump\n", |
31 | | - "from art.attacks.evasion import ZooAttack\n", |
32 | | - "from art.utils import to_categorical\n", |
33 | | - "from art.utils import load_dataset, get_file, compute_accuracy" |
| 29 | + "from art.utils import to_categorical" |
34 | 30 | ] |
35 | 31 | }, |
36 | 32 | { |
|
48 | 44 | "source": [ |
49 | 45 | "# read in images\n", |
50 | 46 | "image_target = imageio.imread(os.path.join(os.path.dirname(os.getcwd()), 'utils/data/tesseract', 'dissent.png'))\n", |
51 | | - "image_init = imageio.imread(os.path.join(os.path.dirname(os.getcwd()), 'utils/data/tesseract', 'assent.png'))" |
| 47 | + "image_init = imageio.imread(os.path.join(os.path.dirname(os.getcwd()), 'utils/data/tesseract', 'assent.png'))\n", |
| 48 | + "\n", |
| 49 | + "# add a channel dimension for jpeg compression\n", |
| 50 | + "image_target = np.expand_dims(image_target, axis=-1)\n", |
| 51 | + "image_init = np.expand_dims(image_init, axis=-1)" |
52 | 52 | ] |
53 | 53 | }, |
54 | 54 | { |
|
65 | 65 | " \n", |
66 | 66 | " for x_i in x:\n", |
67 | 67 | " # save image as intermediate png\n", |
68 | | - " imageio.imsave('tmp.png', x_i.astype(np.uint8))\n", |
| 68 | + " imageio.imsave('tmp.png', x_i[:,:,0].astype(np.uint8))\n", |
69 | 69 | "\n", |
70 | 70 | " # run tesseract\n", |
71 | 71 | " status = os.system(\"tesseract tmp.png out\")\n", |
|
129 | 129 | ], |
130 | 130 | "source": [ |
131 | 131 | "# this is the image we want to target\n", |
132 | | - "plt.imshow(image_target)\n", |
| 132 | + "plt.imshow(image_target[:,:,0])\n", |
133 | 133 | "plt.show()\n", |
134 | 134 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_target], dtype=np.float32)))])" |
135 | 135 | ] |
|
161 | 161 | ], |
162 | 162 | "source": [ |
163 | 163 | "# this is the label we want to perturb to\n", |
164 | | - "plt.imshow(image_init)\n", |
| 164 | + "plt.imshow(image_init[:,:,0])\n", |
165 | 165 | "plt.show()\n", |
166 | 166 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_init], dtype=np.float32)))])" |
167 | 167 | ] |
|
305 | 305 | " print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n", |
306 | 306 | " np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n", |
307 | 307 | " \"and Tesseract output %s.\" % label_dict[np.argmax(classifier.predict(x_adv)[0])])\n", |
308 | | - " plt.imshow(x_adv[0])\n", |
| 308 | + " plt.imshow(x_adv[0,:,:,0])\n", |
309 | 309 | " plt.show(block=False)\n", |
310 | 310 | "\n", |
311 | 311 | " attack.max_iter = iter_step" |
|
356 | 356 | ], |
357 | 357 | "source": [ |
358 | 358 | "# this is the image we want to target\n", |
359 | | - "plt.imshow(image_target)\n", |
| 359 | + "plt.imshow(image_target[:,:,0])\n", |
360 | 360 | "plt.show()\n", |
361 | 361 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_target], dtype=np.float32)))])" |
362 | 362 | ] |
|
388 | 388 | ], |
389 | 389 | "source": [ |
390 | 390 | "# this is the label we want to perturb to\n", |
391 | | - "plt.imshow(image_init)\n", |
| 391 | + "plt.imshow(image_init[:,:,0])\n", |
392 | 392 | "plt.show()\n", |
393 | 393 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_init], dtype=np.float32)))])" |
394 | 394 | ] |
|
532 | 532 | " print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n", |
533 | 533 | " np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n", |
534 | 534 | " \"and Tesseract output %s.\" % label_dict[np.argmax(classifier_def.predict(x_adv)[0])])\n", |
535 | | - " plt.imshow(x_adv[0])\n", |
| 535 | + " plt.imshow(x_adv[0,:,:,0])\n", |
536 | 536 | " plt.show(block=False)\n", |
537 | 537 | "\n", |
538 | 538 | " attack.max_iter = iter_step" |
|
0 commit comments