Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one implement adversarial examples in pytorch?

I wanted to reproduce:

from the paper https://arxiv.org/pdf/1312.6199.pdf. I was wondering, how does one actually implement this in pytorch? My main confusion is that for loss_f I am using a torch.nn.CrossEntropy() criterion for example. Do i just need to change the code that I already have from:

loss = criterion(outputs+r, labels)
loss.backward()

to:

loss = criterion(outputs+r, labels)
loss = loss + c * r.norm(2)
loss.backward()

or something along those lines (of course include r in the optimizer!). I know its not quite right cuz I did not explicitly show how I implemented x+r or the hypercube constraint but those are parts that I still need to figure out.

I think for the moment I want to focus first without the hypercube constraint. If we assume I am ok with going out of that is the above correct? I just want to know if:

loss = loss + c * r.norm(2)

works as it should.


Now if we do include the hypercube constraint how does my solution change? Is that were the "penalty function method" come into place?


https://discuss.pytorch.org/t/how-does-one-implement-adversarial-examples-in-pytorch/15668

like image 474
Charlie Parker Avatar asked Apr 21 '18 01:04

Charlie Parker


1 Answers

This is how i did it. Hope that helps you.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 18 12:39:16 2016

@author: manojacharya
"""

import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision import models,transforms
import numpy as np
from scipy.misc import imread, imresize
import os
import matplotlib.pyplot as plt
import torch.nn.functional as F
import json



normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])

transform = transforms.Compose(
    [transforms.ToTensor(),
     normalize])


def imshow(inp, title=None):
    """Imshow for Tensor."""
    plt.figure()
    inp = inp.data[0]
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    plt.imshow(inp)
    plt.axis('off')
    if title is not None:
        plt.title(title)

with open('imagenet.json') as f:
    imagenet_labels = json.load(f)

# In[model]:

model = models.vgg16(pretrained=True)
for param in model.parameters():
    param.requires_grad = False


def predict(img):
    pred_raw = model(img)
    pred = F.softmax(pred_raw)
    _,indices = torch.topk(pred,k=1)
    for ind in indices.data.numpy().ravel():
        print ("%.2f%% , class: %s , %s" %(100*pred.data[0][ind],str(ind),imagenet_labels[ind]))


# In[image ]:

peppers = imread("dog.jpg")
img =  imresize(peppers,(224,224))
imgtensor = transform(img)
imgvar = Variable(imgtensor.unsqueeze(0),requires_grad=False)
imgvard = Variable(imgtensor.unsqueeze(0),requires_grad=True)
optimizer = torch.optim.Adam([imgvard], lr = 0.1)
loss_fn =  nn.CrossEntropyLoss() 

label  =  torch.LongTensor(1)
#classify the object as this label
label[0] = 80
label = Variable(label)
eps = 2/255.0

#%% 
Nepochs = 50
print ("Starting ...........",predict(imgvar))

for epoch in range(Nepochs):     
    optimizer.zero_grad()
    pred_raw = model(imgvard)
    loss  =  loss_fn(pred_raw,label)

    diff = imgvard.data - imgvar.data
    imgvard.data = torch.clamp(torch.abs(diff),max=eps) + imgvar.data

    loss.backward()
    optimizer.step()

    print('epoch: {}/{}, loss: {}'.format(
                epoch + 1,Nepochs, loss.data[0]))
    predict(imgvard)
print('Finished Training')

#%%
imshow(imgvard)


#%%
plt.figure()
diffimg = diff[0].numpy()
diffimg = diffimg.transpose((1,2,0))
plt.imshow(diffimg)
like image 195
macharya Avatar answered Sep 25 '22 00:09

macharya