|
48 | 48 | "source": [ |
49 | 49 | "# read in images\n", |
50 | 50 | "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'))" |
| 51 | + "image_init = imageio.imread(os.path.join(os.path.dirname(os.getcwd()), 'utils/data/tesseract', 'assent.png'))\n", |
| 52 | + "\n", |
| 53 | + "# add a channel dimension for jpeg compression\n", |
| 54 | + "image_target = np.expand_dims(image_target, axis=-1)\n", |
| 55 | + "image_init = np.expand_dims(image_init, axis=-1)" |
52 | 56 | ] |
53 | 57 | }, |
54 | 58 | { |
|
65 | 69 | " \n", |
66 | 70 | " for x_i in x:\n", |
67 | 71 | " # save image as intermediate png\n", |
68 | | - " imageio.imsave('tmp.png', x_i.astype(np.uint8))\n", |
| 72 | + " imageio.imsave('tmp.png', x_i[:,:,0].astype(np.uint8))\n", |
69 | 73 | "\n", |
70 | 74 | " # run tesseract\n", |
71 | 75 | " status = os.system(\"tesseract tmp.png out\")\n", |
|
129 | 133 | ], |
130 | 134 | "source": [ |
131 | 135 | "# this is the image we want to target\n", |
132 | | - "plt.imshow(image_target)\n", |
| 136 | + "plt.imshow(image_target[:,:,0])\n", |
133 | 137 | "plt.show()\n", |
134 | 138 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_target], dtype=np.float32)))])" |
135 | 139 | ] |
|
161 | 165 | ], |
162 | 166 | "source": [ |
163 | 167 | "# this is the label we want to perturb to\n", |
164 | | - "plt.imshow(image_init)\n", |
| 168 | + "plt.imshow(image_init[:,:,0])\n", |
165 | 169 | "plt.show()\n", |
166 | 170 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_init], dtype=np.float32)))])" |
167 | 171 | ] |
|
305 | 309 | " print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n", |
306 | 310 | " np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n", |
307 | 311 | " \"and Tesseract output %s.\" % label_dict[np.argmax(classifier.predict(x_adv)[0])])\n", |
308 | | - " plt.imshow(x_adv[0])\n", |
| 312 | + " plt.imshow(x_adv[0,:,:,0])\n", |
309 | 313 | " plt.show(block=False)\n", |
310 | 314 | "\n", |
311 | 315 | " attack.max_iter = iter_step" |
|
356 | 360 | ], |
357 | 361 | "source": [ |
358 | 362 | "# this is the image we want to target\n", |
359 | | - "plt.imshow(image_target)\n", |
| 363 | + "plt.imshow(image_target[:,:,0])\n", |
360 | 364 | "plt.show()\n", |
361 | 365 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_target], dtype=np.float32)))])" |
362 | 366 | ] |
|
388 | 392 | ], |
389 | 393 | "source": [ |
390 | 394 | "# this is the label we want to perturb to\n", |
391 | | - "plt.imshow(image_init)\n", |
| 395 | + "plt.imshow(image_init[:,:,0])\n", |
392 | 396 | "plt.show()\n", |
393 | 397 | "print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_init], dtype=np.float32)))])" |
394 | 398 | ] |
|
532 | 536 | " print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n", |
533 | 537 | " np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n", |
534 | 538 | " \"and Tesseract output %s.\" % label_dict[np.argmax(classifier_def.predict(x_adv)[0])])\n", |
535 | | - " plt.imshow(x_adv[0])\n", |
| 539 | + " plt.imshow(x_adv[0,:,:,0])\n", |
536 | 540 | " plt.show(block=False)\n", |
537 | 541 | "\n", |
538 | 542 | " attack.max_iter = iter_step" |
|
0 commit comments