|
| 1 | +<!--Copyright 2023 The HuggingFace Team. All rights reserved. |
| 2 | + |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | + |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + |
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +--> |
| 12 | + |
| 13 | +# Text-to-video synthesis |
| 14 | + |
| 15 | +Text-to-video synthesis from [ModelScope](https://modelscope.cn/) can be considered the same as Stable Diffusion structure-wise but it is extended to videos instead of static images. More specifically, this system allows us to generate videos from a natural language text prompt. |
| 16 | + |
| 17 | +From the [model summary](https://huggingface.co/damo-vilab/modelscope-damo-text-to-video-synthesis): |
| 18 | + |
| 19 | +*This model is based on a multi-stage text-to-video generation diffusion model, which inputs a description text and returns a video that matches the text description. Only English input is supported.* |
| 20 | + |
| 21 | +Resources: |
| 22 | + |
| 23 | +* [Website](https://modelscope.cn/models/damo/text-to-video-synthesis/summary) |
| 24 | +* [GitHub repository](https://github.com/modelscope/modelscope/) |
| 25 | +* [Spaces] (TODO) |
| 26 | + |
| 27 | +## Available Pipelines: |
| 28 | + |
| 29 | +| Pipeline | Tasks | Demo |
| 30 | +|---|---|:---:| |
| 31 | +| [DiffusionPipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py) | *Text-to-Video Generation* | [Spaces] (TODO) |
| 32 | + |
| 33 | +## Usage example |
| 34 | + |
| 35 | +Let's start by generating a short video with the default length of 16 frames (2s at 8 fps): |
| 36 | + |
| 37 | +```python |
| 38 | +import torch |
| 39 | +from diffusers import DiffusionPipeline |
| 40 | +from diffusers.utils import export_to_video |
| 41 | + |
| 42 | +pipe = DiffusionPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b", torch_dtype=torch.float16, variant="fp16") |
| 43 | +pipe = pipe.to("cuda") |
| 44 | + |
| 45 | +prompt = "Spiderman is surfing" |
| 46 | +video_frames = pipe(prompt).frames |
| 47 | +video_path = export_to_video(video_frames) |
| 48 | +video_path |
| 49 | +``` |
| 50 | + |
| 51 | +Diffusers supports different optimization techniques to improve the latency |
| 52 | +and memory footprint of a pipeline. Since videos are often more memory-heavy than images, |
| 53 | +we can enable CPU offloading and VAE slicing to keep the memory footprint at bay. |
| 54 | + |
| 55 | +Let's generate a video of 8 seconds (64 frames) on the same GPU using CPU offloading and VAE slicing: |
| 56 | + |
| 57 | +```python |
| 58 | +import torch |
| 59 | +from diffusers import DiffusionPipeline |
| 60 | +from diffusers.utils import export_to_video |
| 61 | + |
| 62 | +pipe = DiffusionPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b", torch_dtype=torch.float16, variant="fp16") |
| 63 | +pipe.enable_model_cpu_offload() |
| 64 | + |
| 65 | +# memory optimization |
| 66 | +pipe.enable_vae_slicing() |
| 67 | + |
| 68 | +prompt = "Darth Vader surfing a wave" |
| 69 | +video_frames = pipe(prompt, num_frames=64).frames |
| 70 | +video_path = export_to_video(video_frames) |
| 71 | +video_path |
| 72 | +``` |
| 73 | + |
| 74 | +It just takes **7 GBs of GPU memory** to generate the 64 video frames using PyTorch 2.0, "fp16" precision and the techniques mentioned above. |
| 75 | + |
| 76 | +We can also use a different scheduler easily, using the same method we'd use for Stable Diffusion: |
| 77 | + |
| 78 | +```python |
| 79 | +import torch |
| 80 | +from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler |
| 81 | +from diffusers.utils import export_to_video |
| 82 | + |
| 83 | +pipe = DiffusionPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b", torch_dtype=torch.float16, variant="fp16") |
| 84 | +pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) |
| 85 | +pipe.enable_model_cpu_offload() |
| 86 | + |
| 87 | +prompt = "Spiderman is surfing" |
| 88 | +video_frames = pipe(prompt, num_inference_steps=25).frames |
| 89 | +video_path = export_to_video(video_frames) |
| 90 | +video_path |
| 91 | +``` |
| 92 | + |
| 93 | +Here are some sample outputs: |
| 94 | + |
| 95 | +<table> |
| 96 | + <tr> |
| 97 | + <td><center> |
| 98 | + An astronaut riding a horse. |
| 99 | + <br> |
| 100 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astr.gif" |
| 101 | + alt="An astronaut riding a horse." |
| 102 | + style="width: 300px;" /> |
| 103 | + </center></td> |
| 104 | + <td ><center> |
| 105 | + Darth vader surfing in waves. |
| 106 | + <br> |
| 107 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/vader.gif" |
| 108 | + alt="Darth vader surfing in waves." |
| 109 | + style="width: 300px;" /> |
| 110 | + </center></td> |
| 111 | + </tr> |
| 112 | +</table> |
| 113 | + |
| 114 | +## Available checkpoints |
| 115 | + |
| 116 | +* [damo-vilab/text-to-video-ms-1.7b](https://huggingface.co/damo-vilab/text-to-video-ms-1.7b/) |
| 117 | +* [damo-vilab/text-to-video-ms-1.7b-legacy](https://huggingface.co/damo-vilab/text-to-video-ms-1.7b-legacy) |
| 118 | + |
| 119 | +## DiffusionPipeline |
| 120 | +[[autodoc]] DiffusionPipeline |
| 121 | + - all |
| 122 | + - __call__ |
0 commit comments