Spaces:
Paused
Paused
File size: 4,262 Bytes
7578496 1d49c1f ea54e12 7578496 1d49c1f 7578496 ea54e12 7578496 d7af000 7578496 cd666af 7578496 ea54e12 7578496 ea54e12 7578496 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import gradio as gr
import tensorflow as tf
import numpy as np
from huggingface_hub import from_pretrained_keras
from diffusion_sampler import DiffusionSampler
print(f"detected GPUs={tf.config.list_physical_devices('GPU')}")
scheduler_button = gr.Radio(
choices=["Linear", "Cosine"],
label="Noise Scheduler",
value="Linear",
info="""
Decides whether to employ a model trained with a linear scheduler,
as proposed by Jonathan Ho et al. in 'Denoising Diffusion Probabilistic Models',
or the cosine variant introduced by Alex Nichol et al. in 'Improved Denoising Diffusion Probabilistic Models'.
""",
)
sampling_button = gr.Radio(
choices=["DDPM", "DDIM"],
label="Sampling Procedure",
value="DDPM",
info="""
Selects either the stocasthic sampling procedure described by Jonathan Ho et al. in 'Denoising Diffusion Probabilistic Models',
or the implicit variant proposed by Jiaming Song et al. in 'Denoising Diffusion Implicit Models'.
For the latter, it is also necessary to specify the sub-sequence strategy and the number of sampling steps.
""",
)
subsequence_button = gr.Radio(
choices=["Linear", "Quadratic"],
label="Sub-Sequence",
value="Linear",
info="""
Specific to DDIM sampling, this parameter chooses the procedure
for forming the sub-sequence employed during the sampling process.
""",
)
ema_button = gr.Checkbox(
value=True,
label="Exponential Moving Average",
info="""
Whether to invoke the network with the applied exponential moving average on the model parameters.
Recommended for better results.
"""
)
images_button = gr.Number(
label="Number of images to generate",
value=5,
precision=0,
minimum=1,
maximum=64,
info="""
The number of images to be generated.
Larger batch sizes result in longer inference times.
"""
)
step_button = gr.Slider(
minimum=500,
value=1_000,
maximum=1_000,
randomize=True,
label="Number of sampling steps",
info="""
Relevant exclusively to DDIM sampling, this parameter determines the number of steps to be utilized during sampling.
The default value is set to 1000 in the case of DDPM sampling.
"""
)
gallery = gr.Gallery(
columns=4,
label="""
Generated Flowers
"""
)
linear_diffusion_model = DiffusionSampler(
model=from_pretrained_keras("leowajda/linear_diffusion"),
ema_model=from_pretrained_keras("leowajda/linear_diffusion_ema"),
noise_scheduler="linear",
)
cosine_diffusion_model = DiffusionSampler(
model=from_pretrained_keras("leowajda/cosine_diffusion"),
ema_model=from_pretrained_keras("leowajda/cosine_diffusion_ema"),
noise_scheduler="cosine",
)
def call_model(
model_to_call: str,
sample_strategy: str = "ddim",
step_strategy: str = "uniform",
ema: bool = True,
steps: int = 1_000,
num_images: int = 0,
progress=gr.Progress(track_tqdm=True),
):
diffusion_model = linear_diffusion_model if model_to_call.lower() == "linear" else cosine_diffusion_model
images = diffusion_model.generate_images(
num_images=num_images,
steps=steps,
sample_strategy=sample_strategy.lower(),
step_strategy=step_strategy.lower(),
ema=ema,
)
return images.numpy().astype(np.uint8)
demo = gr.Interface(
fn=call_model,
inputs=[scheduler_button, sampling_button, subsequence_button, ema_button, step_button, images_button],
outputs=gallery,
cache_examples=False,
title="""Unconditional Image Generation Through Denoising Diffusion Implicit Models""",
examples=[
["Linear", "DDPM", "Linear", True, 1_000, 10],
["Cosine", "DDIM", "Linear", True, 750, 20],
["Linear", "DDIM", "Quadratic", True, 750, 20]
],
description="""
<p align="center">
Supervisor: <strong>Wojciech Oronowicz – Jaśkowiak, PhD</strong>
 
Author: <strong>Leonardo Wajda</strong>
 
Specialization: <strong>Intelligent Data Processing Systems</strong>
</p>
""",
)
demo.queue().launch()
|