Skip to content

Commit ccd9d4a

Browse files
committed
Fix classifier_blackbox_tesseract.ipynb
Add channel dimension to the input images to match the expected input shape of JpegCompression. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 3cf9b36 commit ccd9d4a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

notebooks/classifier_blackbox_tesseract.ipynb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@
4848
"source": [
4949
"# read in images\n",
5050
"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)"
5256
]
5357
},
5458
{
@@ -65,7 +69,7 @@
6569
" \n",
6670
" for x_i in x:\n",
6771
" # 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",
6973
"\n",
7074
" # run tesseract\n",
7175
" status = os.system(\"tesseract tmp.png out\")\n",
@@ -129,7 +133,7 @@
129133
],
130134
"source": [
131135
"# this is the image we want to target\n",
132-
"plt.imshow(image_target)\n",
136+
"plt.imshow(image_target[:,:,0])\n",
133137
"plt.show()\n",
134138
"print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_target], dtype=np.float32)))])"
135139
]
@@ -161,7 +165,7 @@
161165
],
162166
"source": [
163167
"# this is the label we want to perturb to\n",
164-
"plt.imshow(image_init)\n",
168+
"plt.imshow(image_init[:,:,0])\n",
165169
"plt.show()\n",
166170
"print('Tesseract output is: ' + label_dict[np.argmax(classifier.predict(np.array([image_init], dtype=np.float32)))])"
167171
]
@@ -305,7 +309,7 @@
305309
" print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n",
306310
" np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n",
307311
" \"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",
309313
" plt.show(block=False)\n",
310314
"\n",
311315
" attack.max_iter = iter_step"
@@ -356,7 +360,7 @@
356360
],
357361
"source": [
358362
"# this is the image we want to target\n",
359-
"plt.imshow(image_target)\n",
363+
"plt.imshow(image_target[:,:,0])\n",
360364
"plt.show()\n",
361365
"print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_target], dtype=np.float32)))])"
362366
]
@@ -388,7 +392,7 @@
388392
],
389393
"source": [
390394
"# this is the label we want to perturb to\n",
391-
"plt.imshow(image_init)\n",
395+
"plt.imshow(image_init[:,:,0])\n",
392396
"plt.show()\n",
393397
"print('Tesseract output is: ' + label_dict[np.argmax(classifier_def.predict(np.array([image_init], dtype=np.float32)))])"
394398
]
@@ -532,7 +536,7 @@
532536
" print(\"Adversarial image at step %d.\" % (i * iter_step), \"L2 error\", \n",
533537
" np.linalg.norm(np.reshape(x_adv[0] - image_target, [-1])),\n",
534538
" \"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",
536540
" plt.show(block=False)\n",
537541
"\n",
538542
" attack.max_iter = iter_step"

0 commit comments

Comments
 (0)