Spaces:
Runtime error
Runtime error
beresandras
commited on
Commit
•
36071c0
1
Parent(s):
9bdfb74
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import huggingface_hub as hf_hub
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
|
6 |
+
num_rows = 3
|
7 |
+
num_cols = 3
|
8 |
+
num_images = num_rows * num_cols
|
9 |
+
image_size = 64
|
10 |
+
plot_image_size = 64
|
11 |
+
|
12 |
+
|
13 |
+
def load_model():
|
14 |
+
model = hf_hub.from_pretrained_keras("beresandras/denoising-diffusion-model")
|
15 |
+
return model
|
16 |
+
|
17 |
+
def diffusion_schedule(diffusion_times, min_signal_rate, max_signal_rate):
|
18 |
+
start_angle = tf.acos(max_signal_rate)
|
19 |
+
end_angle = tf.acos(min_signal_rate)
|
20 |
+
|
21 |
+
diffusion_angles = start_angle + diffusion_times * (end_angle - start_angle)
|
22 |
+
|
23 |
+
signal_rates = tf.cos(diffusion_angles)
|
24 |
+
noise_rates = tf.sin(diffusion_angles)
|
25 |
+
|
26 |
+
return noise_rates, signal_rates
|
27 |
+
|
28 |
+
def generate_images(model, num_images, diffusion_steps, stochasticity, min_signal_rate, max_signal_rate):
|
29 |
+
step_size = 1.0 / diffusion_steps
|
30 |
+
initial_noise = tf.random.normal(shape=(num_images, image_size, image_size, 3))
|
31 |
+
|
32 |
+
noisy_images = initial_noise
|
33 |
+
for step in range(diffusion_steps):
|
34 |
+
diffusion_times = tf.ones((num_images, 1, 1, 1)) - step * step_size
|
35 |
+
next_diffusion_times = diffusion_times - step_size
|
36 |
+
|
37 |
+
noise_rates, signal_rates = diffusion_schedule(diffusion_times, min_signal_rate, max_signal_rate)
|
38 |
+
next_noise_rates, next_signal_rates = diffusion_schedule(next_diffusion_times, min_signal_rate, max_signal_rate)
|
39 |
+
|
40 |
+
sample_noises = tf.random.normal(shape=(num_images, image_size, image_size, 3))
|
41 |
+
sample_noise_rates = stochasticity * (1.0 - (signal_rates / next_signal_rates)**2)**0.5 * (next_noise_rates / noise_rates)
|
42 |
+
|
43 |
+
pred_noises = model([noisy_images, noise_rates])
|
44 |
+
pred_images = (noisy_images - noise_rates * pred_noises) / signal_rates
|
45 |
+
noisy_images = (
|
46 |
+
next_signal_rates * pred_images
|
47 |
+
+ (next_noise_rates**2 - sample_noise_rates**2)**0.5 * pred_noises
|
48 |
+
+ sample_noise_rates * sample_noises
|
49 |
+
)
|
50 |
+
|
51 |
+
generated_images = tf.clip_by_value(0.5 + 0.3 * pred_images, 0.0, 1.0)
|
52 |
+
generated_images = tf.image.resize(
|
53 |
+
generated_images, (plot_image_size, plot_image_size), method="nearest"
|
54 |
+
)
|
55 |
+
return generated_images.numpy()
|
56 |
+
|
57 |
+
|
58 |
+
model = load_model()
|
59 |
+
gr.Interface(
|
60 |
+
generate_images,
|
61 |
+
inputs=[
|
62 |
+
model,
|
63 |
+
num_images,
|
64 |
+
gr.inputs.Slider(1, 20, default=10, label="Diffusion steps"),
|
65 |
+
gr.inputs.Slider(0.0, 1.0, step=0.05, default=0.0, label="Stochasticity"),
|
66 |
+
gr.inputs.Slider(0.02, 0.10, step=0.01, default=0.02, label="Minimal signal rate"),
|
67 |
+
gr.inputs.Slider(0.80, 0.95, step=0.01, default=0.95, label="Maximal signal rate"),
|
68 |
+
],
|
69 |
+
outputs="image",
|
70 |
+
).launch()
|