Spaces:
Runtime error
Runtime error
hadisalman
commited on
Commit
•
50b15cd
1
Parent(s):
21f18f8
Add demo
Browse files- .gitattributes +2 -0
- app.py +157 -0
- images/elon_1.jpg +3 -0
- images/elon_2.jpg +3 -0
- images/hadi_and_trevor.jpg +3 -0
- images/trevor_2.jpg +3 -0
- images/trevor_3.jpg +3 -0
- images/trevor_4.jpg +3 -0
- requirements.txt +6 -0
- utils.py +65 -0
.gitattributes
CHANGED
@@ -32,3 +32,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*jpg filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from io import BytesIO
|
2 |
+
import requests
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
import torch
|
6 |
+
from tqdm import tqdm
|
7 |
+
from PIL import Image, ImageOps
|
8 |
+
from diffusers import StableDiffusionInpaintPipeline
|
9 |
+
from torchvision.transforms import ToPILImage
|
10 |
+
from utils import preprocess, prepare_mask_and_masked_image, recover_image, resize_and_crop
|
11 |
+
|
12 |
+
gr.close_all()
|
13 |
+
topil = ToPILImage()
|
14 |
+
|
15 |
+
pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
|
16 |
+
"runwayml/stable-diffusion-inpainting",
|
17 |
+
revision="fp16",
|
18 |
+
torch_dtype=torch.float16,
|
19 |
+
safety_checker=None,
|
20 |
+
)
|
21 |
+
pipe_inpaint = pipe_inpaint.to("cuda")
|
22 |
+
|
23 |
+
## Good params for editing that we used all over the paper --> decent quality and speed
|
24 |
+
GUIDANCE_SCALE = 7.5
|
25 |
+
NUM_INFERENCE_STEPS = 100
|
26 |
+
|
27 |
+
def pgd(X, targets, model, criterion, eps=0.1, step_size=0.015, iters=40, clamp_min=0, clamp_max=1, mask=None):
|
28 |
+
X_adv = X.clone().detach() + (torch.rand(*X.shape)*2*eps-eps).cuda()
|
29 |
+
pbar = tqdm(range(iters))
|
30 |
+
for i in pbar:
|
31 |
+
actual_step_size = step_size - (step_size - step_size / 100) / iters * i
|
32 |
+
X_adv.requires_grad_(True)
|
33 |
+
|
34 |
+
loss = (model(X_adv).latent_dist.mean - targets).norm()
|
35 |
+
pbar.set_description(f"Loss {loss.item():.5f} | step size: {actual_step_size:.4}")
|
36 |
+
|
37 |
+
grad, = torch.autograd.grad(loss, [X_adv])
|
38 |
+
|
39 |
+
X_adv = X_adv - grad.detach().sign() * actual_step_size
|
40 |
+
X_adv = torch.minimum(torch.maximum(X_adv, X - eps), X + eps)
|
41 |
+
X_adv.data = torch.clamp(X_adv, min=clamp_min, max=clamp_max)
|
42 |
+
X_adv.grad = None
|
43 |
+
|
44 |
+
if mask is not None:
|
45 |
+
X_adv.data *= mask
|
46 |
+
|
47 |
+
return X_adv
|
48 |
+
|
49 |
+
def get_target():
|
50 |
+
target_url = 'https://www.rtings.com/images/test-materials/2015/204_Gray_Uniformity.png'
|
51 |
+
response = requests.get(target_url)
|
52 |
+
target_image = Image.open(BytesIO(response.content)).convert("RGB")
|
53 |
+
target_image = target_image.resize((512, 512))
|
54 |
+
return target_image
|
55 |
+
|
56 |
+
def immunize_fn(init_image, mask_image):
|
57 |
+
with torch.autocast('cuda'):
|
58 |
+
mask, X = prepare_mask_and_masked_image(init_image, mask_image)
|
59 |
+
X = X.half().cuda()
|
60 |
+
mask = mask.half().cuda()
|
61 |
+
|
62 |
+
targets = pipe_inpaint.vae.encode(preprocess(get_target()).half().cuda()).latent_dist.mean
|
63 |
+
|
64 |
+
adv_X = pgd(X,
|
65 |
+
targets = targets,
|
66 |
+
model=pipe_inpaint.vae.encode,
|
67 |
+
criterion=torch.nn.MSELoss(),
|
68 |
+
clamp_min=-1,
|
69 |
+
clamp_max=1,
|
70 |
+
eps=0.1,
|
71 |
+
step_size=0.01,
|
72 |
+
iters=200,
|
73 |
+
mask=1-mask
|
74 |
+
)
|
75 |
+
|
76 |
+
adv_X = (adv_X / 2 + 0.5).clamp(0, 1)
|
77 |
+
|
78 |
+
adv_image = topil(adv_X[0]).convert("RGB")
|
79 |
+
adv_image = recover_image(adv_image, init_image, mask_image, background=True)
|
80 |
+
return adv_image
|
81 |
+
|
82 |
+
def run(image, prompt, seed, immunize=False):
|
83 |
+
seed = int(seed)
|
84 |
+
torch.manual_seed(seed)
|
85 |
+
|
86 |
+
init_image = Image.fromarray(image['image'])
|
87 |
+
init_image = resize_and_crop(init_image, (512,512))
|
88 |
+
mask_image = ImageOps.invert(Image.fromarray(image['mask']).convert('RGB')).resize(init_image.size)
|
89 |
+
mask_image = resize_and_crop(mask_image, init_image.size)
|
90 |
+
|
91 |
+
if immunize:
|
92 |
+
immunized_image = immunize_fn(init_image, mask_image)
|
93 |
+
|
94 |
+
image_edited = pipe_inpaint(prompt=prompt,
|
95 |
+
image=init_image if not immunize else immunized_image,
|
96 |
+
mask_image=mask_image,
|
97 |
+
height = init_image.size[0],
|
98 |
+
width = init_image.size[1],
|
99 |
+
eta=1,
|
100 |
+
guidance_scale=GUIDANCE_SCALE,
|
101 |
+
num_inference_steps=NUM_INFERENCE_STEPS,
|
102 |
+
).images[0]
|
103 |
+
|
104 |
+
image_edited = recover_image(image_edited, init_image, mask_image)
|
105 |
+
|
106 |
+
if immunize:
|
107 |
+
return [(immunized_image, 'Immunized Image'), (image_edited, 'Edited After Immunization')]
|
108 |
+
else:
|
109 |
+
return [(image_edited, 'Edited Image')]
|
110 |
+
|
111 |
+
|
112 |
+
demo = gr.Interface(fn=run,
|
113 |
+
inputs=[
|
114 |
+
gr.ImageMask(label='Input Image'),
|
115 |
+
gr.Textbox(label='Prompt', placeholder='A photo of a man in a wedding'),
|
116 |
+
gr.Textbox(label='Seed', placeholder='1234', visible=True),
|
117 |
+
gr.Checkbox(label='Immunize'),
|
118 |
+
],
|
119 |
+
outputs=[gr.Gallery(
|
120 |
+
label="Generated images",
|
121 |
+
show_label=False,
|
122 |
+
elem_id="gallery").style(grid=[1,2], height="auto")],
|
123 |
+
examples=[
|
124 |
+
['./images/hadi_and_trevor.jpg', 'man attending a wedding', '329357'],
|
125 |
+
['./images/trevor_2.jpg', 'two men in prison', '329357'],
|
126 |
+
['./images/trevor_3.jpg', 'man in a private jet', '329357'],
|
127 |
+
['./images/elon_2.jpg', 'man in a metro station', '214213'],
|
128 |
+
],
|
129 |
+
examples_per_page=20,
|
130 |
+
allow_flagging='never',
|
131 |
+
title="Immunize your photos against manipulation by Stable Diffusion",
|
132 |
+
description='''<u>Official</u> demo of our paper: <br>
|
133 |
+
**Raising the Cost of Malicious AI-Powered Image Editing** <br>
|
134 |
+
*Hadi Salman\*, Alaa Khaddaj\*, Guillaume Leclerc\*, Andrew Ilyas, Aleksander Madry* <br>
|
135 |
+
[Paper](https://arxiv.org/abs/2302.06588)
|
136 |
+
[Blog post](https://gradientscience.org/photoguard/)
|
137 |
+
[![](https://badgen.net/badge/icon/GitHub?icon=github&label)](https://github.com/MadryLab/photoguard)
|
138 |
+
<br />
|
139 |
+
We present an approach to mitigating the risks of malicious image editing posed by large diffusion models. The key idea is to immunize images so as to make them resistant to manipulation by these models. This immunization relies on injection of imperceptible adversarial perturbations designed to disrupt the operation of the targeted diffusion models, forcing them to generate unrealistic images.
|
140 |
+
<br />
|
141 |
+
<br />
|
142 |
+
**Demo steps:**
|
143 |
+
+ Upload an image (or select from the below examples!)
|
144 |
+
+ Mask the parts of the image you want to maintain unedited (e.g., faces of people)
|
145 |
+
+ Add a prompt to edit the image accordingly (see examples below)
|
146 |
+
+ Play with the seed and click submit until you get a realistic edit that you are happy with (we have good seeds for you below)
|
147 |
+
|
148 |
+
Now let's immunize your image and try again!
|
149 |
+
+ Click on the "immunize" button, then submit.
|
150 |
+
+ You will get the immunized image (which looks identical to the original one) and the edited image, which is now hopefully unrealistic!
|
151 |
+
<br />
|
152 |
+
**This is a research project and is not production-ready.**
|
153 |
+
''',
|
154 |
+
)
|
155 |
+
|
156 |
+
demo.launch()
|
157 |
+
# demo.launch(server_name='0.0.0.0', share=False, server_port=7860, inline=False, )
|
images/elon_1.jpg
ADDED
Git LFS Details
|
images/elon_2.jpg
ADDED
Git LFS Details
|
images/hadi_and_trevor.jpg
ADDED
Git LFS Details
|
images/trevor_2.jpg
ADDED
Git LFS Details
|
images/trevor_3.jpg
ADDED
Git LFS Details
|
images/trevor_4.jpg
ADDED
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers==0.10.2
|
2 |
+
transformers
|
3 |
+
scipy
|
4 |
+
accelerate
|
5 |
+
torchvision
|
6 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
utils.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
from torchvision.transforms import ToPILImage, ToTensor
|
6 |
+
totensor = ToTensor()
|
7 |
+
topil = ToPILImage()
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def resize_and_crop(img, size, crop_type="center"):
|
12 |
+
'''Resize and crop the image to the given size.'''
|
13 |
+
if crop_type == "top":
|
14 |
+
center = (0, 0)
|
15 |
+
elif crop_type == "center":
|
16 |
+
center = (0.5, 0.5)
|
17 |
+
else:
|
18 |
+
raise ValueError
|
19 |
+
|
20 |
+
resize = list(size)
|
21 |
+
if size[0] is None:
|
22 |
+
resize[0] = img.size[0]
|
23 |
+
if size[1] is None:
|
24 |
+
resize[1] = img.size[1]
|
25 |
+
return ImageOps.fit(img, resize, centering=center)
|
26 |
+
|
27 |
+
|
28 |
+
def recover_image(image, init_image, mask, background=False):
|
29 |
+
image = totensor(image)
|
30 |
+
mask = totensor(mask)[0]
|
31 |
+
init_image = totensor(init_image)
|
32 |
+
|
33 |
+
if background:
|
34 |
+
result = mask * init_image + (1 - mask) * image
|
35 |
+
else:
|
36 |
+
result = mask * image + (1 - mask) * init_image
|
37 |
+
return topil(result)
|
38 |
+
|
39 |
+
|
40 |
+
def preprocess(image):
|
41 |
+
w, h = image.size
|
42 |
+
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
|
43 |
+
image = image.resize((w, h), resample=Image.LANCZOS)
|
44 |
+
image = np.array(image).astype(np.float32) / 255.0
|
45 |
+
image = image[None].transpose(0, 3, 1, 2)
|
46 |
+
image = torch.from_numpy(image)
|
47 |
+
return 2.0 * image - 1.0
|
48 |
+
|
49 |
+
|
50 |
+
def prepare_mask_and_masked_image(image, mask):
|
51 |
+
|
52 |
+
image = np.array(image.convert("RGB"))
|
53 |
+
image = image[None].transpose(0, 3, 1, 2)
|
54 |
+
image = torch.from_numpy(image).to(dtype=torch.float32) / 127.5 - 1.0
|
55 |
+
|
56 |
+
mask = np.array(mask.convert("L"))
|
57 |
+
mask = mask.astype(np.float32) / 255.0
|
58 |
+
mask = mask[None, None]
|
59 |
+
mask[mask < 0.5] = 0
|
60 |
+
mask[mask >= 0.5] = 1
|
61 |
+
mask = torch.from_numpy(mask)
|
62 |
+
|
63 |
+
masked_image = image * (mask < 0.5)
|
64 |
+
|
65 |
+
return mask, masked_image
|