Spaces:
Paused
Paused
minor improvements
Browse files- .gitignore +1 -1
- README.md +1 -1
- app.py +8 -6
- diffusion_sampler.py +2 -4
.gitignore
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
.idea
|
| 2 |
__pycache__/
|
|
|
|
| 1 |
+
.idea/
|
| 2 |
__pycache__/
|
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Diffusion Model
|
| 3 |
-
emoji: ๐ผ
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: Diffusion Model
|
| 3 |
+
emoji: ๐ผ๐ธ๐ค๐ต๏ธ๐บ
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 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",
|
|
@@ -39,8 +42,7 @@ ema_button = gr.Checkbox(
|
|
| 39 |
value=True,
|
| 40 |
label="Exponential Moving Average",
|
| 41 |
info="""
|
| 42 |
-
Whether to invoke the network with the applied exponential moving average on the model
|
| 43 |
-
Recommended for better results.
|
| 44 |
"""
|
| 45 |
)
|
| 46 |
|
|
@@ -57,7 +59,7 @@ images_button = gr.Number(
|
|
| 57 |
)
|
| 58 |
|
| 59 |
step_button = gr.Slider(
|
| 60 |
-
minimum=
|
| 61 |
value=1_000,
|
| 62 |
maximum=1_000,
|
| 63 |
randomize=True,
|
|
@@ -119,12 +121,12 @@ demo = gr.Interface(
|
|
| 119 |
fn=call_model,
|
| 120 |
inputs=[scheduler_button, sampling_button, subsequence_button, ema_button, step_button, images_button],
|
| 121 |
outputs=gallery,
|
| 122 |
-
cache_examples=
|
| 123 |
title="""Unconditional Image Generation Through Denoising Diffusion Implicit Models""",
|
| 124 |
examples=[
|
| 125 |
["Linear", "DDPM", "Linear", True, 1_000, 10],
|
| 126 |
-
["Cosine", "
|
| 127 |
-
["Linear", "DDIM", "
|
| 128 |
],
|
| 129 |
description="""
|
| 130 |
<p align="center">
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
from huggingface_hub import from_pretrained_keras
|
| 5 |
from diffusion_sampler import DiffusionSampler
|
| 6 |
|
| 7 |
+
print('\n'.join([f'- {device.name}' for device in tf.config.list_physical_devices('GPU')]) or 'No GPU devices found.')
|
| 8 |
+
|
| 9 |
scheduler_button = gr.Radio(
|
| 10 |
choices=["Linear", "Cosine"],
|
| 11 |
label="Noise Scheduler",
|
|
|
|
| 42 |
value=True,
|
| 43 |
label="Exponential Moving Average",
|
| 44 |
info="""
|
| 45 |
+
Whether to invoke the network with the applied exponential moving average on the model's weights.
|
|
|
|
| 46 |
"""
|
| 47 |
)
|
| 48 |
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
step_button = gr.Slider(
|
| 62 |
+
minimum=700,
|
| 63 |
value=1_000,
|
| 64 |
maximum=1_000,
|
| 65 |
randomize=True,
|
|
|
|
| 121 |
fn=call_model,
|
| 122 |
inputs=[scheduler_button, sampling_button, subsequence_button, ema_button, step_button, images_button],
|
| 123 |
outputs=gallery,
|
| 124 |
+
cache_examples=True,
|
| 125 |
title="""Unconditional Image Generation Through Denoising Diffusion Implicit Models""",
|
| 126 |
examples=[
|
| 127 |
["Linear", "DDPM", "Linear", True, 1_000, 10],
|
| 128 |
+
["Cosine", "DDPM", "Linear", True, 750, 20],
|
| 129 |
+
["Linear", "DDIM", "Linear", True, 750, 20]
|
| 130 |
],
|
| 131 |
description="""
|
| 132 |
<p align="center">
|
diffusion_sampler.py
CHANGED
|
@@ -16,7 +16,7 @@ def batch_reshape(t: tf.Tensor, x: tf.Tensor) -> tf.Tensor:
|
|
| 16 |
return inner_function
|
| 17 |
|
| 18 |
|
| 19 |
-
class DiffusionSampler
|
| 20 |
def __init__(
|
| 21 |
self,
|
| 22 |
model: keras.Model | str,
|
|
@@ -26,9 +26,7 @@ class DiffusionSampler(keras.Model):
|
|
| 26 |
beta_end: float | None = 0.02,
|
| 27 |
noise_scheduler: str = "linear",
|
| 28 |
ema: float = 0.999,
|
| 29 |
-
**kwargs,
|
| 30 |
):
|
| 31 |
-
super().__init__(**kwargs)
|
| 32 |
self.noise_predictor = load_model(filepath=model, safe_mode=False) if isinstance(model, str) else model
|
| 33 |
self.ema_noise_predictor = load_model(filepath=ema_model, safe_mode=False) if isinstance(model, str) else ema_model
|
| 34 |
self.ema = ema
|
|
@@ -118,7 +116,7 @@ class DiffusionSampler(keras.Model):
|
|
| 118 |
sqrt_one_minus_alpha_cum_prod = at_timestep(self.sqrt_one_minus_alphas_cum_prod)
|
| 119 |
return sqrt_alpha_cum_prod * x_start + sqrt_one_minus_alpha_cum_prod * noise
|
| 120 |
|
| 121 |
-
@tf.function
|
| 122 |
def generate_images(
|
| 123 |
self,
|
| 124 |
num_images: int,
|
|
|
|
| 16 |
return inner_function
|
| 17 |
|
| 18 |
|
| 19 |
+
class DiffusionSampler:
|
| 20 |
def __init__(
|
| 21 |
self,
|
| 22 |
model: keras.Model | str,
|
|
|
|
| 26 |
beta_end: float | None = 0.02,
|
| 27 |
noise_scheduler: str = "linear",
|
| 28 |
ema: float = 0.999,
|
|
|
|
| 29 |
):
|
|
|
|
| 30 |
self.noise_predictor = load_model(filepath=model, safe_mode=False) if isinstance(model, str) else model
|
| 31 |
self.ema_noise_predictor = load_model(filepath=ema_model, safe_mode=False) if isinstance(model, str) else ema_model
|
| 32 |
self.ema = ema
|
|
|
|
| 116 |
sqrt_one_minus_alpha_cum_prod = at_timestep(self.sqrt_one_minus_alphas_cum_prod)
|
| 117 |
return sqrt_alpha_cum_prod * x_start + sqrt_one_minus_alpha_cum_prod * noise
|
| 118 |
|
| 119 |
+
@tf.function
|
| 120 |
def generate_images(
|
| 121 |
self,
|
| 122 |
num_images: int,
|