Skip to content

Commit a0c9336

Browse files
authored
Merge pull request #1566 from sechkova/classifier-blackbox-tesseract-nb
Add channel dimension to input in classifier_blackbox_tesseract
2 parents e8e835b + 0c41cb2 commit a0c9336

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

notebooks/classifier_blackbox_tesseract.ipynb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@
1919
"\n",
2020
"import numpy as np\n",
2121
"import imageio\n",
22-
"import visvis as vv\n",
2322
"from matplotlib import pyplot as plt\n",
2423
"from IPython.display import clear_output\n",
2524
"import os \n",
2625
"\n",
27-
"from art import config\n",
2826
"from art.estimators.classification import BlackBoxClassifier\n",
2927
"from art.defences.preprocessor import JpegCompression\n",
3028
"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"
3430
]
3531
},
3632
{
@@ -48,7 +44,11 @@
4844
"source": [
4945
"# read in images\n",
5046
"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)"
5252
]
5353
},
5454
{
@@ -65,7 +65,7 @@
6565
" \n",
6666
" for x_i in x:\n",
6767
" # 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",
6969
"\n",
7070
" # run tesseract\n",
7171
" status = os.system(\"tesseract tmp.png out\")\n",
@@ -129,7 +129,7 @@
129129
],
130130
"source": [
131131
"# this is the image we want to target\n",
132-
"plt.imshow(image_target)\n",
132+
"plt.imshow(image_target[:,:,0])\n",
133133
"plt.show()\n",
134134
"print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_target], dtype=np.float32)))])"
135135
]
@@ -161,7 +161,7 @@
161161
],
162162
"source": [
163163
"# this is the label we want to perturb to\n",
164-
"plt.imshow(image_init)\n",
164+
"plt.imshow(image_init[:,:,0])\n",
165165
"plt.show()\n",
166166
"print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_init], dtype=np.float32)))])"
167167
]
@@ -305,7 +305,7 @@
305305
" print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n",
306306
" np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n",
307307
" \"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",
309309
" plt.show(block=False)\n",
310310
"\n",
311311
" attack.max_iter = iter_step"
@@ -356,7 +356,7 @@
356356
],
357357
"source": [
358358
"# this is the image we want to target\n",
359-
"plt.imshow(image_target)\n",
359+
"plt.imshow(image_target[:,:,0])\n",
360360
"plt.show()\n",
361361
"print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_target], dtype=np.float32)))])"
362362
]
@@ -388,7 +388,7 @@
388388
],
389389
"source": [
390390
"# this is the label we want to perturb to\n",
391-
"plt.imshow(image_init)\n",
391+
"plt.imshow(image_init[:,:,0])\n",
392392
"plt.show()\n",
393393
"print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_init], dtype=np.float32)))])"
394394
]
@@ -532,7 +532,7 @@
532532
" print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n",
533533
" np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n",
534534
" \"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",
536536
" plt.show(block=False)\n",
537537
"\n",
538538
" attack.max_iter = iter_step"

0 commit comments

Comments
 (0)