File size: 7,764 Bytes
9e188c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| default_exp app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "import gradio as gr\n",
    "import cf_guidance\n",
    "import min_diffusion\n",
    "import torch\n",
    "import nbdev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "\n",
    "## MODEL SETUP\n",
    "######################################\n",
    "######################################\n",
    "model_name = 'stabilityai/stable-diffusion-2'\n",
    "revision = 'fp16'\n",
    "dtype = torch.float16\n",
    "device = ('cpu','cuda')[torch.cuda.is_available()]\n",
    "\n",
    "# model parameters\n",
    "better_vae = ''\n",
    "unet_attn_slice = True\n",
    "sampler_kls = 'dpm_multi'\n",
    "hf_sampler = 'dpm_multi'\n",
    "\n",
    "model_kwargs = {\n",
    "    'better_vae': better_vae,\n",
    "    'unet_attn_slice': unet_attn_slice,\n",
    "    'sampler_kls': hf_sampler,\n",
    "}\n",
    "\n",
    "def load_model():\n",
    "    pipeline = min_diffusion.core.MinimalDiffusion(\n",
    "        model_name,\n",
    "        device,\n",
    "        dtype,\n",
    "        revision,\n",
    "        **model_kwargs,\n",
    "    )\n",
    "    pipeline.load()\n",
    "    return pipeline\n",
    "######################################\n",
    "######################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export \n",
    "\n",
    "## GENERATION PARAMETERS\n",
    "######################################\n",
    "######################################\n",
    "num_steps = 18\n",
    "height, width = 768, 768\n",
    "k_sampler = 'k_dpmpp_2m' #'k_dpmpp_sde'\n",
    "use_karras_sigmas = True\n",
    "\n",
    "# a good negative prompt\n",
    "NEG_PROMPT = \"ugly, stock photo, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy, blurred, text, watermark, grainy\"\n",
    "\n",
    "generation_kwargs = {\n",
    "    'num_steps': num_steps,\n",
    "    'height': height,\n",
    "    'width': width,\n",
    "    'k_sampler':  k_sampler,\n",
    "    'negative_prompt': NEG_PROMPT,\n",
    "    'use_karras_sigmas': use_karras_sigmas,\n",
    "}\n",
    "######################################\n",
    "######################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export \n",
    "\n",
    "## dynamicCFG SETUP\n",
    "######################################\n",
    "######################################\n",
    "\n",
    "# default cosine schedule parameters\n",
    "baseline_g        = 9    # default, static guidance value\n",
    "max_val           = 9    # the max scheduled guidance scaling value\n",
    "min_val           = 6    # the minimum scheduled guidance value\n",
    "num_warmup_steps  = 0    # number of warmup steps\n",
    "warmup_init_val   = 0    # the intial warmup value\n",
    "num_cycles        = 0.5  # number of cosine cycles\n",
    "k_decay           = 1    # k-decay for cosine curve scaling \n",
    "\n",
    "# group the default schedule parameters\n",
    "DEFAULT_COS_PARAMS = {\n",
    "    'max_val':           max_val,\n",
    "    'num_steps':         num_steps,\n",
    "    'min_val':           min_val,\n",
    "    'num_cycles':        num_cycles,\n",
    "    'k_decay':           k_decay,\n",
    "    'num_warmup_steps':  num_warmup_steps,\n",
    "    'warmup_init_val':   warmup_init_val,\n",
    "}\n",
    "\n",
    "def cos_harness(new_params: dict) -> dict:\n",
    "    '''Creates cosine schedules with updated parameters in `new_params`\n",
    "    '''\n",
    "    # start from the given baseline `default_params`\n",
    "    cos_params = dict(DEFAULT_COS_PARAMS)\n",
    "    # update the with the new, given parameters\n",
    "    cos_params.update(new_params)\n",
    "    \n",
    "    # return the new cosine schedule\n",
    "    sched = cf_guidance.schedules.get_cos_sched(**cos_params)\n",
    "    return sched\n",
    "\n",
    "\n",
    "# build the static schedule\n",
    "static_sched = [baseline_g] * num_steps\n",
    "\n",
    "# build the inverted kdecay schedule\n",
    "k_sched = cos_harness({'k_decay': 0.2})\n",
    "inv_k_sched = [max_val - g + min_val for g in k_sched]\n",
    "\n",
    "# group the schedules \n",
    "schedules = {\n",
    "    'cosine': {'g': inv_k_sched},\n",
    "    'static': {'g': static_sched},\n",
    "}\n",
    "######################################\n",
    "######################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export \n",
    "\n",
    "def compare_dynamic_guidance(prompt):\n",
    "    '''\n",
    "    Compares the default, static Classifier-free Guidance to a dynamic schedule.  \n",
    "\n",
    "    Model and sampling paramters:\n",
    "        Stable Diffusion 2 v-model\n",
    "        Half-precision\n",
    "        DPM++ 2M sampler, with Karras sigma schedule\n",
    "        18 sampling steps\n",
    "        (768 x 768) image\n",
    "        Using a generic negative prompt\n",
    "\n",
    "    Schedules:\n",
    "        Static guidance with scale of 9\n",
    "        Inverse kDecay (cosine variant) scheduled guidance\n",
    "    '''\n",
    "    # load the model\n",
    "    pipeline = load_model()\n",
    "\n",
    "    # stores the output images\n",
    "    res = []\n",
    "\n",
    "    # generate images with static and dynamic schedules\n",
    "    for (name,sched) in schedules.items():\n",
    "        # make the guidance norm\n",
    "        gtfm = cf_guidance.transforms.GuidanceTfm(sched)\n",
    "        # generate the image\n",
    "        with torch.autocast(device), torch.no_grad():\n",
    "            img = pipeline.generate(prompt, gtfm, **generation_kwargs)\n",
    "        # add the generated image\n",
    "        res.append(name)\n",
    "\n",
    "    # return the generated images\n",
    "    return {\n",
    "        'values': res,\n",
    "        'label': 'Cosine vs. Static CFG'\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#| export\n",
    "\n",
    "iface = gr.Interface(\n",
    "    compare_dynamic_guidance,\n",
    "    inputs=\"text\",\n",
    "    outputs=gr.Gallery(),\n",
    "    title=\"Comparison with dynamic Classifier-free Guidance Comparison\",\n",
    ")\n",
    "iface.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nbdev\n",
    "nbdev.export.nb_export('app.ipynb', '')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "sdiffkernel",
   "language": "python",
   "name": "sdiffkernel"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "7aa72ffd68a1153f913726b8656445c52d825f656451987cb25ebe84c64ea44d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}