188 lines
5.9 KiB
Python
188 lines
5.9 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
import torch.optim as optim
|
|
from torchvision import datasets, transforms
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import cv2
|
|
from mnist import Net
|
|
from pykuwahara import kuwahara
|
|
|
|
epsilons = np.arange(0.05,0.35,0.05)
|
|
pretrained_model = "mnist_cnn_unfiltered.pt"
|
|
use_cuda=False
|
|
|
|
torch.manual_seed(69)
|
|
|
|
|
|
test_loader = torch.utils.data.DataLoader(
|
|
datasets.MNIST('data/', train=False, download=True, transform=transforms.Compose([
|
|
transforms.ToTensor(),
|
|
transforms.Normalize((0.1307,), (0.3081,)),
|
|
])),
|
|
batch_size=1, shuffle=True)
|
|
|
|
print("CUDA Available: ", torch.cuda.is_available())
|
|
device = torch.device("cuda" if use_cuda and torch.cuda.is_available() else "cpu")
|
|
|
|
model = Net().to(device)
|
|
print(type(model))
|
|
|
|
model.load_state_dict(torch.load(pretrained_model, map_location=device))
|
|
|
|
model.eval()
|
|
|
|
def fgsm_attack(image, epsilon, data_grad):
|
|
# Collect the element-wise sign of the data gradient
|
|
sign_data_grad = data_grad.sign()
|
|
|
|
# Create the perturbed image by adjusting each pixel of the input image
|
|
perturbed_image = image + epsilon*sign_data_grad
|
|
|
|
# Adding clipping to maintain [0, 1] range
|
|
perturbed_image = torch.clamp(perturbed_image, 0, 1)
|
|
|
|
return perturbed_image
|
|
|
|
def denorm(batch, mean=[0.1307], std=[0.3081]):
|
|
"""
|
|
Convert a batch of tensors to their original scale.
|
|
|
|
Args:
|
|
batch (torch.Tensor): Batch of normalized tensors.
|
|
mean (torch.Tensor or list): Man used for normalization.
|
|
std (torch.Tensor or list): Standard deviation used for normalization.
|
|
|
|
Returns:
|
|
torch.Tensor: batch of tensors without normalization applied to them.
|
|
"""
|
|
|
|
if isinstance(mean, list):
|
|
mean = torch.tensor(mean).to(device)
|
|
if isinstance(std, list):
|
|
std = torch.tensor(std).to(device)
|
|
|
|
return batch * std.view(1, -1, 1, 1) + mean.view(1, -1, 1, 1)
|
|
|
|
def test(model, device, test_loader, epsilon):
|
|
# Original dataset correct classifications
|
|
orig_correct = 0
|
|
# Attacked dataset correct classifications
|
|
attacked_correct = 0
|
|
# Filtered attacked dataset correct classifications
|
|
filtered_correct = 0
|
|
|
|
adv_examples = []
|
|
|
|
for data, target in test_loader:
|
|
data, target = data.to(device), target.to(device)
|
|
data.requires_grad = True
|
|
|
|
output_orig = model(data)
|
|
orig_pred = output_orig.max(1, keepdim=True)[1]
|
|
|
|
# Calculate the loss
|
|
loss = F.nll_loss(output_orig, target)
|
|
|
|
# Zero all existing gradients
|
|
model.zero_grad()
|
|
|
|
# Calculate gradients of model in backward pass
|
|
loss.backward()
|
|
|
|
# Collect ''datagrad''
|
|
data_grad = data.grad.data
|
|
|
|
# Restore the data to its original scale
|
|
data_denorm = denorm(data)
|
|
|
|
# Apply the FGSM attack
|
|
perturbed_data = fgsm_attack(data_denorm, epsilon, data_grad)
|
|
|
|
# Reapply normalization
|
|
perturbed_data_normalized = transforms.Normalize((0.1307,), (0.3081,))(perturbed_data)
|
|
|
|
# Filter the attacked image
|
|
perturbed_data_filtered = filtered(perturbed_data_normalized, len(perturbed_data_normalized))
|
|
|
|
# evaluate the model on the attacked and filtered images
|
|
output_attacked = model(perturbed_data_normalized)
|
|
output_filtered = model(perturbed_data_filtered)
|
|
|
|
attacked_pred = output_attacked.max(1, keepdim=True)[1]
|
|
filtered_pred = output_filtered.max(1, keepdim=True)[1]
|
|
|
|
if orig_pred.item() == target.item():
|
|
orig_correct += 1
|
|
|
|
if attacked_pred.item() == target.item():
|
|
attacked_correct += 1
|
|
|
|
if epsilon == 0 and len(adv_examples) < 5:
|
|
adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
|
|
adv_examples.append( (orig_pred.item(), attacked_pred.item(), adv_ex) )
|
|
|
|
if filtered_pred.item() == target.item():
|
|
filtered_correct += 1
|
|
|
|
if epsilon == 0 and len(adv_examples) < 5:
|
|
adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
|
|
adv_examples.append( (orig_pred.item(), filtered_pred.item(), adv_ex) )
|
|
|
|
orig_acc = orig_correct/float(len(test_loader))
|
|
attacked_acc = attacked_correct/float(len(test_loader))
|
|
filtered_acc = filtered_correct/float(len(test_loader))
|
|
print(f"Epsilon: {epsilon}")
|
|
print(f"Original Accuracy = {orig_correct} / {len(test_loader)} = {orig_acc}")
|
|
print(f"Attacked Accuracy = {attacked_correct} / {len(test_loader)} = {attacked_acc}")
|
|
print(f"Filtered Accuracy = {filtered_correct} / {len(test_loader)} = {filtered_acc}")
|
|
print(f"Filtered:Attacked = {filtered_acc} / {attacked_acc} = {filtered_acc/attacked_acc}")
|
|
|
|
return attacked_acc, filtered_acc, adv_examples
|
|
|
|
|
|
def filtered(data, batch_size=64):
|
|
# Turn the tensor into an image
|
|
images = None
|
|
try:
|
|
images = data.numpy().transpose(0,2,3,1)
|
|
except RuntimeError:
|
|
images = data.detach().numpy().transpose(0,2,3,1)
|
|
|
|
# Apply the Kuwahara filter
|
|
filtered_images = np.ndarray((batch_size,28,28,1))
|
|
|
|
for i in range(batch_size):
|
|
filtered_images[i] = kuwahara(images[i], method='gaussian', radius=5, image_2d=images[i])
|
|
|
|
# Modify the data with the filtered image
|
|
filtered_images = filtered_images.transpose(0,3,1,2)
|
|
return torch.tensor(filtered_images).float()
|
|
|
|
attacked_accuracies = []
|
|
filtered_accuracies = []
|
|
ratios = []
|
|
examples = []
|
|
|
|
print(f"Model: {pretrained_model}")
|
|
|
|
for eps in epsilons:
|
|
aacc, facc, ex = test(model, device, test_loader, eps)
|
|
attacked_accuracies.append(aacc)
|
|
filtered_accuracies.append(facc)
|
|
ratios.append(facc/aacc)
|
|
examples.append(ex)
|
|
|
|
# Plot the results
|
|
plt.subplot(121)
|
|
plt.plot(epsilons, attacked_accuracies, label="Attacked Accuracy")
|
|
plt.plot(epsilons, filtered_accuracies, label="Filtered Accuracy")
|
|
plt.legend()
|
|
|
|
plt.subplot(122)
|
|
plt.plot(epsilons, ratios, label="Filtered:Attacked")
|
|
plt.legend()
|
|
plt.show()
|
|
|