h1t commited on
Commit
4cd1133
β€’
1 Parent(s): 33e9427

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +0 -377
README.md CHANGED
@@ -7,380 +7,3 @@ colorFrom: red
7
  colorTo: green
8
  short_description: Official Demo Space for Trajectory Consistency Distillation
9
  ---
10
- # Trajectory Consistency Distillation
11
-
12
-
13
- Official Repository of the paper: [Trajectory Consistency Distillation]()
14
-
15
- ![](./assets/teaser_fig.png)
16
-
17
- ## πŸ“£ News
18
- - (πŸ”₯New) 2024/2/29 We provided a demo of TCD on πŸ€— Hugging Face Space. Try it out [here](https://huggingface.co/spaces/h1t/TCD-SDXL-LoRA).
19
- - (πŸ”₯New) 2024/2/29 We released our model [TCD-SDXL-Lora](https://huggingface.co/h1t/TCD-SDXL-LoRA) in πŸ€— Hugging Face.
20
- - (πŸ”₯New) 2024/2/29 TCD is now integrated into the 🧨 Diffusers library. Please refer to the [Usage](#usage-anchor) for more information.
21
-
22
- ## Introduction
23
-
24
- TCD, inspired by [Consistency Models](https://arxiv.org/abs/2303.01469), is a novel distillation technology that enables the distillation of knowledge from pre-trained diffusion models into a few-step sampler. In this repository, we release the inference code and our model named TCD-SDXL, which is distilled from [SDXL Base 1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0). We provide the LoRA checkpoint in this πŸ”₯[repository](https://huggingface.co/h1t/TCD-SDXL-LoRA).
25
-
26
- ⭐ TCD has following advantages:
27
-
28
- - `High-Quality with Few-Step`: TCD significantly surpasses the previous state-of-the-art few-step text-to-image model [LCM](https://github.com/luosiallen/latent-consistency-model/tree/main) in terms of image quality. Notably, LCM experiences a notable decline in quality at high NFEs. In contrast, _**TCD maintains superior generative quality at high NFEs, even exceeding the performance of DPM-Solver++(2S) with origin SDXL**_.
29
- ![](./assets/teaser.jpeg)
30
- <!-- We observed that the images generated with 8 steps by TCD-SDXL are already highly impressive, even outperforming the original SDXL 50-steps generation results. -->
31
- - `Versatility`: Integrated with LoRA technology, TCD can be directly applied to various models (including the custom Community Models, styled LoRA, ControlNet, IP-Adapter) that share the same backbone, as demonstrated in the [Usage](#usage-anchor).
32
- ![](./assets/versatility.png)
33
- - `Avoiding Mode Collapse`: TCD achieves few-step generation without the need for adversarial training, thus circumventing mode collapse caused by the GAN objective.
34
- In contrast to the concurrent work [SDXL-Lightning](https://huggingface.co/ByteDance/SDXL-Lightning), which relies on Adversarial Diffusion Distillation, TCD can synthesize results that are more realistic and slightly more diverse, without the presence of "Janus" artifacts.
35
- ![](./assets/compare_sdxl_lightning.png)
36
-
37
- For more information, please refer to our paper [Trajectory Consistency Distillation]().
38
-
39
- <a id="usage-anchor"></a>
40
-
41
- ## Usage
42
- To run the model yourself, you can leverage the 🧨 Diffusers library.
43
- ```bash
44
- pip install diffusers transformers accelerate peft
45
- ```
46
- And then we clone the repo.
47
- ```bash
48
- git clone https://github.com/jabir-zheng/TCD.git
49
- cd TCD
50
- ```
51
- Here, we demonstrate the applicability of our TCD LoRA to various models, including [SDXL](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0), [SDXL Inpainting](https://huggingface.co/diffusers/stable-diffusion-xl-1.0-inpainting-0.1), a community model named [Animagine XL](https://huggingface.co/cagliostrolab/animagine-xl-3.0), a styled LoRA [Papercut](https://huggingface.co/TheLastBen/Papercut_SDXL), pretrained [Depth Controlnet](https://huggingface.co/diffusers/controlnet-depth-sdxl-1.0), [Canny Controlnet](https://huggingface.co/diffusers/controlnet-canny-sdxl-1.0) and [IP-Adapter](https://github.com/tencent-ailab/IP-Adapter) to accelerate image generation with high quality in few steps.
52
-
53
- ### Text-to-Image generation
54
- ```py
55
- import torch
56
- from diffusers import StableDiffusionXLPipeline
57
- from scheduling_tcd import TCDScheduler
58
-
59
- device = "cuda"
60
- base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
61
- tcd_lora_id = "h1t/TCD-SDXL-LoRA"
62
-
63
- pipe = StableDiffusionXLPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, variant="fp16").to(device)
64
- pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
65
-
66
- pipe.load_lora_weights(tcd_lora_id)
67
- pipe.fuse_lora()
68
-
69
- prompt = "Beautiful woman, bubblegum pink, lemon yellow, minty blue, futuristic, high-detail, epic composition, watercolor."
70
-
71
- image = pipe(
72
- prompt=prompt,
73
- num_inference_steps=4,
74
- guidance_scale=0,
75
- # Eta (referred to as `gamma` in the paper) is used to control the stochasticity in every step.
76
- # A value of 0.3 often yields good results.
77
- # We recommend using a higher eta when increasing the number of inference steps.
78
- eta=0.3,
79
- generator=torch.Generator(device=device).manual_seed(0),
80
- ).images[0]
81
- ```
82
- ![](./assets/t2i_tcd.png)
83
-
84
- ### Inpainting
85
- ```py
86
- import torch
87
- from diffusers import AutoPipelineForInpainting
88
- from diffusers.utils import load_image, make_image_grid
89
- from scheduling_tcd import TCDScheduler
90
-
91
- device = "cuda"
92
- base_model_id = "diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
93
- tcd_lora_id = "h1t/TCD-SDXL-LoRA"
94
-
95
- pipe = AutoPipelineForInpainting.from_pretrained(base_model_id, torch_dtype=torch.float16, variant="fp16").to(device)
96
- pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
97
-
98
- pipe.load_lora_weights(tcd_lora_id)
99
- pipe.fuse_lora()
100
-
101
- img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
102
- mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
103
-
104
- init_image = load_image(img_url).resize((1024, 1024))
105
- mask_image = load_image(mask_url).resize((1024, 1024))
106
-
107
- prompt = "a tiger sitting on a park bench"
108
-
109
- image = pipe(
110
- prompt=prompt,
111
- image=init_image,
112
- mask_image=mask_image,
113
- num_inference_steps=8,
114
- guidance_scale=0,
115
- eta=0.3, # Eta (referred to as `gamma` in the paper) is used to control the stochasticity in every step. A value of 0.3 often yields good results.
116
- strength=0.99, # make sure to use `strength` below 1.0
117
- generator=torch.Generator(device=device).manual_seed(0),
118
- ).images[0]
119
-
120
- grid_image = make_image_grid([init_image, mask_image, image], rows=1, cols=3)
121
- ```
122
- ![](./assets/inpainting_tcd.png)
123
-
124
- ### Versatile for Community Models
125
- ```py
126
- import torch
127
- from diffusers import StableDiffusionXLPipeline
128
- from scheduling_tcd import TCDScheduler
129
-
130
- device = "cuda"
131
- base_model_id = "cagliostrolab/animagine-xl-3.0"
132
- tcd_lora_id = "h1t/TCD-SDXL-LoRA"
133
-
134
- pipe = StableDiffusionXLPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, variant="fp16").to(device)
135
- pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
136
-
137
- pipe.load_lora_weights(tcd_lora_id)
138
- pipe.fuse_lora()
139
-
140
- prompt = "A man, clad in a meticulously tailored military uniform, stands with unwavering resolve. The uniform boasts intricate details, and his eyes gleam with determination. Strands of vibrant, windswept hair peek out from beneath the brim of his cap."
141
-
142
- image = pipe(
143
- prompt=prompt,
144
- num_inference_steps=8,
145
- guidance_scale=0,
146
- # Eta (referred to as `gamma` in the paper) is used to control the stochasticity in every step.
147
- # A value of 0.3 often yields good results.
148
- # We recommend using a higher eta when increasing the number of inference steps.
149
- eta=0.3,
150
- generator=torch.Generator(device=device).manual_seed(0),
151
- ).images[0]
152
- ```
153
- ![](./assets/animagine_xl.png)
154
-
155
- ### Combine with styled LoRA
156
- ```py
157
- import torch
158
- from diffusers import StableDiffusionXLPipeline
159
- from scheduling_tcd import TCDScheduler
160
-
161
- device = "cuda"
162
- base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
163
- tcd_lora_id = "h1t/TCD-SDXL-LoRA"
164
- styled_lora_id = "TheLastBen/Papercut_SDXL"
165
-
166
- pipe = StableDiffusionXLPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, variant="fp16").to(device)
167
- pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
168
-
169
- pipe.load_lora_weights(tcd_lora_id, adapter_name="tcd")
170
- pipe.load_lora_weights(styled_lora_id, adapter_name="style")
171
- pipe.set_adapters(["tcd", "style"], adapter_weights=[1.0, 1.0])
172
-
173
- prompt = "papercut of a winter mountain, snow"
174
-
175
- image = pipe(
176
- prompt=prompt,
177
- num_inference_steps=4,
178
- guidance_scale=0,
179
- # Eta (referred to as `gamma` in the paper) is used to control the stochasticity in every step.
180
- # A value of 0.3 often yields good results.
181
- # We recommend using a higher eta when increasing the number of inference steps.
182
- eta=0.3,
183
- generator=torch.Generator(device=device).manual_seed(0),
184
- ).images[0]
185
- ```
186
- ![](./assets/styled_lora.png)
187
-
188
- ### Compatibility with ControlNet
189
- #### Depth ControlNet
190
- ```py
191
- import torch
192
- import numpy as np
193
- from PIL import Image
194
- from transformers import DPTFeatureExtractor, DPTForDepthEstimation
195
- from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline
196
- from diffusers.utils import load_image, make_image_grid
197
- from scheduling_tcd import TCDScheduler
198
-
199
- device = "cuda"
200
- depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(device)
201
- feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
202
-
203
- def get_depth_map(image):
204
- image = feature_extractor(images=image, return_tensors="pt").pixel_values.to(device)
205
- with torch.no_grad(), torch.autocast(device):
206
- depth_map = depth_estimator(image).predicted_depth
207
-
208
- depth_map = torch.nn.functional.interpolate(
209
- depth_map.unsqueeze(1),
210
- size=(1024, 1024),
211
- mode="bicubic",
212
- align_corners=False,
213
- )
214
- depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
215
- depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
216
- depth_map = (depth_map - depth_min) / (depth_max - depth_min)
217
- image = torch.cat([depth_map] * 3, dim=1)
218
-
219
- image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
220
- image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
221
- return image
222
-
223
- base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
224
- controlnet_id = "diffusers/controlnet-depth-sdxl-1.0"
225
- tcd_lora_id = "h1t/TCD-SDXL-LoRA"
226
-
227
- controlnet = ControlNetModel.from_pretrained(
228
- controlnet_id,
229
- torch_dtype=torch.float16,
230
- variant="fp16",
231
- ).to(device)
232
- pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
233
- base_model_id,
234
- controlnet=controlnet,
235
- torch_dtype=torch.float16,
236
- variant="fp16",
237
- ).to(device)
238
- pipe.enable_model_cpu_offload()
239
-
240
- pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
241
-
242
- pipe.load_lora_weights(tcd_lora_id)
243
- pipe.fuse_lora()
244
-
245
- prompt = "stormtrooper lecture, photorealistic"
246
-
247
- image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")
248
- depth_image = get_depth_map(image)
249
-
250
- controlnet_conditioning_scale = 0.5 # recommended for good generalization
251
-
252
- image = pipe(
253
- prompt,
254
- image=depth_image,
255
- num_inference_steps=4,
256
- guidance_scale=0,
257
- eta=0.3, # A parameter (referred to as `gamma` in the paper) is used to control the stochasticity in every step. A value of 0.3 often yields good results.
258
- controlnet_conditioning_scale=controlnet_conditioning_scale,
259
- generator=torch.Generator(device=device).manual_seed(0),
260
- ).images[0]
261
-
262
- grid_image = make_image_grid([depth_image, image], rows=1, cols=2)
263
- ```
264
- ![](./assets/controlnet_depth_tcd.png)
265
-
266
- #### Canny ControlNet
267
- ```py
268
- import torch
269
- from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline
270
- from diffusers.utils import load_image, make_image_grid
271
- from scheduling_tcd import TCDScheduler
272
-
273
- device = "cuda"
274
- base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
275
- controlnet_id = "diffusers/controlnet-canny-sdxl-1.0"
276
- tcd_lora_id = "h1t/TCD-SDXL-LoRA"
277
-
278
- controlnet = ControlNetModel.from_pretrained(
279
- controlnet_id,
280
- torch_dtype=torch.float16,
281
- variant="fp16",
282
- ).to(device)
283
- pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
284
- base_model_id,
285
- controlnet=controlnet,
286
- torch_dtype=torch.float16,
287
- variant="fp16",
288
- ).to(device)
289
- pipe.enable_model_cpu_offload()
290
-
291
- pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
292
-
293
- pipe.load_lora_weights(tcd_lora_id)
294
- pipe.fuse_lora()
295
-
296
- prompt = "ultrarealistic shot of a furry blue bird"
297
-
298
- canny_image = load_image("https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png")
299
-
300
- controlnet_conditioning_scale = 0.5 # recommended for good generalization
301
-
302
- image = pipe(
303
- prompt,
304
- image=canny_image,
305
- num_inference_steps=4,
306
- guidance_scale=0,
307
- eta=0.3, # A parameter (referred to as `gamma` in the paper) is used to control the stochasticity in every step. A value of 0.3 often yields good results.
308
- controlnet_conditioning_scale=controlnet_conditioning_scale,
309
- generator=torch.Generator(device=device).manual_seed(0),
310
- ).images[0]
311
-
312
- grid_image = make_image_grid([canny_image, image], rows=1, cols=2)
313
- ```
314
-
315
- ![](./assets/controlnet_canny_tcd.png)
316
-
317
- ### Compatibility with IP-Adapter
318
- ⚠️ Please refer to the official [repository](https://github.com/tencent-ailab/IP-Adapter/tree/main) for instructions on installing dependencies for IP-Adapter.
319
- ```py
320
- import torch
321
- from diffusers import StableDiffusionXLPipeline
322
- from diffusers.utils import load_image, make_image_grid
323
-
324
- from ip_adapter import IPAdapterXL
325
- from scheduling_tcd import TCDScheduler
326
-
327
- device = "cuda"
328
- base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
329
- image_encoder_path = "sdxl_models/image_encoder"
330
- ip_ckpt = "sdxl_models/ip-adapter_sdxl.bin"
331
- tcd_lora_id = "h1t/TCD-SDXL-LoRA"
332
-
333
- pipe = StableDiffusionXLPipeline.from_pretrained(
334
- base_model_path,
335
- torch_dtype=torch.float16,
336
- variant="fp16"
337
- )
338
- pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
339
-
340
- pipe.load_lora_weights(tcd_lora_id)
341
- pipe.fuse_lora()
342
-
343
- ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device)
344
-
345
- ref_image = load_image("https://raw.githubusercontent.com/tencent-ailab/IP-Adapter/main/assets/images/woman.png").resize((512, 512))
346
-
347
- prompt = "best quality, high quality, wearing sunglasses"
348
-
349
- image = ip_model.generate(
350
- pil_image=ref_image,
351
- prompt=prompt,
352
- scale=0.5,
353
- num_samples=1,
354
- num_inference_steps=4,
355
- guidance_scale=0,
356
- eta=0.3, # A parameter (referred to as `gamma` in the paper) is used to control the stochasticity in every step. A value of 0.3 often yields good results.
357
- seed=0,
358
- )[0]
359
-
360
- grid_image = make_image_grid([ref_image, image], rows=1, cols=2)
361
- ```
362
- ![](./assets/ip_adapter.png)
363
-
364
- ### Local Gradio Demo
365
- Install the `gradio` library first,
366
- ```bash
367
- pip install gradio==3.50.2
368
- ```
369
- then local gradio demo can be launched by:
370
- ```py
371
- python gradio_app.py
372
- ```
373
- ![](./assets/gradio_demo.png)
374
-
375
- ## Citation
376
- ```bibtex
377
- @article{zheng2024trajectory,
378
- title = {Trajectory Consistency Distillation},
379
- author = {Zheng, Jianbin and Hu, Minghui and Fan, Zhongyi and Wang, Chaoyue and Ding, Changxing and Tao, Dacheng and Cham, Tat-Jen},
380
- journal = {arXiv},
381
- year = {2024},
382
- }
383
- ```
384
-
385
- ## Acknowledgments
386
- This codebase heavily relies on the πŸ€—[Diffusers](https://github.com/huggingface/diffusers) library and [LCM](https://github.com/luosiallen/latent-consistency-model).
 
7
  colorTo: green
8
  short_description: Official Demo Space for Trajectory Consistency Distillation
9
  ---