leowajda commited on
Commit
d342810
·
1 Parent(s): 2b0144e

refactor code

Browse files
Files changed (3) hide show
  1. README.md +7 -5
  2. app.py +15 -16
  3. diffusion_sampler.py +4 -6
README.md CHANGED
@@ -1,13 +1,15 @@
1
  ---
2
- title: Temp Diffusion
3
- emoji: 📈
4
- colorFrom: indigo
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 4.10.0
8
  app_file: app.py
9
- pinned: false
10
  license: agpl-3.0
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Diffusion Model
3
+ emoji: 🌼
4
+ colorFrom: yellow
5
+ colorTo: orange
6
  sdk: gradio
7
  sdk_version: 4.10.0
8
  app_file: app.py
9
+ pinned: true
10
  license: agpl-3.0
11
+ suggested_storage: "small"
12
+ suggested_hardware: "t4-small"
13
  ---
14
 
15
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,11 +1,8 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
  import numpy as np
4
  from huggingface_hub import from_pretrained_keras
5
  from diffusion_sampler import DiffusionSampler
6
 
7
- print(f"detected GPUs={tf.config.list_physical_devices('GPU')}")
8
-
9
  scheduler_button = gr.Radio(
10
  choices=["Linear", "Cosine"],
11
  label="Noise Scheduler",
@@ -81,18 +78,21 @@ gallery = gr.Gallery(
81
  """
82
  )
83
 
 
 
 
 
 
 
 
84
 
85
- linear_diffusion_model = DiffusionSampler(
86
- model=from_pretrained_keras("leowajda/linear_diffusion"),
87
- ema_model=from_pretrained_keras("leowajda/linear_diffusion_ema"),
88
- noise_scheduler="linear",
89
- )
90
-
91
- cosine_diffusion_model = DiffusionSampler(
92
- model=from_pretrained_keras("leowajda/cosine_diffusion"),
93
- ema_model=from_pretrained_keras("leowajda/cosine_diffusion_ema"),
94
- noise_scheduler="cosine",
95
- )
96
 
97
 
98
  def call_model(
@@ -102,9 +102,8 @@ def call_model(
102
  ema: bool = True,
103
  steps: int = 1_000,
104
  num_images: int = 0,
105
- progress=gr.Progress(track_tqdm=True),
106
  ):
107
- diffusion_model = linear_diffusion_model if model_to_call.lower() == "linear" else cosine_diffusion_model
108
  images = diffusion_model.generate_images(
109
  num_images=num_images,
110
  steps=steps,
 
1
  import gradio as gr
 
2
  import numpy as np
3
  from huggingface_hub import from_pretrained_keras
4
  from diffusion_sampler import DiffusionSampler
5
 
 
 
6
  scheduler_button = gr.Radio(
7
  choices=["Linear", "Cosine"],
8
  label="Noise Scheduler",
 
78
  """
79
  )
80
 
81
+ diffusion_models = {
82
+ "linear":
83
+ DiffusionSampler(
84
+ model=from_pretrained_keras("leowajda/linear_diffusion"),
85
+ ema_model=from_pretrained_keras("leowajda/linear_diffusion_ema"),
86
+ noise_scheduler="linear"
87
+ ),
88
 
89
+ "cosine":
90
+ DiffusionSampler(
91
+ model=from_pretrained_keras("leowajda/cosine_diffusion"),
92
+ ema_model=from_pretrained_keras("leowajda/cosine_diffusion_ema"),
93
+ noise_scheduler="cosine"
94
+ )
95
+ }
 
 
 
 
96
 
97
 
98
  def call_model(
 
102
  ema: bool = True,
103
  steps: int = 1_000,
104
  num_images: int = 0,
 
105
  ):
106
+ diffusion_model = diffusion_models[model_to_call.lower()]
107
  images = diffusion_model.generate_images(
108
  num_images=num_images,
109
  steps=steps,
diffusion_sampler.py CHANGED
@@ -1,5 +1,3 @@
1
- import numpy as np
2
- import tqdm as tqdm
3
  import tensorflow as tf
4
  import math
5
  from tensorflow import keras
@@ -100,10 +98,10 @@ class DiffusionSampler(keras.Model):
100
  return sqrt_alpha_cum_prod_prev * x0_t + x_t_dir * pred_noise + c1 * random_noise
101
 
102
  def noise_scheduler(self, scheduler: str, max_beta: int = 0.02) -> tf.Tensor:
103
- alpha_bar = lambda t: tf.math.cos((t + 0.008) / 1.008 * tf.constant(math.pi, dtype=tf.float64) / 2) ** 2
104
- cosine_scheduler = lambda i: tf.minimum(
105
- 1 - alpha_bar((i + 1) / tf.cast(self.timesteps, dtype=tf.float64)) / alpha_bar(
106
- i / tf.cast(self.timesteps, dtype=tf.float64)), max_beta)
107
 
108
  if scheduler == "linear":
109
  x = tf.linspace(start=self.beta_start, stop=self.beta_end, num=self.timesteps)
 
 
 
1
  import tensorflow as tf
2
  import math
3
  from tensorflow import keras
 
98
  return sqrt_alpha_cum_prod_prev * x0_t + x_t_dir * pred_noise + c1 * random_noise
99
 
100
  def noise_scheduler(self, scheduler: str, max_beta: int = 0.02) -> tf.Tensor:
101
+ pi, t = [tf.constant(num, dtype=tf.float64) for num in (math.pi, self.timesteps)]
102
+
103
+ alpha_bar = lambda t: tf.math.cos((t + 0.008) / 1.008 * pi / 2) ** 2
104
+ cosine_scheduler = lambda i: tf.minimum(1 - alpha_bar((i + 1) / t) / alpha_bar(i / t), max_beta)
105
 
106
  if scheduler == "linear":
107
  x = tf.linspace(start=self.beta_start, stop=self.beta_end, num=self.timesteps)