File size: 4,598 Bytes
7578496
ea54e12
b7f0ee7
7578496
 
 
bde29c8
 
b7f0ee7
7578496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f0ee7
7578496
 
 
 
 
2b0144e
7578496
 
 
 
 
 
 
 
 
 
b7f0ee7
7578496
 
 
 
 
 
 
 
 
 
 
ea54e12
516076a
 
 
7578496
 
 
 
 
d342810
 
 
7195a47
 
d342810
 
7578496
d342810
 
7195a47
 
d342810
 
 
7578496
 
 
 
 
 
 
 
 
 
d342810
ea54e12
 
 
7578496
 
 
 
 
ea54e12
 
7578496
 
 
 
 
b7f0ee7
7578496
 
bde29c8
 
 
 
 
7578496
 
 
 
 
 
 
 
 
 
 
 
516076a
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
140
141
142
143
144
145
146
import gradio as gr
import numpy as np
import tensorflow as tf
from huggingface_hub import from_pretrained_keras
from diffusion_sampler import DiffusionSampler

devices = '\n'.join([f'- {device.name}' for device in tf.config.list_physical_devices('GPU')]) or 'No GPU devices found.'
print(f"GPUs available: {devices}")

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's weights.
    """
)

images_button = gr.Number(
    label="Number of images to generate",
    value=10,
    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=700,
    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,
    allow_preview=False,
    show_download_button=False,
    show_share_button=False,
    label="""
        Generated Flowers
    """
)

diffusion_models = {
    "linear":
        DiffusionSampler(
            model=from_pretrained_keras("leowajda/linear_diffusion", cache_dir="cache"),
            ema_model=from_pretrained_keras("leowajda/linear_diffusion_ema", cache_dir="cache"),
            noise_scheduler="linear"
        ),

    "cosine":
        DiffusionSampler(
            model=from_pretrained_keras("leowajda/cosine_diffusion", cache_dir="cache"),
            ema_model=from_pretrained_keras("leowajda/cosine_diffusion_ema", cache_dir="cache"),
            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,
):
    diffusion_model = diffusion_models[model_to_call.lower()]
    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=True,
    title="""Unconditional Image Generation Through Denoising Diffusion Implicit Models""",
    examples=[
        ["Linear", "DDPM", "Linear", True, 1_000, 25],
        ["Linear", "DDPM", "Linear", False, 1_000, 25],
        ["Cosine", "DDPM", "Linear", False, 750, 25],
        ["Cosine", "DDPM", "Linear", True, 750, 25],
        ["Linear", "DDIM", "Linear", True, 750, 25],
    ],
    description="""                
        <p align="center">
            Supervisor: <strong>Wojciech Oronowicz – Jaśkowiak, PhD</strong>
            &emsp;
            Author: <strong>Leonardo Wajda</strong>
            &emsp;
            Specialization: <strong>Intelligent Data Processing Systems</strong>
        </p>
    """,
)

demo.queue(default_concurrency_limit=None).launch()