...

/

Generative Adversarial Examples: Working with Classifier

Generative Adversarial Examples: Working with Classifier

Practice generating adversarial examples and breaking some models using cats vs. dogs dataset.

Let's try generating adversarial examples with GANs and break some models.

Preparing an ensemble classifier for Kaggle’s cats vs. dogs

⚠️ The dataset is intended only for non-commercial research and educational use.

To make our demonstration more similar to practical scenarios, we will train a decent model on Kaggle’s cats vs. dogs datasethttps://www.kaggle.com/c/dogs-vs-cats, then break the model with adversarial examples generated by GAN. This dataset contains 25,000 training images and 12,500 testing images of either dogs or cats. Here, we will only use the 25,000 training images in our experiment.

For convenience, after downloading the dataset, put images of cats and dogs in separate folders so that the file structure looks like this:

/cats-dogs-kaggle
/cat
/cat.0.jpg
/cat.1.jpg
...
/dog
/dog.0.jpg
/dog.1.jpg
...
Dataset file structure

The model we are training on this dataset is formed of several pre-trained models provided by PyTorch Hubhttps://github.com/pytorch/hub. We will also need to perform transfer training on the pre-trained models to fit our dataset:

Press + to interact
Ensemble model for Kaggle's cats vs. dogs
Ensemble model for Kaggle's cats vs. dogs

Now, we need to load and preprocess the data, create an ensemble classifier, and train this model. Here are the detailed steps:

  1. Create a Python file named cats_dogs.py and import the Python modules:

Press + to interact
import argparse
import os
import random
import sys
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.utils.data
import torchvision
import torchvision.datasets as dset
import torchvision.utils as vutils
import utils
from advGAN import AdvGAN_Attack
from data_utils import data_prefetcher, _transforms_catsdogs
from model_ensemble import transfer_init, ModelEnsemble

Here, the custom module files, advGAN, data_utils, and model_ensemble, are discussed below.

  1. Define the main entry point in cats_ ...