Spaces:
Sleeping
Sleeping
onehowon
commited on
Commit
·
05c1605
1
Parent(s):
3060a39
file uploaded
Browse files- app.py +72 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import transforms, models
|
3 |
+
from art.attacks.evasion import FastGradientMethod
|
4 |
+
from art.estimators.classification import PyTorchClassifier
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
import io
|
8 |
+
import base64
|
9 |
+
from blind_watermark import WaterMark
|
10 |
+
|
11 |
+
def load_model():
|
12 |
+
model = models.resnet50(pretrained=False)
|
13 |
+
num_ftrs = model.fc.in_features
|
14 |
+
model.fc = torch.nn.Linear(num_ftrs, 10)
|
15 |
+
|
16 |
+
model.load_state_dict(torch.load("model.pt", map_location=torch.device('cpu')))
|
17 |
+
model.eval()
|
18 |
+
return model
|
19 |
+
|
20 |
+
def process_image(inputs: dict):
|
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)
|
27 |
+
|
28 |
+
criterion = torch.nn.CrossEntropyLoss()
|
29 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
|
30 |
+
|
31 |
+
classifier = PyTorchClassifier(
|
32 |
+
model=model,
|
33 |
+
loss=criterion,
|
34 |
+
optimizer=optimizer,
|
35 |
+
input_shape=(3, 64, 64),
|
36 |
+
nb_classes=10,
|
37 |
+
)
|
38 |
+
|
39 |
+
transform = transforms.Compose([
|
40 |
+
transforms.ToTensor(),
|
41 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
42 |
+
std=[0.229, 0.224, 0.225])
|
43 |
+
])
|
44 |
+
|
45 |
+
img = Image.open(io.BytesIO(base64.b64decode(input_image))).convert('RGB')
|
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())
|
50 |
+
adv_img_tensor = torch.tensor(adv_img_tensor).to(device)
|
51 |
+
|
52 |
+
adv_img_np = adv_img_tensor.squeeze(0).cpu().numpy()
|
53 |
+
mean = np.array([0.485, 0.456, 0.406])
|
54 |
+
std = np.array([0.229, 0.224, 0.225])
|
55 |
+
adv_img_np = (adv_img_np * std[:, None, None]) + mean[:, None, None]
|
56 |
+
adv_img_np = np.clip(adv_img_np, 0, 1)
|
57 |
+
adv_img_np = adv_img_np.transpose(1, 2, 0)
|
58 |
+
|
59 |
+
adv_image_pil = Image.fromarray((adv_img_np * 255).astype(np.uint8))
|
60 |
+
|
61 |
+
wm_text = "123"
|
62 |
+
bwm = WaterMark(password_img=123, password_wm=456)
|
63 |
+
|
64 |
+
img_bytes = io.BytesIO()
|
65 |
+
adv_image_pil.save(img_bytes, format='PNG')
|
66 |
+
bwm.read_img(img_bytes)
|
67 |
+
bwm.read_wm(wm_text, mode='str')
|
68 |
+
|
69 |
+
bwm.embed(img_bytes)
|
70 |
+
result_image = base64.b64encode(img_bytes.getvalue()).decode('utf-8')
|
71 |
+
|
72 |
+
return {"image": result_image}
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
numpy
|
4 |
+
pillow
|
5 |
+
blind-watermark
|
6 |
+
art
|