Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- AdversarialStopSignDemo.ipynb +0 -0
- app.py +81 -0
- requirements.txt +5 -0
AdversarialStopSignDemo.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
import torchvision.transforms as transforms
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import requests
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# Load model
|
| 11 |
+
model = torchvision.models.resnet18(pretrained=True)
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
# Load ImageNet labels
|
| 15 |
+
LABELS_URL = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
|
| 16 |
+
response = requests.get(LABELS_URL)
|
| 17 |
+
labels = response.text.strip().split("\n")
|
| 18 |
+
|
| 19 |
+
# Preprocess image
|
| 20 |
+
transform = transforms.Compose([
|
| 21 |
+
transforms.Resize((224, 224)),
|
| 22 |
+
transforms.ToTensor(),
|
| 23 |
+
])
|
| 24 |
+
|
| 25 |
+
# Load and preprocess stop sign
|
| 26 |
+
img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Stop_sign_light_red.svg/768px-Stop_sign_light_red.svg.png?20211116183705"
|
| 27 |
+
image = Image.open(BytesIO(requests.get(img_url).content)).convert("RGB")
|
| 28 |
+
input_tensor = transform(image).unsqueeze(0)
|
| 29 |
+
|
| 30 |
+
# Original prediction
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
original_output = model(input_tensor)
|
| 33 |
+
original_label = labels[original_output.argmax().item()]
|
| 34 |
+
|
| 35 |
+
# Generate adversarial perturbation
|
| 36 |
+
input_tensor.requires_grad = True
|
| 37 |
+
output = model(input_tensor)
|
| 38 |
+
target = torch.tensor([400]) # Target class: "speedboat"
|
| 39 |
+
loss = F.cross_entropy(output, target)
|
| 40 |
+
loss.backward()
|
| 41 |
+
|
| 42 |
+
grad_sign = input_tensor.grad.sign().detach()
|
| 43 |
+
input_tensor_orig = input_tensor.detach().clone()
|
| 44 |
+
|
| 45 |
+
# Convert original image to PIL
|
| 46 |
+
original_image = transforms.ToPILImage()(input_tensor_orig.squeeze())
|
| 47 |
+
|
| 48 |
+
# Function to apply epsilon and get prediction
|
| 49 |
+
def apply_perturbation(epsilon):
|
| 50 |
+
perturbed = input_tensor_orig + epsilon * grad_sign
|
| 51 |
+
perturbed = torch.clamp(perturbed, 0, 1)
|
| 52 |
+
perturbed_image = transforms.ToPILImage()(perturbed.squeeze())
|
| 53 |
+
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
logits = model(perturbed)
|
| 56 |
+
predicted_label = labels[logits.argmax().item()]
|
| 57 |
+
|
| 58 |
+
return perturbed_image, predicted_label
|
| 59 |
+
|
| 60 |
+
# Gradio UI
|
| 61 |
+
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("# 🛑 Adversarial Stop Sign Attack")
|
| 63 |
+
gr.Markdown("Adjust the slider to change perturbation strength (ε) and see the model's prediction!")
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
gr.Image(value=original_image, label="Original Image")
|
| 67 |
+
gr.Label(value=original_label, label="Original Prediction")
|
| 68 |
+
|
| 69 |
+
epsilon = gr.Slider(0.0, 0.2, value=0.00, step=0.01, label="Perturbation Strength (ε)")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
perturbed_image = gr.Image(label="Perturbed Image", interactive=False)
|
| 73 |
+
perturbed_label = gr.Label(label="Adversarial Prediction")
|
| 74 |
+
|
| 75 |
+
epsilon.change(
|
| 76 |
+
fn=apply_perturbation,
|
| 77 |
+
inputs=epsilon,
|
| 78 |
+
outputs=[perturbed_image, perturbed_label]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
requests
|
| 4 |
+
Pillow
|
| 5 |
+
gradio
|