Untitled

 avatar
unknown
python
2 years ago
9.9 kB
3
Indexable
import argparse

epsilon = 0.0001

class Args(argparse.Namespace):
    eps = epsilon
    iter = 2
    T_enc = 100
    T_atk = 5
    T_inf = 25
    start_t = 5
    attack_iter = 1
    attack_inf_iter = 4
    repeat_times = 1
    cnt_skip = 0

args = Args()

eps = args.eps
iter = args.iter
T_enc =  args.T_enc
T_atk = args.T_atk
T_inf = args.T_inf
start_t = args.start_t
attack_iter = args.attack_iter
attack_inf_iter = args.attack_inf_iter
repeat_times = args.repeat_times
cnt_skip = args.cnt_skip


pgd_attacker = PGDAttack(predict=diff_resnet,
                        eps=eps,
                        nb_iter=iter,
                        T_atk=T_atk,
                        T_enc=T_enc,
                        start_t=start_t,
                        attack_iter=attack_iter,
                        attack_inf_iter=attack_inf_iter,
                        repeat_times=repeat_times,
                        cnt_skip=cnt_skip,
                        clip_min=-1e6,
                        clip_max=1e6,
                        targeted=False,
                        rand_init=False)
                        
                        

Shape of cond: torch.Size([5, 512])
t: tensor([0, 0, 0, 0, 0], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([1, 1, 1, 1, 1], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([2, 2, 2, 2, 2], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([3, 3, 3, 3, 3], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([4, 4, 4, 4, 4], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([5, 5, 5, 5, 5], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([6, 6, 6, 6, 6], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([7, 7, 7, 7, 7], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([8, 8, 8, 8, 8], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([9, 9, 9, 9, 9], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([10, 10, 10, 10, 10], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([11, 11, 11, 11, 11], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([12, 12, 12, 12, 12], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([13, 13, 13, 13, 13], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([14, 14, 14, 14, 14], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([15, 15, 15, 15, 15], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([16, 16, 16, 16, 16], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([17, 17, 17, 17, 17], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([18, 18, 18, 18, 18], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([19, 19, 19, 19, 19], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([20, 20, 20, 20, 20], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([21, 21, 21, 21, 21], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([22, 22, 22, 22, 22], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([23, 23, 23, 23, 23], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
t: tensor([24, 24, 24, 24, 24], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([5])
(B, ) shape: (5,)
  0%|          | 0/2 [00:03<?, ?it/s]
t: tensor([20], device='cuda:0')
x.shape: torch.Size([5, 3, 64, 64])
t.shape: torch.Size([1])
(B, ) shape: (5,)

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[13], line 65
     63 xT = diff_model.encode_stochastic(original_image, cond, T=25)
     64 #z_adv, adv_img = pgd_attacker.perturb(cond, labels[i].unsqueeze(0).cuda(), xT)
---> 65 z_adv, adv_img = pgd_attacker.perturb(cond, labels.cuda(), xT)
     66 adv_render_img = diff_model.render(xT, z_adv, T=T_inf)
     67 #display_images(original_image.squeeze(0), xT, adv_render_img.squeeze(0), i + 1)

File ~/diff-ule/pgd_attack.py:157, in PGDAttack.perturb(self, semantic_space, labels, encoded)
    155 delta = torch.zeros_like(semantic_space)
    156 delta = nn.Parameter(delta)
--> 157 rval = perturb_iterative(
    158     semantic_space, labels, encoded, self.predict, nb_iter=self.nb_iter,
    159     eps=self.eps, eps_iter=self.eps_iter,
    160     loss_fn=self.loss_fn,
    161     clip_min=self.clip_min,
    162     clip_max=self.clip_max, delta_init=delta,
    163     T=self.T_atk,
    164     T_enc=self.T_enc, start_t=self.start_t, attack_iter=self.attack_iter,
    165     attack_inf_iter=self.attack_inf_iter, repeat_times=self.repeat_times, cnt_skip=self.cnt_skip
    166 )
    167 return rval[0].data, rval[1].data

File ~/diff-ule/pgd_attack.py:44, in perturb_iterative(semantic_space, labels, encoded, predict, nb_iter, eps, eps_iter, loss_fn, T, T_enc, start_t, attack_iter, attack_inf_iter, repeat_times, cnt_skip, delta_init, clip_min, clip_max, alpha, step_size)
     40 t = torch.tensor(indices[T - start_t + (k * (attack_inf_iter +
     41                  attack_iter) + ii)] * xT_tmp.size(0), device='cuda').unsqueeze(0)
     43 xT_tmp.requires_grad_()
---> 44 outputs,  x = predict(
     45     xT_tmp, semantic_space + delta, T, t, False, False)
     46 loss = loss_fn(outputs, labels)
     47 loss.backward()

File ~/diff-ule/env/lib/python3.8/site-packages/torch/nn/modules/module.py:1051, in Module._call_impl(self, *input, **kwargs)
   1047 # If we don't have any hooks, we want to skip the rest of the logic in
   1048 # this function, and just call forward.
   1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051     return forward_call(*input, **kwargs)
   1052 # Do not call functions when jit is used
   1053 full_backward_hooks, non_full_backward_hooks = [], []

Cell In[10], line 27, in DiffResNet50.forward(self, z, xT, T, t, predict, is_last)
     25     x_pred = z
     26 else:
---> 27     x, x_pred = self.decoder.render(z, xT, T, True, t, is_last)
     28 logits = torch.sigmoid(self.network(x_pred))
     29 return logits, x_pred

File ~/diff-ule/experiment.py:129, in LitModel.render(self, noise, cond, T, inter, t, is_last)
    127 if cond is not None:
    128     if inter:
--> 129         out = render_condition_inter(self.conf,
    130                                      self.ema_model,
    131                                      noise,
    132                                      sampler=sampler,
    133                                      cond=cond,
    134                                      t=t)
    135         pred_img = out["pred_xstart"]
    136         pred_img = (pred_img + 1) / 2

File ~/diff-ule/renderer.py:78, in render_condition_inter(conf, model, x_T, sampler, x_start, cond, t)
     75     if cond is None:
     76         cond = model.encode(x_start)
---> 78     out = sampler.ddim_sample(
     79         model=model, x=x_T, t=t, model_kwargs={'cond': cond})
     81     return out
     82 else:

File ~/diff-ule/diffusion/base.py:606, in GaussianDiffusionBeatGans.ddim_sample(self, model, x, t, clip_denoised, denoised_fn, cond_fn, model_kwargs, eta)
    590 def ddim_sample(
    591     self,
    592     model: Model,
   (...)
    599     eta=0.0,
    600 ):
    601     """
    602     Sample x_{t-1} from the model using DDIM.
    603 
    604     Same usage as p_sample().
    605     """
--> 606     out = self.p_mean_variance(
    607         model,
    608         x,
    609         t,
    610         clip_denoised=clip_denoised,
    611         denoised_fn=denoised_fn,
    612         model_kwargs=model_kwargs,
    613     )
    614     if cond_fn is not None:
    615         out = self.condition_score(cond_fn,
    616                                    out,
    617                                    x,
    618                                    t,
    619                                    model_kwargs=model_kwargs)

File ~/diff-ule/diffusion/diffusion.py:96, in SpacedDiffusionBeatGans.p_mean_variance(self, model, *args, **kwargs)
     95 def p_mean_variance(self, model: Model, *args, **kwargs):  # pylint: disable=signature-differs
---> 96     return super().p_mean_variance(self._wrap_model(model), *args,
     97                                    **kwargs)

File ~/diff-ule/diffusion/base.py:311, in GaussianDiffusionBeatGans.p_mean_variance(self, model, x, t, clip_denoised, denoised_fn, model_kwargs)
    308 print(f't.shape: {t.shape}')
    309 print(f'(B, ) shape: {(B, )}')
--> 311 assert t.shape == (B, )
    312 with autocast(self.conf.fp16):
    313     model_forward = model.forward(x=x,
    314                                   t=self._scale_timesteps(t),
    315                                   **model_kwargs)

AssertionError: 
Editor is loading...
Leave a Comment