VIVEK JAYARAM commited on
Commit
c63740a
·
1 Parent(s): b487d98

Diffusers unet working

Browse files
cdim/diffusion/diffusion_pipeline.py CHANGED
@@ -15,7 +15,8 @@ def run_diffusion(
15
  num_inference_steps: int = 1000,
16
  K=5,
17
  image_dim=256,
18
- image_channels=3
 
19
  ):
20
  batch_size = noisy_observation.shape[0]
21
  image_shape = (batch_size, image_channels, image_dim, image_dim)
@@ -26,7 +27,8 @@ def run_diffusion(
26
 
27
  for i, t in tqdm(enumerate(scheduler.timesteps), total=len(scheduler.timesteps), desc="Processing timesteps"):
28
  # 1. predict noise model_output
29
- model_output = model(image, t.unsqueeze(0).to(device))[:, :3]
 
30
 
31
  # 2. compute previous image: x_t -> x_t-1
32
  image = scheduler.step(model_output, t, image).prev_sample
@@ -38,7 +40,8 @@ def run_diffusion(
38
 
39
  with torch.enable_grad():
40
  # Calculate x^hat_0
41
- model_output = model(image, (t - t_skip).unsqueeze(0).to(device))[:, :3]
 
42
  x_0 = (image - beta_prod_t_prev ** (0.5) * model_output) / alpha_prod_t_prev ** (0.5)
43
 
44
  distance = operator(x_0) - noisy_observation
@@ -48,6 +51,6 @@ def run_diffusion(
48
  print(loss.mean())
49
  loss.mean().backward()
50
 
51
- image -= 10 / torch.linalg.norm(image.grad) * image.grad
52
 
53
  return image
 
15
  num_inference_steps: int = 1000,
16
  K=5,
17
  image_dim=256,
18
+ image_channels=3,
19
+ model_type="diffusers"
20
  ):
21
  batch_size = noisy_observation.shape[0]
22
  image_shape = (batch_size, image_channels, image_dim, image_dim)
 
27
 
28
  for i, t in tqdm(enumerate(scheduler.timesteps), total=len(scheduler.timesteps), desc="Processing timesteps"):
29
  # 1. predict noise model_output
30
+ model_output = model(image, t.unsqueeze(0).to(device))
31
+ model_output = model_output.sample if model_type == "diffusers" else model_output[:, :3]
32
 
33
  # 2. compute previous image: x_t -> x_t-1
34
  image = scheduler.step(model_output, t, image).prev_sample
 
40
 
41
  with torch.enable_grad():
42
  # Calculate x^hat_0
43
+ model_output = model(image, (t - t_skip).unsqueeze(0).to(device))
44
+ model_output = model_output.sample if model_type == "diffusers" else model_output[:, :3]
45
  x_0 = (image - beta_prod_t_prev ** (0.5) * model_output) / alpha_prod_t_prev ** (0.5)
46
 
47
  distance = operator(x_0) - noisy_observation
 
51
  print(loss.mean())
52
  loss.mean().backward()
53
 
54
+ image -= 15 / torch.linalg.norm(image.grad) * image.grad
55
 
56
  return image
inference.py CHANGED
@@ -7,6 +7,8 @@ from PIL import Image
7
  import numpy as np
8
  import torch
9
 
 
 
10
  from cdim.noise import get_noise
11
  from cdim.operators import get_operator
12
  from cdim.image_utils import save_to_image
@@ -51,11 +53,18 @@ def main(args):
51
  operator_config["device"] = device
52
  operator = get_operator(**operator_config)
53
 
54
- # Load the model
55
- model_config = load_yaml(args.model_config)
56
- model = create_model(**model_config)
57
- model = model.to(device)
58
- model.eval()
 
 
 
 
 
 
 
59
 
60
  # All the models have the same scheduler.
61
  # you can change this for different models
@@ -77,7 +86,8 @@ def main(args):
77
  model, ddim_scheduler,
78
  noisy_measurement, operator, noise_function, device,
79
  num_inference_steps=args.T,
80
- K=args.K)
 
81
  print(f"total time {time.time() - t0}")
82
 
83
  save_to_image(output_image, os.path.join(args.output_dir, "output.png"))
 
7
  import numpy as np
8
  import torch
9
 
10
+ from diffusers import DiffusionPipeline
11
+
12
  from cdim.noise import get_noise
13
  from cdim.operators import get_operator
14
  from cdim.image_utils import save_to_image
 
53
  operator_config["device"] = device
54
  operator = get_operator(**operator_config)
55
 
56
+ if args.model_config.endswith(".yaml"):
57
+ # Local model from DPS
58
+ model_type = "dps"
59
+ model_config = load_yaml(args.model_config)
60
+ model = create_model(**model_config)
61
+ model = model.to(device)
62
+ model.eval()
63
+
64
+ else:
65
+ # Huggingface diffusers model
66
+ model_type = "diffusers"
67
+ model = DiffusionPipeline.from_pretrained(args.model_config).to("cuda").unet
68
 
69
  # All the models have the same scheduler.
70
  # you can change this for different models
 
86
  model, ddim_scheduler,
87
  noisy_measurement, operator, noise_function, device,
88
  num_inference_steps=args.T,
89
+ K=args.K,
90
+ model_type=model_type)
91
  print(f"total time {time.time() - t0}")
92
 
93
  save_to_image(output_image, os.path.join(args.output_dir, "output.png"))
requirements.txt CHANGED
@@ -1,2 +1,5 @@
 
1
  numpy==2.1.2
2
  Pillow==11.0.0
 
 
 
1
+ diffusers==0.30.3
2
  numpy==2.1.2
3
  Pillow==11.0.0
4
+ PyYAML==6.0.2
5
+ tqdm==4.66.5