-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathinfer.py
More file actions
126 lines (106 loc) · 4.83 KB
/
Copy pathinfer.py
File metadata and controls
126 lines (106 loc) · 4.83 KB
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
import torch
import os
from PIL import Image
import numpy as np
from diffusers import AutoencoderKLWan
from transformers import CLIPVisionModel
from diffusers.video_processor import VideoProcessor
from diffusers import UniPCMultistepScheduler
from diffusers.utils import export_to_video, load_image
from diffusers.image_processor import VaeImageProcessor
from models.transformer_a2 import A2Model
from models.pipeline_a2 import A2Pipeline
from models.utils import _crop_and_resize_pad, _crop_and_resize, write_mp4
from huggingface_hub import snapshot_download
prompt = "A man is holding a teddy bear in the forest."
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"
refer_images = ['assets/human.png', 'assets/thing.png', 'assets/env.png']
width = 832
height = 480
seed = 42
use_teacache = True
if use_teacache:
tea_cache_l1_thresh = 0.3
tea_cache_model_id = "Wan2.1-I2V-14B-480P"
else:
tea_cache_l1_thresh = None
tea_cache_model_id = ""
# model parameters
device = "cuda"
video_path = "output.mp4"
pipeline_path = "Skywork/SkyReels-A2"
dtype = torch.bfloat16
# download models
snapshot_download(repo_id="Skywork/SkyReels-A2", local_dir="Skywork/SkyReels-A2")
# load models
image_encoder = CLIPVisionModel.from_pretrained(pipeline_path, subfolder="image_encoder", torch_dtype=torch.float32)
vae = AutoencoderKLWan.from_pretrained(pipeline_path, subfolder="vae", torch_dtype=torch.float32)
# print("load transformer...")
model_path = os.path.join(pipeline_path, 'transformer')
transformer = A2Model.from_pretrained(model_path, torch_dtype=dtype, use_safetensors=True)
# # transformer.save_pretrained("transformer", max_shard_size="5GB")
transformer.to(device, dtype=dtype)
pipe = A2Pipeline.from_pretrained(pipeline_path, transformer=transformer, vae=vae, image_encoder=image_encoder, torch_dtype=dtype)
scheduler = UniPCMultistepScheduler(prediction_type='flow_prediction', use_flow_sigmas=True, num_train_timesteps=1000, flow_shift=8)
pipe.scheduler = scheduler
pipe.to(device)
VAE_SCALE_FACTOR_SPATIAL = 8
video_processor = VideoProcessor(vae_scale_factor=VAE_SCALE_FACTOR_SPATIAL)
# prepare reference images
clip_image_list = []
vae_image_list = []
for image_id, image_path in enumerate(refer_images):
image = load_image(image=image_path).convert("RGB")
# for clip
image_clip = _crop_and_resize_pad(image, height=512, width=512)
clip_image_list.append(image_clip)
# for vae
if image_id == 0 or image_id == 1:
image_vae = _crop_and_resize_pad(image, height=height, width=width) # ref image
else:
image_vae = _crop_and_resize(image, height=height, width=width) # background image
image_vae = video_processor.preprocess(image_vae, height=height, width=width).to(memory_format=torch.contiguous_format) # (1, 3, 480, 320)
image_vae = image_vae.unsqueeze(2).to(device, dtype=torch.float32)
vae_image_list.append(image_vae) #.to(device, dtype=dtype))
# forward
generator = torch.Generator(device).manual_seed(seed)
video_pt = pipe(
image_clip=clip_image_list,
image_vae=vae_image_list,
prompt=prompt,
negative_prompt=negative_prompt,
height=480,
width=width,
num_frames=81,
guidance_scale=5.0,
generator=generator,
output_type="pt",
num_inference_steps=50,
vae_combine="before",
tea_cache_l1_thresh=tea_cache_l1_thresh,
tea_cache_model_id=tea_cache_model_id,
).frames
# combine results
batch_size = video_pt.shape[0]
batch_video_frames = []
for batch_idx in range(batch_size):
pt_image = video_pt[batch_idx]
pt_image = torch.stack([pt_image[i] for i in range(pt_image.shape[0])])
pt_image = pt_image[12:]
image_np = VaeImageProcessor.pt_to_numpy(pt_image)
image_pil = VaeImageProcessor.numpy_to_pil(image_np)
batch_video_frames.append(image_pil)
video_generate = batch_video_frames[0]
final_images = []
for q in range(len(video_generate)):
frame1 = _crop_and_resize_pad(load_image(image=refer_images[0]), height, width)
frame2 = _crop_and_resize_pad(load_image(image=refer_images[1]), height, width)
frame3 = _crop_and_resize_pad(load_image(image=refer_images[2]), height, width)
frame4 = Image.fromarray(np.array(video_generate[q])).convert("RGB")
result = Image.new('RGB', (width * 4, height),color="white")
result.paste(frame1, (0, 0))
result.paste(frame2, (width, 0))
result.paste(frame3, (width*2, 0))
result.paste(frame4, (width*3, 0))
final_images.append(np.array(result))
write_mp4(video_path, final_images, fps=15)