<jupyter_start><jupyter_text>Simple Black-box Adversarial Attacks Paper link: https://arxiv.org/abs/1905.07121<jupyter_code>from bat.attacks import SimBA
from bat.apis.deepapi import VGG16Cifar10
import numpy as np
np.set_printoptions(suppress=True)

from PIL import Image<jupyter_output><empty_output><jupyter_text>Step 1: Load image and make predictions<jupyter_code># Load Image ranging from [0.0, 1.0]
x = np.asarray(Image.open("dog.jpg").resize((32, 32))) / 255.0
from matplotlib.pyplot import imshow
%matplotlib inline
imshow(np.asarray(x))
# Initialize the DeepAPI Model
model = VGG16Cifar10("https://api.wuhanstudio.uk" + "/vgg16_cifar10")
# Get Preditction
y_pred = model.predict(np.array([x]))[0]

# Print result
model.print(y_pred)
print()
print('Prediction', np.argmax(y_pred), model.get_class_name(np.argmax(y_pred)))<jupyter_output>airplane       0.00001
automobile     0.00001
bird           0.00003
cat            0.00002
deer           0.00003
dog            0.99964
frog           0.00001
horse          0.00024
ship           0.00000
truck          0.00001

Prediction 5 dog<jupyter_text>Step 2: Apply the SimpleBA Attack<jupyter_code>simba = SimBA(model)
x_adv = simba.attack(x, epsilon=0.1, max_it=1000, distributed=True, batch=50, max_workers=10)
imshow(np.asarray(x_adv))
# Get predictions after attack
y_pred = model.predict(np.array([x_adv]))[0]

# Print result
model.print(y_pred)
print()
print('Prediction', np.argmax(y_pred), model.get_class_name(np.argmax(y_pred)))<jupyter_output>airplane       0.00005
automobile     0.00003
bird           0.00036
cat            0.00115
deer           0.00060
dog            0.28446
frog           0.00003
horse          0.71330
ship           0.00001
truck          0.00002

Prediction 7 horse
{'fraction_non_alphanumeric': 0.11085844229675952, 'fraction_numerical': 0.09607731665719159, 'mean_word_length': 3.467005076142132, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '456169', 'license': 'MIT', 'n_tokens_mistral': 744, 'n_tokens_neox': 641, 'n_words': 135, 'path': 'notebooks/simple_ba_example.ipynb', 'repo_name': 'wuhanstudio/blackbox-adversarial-toolbox', 'size': '1759'}