Spaces:
Paused
Paused
Expose PGD eps as a tunable evasion lever
Browse filesCo-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- Diffusion_chain_attack.py +4 -4
- app.py +7 -5
Diffusion_chain_attack.py
CHANGED
|
@@ -230,10 +230,10 @@ def diffattack(
|
|
| 230 |
)
|
| 231 |
|
| 232 |
# PGD Attack
|
| 233 |
-
attack = torchattacks.PGD(classifier, eps=
|
| 234 |
attack.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 235 |
|
| 236 |
-
# attack_supp2 = torchattacks.PGD(classifier_supp2, eps=
|
| 237 |
# attack_supp2.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 238 |
|
| 239 |
adv_images = attack(test_image, torch.tensor([0]).to(test_image.device))
|
|
@@ -392,8 +392,8 @@ def diffattack(
|
|
| 392 |
latents = diffusion_step(model, latents, context[ind].to(latents.device), t, guidance_scale)
|
| 393 |
|
| 394 |
if args.is_encoder == 1:
|
| 395 |
-
attack = torchattacks.PGD(classifier, eps=
|
| 396 |
-
attack_supp2 = torchattacks.PGD(classifier_supp2, eps=
|
| 397 |
attack.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 398 |
|
| 399 |
adv_images = attack(normalize(init_image * 0.5 + 0.5), torch.tensor([0]).to(init_image.device))
|
|
|
|
| 230 |
)
|
| 231 |
|
| 232 |
# PGD Attack
|
| 233 |
+
attack = torchattacks.PGD(classifier, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
|
| 234 |
attack.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 235 |
|
| 236 |
+
# attack_supp2 = torchattacks.PGD(classifier_supp2, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
|
| 237 |
# attack_supp2.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 238 |
|
| 239 |
adv_images = attack(test_image, torch.tensor([0]).to(test_image.device))
|
|
|
|
| 392 |
latents = diffusion_step(model, latents, context[ind].to(latents.device), t, guidance_scale)
|
| 393 |
|
| 394 |
if args.is_encoder == 1:
|
| 395 |
+
attack = torchattacks.PGD(classifier, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
|
| 396 |
+
attack_supp2 = torchattacks.PGD(classifier_supp2, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
|
| 397 |
attack.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 398 |
|
| 399 |
adv_images = attack(normalize(init_image * 0.5 + 0.5), torch.tensor([0]).to(init_image.device))
|
app.py
CHANGED
|
@@ -63,12 +63,13 @@ def _setup():
|
|
| 63 |
return _STATE
|
| 64 |
|
| 65 |
|
| 66 |
-
def _erase_224(image, st, diffusion_steps, start_step, iterations, is_encoder, save_dir):
|
| 67 |
"""Run the faithful ERASE pipeline at its native 224x224 and return the
|
| 68 |
224 uint8 result plus detector/quality metrics."""
|
| 69 |
args = types.SimpleNamespace(
|
| 70 |
dataset_name="ours_try",
|
| 71 |
is_encoder=int(is_encoder),
|
|
|
|
| 72 |
encoder_weights=os.path.join(CKPT_DIR, "ckpt_ori/Controlvae.pt"),
|
| 73 |
)
|
| 74 |
logger = Logger(name="space", log_path=os.path.join(save_dir, "run.log"))
|
|
@@ -84,7 +85,7 @@ def _erase_224(image, st, diffusion_steps, start_step, iterations, is_encoder, s
|
|
| 84 |
return np.asarray(adv, dtype=np.uint8), (a0, a1, a2, a3, psnr, ssim, secs)
|
| 85 |
|
| 86 |
|
| 87 |
-
def run(image, diffusion_steps, start_step, iterations, is_encoder, full_res, max_side):
|
| 88 |
if image is None:
|
| 89 |
return None, "Upload an image first."
|
| 90 |
st = _setup()
|
|
@@ -92,7 +93,7 @@ def run(image, diffusion_steps, start_step, iterations, is_encoder, full_res, ma
|
|
| 92 |
orig = image.convert("RGB")
|
| 93 |
|
| 94 |
adv224, (a0, a1, a2, a3, psnr, ssim, secs) = _erase_224(
|
| 95 |
-
orig, st, diffusion_steps, start_step, iterations, is_encoder, save_dir)
|
| 96 |
|
| 97 |
if not full_res:
|
| 98 |
out = Image.fromarray(adv224)
|
|
@@ -116,7 +117,7 @@ def run(image, diffusion_steps, start_step, iterations, is_encoder, full_res, ma
|
|
| 116 |
|
| 117 |
metrics = (f"PSNR: {psnr:.2f} dB\nSSIM: {ssim:.4f} (measured at 224, the method's native size)\n"
|
| 118 |
f"Still detected as fake by E/R/D/S: {int(a0)}/{int(a1)}/{int(a2)}/{int(a3)} "
|
| 119 |
-
f"(0 = evaded)\n{mode_note}\nTime: {secs:.1f}s")
|
| 120 |
return out, metrics
|
| 121 |
|
| 122 |
with gr.Blocks(title="ERASE (research)") as demo:
|
|
@@ -132,6 +133,7 @@ with gr.Blocks(title="ERASE (research)") as demo:
|
|
| 132 |
steps = gr.Slider(10, 50, value=20, step=1, label="Diffusion steps")
|
| 133 |
start = gr.Slider(1, 49, value=18, step=1, label="Start step (higher = smaller perturbation)")
|
| 134 |
iters = gr.Slider(1, 30, value=10, step=1, label="Iterations")
|
|
|
|
| 135 |
enc = gr.Checkbox(value=True, label="Decoupled frequency processing (is_encoder)")
|
| 136 |
full_res = gr.Checkbox(value=True, label="Full-resolution output (overlay ERASE change onto your original)")
|
| 137 |
max_side = gr.Slider(512, 4096, value=2048, step=64, label="Max output long side (px)")
|
|
@@ -139,7 +141,7 @@ with gr.Blocks(title="ERASE (research)") as demo:
|
|
| 139 |
with gr.Column():
|
| 140 |
out_img = gr.Image(label="Processed image")
|
| 141 |
out_txt = gr.Textbox(label="Metrics", lines=5)
|
| 142 |
-
btn.click(run, [inp, steps, start, iters, enc, full_res, max_side], [out_img, out_txt], api_name="run")
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
| 145 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False, show_error=True)
|
|
|
|
| 63 |
return _STATE
|
| 64 |
|
| 65 |
|
| 66 |
+
def _erase_224(image, st, diffusion_steps, start_step, iterations, is_encoder, eps_over255, save_dir):
|
| 67 |
"""Run the faithful ERASE pipeline at its native 224x224 and return the
|
| 68 |
224 uint8 result plus detector/quality metrics."""
|
| 69 |
args = types.SimpleNamespace(
|
| 70 |
dataset_name="ours_try",
|
| 71 |
is_encoder=int(is_encoder),
|
| 72 |
+
eps=float(eps_over255) / 255.0,
|
| 73 |
encoder_weights=os.path.join(CKPT_DIR, "ckpt_ori/Controlvae.pt"),
|
| 74 |
)
|
| 75 |
logger = Logger(name="space", log_path=os.path.join(save_dir, "run.log"))
|
|
|
|
| 85 |
return np.asarray(adv, dtype=np.uint8), (a0, a1, a2, a3, psnr, ssim, secs)
|
| 86 |
|
| 87 |
|
| 88 |
+
def run(image, diffusion_steps, start_step, iterations, is_encoder, eps_over255, full_res, max_side):
|
| 89 |
if image is None:
|
| 90 |
return None, "Upload an image first."
|
| 91 |
st = _setup()
|
|
|
|
| 93 |
orig = image.convert("RGB")
|
| 94 |
|
| 95 |
adv224, (a0, a1, a2, a3, psnr, ssim, secs) = _erase_224(
|
| 96 |
+
orig, st, diffusion_steps, start_step, iterations, is_encoder, eps_over255, save_dir)
|
| 97 |
|
| 98 |
if not full_res:
|
| 99 |
out = Image.fromarray(adv224)
|
|
|
|
| 117 |
|
| 118 |
metrics = (f"PSNR: {psnr:.2f} dB\nSSIM: {ssim:.4f} (measured at 224, the method's native size)\n"
|
| 119 |
f"Still detected as fake by E/R/D/S: {int(a0)}/{int(a1)}/{int(a2)}/{int(a3)} "
|
| 120 |
+
f"(0 = evaded) [eps={int(eps_over255)}/255]\n{mode_note}\nTime: {secs:.1f}s")
|
| 121 |
return out, metrics
|
| 122 |
|
| 123 |
with gr.Blocks(title="ERASE (research)") as demo:
|
|
|
|
| 133 |
steps = gr.Slider(10, 50, value=20, step=1, label="Diffusion steps")
|
| 134 |
start = gr.Slider(1, 49, value=18, step=1, label="Start step (higher = smaller perturbation)")
|
| 135 |
iters = gr.Slider(1, 30, value=10, step=1, label="Iterations")
|
| 136 |
+
eps_s = gr.Slider(1, 32, value=4, step=1, label="Attack budget eps (x/255; higher = stronger evasion, lower fidelity)")
|
| 137 |
enc = gr.Checkbox(value=True, label="Decoupled frequency processing (is_encoder)")
|
| 138 |
full_res = gr.Checkbox(value=True, label="Full-resolution output (overlay ERASE change onto your original)")
|
| 139 |
max_side = gr.Slider(512, 4096, value=2048, step=64, label="Max output long side (px)")
|
|
|
|
| 141 |
with gr.Column():
|
| 142 |
out_img = gr.Image(label="Processed image")
|
| 143 |
out_txt = gr.Textbox(label="Metrics", lines=5)
|
| 144 |
+
btn.click(run, [inp, steps, start, iters, enc, eps_s, full_res, max_side], [out_img, out_txt], api_name="run")
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False, show_error=True)
|