dedelste commited on
Commit
b9255c1
·
1 Parent(s): acdb7db
Files changed (1) hide show
  1. ambient_diffusion.py +17 -18
ambient_diffusion.py CHANGED
@@ -59,7 +59,7 @@ def make_ambient_batch(
59
  A = sample_inpainting_mask((B, C, H, W), p=p, device=device)
60
 
61
  # 2. add diffusion noise → x_t , then A x_t
62
- noise = torch.randn_like(clean)
63
 
64
  if timesteps.ndim == 0:
65
  timesteps = torch.full((B,), timesteps, device=device, dtype=torch.long)
@@ -114,10 +114,10 @@ class AmbientDDPMPipeline(DDPMPipeline):
114
  device = self.device
115
  h = w = self.unet.config.sample_size
116
 
117
- mask = sample_inpainting_mask(
118
- (batch_size, 1, h, w), p=self.p_mask, device=device
119
- )
120
 
 
121
  x = torch.randn(
122
  (batch_size, 1, h, w),
123
  generator=generator,
@@ -126,29 +126,28 @@ class AmbientDDPMPipeline(DDPMPipeline):
126
 
127
  self.scheduler.set_timesteps(num_inference_steps, device=device)
128
 
129
-
130
  if class_labels is None:
131
- cond_lbls = torch.zeros(
132
- (batch_size,
133
- getattr(self.unet.config, "multihot_dim", 1)),
134
- dtype=torch.long if not hasattr(self.unet.config, "multihot_dim") else torch.float,
135
- device=device,
136
- )
137
  else:
138
  cond_lbls = class_labels.to(device)
139
 
140
  uncond_lbls = torch.zeros_like(cond_lbls)
141
 
142
  for t in self.scheduler.timesteps:
 
 
143
 
144
  if guidance_scale == 1.0 or class_labels is None:
145
- # single unconditional (or conditional) pass
146
- net_inp = torch.cat([mask * x, mask[:, :1]], dim=1)
147
- eps = self.unet(net_inp, t, class_labels=cond_lbls).sample
148
  else:
149
- # duplicate batch: cond | uncond
150
- net_inp = torch.cat([mask * x, mask[:, :1]], dim=1) # (B,C+1,H,W)
151
- inp = torch.cat([net_inp, net_inp], dim=0) # (2B,C+1,H,W)
152
  lbls = torch.cat([cond_lbls, uncond_lbls], dim=0)
153
  tids = t.expand(2 * batch_size)
154
 
@@ -157,7 +156,7 @@ class AmbientDDPMPipeline(DDPMPipeline):
157
  )
158
  eps = eps_uncond + guidance_scale * (eps_cond - eps_uncond)
159
 
160
- # ---------- Eq. 3.3 update (must be INSIDE the loop) ----------
161
  x0_hat = eps
162
  sigma = self.scheduler._get_variance(t).sqrt()
163
  gamma = sigma ** 2 / (sigma ** 2 + 1)
 
59
  A = sample_inpainting_mask((B, C, H, W), p=p, device=device)
60
 
61
  # 2. add diffusion noise → x_t , then A x_t
62
+ noise = torch.randn_like(clean)
63
 
64
  if timesteps.ndim == 0:
65
  timesteps = torch.full((B,), timesteps, device=device, dtype=torch.long)
 
114
  device = self.device
115
  h = w = self.unet.config.sample_size
116
 
117
+ # Create a dummy mask that's all ones (no masking)
118
+ dummy_mask = torch.ones((batch_size, 1, h, w), device=device)
 
119
 
120
+ # Start with random noise
121
  x = torch.randn(
122
  (batch_size, 1, h, w),
123
  generator=generator,
 
126
 
127
  self.scheduler.set_timesteps(num_inference_steps, device=device)
128
 
129
+ # Set up conditioning labels
130
  if class_labels is None:
131
+ cond_lbls = torch.zeros(
132
+ (batch_size, getattr(self.unet.config, "multihot_dim", 1)),
133
+ dtype=torch.long if not hasattr(self.unet.config, "multihot_dim") else torch.float,
134
+ device=device,
135
+ )
 
136
  else:
137
  cond_lbls = class_labels.to(device)
138
 
139
  uncond_lbls = torch.zeros_like(cond_lbls)
140
 
141
  for t in self.scheduler.timesteps:
142
+ # Always include mask channel to match training, but mask is all ones
143
+ x_with_mask = torch.cat([x, dummy_mask], dim=1)
144
 
145
  if guidance_scale == 1.0 or class_labels is None:
146
+ # Single conditional/unconditional pass
147
+ eps = self.unet(x_with_mask, t, class_labels=cond_lbls).sample
 
148
  else:
149
+ # Classifier-free guidance with duplicate batch
150
+ inp = torch.cat([x_with_mask, x_with_mask], dim=0)
 
151
  lbls = torch.cat([cond_lbls, uncond_lbls], dim=0)
152
  tids = t.expand(2 * batch_size)
153
 
 
156
  )
157
  eps = eps_uncond + guidance_scale * (eps_cond - eps_uncond)
158
 
159
+ # Standard update (same as before)
160
  x0_hat = eps
161
  sigma = self.scheduler._get_variance(t).sqrt()
162
  gamma = sigma ** 2 / (sigma ** 2 + 1)