|
19 | 19 | import PIL
|
20 | 20 | import regex as re
|
21 | 21 | import torch
|
22 |
| -from transformers import AutoTokenizer, CLIPImageProcessor, CLIPVisionModelWithProjection, UMT5EncoderModel |
| 22 | +from transformers import AutoTokenizer, CLIPImageProcessor, CLIPVisionModel, UMT5EncoderModel |
23 | 23 |
|
24 | 24 | from ...callbacks import MultiPipelineCallbacks, PipelineCallback
|
25 | 25 | from ...image_processor import PipelineImageInput
|
|
46 | 46 | Examples:
|
47 | 47 | ```python
|
48 | 48 | >>> import torch
|
| 49 | + >>> import numpy as np |
49 | 50 | >>> from diffusers import AutoencoderKLWan, WanImageToVideoPipeline
|
50 | 51 | >>> from diffusers.utils import export_to_video, load_image
|
| 52 | + >>> from transformers import CLIPVisionModel |
51 | 53 |
|
52 |
| - >>> # Available models: Wan-AI/Wan2.1-I2V-14B-480P-Diffusers, Wan-AI/Wan2.1-I2V-1.3B-720P-Diffusers |
| 54 | + >>> # Available models: Wan-AI/Wan2.1-I2V-14B-480P-Diffusers, Wan-AI/Wan2.1-I2V-14B-720P-Diffusers |
53 | 55 | >>> model_id = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
|
| 56 | + >>> image_encoder = CLIPVisionModel.from_pretrained( |
| 57 | + ... model_id, subfolder="image_encoder", torch_dtype=torch.float32 |
| 58 | + ... ) |
54 | 59 | >>> vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
55 |
| - >>> pipe = WanImageToVideoPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) |
| 60 | + >>> pipe = WanImageToVideoPipeline.from_pretrained( |
| 61 | + ... model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16 |
| 62 | + ... ) |
56 | 63 | >>> pipe.to("cuda")
|
57 | 64 |
|
58 |
| - >>> height, width = 480, 832 |
59 | 65 | >>> image = load_image(
|
60 | 66 | ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
|
61 |
| - ... ).resize((width, height)) |
| 67 | + ... ) |
| 68 | + >>> max_area = 480 * 832 |
| 69 | + >>> aspect_ratio = image.height / image.width |
| 70 | + >>> mod_value = pipe.vae_scale_factor_spatial * pipe.transformer.config.patch_size[1] |
| 71 | + >>> height = round(np.sqrt(max_area * aspect_ratio)) // mod_value * mod_value |
| 72 | + >>> width = round(np.sqrt(max_area / aspect_ratio)) // mod_value * mod_value |
| 73 | + >>> image = image.resize((width, height)) |
62 | 74 | >>> prompt = (
|
63 | 75 | ... "An astronaut hatching from an egg, on the surface of the moon, the darkness and depth of space realised in "
|
64 | 76 | ... "the background. High quality, ultrarealistic detail and breath-taking movie-like camera shot."
|
65 | 77 | ... )
|
66 | 78 | >>> negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"
|
67 | 79 |
|
68 | 80 | >>> output = pipe(
|
69 |
| - ... image=image, prompt=prompt, negative_prompt=negative_prompt, num_frames=81, guidance_scale=5.0 |
| 81 | + ... image=image, |
| 82 | + ... prompt=prompt, |
| 83 | + ... negative_prompt=negative_prompt, |
| 84 | + ... height=height, |
| 85 | + ... width=width, |
| 86 | + ... num_frames=81, |
| 87 | + ... guidance_scale=5.0, |
70 | 88 | ... ).frames[0]
|
71 |
| - >>> export_to_video(output, "output.mp4", fps=15) |
| 89 | + >>> export_to_video(output, "output.mp4", fps=16) |
72 | 90 | ```
|
73 | 91 | """
|
74 | 92 |
|
@@ -137,7 +155,7 @@ def __init__(
|
137 | 155 | self,
|
138 | 156 | tokenizer: AutoTokenizer,
|
139 | 157 | text_encoder: UMT5EncoderModel,
|
140 |
| - image_encoder: CLIPVisionModelWithProjection, |
| 158 | + image_encoder: CLIPVisionModel, |
141 | 159 | image_processor: CLIPImageProcessor,
|
142 | 160 | transformer: WanTransformer3DModel,
|
143 | 161 | vae: AutoencoderKLWan,
|
@@ -204,7 +222,7 @@ def _get_t5_prompt_embeds(
|
204 | 222 | def encode_image(self, image: PipelineImageInput):
|
205 | 223 | image = self.image_processor(images=image, return_tensors="pt").to(self.device)
|
206 | 224 | image_embeds = self.image_encoder(**image, output_hidden_states=True)
|
207 |
| - return image_embeds.hidden_states[-1] |
| 225 | + return image_embeds.hidden_states[-2] |
208 | 226 |
|
209 | 227 | # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.encode_prompt
|
210 | 228 | def encode_prompt(
|
|
0 commit comments