Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,12 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
import timm
|
| 7 |
-
|
| 8 |
-
model
|
| 9 |
-
model.train()
|
| 10 |
|
| 11 |
import os
|
| 12 |
|
|
@@ -20,6 +21,24 @@ def print_bn():
|
|
| 20 |
bn_data.append(m.momentum)
|
| 21 |
return bn_data
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def greet(image):
|
| 24 |
# url = f'https://huggingface.co/spaces?p=1&sort=modified&search=GPT'
|
| 25 |
# html = request_url(url)
|
|
@@ -43,6 +62,8 @@ def greet(image):
|
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
iface.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
+
from badnet_m import BadNet
|
| 6 |
+
import torchvision.transforms as transforms
|
| 7 |
|
| 8 |
+
# import timm
|
| 9 |
+
# model = timm.create_model("hf_hub:nateraw/resnet18-random", pretrained=True)
|
| 10 |
+
# model.train()
|
|
|
|
| 11 |
|
| 12 |
import os
|
| 13 |
|
|
|
|
| 21 |
bn_data.append(m.momentum)
|
| 22 |
return bn_data
|
| 23 |
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
model = BadNet(10)
|
| 27 |
+
model.load_state_dict('./cifar10_clean.pth')
|
| 28 |
+
|
| 29 |
+
transform_nor = transforms.Compose([transforms.ToTensor(), transforms.Resize((32,32)), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))])
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def greet_backdoor(image):
|
| 33 |
+
if image is None:
|
| 34 |
+
model.load_state_dict('./cifar10_badnet.pth')
|
| 35 |
+
return 'changed'
|
| 36 |
+
else:
|
| 37 |
+
image = transform_nor(image).unsqueeze(0)
|
| 38 |
+
output = model(image).squeeze()
|
| 39 |
+
return 'classified: ' + torch.argmax(output)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
def greet(image):
|
| 43 |
# url = f'https://huggingface.co/spaces?p=1&sort=modified&search=GPT'
|
| 44 |
# html = request_url(url)
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
image = gr.inputs.Image(label="Upload a photo for classify", shape=(32,32))
|
| 68 |
+
iface = gr.Interface(fn=greet_backdoor, inputs=image, outputs="text")
|
| 69 |
iface.launch()
|