How to do it...

Let's go ahead and implement this strategy in code (The code file is available as Adversarial_attack.ipynb in GitHub):

  1. Read the image of a cat:
import matplotlib.pyplot as plt%matplotlib inlineimg = cv2.imread('/content/cat.JPG')img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img = cv2.resize(img, (299,299))plt.imshow(img)plt.axis('off')

The plot of the image looks as follows:

  1. Preprocess the image so that it can then be passed to an inception network:
original_image = cv2.resize(img,(299,299)).astype(float)original_image /= 255.original_image -= 0.5original_image *= 2.original_image = np.expand_dims(original_image, axis=0)

Get Neural Networks with Keras Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.