hasanfaesal's picture
Upload folder using huggingface_hub
71a2345 verified
Raw History Blame Contribute Delete
3.48 kB
"""Generate image-to-video clips with Wan 2.2 TI2V-5B for every row of prompts.jsonl.
Each row is rendered once per seed at native 720p (121 frames, 24 fps, 50 steps).
Existing clips are skipped, so the script can be re-run after an interruption.
Timings go to out/log.csv.
python gen.py --limit 1 # pilot: first clip only
python gen.py # everything
"""
import argparse
import csv
import json
import time
from pathlib import Path
import torch
from diffusers import AutoencoderKLWan, WanImageToVideoPipeline
from diffusers.utils import export_to_video, load_image
HERE = Path(__file__).parent
MODEL_ID = "Wan-AI/Wan2.2-TI2V-5B-Diffusers"
# Default negative prompt from the Wan 2.2 model card.
NEGATIVE = "色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--prompts", default=HERE / "prompts.jsonl", type=Path)
ap.add_argument("--images", default=HERE / "images-resize", type=Path)
ap.add_argument("--out", default=HERE / "out", type=Path)
ap.add_argument("--seeds", default=[0, 1], type=int, nargs="+")
ap.add_argument("--limit", type=int, help="stop after this many clips")
args = ap.parse_args()
rows = [json.loads(line) for line in args.prompts.read_text().splitlines() if line.strip()]
jobs = [(r, s) for r in rows for s in args.seeds]
if args.limit:
jobs = jobs[: args.limit]
args.out.mkdir(exist_ok=True)
t0 = time.time()
vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID, vae=vae, torch_dtype=torch.bfloat16).to("cuda")
print(f"model loaded in {time.time() - t0:.0f}s", flush=True)
log_path = args.out / "log.csv"
new_log = not log_path.exists()
with open(log_path, "a", newline="") as f:
log = csv.writer(f)
if new_log:
log.writerow(["clip", "image", "group", "condition", "seed", "seconds"])
for i, (row, seed) in enumerate(jobs, 1):
name = f"{Path(row['image']).stem}__{row['condition']}__s{seed}.mp4"
dst = args.out / name
if dst.exists():
print(f"[{i}/{len(jobs)}] skip {name}", flush=True)
continue
image = load_image(str(args.images / row["image"]))
t = time.time()
frames = pipe(
image=image,
prompt=row["prompt"],
negative_prompt=NEGATIVE,
height=image.height,
width=image.width,
num_frames=121,
num_inference_steps=50,
guidance_scale=5.0,
generator=torch.Generator("cuda").manual_seed(seed),
).frames[0]
export_to_video(frames, str(dst), fps=24)
secs = time.time() - t
log.writerow([name, row["image"], row["group"], row["condition"], seed, f"{secs:.1f}"])
f.flush()
print(f"[{i}/{len(jobs)}] {name} {secs:.0f}s", flush=True)
if __name__ == "__main__":
main()