Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# AnimatedDiff ControlNet SDXL Example
|
3 |
+
|
4 |
+
This document provides a step-by-step guide to setting up and running the `animatediff_controlnet_sdxl.py` script from the Hugging Face repository. The script leverages the `diffusers-sdxl-controlnet` library to generate animated images using ControlNet and SDXL models.
|
5 |
+
|
6 |
+
## Prerequisites
|
7 |
+
|
8 |
+
Before running the script, ensure you have the necessary dependencies installed. You can install them using the following commands:
|
9 |
+
|
10 |
+
### System Dependencies
|
11 |
+
|
12 |
+
```bash
|
13 |
+
sudo apt-get update && sudo apt-get install git-lfs cbm ffmpeg
|
14 |
+
```
|
15 |
+
|
16 |
+
### Python Dependencies
|
17 |
+
|
18 |
+
```bash
|
19 |
+
pip install git+https://huggingface.co/svjack/diffusers-sdxl-controlnet
|
20 |
+
pip install transformers peft sentencepiece moviepy controlnet_aux
|
21 |
+
```
|
22 |
+
|
23 |
+
### Clone the Repository
|
24 |
+
|
25 |
+
```bash
|
26 |
+
git clone https://huggingface.co/svjack/diffusers-sdxl-controlnet
|
27 |
+
```
|
28 |
+
|
29 |
+
## Script Modifications
|
30 |
+
|
31 |
+
The script requires some modifications to work correctly. Specifically, you need to comment out certain lines related to LoRA processors:
|
32 |
+
|
33 |
+
```python
|
34 |
+
'''
|
35 |
+
drop #LoRAAttnProcessor2_0,
|
36 |
+
#LoRAXFormersAttnProcessor,
|
37 |
+
'''
|
38 |
+
```
|
39 |
+
|
40 |
+
## GIF to Frames Conversion
|
41 |
+
|
42 |
+
The script includes a function to convert a GIF into individual frames. This is useful for preparing input data for the animation pipeline.
|
43 |
+
|
44 |
+
```python
|
45 |
+
from PIL import Image, ImageSequence
|
46 |
+
import os
|
47 |
+
|
48 |
+
def gif_to_frames(gif_path, output_folder):
|
49 |
+
# Open the GIF file
|
50 |
+
gif = Image.open(gif_path)
|
51 |
+
|
52 |
+
# Ensure the output folder exists
|
53 |
+
if not os.path.exists(output_folder):
|
54 |
+
os.makedirs(output_folder)
|
55 |
+
|
56 |
+
# Iterate through each frame of the GIF
|
57 |
+
for i, frame in enumerate(ImageSequence.Iterator(gif)):
|
58 |
+
# Copy the frame
|
59 |
+
frame_copy = frame.copy()
|
60 |
+
|
61 |
+
# Save the frame to the specified folder
|
62 |
+
frame_path = os.path.join(output_folder, f"frame_{i:04d}.png")
|
63 |
+
frame_copy.save(frame_path)
|
64 |
+
|
65 |
+
print(f"Successfully extracted {i + 1} frames to {output_folder}")
|
66 |
+
|
67 |
+
# Example call
|
68 |
+
gif_to_frames("girl-pose.gif", "girl_pose_frames")
|
69 |
+
```
|
70 |
+
|
71 |
+
## Running the Script
|
72 |
+
|
73 |
+
To run the script, follow these steps:
|
74 |
+
|
75 |
+
1. **Add the Script Path to System Path**:
|
76 |
+
|
77 |
+
```python
|
78 |
+
import sys
|
79 |
+
sys.path.insert(0, "diffusers-sdxl-controlnet/examples/community/")
|
80 |
+
from animatediff_controlnet_sdxl import *
|
81 |
+
from controlnet_aux.processor import Processor
|
82 |
+
```
|
83 |
+
|
84 |
+
2. **Load Necessary Libraries and Models**:
|
85 |
+
|
86 |
+
```python
|
87 |
+
import torch
|
88 |
+
from diffusers.models import MotionAdapter
|
89 |
+
from diffusers import DDIMScheduler
|
90 |
+
from diffusers.utils import export_to_gif
|
91 |
+
from diffusers import AutoPipelineForText2Image, ControlNetModel
|
92 |
+
from diffusers.utils import load_image
|
93 |
+
from PIL import Image
|
94 |
+
```
|
95 |
+
|
96 |
+
3. **Load the MotionAdapter Model**:
|
97 |
+
|
98 |
+
```python
|
99 |
+
adapter = MotionAdapter.from_pretrained(
|
100 |
+
"a-r-r-o-w/animatediff-motion-adapter-sdxl-beta",
|
101 |
+
torch_dtype=torch.float16
|
102 |
+
)
|
103 |
+
```
|
104 |
+
|
105 |
+
4. **Configure the Scheduler and ControlNet**:
|
106 |
+
|
107 |
+
```python
|
108 |
+
model_id = "svjack/GenshinImpact_XL_Base"
|
109 |
+
scheduler = DDIMScheduler.from_pretrained(
|
110 |
+
model_id,
|
111 |
+
subfolder="scheduler",
|
112 |
+
clip_sample=False,
|
113 |
+
timestep_spacing="linspace",
|
114 |
+
beta_schedule="linear",
|
115 |
+
steps_offset=1,
|
116 |
+
)
|
117 |
+
|
118 |
+
controlnet = ControlNetModel.from_pretrained(
|
119 |
+
"thibaud/controlnet-openpose-sdxl-1.0",
|
120 |
+
torch_dtype=torch.float16,
|
121 |
+
).to("cuda")
|
122 |
+
```
|
123 |
+
|
124 |
+
5. **Load the AnimateDiffSDXLControlnetPipeline**:
|
125 |
+
|
126 |
+
```python
|
127 |
+
pipe = AnimateDiffSDXLControlnetPipeline.from_pretrained(
|
128 |
+
model_id,
|
129 |
+
controlnet=controlnet,
|
130 |
+
motion_adapter=adapter,
|
131 |
+
scheduler=scheduler,
|
132 |
+
torch_dtype=torch.float16,
|
133 |
+
).to("cuda")
|
134 |
+
```
|
135 |
+
|
136 |
+
6. **Enable Memory Saving Features**:
|
137 |
+
|
138 |
+
```python
|
139 |
+
pipe.enable_vae_slicing()
|
140 |
+
pipe.enable_vae_tiling()
|
141 |
+
```
|
142 |
+
|
143 |
+
7. **Load Conditioning Frames**:
|
144 |
+
|
145 |
+
```python
|
146 |
+
import os
|
147 |
+
folder_path = "girl_pose_frames/"
|
148 |
+
frames = os.listdir(folder_path)
|
149 |
+
frames = list(filter(lambda x: x.endswith(".png"), frames))
|
150 |
+
frames.sort()
|
151 |
+
conditioning_frames = list(map(lambda x: Image.open(os.path.join(folder_path ,x)).resize((1024, 1024)), frames))[:16]
|
152 |
+
```
|
153 |
+
|
154 |
+
8. **Process Conditioning Frames**:
|
155 |
+
|
156 |
+
```python
|
157 |
+
p2 = Processor("openpose")
|
158 |
+
cn2 = [p2(frame) for frame in conditioning_frames]
|
159 |
+
```
|
160 |
+
|
161 |
+
9. **Define Prompts**:
|
162 |
+
|
163 |
+
```python
|
164 |
+
prompt = '''
|
165 |
+
solo,Xiangling\(genshin impact\),1girl,
|
166 |
+
full body professional photograph of a stunning detailed, sharp focus, dramatic
|
167 |
+
cinematic lighting, octane render unreal engine (film grain, blurry background
|
168 |
+
'''
|
169 |
+
prompt = "solo,Xiangling\(genshin impact\),1girl,full body professional photograph of a stunning detailed"
|
170 |
+
negative_prompt = "bad quality, worst quality, jpeg artifacts, ugly"
|
171 |
+
```
|
172 |
+
|
173 |
+
10. **Generate Output**:
|
174 |
+
|
175 |
+
```python
|
176 |
+
output = pipe(
|
177 |
+
prompt=prompt,
|
178 |
+
negative_prompt=negative_prompt,
|
179 |
+
num_inference_steps=50,
|
180 |
+
guidance_scale=20,
|
181 |
+
controlnet_conditioning_scale = 1.0,
|
182 |
+
width=512,
|
183 |
+
height=768,
|
184 |
+
num_frames=16,
|
185 |
+
conditioning_frames=cn2,
|
186 |
+
)
|
187 |
+
```
|
188 |
+
|
189 |
+
11. **Export Frames to GIF**:
|
190 |
+
|
191 |
+
```python
|
192 |
+
frames = output.frames[0]
|
193 |
+
export_to_gif(frames, "keqing_animation.gif")
|
194 |
+
```
|
195 |
+
|
196 |
+
12. **Display the Result**:
|
197 |
+
|
198 |
+
```python
|
199 |
+
from IPython import display
|
200 |
+
display.Image("keqing_animation.gif")
|
201 |
+
```
|
202 |
+
|
203 |
+
## Conclusion
|
204 |
+
|
205 |
+
This script demonstrates how to use the `diffusers-sdxl-controlnet` library to generate animated images with ControlNet and SDXL models. By following the steps outlined above, you can create and visualize your own animated sequences.
|