Spaces:
Sleeping
Sleeping
onehowon commited on
Commit ·
4e966fe
1
Parent(s): 001997d
requirement
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from torchvision import transforms, models
|
| 3 |
from art.attacks.evasion import FastGradientMethod
|
|
@@ -17,10 +18,7 @@ def load_model():
|
|
| 17 |
model.eval()
|
| 18 |
return model
|
| 19 |
|
| 20 |
-
def process_image(
|
| 21 |
-
input_image = inputs["inputs"]
|
| 22 |
-
eps_value = inputs.get("eps", 0.3)
|
| 23 |
-
|
| 24 |
model = load_model()
|
| 25 |
device = torch.device("cpu")
|
| 26 |
model = model.to(device)
|
|
@@ -42,8 +40,7 @@ def process_image(inputs: dict):
|
|
| 42 |
std=[0.229, 0.224, 0.225])
|
| 43 |
])
|
| 44 |
|
| 45 |
-
|
| 46 |
-
img_tensor = transform(img).unsqueeze(0).to(device)
|
| 47 |
|
| 48 |
attack = FastGradientMethod(estimator=classifier, eps=eps_value)
|
| 49 |
adv_img_tensor = attack.generate(x=img_tensor.cpu().numpy())
|
|
@@ -69,4 +66,14 @@ def process_image(inputs: dict):
|
|
| 69 |
bwm.embed(img_bytes)
|
| 70 |
result_image = base64.b64encode(img_bytes.getvalue()).decode('utf-8')
|
| 71 |
|
| 72 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from torchvision import transforms, models
|
| 4 |
from art.attacks.evasion import FastGradientMethod
|
|
|
|
| 18 |
model.eval()
|
| 19 |
return model
|
| 20 |
|
| 21 |
+
def process_image(input_image, eps_value=0.3):
|
|
|
|
|
|
|
|
|
|
| 22 |
model = load_model()
|
| 23 |
device = torch.device("cpu")
|
| 24 |
model = model.to(device)
|
|
|
|
| 40 |
std=[0.229, 0.224, 0.225])
|
| 41 |
])
|
| 42 |
|
| 43 |
+
img_tensor = transform(input_image).unsqueeze(0).to(device)
|
|
|
|
| 44 |
|
| 45 |
attack = FastGradientMethod(estimator=classifier, eps=eps_value)
|
| 46 |
adv_img_tensor = attack.generate(x=img_tensor.cpu().numpy())
|
|
|
|
| 66 |
bwm.embed(img_bytes)
|
| 67 |
result_image = base64.b64encode(img_bytes.getvalue()).decode('utf-8')
|
| 68 |
|
| 69 |
+
return result_image
|
| 70 |
+
|
| 71 |
+
# Gradio 인터페이스 정의
|
| 72 |
+
def inference(image, eps_value):
|
| 73 |
+
return process_image(image, eps_value)
|
| 74 |
+
|
| 75 |
+
gr.Interface(
|
| 76 |
+
fn=inference,
|
| 77 |
+
inputs=[gr.inputs.Image(type="pil"), gr.inputs.Slider(0.1, 1.0, step=0.1, default=0.3, label="Epsilon")],
|
| 78 |
+
outputs="image"
|
| 79 |
+
).launch()
|