Spaces:
Sleeping
Sleeping
Commit
·
ce3b424
1
Parent(s):
f6cf0c4
Update app.py
Browse files
app.py
CHANGED
@@ -27,12 +27,12 @@ pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
|
|
27 |
)
|
28 |
pipe_img2img = pipe_img2img.to("cuda")
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
def pgd(X, model, eps=0.1, step_size=0.015, iters=40, clamp_min=0, clamp_max=1, mask=None):
|
38 |
X_adv = X.clone().detach() + (torch.rand(*X.shape)*2*eps-eps).cuda()
|
@@ -95,77 +95,77 @@ def process_image_img2img(raw_image,prompt):
|
|
95 |
|
96 |
return [(init_image,"Source Image"), (adv_image, "Adv Image"), (image_nat,"Gen. Image Nat"), (image_adv, "Gen. Image Adv")]
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
#
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
|
129 |
-
|
130 |
|
131 |
-
|
132 |
-
|
133 |
|
134 |
-
#
|
135 |
-
|
136 |
|
137 |
-
#
|
138 |
-
#
|
139 |
|
140 |
-
|
141 |
-
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
|
168 |
-
|
169 |
|
170 |
|
171 |
examples_list = [["dog.png", "dog under heavy rain and muddy ground real"]]
|
@@ -185,11 +185,11 @@ with gr.Blocks() as demo:
|
|
185 |
input_prompt_img2img = gr.Textbox(label="Prompt")
|
186 |
run_btn_img2img = gr.Button('Run')
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
|
194 |
with gr.Row():
|
195 |
result_gallery = gr.Gallery(
|
@@ -198,7 +198,7 @@ with gr.Blocks() as demo:
|
|
198 |
|
199 |
run_btn_img2img.click(process_image_img2img, inputs = [input_image_img2img,input_prompt_img2img], outputs = [result_gallery])
|
200 |
examples = gr.Examples(examples=examples_list,inputs = [input_image_img2img,input_prompt_img2img], outputs = [result_gallery], cache_examples = True, fn = process_image_img2img)
|
201 |
-
|
202 |
|
203 |
|
204 |
demo.launch(debug=True)
|
|
|
27 |
)
|
28 |
pipe_img2img = pipe_img2img.to("cuda")
|
29 |
|
30 |
+
pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
|
31 |
+
"runwayml/stable-diffusion-inpainting",
|
32 |
+
revision="fp16",
|
33 |
+
torch_dtype=torch.float16,
|
34 |
+
)
|
35 |
+
pipe_inpaint = pipe_inpaint.to("cuda")
|
36 |
|
37 |
def pgd(X, model, eps=0.1, step_size=0.015, iters=40, clamp_min=0, clamp_max=1, mask=None):
|
38 |
X_adv = X.clone().detach() + (torch.rand(*X.shape)*2*eps-eps).cuda()
|
|
|
95 |
|
96 |
return [(init_image,"Source Image"), (adv_image, "Adv Image"), (image_nat,"Gen. Image Nat"), (image_adv, "Gen. Image Adv")]
|
97 |
|
98 |
+
def process_image_inpaint(raw_image,mask, prompt):
|
99 |
+
init_image = raw_image.convert('RGB').resize((512,512))
|
100 |
+
mask_image = mask.convert('RGB')
|
101 |
+
mask_image = ImageOps.invert(mask_image).resize((512,512))
|
102 |
+
|
103 |
+
# Attack using embedding of random image from internet
|
104 |
+
target_url = "https://bostonglobe-prod.cdn.arcpublishing.com/resizer/2-ZvyQ3aRNl_VNo7ja51BM5-Kpk=/960x0/cloudfront-us-east-1.images.arcpublishing.com/bostonglobe/CZOXE32LQQX5UNAB42AOA3SUY4.jpg"
|
105 |
+
response = requests.get(target_url)
|
106 |
+
target_image = Image.open(BytesIO(response.content)).convert("RGB")
|
107 |
+
target_image = target_image.resize((512, 512))
|
108 |
+
|
109 |
+
with torch.autocast('cuda'):
|
110 |
+
mask, X = prepare_mask_and_masked_image(init_image, mask_image)
|
111 |
+
X = X.half().cuda()
|
112 |
+
mask = mask.half().cuda()
|
113 |
|
114 |
+
# Here we attack towards the embedding of a random target image. You can also simply attack towards an embedding of zeros!
|
115 |
+
target = pipe_inpaint.vae.encode(preprocess(target_image).half().cuda()).latent_dist.mean
|
116 |
|
117 |
+
adv_X = pgd(X,
|
118 |
+
target = target,
|
119 |
+
model=pipe_inpaint.vae.encode,
|
120 |
+
criterion=torch.nn.MSELoss(),
|
121 |
+
clamp_min=-1,
|
122 |
+
clamp_max=1,
|
123 |
+
eps=0.06,
|
124 |
+
step_size=0.01,
|
125 |
+
iters=1000,
|
126 |
+
mask=1-mask
|
127 |
+
)
|
128 |
|
129 |
+
adv_X = (adv_X / 2 + 0.5).clamp(0, 1)
|
130 |
|
131 |
+
adv_image = to_pil(adv_X[0]).convert("RGB")
|
132 |
+
adv_image = recover_image(adv_image, init_image, mask_image, background=True)
|
133 |
|
134 |
+
# A good seed
|
135 |
+
SEED = 9209
|
136 |
|
137 |
+
# Uncomment the below to generated other images
|
138 |
+
# SEED = np.random.randint(low=0, high=100000)
|
139 |
|
140 |
+
torch.manual_seed(SEED)
|
141 |
+
print(SEED)
|
142 |
|
143 |
+
strength = 0.7
|
144 |
+
guidance_scale = 7.5
|
145 |
+
num_inference_steps = 100
|
146 |
|
147 |
+
image_nat = pipe_inpaint(prompt=prompt,
|
148 |
+
image=init_image,
|
149 |
+
mask_image=mask_image,
|
150 |
+
eta=1,
|
151 |
+
num_inference_steps=num_inference_steps,
|
152 |
+
guidance_scale=guidance_scale,
|
153 |
+
strength=strength
|
154 |
+
).images[0]
|
155 |
+
image_nat = recover_image(image_nat, init_image, mask_image)
|
156 |
|
157 |
+
torch.manual_seed(SEED)
|
158 |
+
image_adv = pipe_inpaint(prompt=prompt,
|
159 |
+
image=adv_image,
|
160 |
+
mask_image=mask_image,
|
161 |
+
eta=1,
|
162 |
+
num_inference_steps=num_inference_steps,
|
163 |
+
guidance_scale=guidance_scale,
|
164 |
+
strength=strength
|
165 |
+
).images[0]
|
166 |
+
image_adv = recover_image(image_adv, init_image, mask_image)
|
167 |
|
168 |
+
return [(init_image,"Source Image"), (adv_image, "Adv Image"), (image_nat,"Gen. Image Nat"), (image_adv, "Gen. Image Adv")]
|
169 |
|
170 |
|
171 |
examples_list = [["dog.png", "dog under heavy rain and muddy ground real"]]
|
|
|
185 |
input_prompt_img2img = gr.Textbox(label="Prompt")
|
186 |
run_btn_img2img = gr.Button('Run')
|
187 |
|
188 |
+
with gr.Tab("Simple Inpainting"):
|
189 |
+
input_image_inpaint = gr.Image(type="pil", label = "Source Image")
|
190 |
+
mask_image_inpaint = gr.Image(type="pil", label = "Mask")
|
191 |
+
input_prompt_inpaint = gr.Textbox(label="Prompt")
|
192 |
+
run_btn_inpaint = gr.Button('Run')
|
193 |
|
194 |
with gr.Row():
|
195 |
result_gallery = gr.Gallery(
|
|
|
198 |
|
199 |
run_btn_img2img.click(process_image_img2img, inputs = [input_image_img2img,input_prompt_img2img], outputs = [result_gallery])
|
200 |
examples = gr.Examples(examples=examples_list,inputs = [input_image_img2img,input_prompt_img2img], outputs = [result_gallery], cache_examples = True, fn = process_image_img2img)
|
201 |
+
run_btn_inpaint.click(process_image_inpaint, inputs = [input_image_inpaint,mask_image_inpaint,input_prompt_inpaint], outputs = [result_gallery])
|
202 |
|
203 |
|
204 |
demo.launch(debug=True)
|