# How to Simulate AutoAttack ## Introduction This notebook provides a beginner friendly introduction to using auto attack on image classification as part of Test & Evaluation of a small benchmark dataset (Visdrone). AutoAttack is an ensemble or a collection of other attacks or attack configuration, which can be run in parallel. In this notebook, we learn how to instantiate the attack and deduce which attack from all is the most successful. Testing an ensemble of attacks for best performance is a crucial step in T&E. :::: {grid} 5 :::{grid-item} **Intended Audience:** All T&E Users ::: :::{grid-item} **Requirements:** Basic Python and Torchvision / ML Skills ::: :::{grid-item} **Notebook Runtime:** Full run of the notebook: <2 minutes ::: :::{grid-item} **Reading time:** ~10 Minutes ::: :::{grid-item} **Order of Completion:** As in contents ::: :::: ::::{grid} 2 :::{grid-item} :columns: 8 Before you begin, you will want to make sure that you download the how-to guide's companion Jupyter notebook. This notebook allows you to follow along in your own environment and interact with the code as you learn. The code snippets are also included in the documentation, but the notebook is provided for ease of use and to enable you to try things on your own. ::: :::{grid-item} :child-align: center :columns: 4 ```{note} The [How to Simulate Auto Attacks for Image Classification Companion Notebook](https://github.com/IBM/heart-library/blob/main/notebooks/how_tos/image_classification/3_How_to_Simulate_Auto_Attack_for_Image_Classification.ipynb) can be downloaded via the HEART public GitHub. ``` ::: :::: ### Contents 1. Imports 1. Load Visdrone Classification Task Data and Model 1. AutoAttack Initialization 1. AutoAttack Evaluation 1. Calculate Clean and Robust Accuracy 1. Further Evaluation: Plot Samples, Best Attacks 1. Conclusion 1. Next Steps ### Learning Objectives - Autoattack bundles several attacks or parameters for a different attack - This allows easy evalution, as the best attack (configuration) can be found ## 1. Imports and Set-up We import all necessary libraries for this tutorial. In this order, we first import general libraries such as numpy, then load relevant methods from ART. We then load the corresponding HEART functionality and specific torch functions to support the model. Lastly, we use a command to plot within the notebook. ```python import numpy as np import os import torch from typing import Tuple, Dict, Any import matplotlib.pyplot as plt from datasets import load_dataset from torchvision import transforms import torchvision # ART imports from art.attacks.evasion.projected_gradient_descent.projected_gradient_descent_pytorch import ProjectedGradientDescentPyTorch from art.attacks.evasion.auto_attack import AutoAttack # HEART imports from heart_library.estimators.classification.pytorch import JaticPyTorchClassifier from heart_library.attacks.attack import JaticAttack from heart_library.metrics import AccuracyPerturbationMetric # MAITE import for evaluation from maite.protocols.image_classification import Dataset as ic_dataset # using matplotlib inline to see the figures %matplotlib inline ``` ## 2. Load Visdrone Data and Model for Classification We now load the data, importing only a small part to save compute for this small demonstration. We then define the model and wrap it as JATIC pytorch classifier. Here, we first define the Visdrone labels and then load Visdrone images as numpy arrays. We use a subset of the data to save runtime, a total of 15 samples. In addition, we load a dataset as a modified dataframe to be compatible with JATIC. ```python labels = { 0:'Building', 1:'Construction Site', 2:'Engineering Vehicle', 3:'Fishing Vessel', 4:'Oil Tanker', 5:'Vehicle Lot' } data = load_dataset("CDAO/xview-subset-classification", split="test[0:15]") idx = 3 plt.title(f"Prediction: {labels[data[idx]['label']]}") plt.imshow(data[idx]['image']) ''' Transform dataset ''' IMAGE_H, IMAGE_W = 224, 224 preprocess = transforms.Compose([ transforms.Resize((IMAGE_H, IMAGE_W)), transforms.ToTensor() ]) data = data.map(lambda x: {"image": preprocess(x["image"]), "label": x["label"]}) to_image = lambda x: transforms.ToPILImage()(torch.Tensor(x)) sample_data = torch.utils.data.Subset(data, range(5)) ``` ```text Resolving data files: 0%| | 0/31 [00:00