Spaces:
Paused
Paused
File size: 20,439 Bytes
0db62a0 dc4fefe 0db62a0 dc4fefe 0db62a0 333aee3 0db62a0 8b798c5 333aee3 653b40d 333aee3 99dfbad 653b40d 333aee3 bbbf729 0db62a0 333aee3 3cc4a81 99dfbad ed75beb 653b40d ed75beb 99dfbad 3cc4a81 8b798c5 3cc4a81 bbbf729 3cc4a81 333aee3 0db62a0 dc4fefe 0db62a0 bbbf729 0db62a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | from erase_device import DEVICE, ROOT
import os
import numpy as np
import torch.nn as nn
import torch
from torchvision import transforms
from PIL import Image
from tqdm import tqdm
from torch import optim
from utils import view_images
from distances import LpDistance
import cv2
import math
from Frequency_Domain_Processing_low import NewEncoder, NewDecoder
import torchattacks
import torch.nn.functional as F
import random
import pywt
import time
def calculate_psnr(img1, img2, border=0):
if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.')
h, w = img1.shape[:2]
img1 = img1[border:h - border, border:w - border]
img2 = img2[border:h - border, border:w - border]
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return float('inf')
return 20 * math.log10(255.0 / math.sqrt(mse))
def ssim(img1, img2):
C1 = (0.01 * 255) ** 2
C2 = (0.03 * 255) ** 2
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
kernel = cv2.getGaussianKernel(11, 1.5)
window = np.outer(kernel, kernel.transpose())
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5]
mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
mu1_sq = mu1 ** 2
mu2_sq = mu2 ** 2
mu1_mu2 = mu1 * mu2
sigma1_sq = cv2.filter2D(img1 ** 2, -1, window)[5:-5, 5:-5] - mu1_sq
sigma2_sq = cv2.filter2D(img2 ** 2, -1, window)[5:-5, 5:-5] - mu2_sq
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
(sigma1_sq + sigma2_sq + C2))
return ssim_map.mean()
def calculate_ssim(img1, img2, border=0):
if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.')
h, w = img1.shape[:2]
img1 = img1[border:h - border, border:w - border]
img2 = img2[border:h - border, border:w - border]
if img1.ndim == 2:
return ssim(img1, img2)
elif img1.ndim == 3:
if img1.shape[2] == 3:
ssims = []
for i in range(3):
ssims.append(ssim(img1[:, :, i], img2[:, :, i]))
return np.array(ssims).mean()
elif img1.shape[2] == 1:
return ssim(np.squeeze(img1), np.squeeze(img2))
else:
raise ValueError('Wrong input image dimensions.')
def preprocess(image, res=512, device=None):
image = image.resize((res, res), resample=Image.LANCZOS)
image = np.array(image).astype(np.float32) / 255.0
image = image[None].transpose(0, 3, 1, 2)
image = torch.from_numpy(image)[:, :3, :, :]
if device is not None:
image = image.to(device)
else:
image = image.to(DEVICE)
return 2.0 * image - 1.0
def encoder(image, model, res=512):
device = next(model.vae.parameters()).device
image = preprocess(image, res, device=device)
generator = torch.Generator(device=device).manual_seed(8888)
return 0.18215 * model.vae.encode(image).latent_dist.sample(generator=generator)
@torch.no_grad()
def ddim_reverse_sample(image, prompt, model, num_inference_steps=20, guidance_scale=2.5, res=512):
"""DDIM Inversion"""
batch_size = 1
max_length = 77
uncond_input = model.tokenizer(
[""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
)
uncond_embeddings = model.text_encoder(uncond_input.input_ids.to(model.device))[0]
text_input = model.tokenizer(
prompt[0],
padding="max_length",
max_length=model.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
text_embeddings = model.text_encoder(text_input.input_ids.to(model.device))[0]
context = [uncond_embeddings, text_embeddings]
context = torch.cat(context)
model.scheduler.set_timesteps(num_inference_steps)
latents = encoder(image, model, res=res)
timesteps = model.scheduler.timesteps.flip(0)
all_latents = [latents]
skip_prob = 0.1
noise_prob = 0.1
noise_strength = 0.05
count_i = 0
for t in tqdm(timesteps[:-1], desc="DDIM_inverse"):
count_i += 1
if random.random() < skip_prob:
all_latents.append(latents.clone())
continue
latents_input = torch.cat([latents] * 2)
noise_pred = model.unet(latents_input, t, encoder_hidden_states=context)["sample"]
noise_pred_uncond, noise_prediction_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_prediction_text - noise_pred_uncond)
next_timestep = t + model.scheduler.config.num_train_timesteps // model.scheduler.num_inference_steps
alpha_bar_next = model.scheduler.alphas_cumprod[next_timestep] \
if next_timestep <= model.scheduler.config.num_train_timesteps else torch.tensor(0.0)
reverse_x0 = (1 / torch.sqrt(model.scheduler.alphas_cumprod[t]) * (
latents - noise_pred * torch.sqrt(1 - model.scheduler.alphas_cumprod[t])))
latents = reverse_x0 * torch.sqrt(alpha_bar_next) + torch.sqrt(1 - alpha_bar_next) * noise_pred
if count_i < 18 and random.random() < noise_prob:
noise = torch.randn_like(latents) * noise_strength
latents = latents + noise
all_latents.append(latents)
return latents, all_latents
def init_latent(latent, model, height, width, batch_size):
latents = latent.expand(batch_size, model.unet.in_channels, height // 8, width // 8).to(model.device)
return latent, latents
def diffusion_step(model, latents, context, t, guidance_scale):
latents_input = torch.cat([latents] * 2)
noise_pred = model.unet(latents_input, t, encoder_hidden_states=context)["sample"]
noise_pred_uncond, noise_prediction_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_prediction_text - noise_pred_uncond)
latents = model.scheduler.step(noise_pred, t, latents)["prev_sample"]
return latents
@torch.enable_grad()
def diffattack(
model,
label,
num_inference_steps=20,
guidance_scale=2.5,
image=None,
compare=None,
model_name=["E", "R", "D", "S"],
save_path="",
res=224,
start_step=15,
classes=None,
iterations=30,
verbose=True,
topN=1,
logger=None,
args=None,
mode=None,
idx=0,
adm=None,
):
start_time = time.time()
if args.dataset_name == "ours_try":
from dataset_caption import ours_label as imagenet_label
else:
raise NotImplementedError
label = torch.from_numpy(label).long().to(DEVICE)
classifier, classifier_supp, classifier_supp1, classifier_supp2 = classes
classifier = classifier.eval().to(DEVICE)
classifier_supp = classifier_supp.eval().to(DEVICE)
classifier_supp1 = classifier_supp1.eval().to(DEVICE)
classifier_supp2 = classifier_supp2.eval().to(DEVICE)
height = width = res
test_image = image.resize((height, height), resample=Image.LANCZOS)
test_image = np.float32(test_image) / 255.0
test_image = test_image[:, :, :3]
test_image[:, :, ] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
test_image[:, :, ] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))
test_image = test_image.transpose((2, 0, 1))
test_image = torch.from_numpy(test_image).unsqueeze(0).to(DEVICE)
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize = transforms.Normalize(mean=mean, std=std)
denormalize = transforms.Normalize(
mean=[-m / s for m, s in zip(mean, std)],
std=[1 / s for s in std]
)
# PGD Attack
attack = torchattacks.PGD(classifier, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
attack.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# attack_supp2 = torchattacks.PGD(classifier_supp2, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
# attack_supp2.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
adv_images = attack(test_image, torch.tensor([0]).to(test_image.device))
imgg = np.uint8(denormalize(adv_images).detach().cpu().numpy()[0].transpose((1, 2, 0)) * 255.)
image = Image.fromarray(imgg)
pred = classifier_supp(test_image)
pred1 = classifier_supp(test_image)
pred2 = classifier_supp1(test_image)
pred3 = classifier_supp2(test_image)
del test_image
_, pred_labels = pred.topk(1, largest=True, sorted=True)
target_prompt = " ".join([imagenet_label.refined_Label[label.item()] for i in range(1, 1)])
prompt = [""] * 2
# DDIM Inversion
latent, inversion_latents = ddim_reverse_sample(image, prompt, model, num_inference_steps, 0, res=height)
inversion_latents = inversion_latents[::-1]
init_prompt = [""]
batch_size = len(init_prompt)
latent = inversion_latents[start_step - 1]
max_length = 77
uncond_input = model.tokenizer(
[""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
)
model.text_encoder = model.text_encoder.to(DEVICE)
uncond_embeddings = model.text_encoder(uncond_input.input_ids.to(model.text_encoder.device))[0]
text_input = model.tokenizer(
init_prompt,
padding="max_length",
max_length=model.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
all_uncond_emb = []
latent, latents = init_latent(latent, model, height, width, batch_size)
uncond_embeddings.requires_grad_(True)
for ind, t in enumerate(tqdm(model.scheduler.timesteps[1 + start_step - 1:], desc="Optimize_uncond_embed")):
all_uncond_emb.append(uncond_embeddings.detach().clone())
# Latents Attack
batch_size = len(prompt)
text_input = model.tokenizer(
prompt,
padding="max_length",
max_length=model.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
text_embeddings = model.text_encoder(text_input.input_ids.to(model.text_encoder.device))[0]
context = [[torch.cat([all_uncond_emb[i]] * batch_size), text_embeddings] for i in range(len(all_uncond_emb))]
context = [torch.cat(i) for i in context]
original_latent = latent.clone()
latent.requires_grad_(True)
optimizer = optim.AdamW([latent], lr=getattr(args, "lr", 1e-2))
cross_entro = torch.nn.CrossEntropyLoss()
init_image = preprocess(image, res, device=DEVICE)
init_image_compare = preprocess(compare, res, device=DEVICE)
# Load encoder parameters
params = {
"act_fn": "silu",
"block_out_channels": [128, 256, 512, 512],
"down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D"],
"in_channels": 3,
"latent_channels": 4,
"layers_per_block": 2,
"norm_num_groups": 32,
"out_channels": 3,
"sample_size": 512,
"up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"]
}
params_encoder = {
"in_channels": params["in_channels"],
"out_channels": params["latent_channels"],
"down_block_types": params["down_block_types"],
"block_out_channels": params["block_out_channels"],
"layers_per_block": params["layers_per_block"],
"act_fn": params["act_fn"],
"norm_num_groups": params["norm_num_groups"],
"double_z": True,
}
params_decoder = {
"in_channels": params["latent_channels"],
"out_channels": params["out_channels"],
"up_block_types": params["up_block_types"],
"block_out_channels": params["block_out_channels"],
"layers_per_block": params["layers_per_block"],
"act_fn": params["act_fn"],
"norm_num_groups": params["norm_num_groups"],
}
pbar = tqdm(range(iterations), desc="Iterations")
encoder_freq = NewEncoder(**params_encoder).to(DEVICE)
f = args.encoder_weights
state_dict_freq = torch.load(f, map_location="cpu")
encoder_freq.load_state_dict(state_dict_freq["encoder"], strict=False)
decoder_freq = NewDecoder(**params_decoder).to(DEVICE)
decoder_freq.load_state_dict(model.vae.decoder.state_dict(), strict=False)
encoder_freq.requires_grad_(False)
decoder_freq.requires_grad_(False)
# Only `latent` is optimized; detach constants so each iteration builds a
# fresh graph (prevents 'backward through the graph a second time').
context = [cc.detach() for cc in context]
init_image = init_image.detach()
model.vae = model.vae.to(DEVICE)
# ---- Deeper rebuild: optimize the latent through the ACTUAL scored path ----
# The scored image is decoder_freq(post_quant_conv(1/0.1825*latents), down_feats),
# NOT vae.decode(latents). down_feats (high-frequency skip features) are fixed
# w.r.t. the latent, so precompute them once; then each iteration pushes the
# latent so the real output fools ALL FOUR detectors (collaborative evasion).
import torch.nn as _nn
all_classifiers = [classifier, classifier_supp, classifier_supp1, classifier_supp2]
class _Ensemble(_nn.Module):
def __init__(self, clfs):
super().__init__(); self.clfs = clfs
def forward(self, x):
return sum(c(x) for c in self.clfs) / len(self.clfs)
# High-frequency path: attack the skip features against the FLIPPABLE detectors
# (R, D, S); EfficientNet is pathologically confident here, so attacking it wastes budget.
_pgd = torchattacks.PGD(_Ensemble([classifier_supp, classifier_supp1, classifier_supp2]),
eps=args.eps, alpha=max(args.eps / 4, 1.0 / 255), steps=10, random_start=False)
_pgd.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
_adv = _pgd(normalize(init_image * 0.5 + 0.5), torch.tensor([0]).to(init_image.device))
with torch.no_grad():
_init1 = (2. * denormalize(_adv) - 1).detach()
down_features_opt = encoder_freq(_init1)[1]
_beta = getattr(args, "fid_weight", 0.5)
for i, _ in enumerate(pbar):
if mode == "Generate":
break
latents = torch.cat([original_latent, latent])
for ind, t in enumerate(model.scheduler.timesteps[1 + start_step - 1:]):
latents = diffusion_step(model, latents, context[ind].to(latents.device), t, guidance_scale)
decode_latent_freq = model.vae.post_quant_conv((1 / 0.1825 * latents).to(DEVICE))
out_raw = decoder_freq(decode_latent_freq, down_features_opt)[1:]
out_image = normalize((out_raw / 2 + 0.5).clamp(0, 1))
optimizer.zero_grad()
loss_atk = 0.
# Drop E (EfficientNet is pathologically confident here, immovable) so all the
# gradient serves the reachable detectors; weight the harder R more than D/S.
for clf, w in ((classifier_supp, 2.0), (classifier_supp1, 1.0), (classifier_supp2, 1.5)):
fake = clf(out_image)[:, 1]
loss_atk = loss_atk + w * _nn.functional.softplus(torch.clamp(fake, max=12.0)).mean()
loss_fid = _beta * ((latent - original_latent) ** 2).mean()
loss = args.attack_loss_weight * loss_atk + loss_fid
loss.backward()
optimizer.step()
if torch.cuda.is_available():
torch.cuda.empty_cache()
try:
pbar.set_postfix(atk=float(loss_atk.detach().cpu()))
except Exception:
pass
# ---------------------------------------------------------------------------
init_image = init_image.to(DEVICE)
with torch.no_grad():
latents = torch.cat([original_latent, latent])
for ind, t in enumerate(model.scheduler.timesteps[1 + start_step - 1:]):
latents = diffusion_step(model, latents, context[ind].to(latents.device), t, guidance_scale)
if args.is_encoder == 1:
attack = torchattacks.PGD(classifier, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
attack_supp2 = torchattacks.PGD(classifier_supp2, eps=args.eps, alpha=max(args.eps/4, 1.0/255), steps=10, random_start=False)
attack.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
adv_images = attack(normalize(init_image * 0.5 + 0.5), torch.tensor([0]).to(init_image.device))
if adm is not None:
x_diff = adm.sdedit(denormalize(adv_images), 1).detach()
x_diff = torch.clamp(x_diff, 0, 1)
adv_images = normalize(x_diff)
init_image1 = 2. * denormalize(adv_images) - 1
print(f"Using frequency encoder -> CLIP encoder cascade")
down_features_freq = encoder_freq(init_image1)[1]
decode_latent_freq = model.vae.post_quant_conv((1 / 0.1825 * latents).to(DEVICE))
out_image = decoder_freq(decode_latent_freq, down_features_freq)[1:]
else:
decoder = None
down_features = None
out_image = model.vae.decode(1 / 0.18215 * latents.to(DEVICE).detach())['sample'][1:] * init_mask + (
1 - init_mask) * init_image.to(DEVICE)
out_image = (out_image / 2 + 0.5).clamp(0, 1)
out_image = normalize(out_image)
imgg = np.uint8(denormalize(out_image).detach().cpu().numpy()[0].transpose((1, 2, 0)) * 255.)
# Wavelet high-frequency perturbation
print("Applying wavelet high-frequency perturbation...")
image_float = imgg.astype(np.float32)
channels = []
for i in range(3):
coeffs = pywt.dwt2(image_float[..., i], 'haar')
cA, (cH, cV, cD) = coeffs
cD += 0.05 * np.random.randn(*cD.shape)
noisy_channel = pywt.idwt2((cA, (cH, cV, cD)), 'haar')
noisy_channel = np.clip(noisy_channel, 0, 255)
channels.append(noisy_channel)
noisy_image = np.stack(channels, axis=-1).astype(np.uint8)
# image_float = imgg.astype(np.float32)
# coeffs = pywt.dwt2(image_float, 'haar')
# cA, (cH, cV, cD) = coeffs
# cD += 0.1 * np.random.randn(*cD.shape)
# noisy_image = pywt.idwt2((cA, (cH, cV, cD)), 'haar')
# noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
classifier_supp = classifier_supp.to(out_image.device)
classifier_supp1 = classifier_supp1.to(out_image.device)
classifier_supp2 = classifier_supp2.to(out_image.device)
pred = classifier(out_image).to(label.device)
pred_label = torch.argmax(pred, 1).detach()
pred_accuracy = (torch.argmax(pred, 1).detach() == label).sum().item() / len(label)
label = label.to(out_image.device)
pred1 = classifier_supp(out_image)
pred_label1 = torch.argmax(pred1, 1).detach()
pred_accuracy1 = (torch.argmax(pred1, 1).detach() == label).sum().item() / len(label)
pred2 = classifier_supp1(out_image)
pred_label2 = torch.argmax(pred2, 1).detach()
pred_accuracy2 = (torch.argmax(pred2, 1).detach() == label).sum().item() / len(label)
pred3 = classifier_supp2(out_image)
pred_label3 = torch.argmax(pred3, 1).detach()
pred_accuracy3 = (torch.argmax(pred3, 1).detach() == label).sum().item() / len(label)
try:
print("DETECTOR_LOGITS(fake; <0 => evaded) E/R/D/S:",
float(pred[0,1]), float(pred1[0,1]), float(pred2[0,1]), float(pred3[0,1]), flush=True)
except Exception as _e:
print("logit-print failed", _e, flush=True)
out_image = out_image.detach()
# Visualization
real = (init_image_compare / 2 + 0.5).clamp(0, 1).permute(0, 2, 3, 1).cpu().numpy()
image = imgg[None]
perturbed = image.astype(np.float32) / 255
view_images(perturbed * 255, show=False, save_path=save_path + "_adv_image.png")
L1 = LpDistance(1)
L2 = LpDistance(2)
Linf = LpDistance(float("inf"))
psnrv = calculate_psnr(image[0], (real[0]*255.).astype(np.uint8), border=0)
ssimv = calculate_ssim(image[0], (real[0]*255.).astype(np.uint8), border=0)
end_time = time.time()
processing_time = end_time - start_time
print(f"Processing time: {processing_time:.2f} seconds")
return Image.fromarray(image[0]), pred_label, pred_label1, pred_label2, pred_label3, psnrv, ssimv, processing_time
|