File size: 1,635 Bytes
4c75661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""ArtiFixer μΊ‘μ…˜ μž„λ² λ”© (caption.h5) β€” artifixer ν™˜κ²½μ—μ„œ μ‹€ν–‰ν•œλ‹€ (UMT5-XXL ν…μŠ€νŠΈ 인코더, CPU).

  ~/micromamba/envs/artifixer/bin/python scripts/artifixer_caption.py outputs/artifixer/<μ€€λΉ„ 디렉터리> [...]

ν˜•μ‹μ€ artifixer data_processing/captioning/generate_captions.py 와 κ°™λ‹€: 데이터셋 ν•˜λ‚˜ = bf16 μž„λ² λ”©μ„ uint16 으둜 λ³Έ λ°°μ—΄
(토큰 수 Γ— 4096), attrs caption Β· image_indices. ν…μŠ€νŠΈ μΈμ½”λ”λŠ” 22 GB(fp32 파일) β€” bf16 으둜 CPU 에 μ˜¬λ¦°λ‹€ (VRAM 은 μ“°μ§€ μ•ŠμŒ).
"""

import sys
from pathlib import Path

import h5py
import torch

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "third_party/artifixer"))
from data_processing.captioning.generate_captions import generate_text_embedding  # noqa: E402
from transformers import AutoTokenizer, UMT5EncoderModel  # noqa: E402

MODEL_ID = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"


def main(dirs: list[str]) -> None:
    tok = AutoTokenizer.from_pretrained(MODEL_ID, subfolder="tokenizer")
    enc = UMT5EncoderModel.from_pretrained(MODEL_ID, subfolder="text_encoder", torch_dtype=torch.bfloat16).eval()
    for d in map(Path, dirs):
        cap = (d / "caption.txt").read_text().strip()
        with torch.inference_mode():
            emb = generate_text_embedding(cap, enc, tok, 512)
        with h5py.File(d / "caption.h5", "w") as hf:
            ds = hf.create_dataset("frames_0", data=emb)
            ds.attrs["caption"] = cap
            ds.attrs["image_indices"] = [0]
        print(f"{d / 'caption.h5'}: {emb.shape} β€” {cap[:60]}")


if __name__ == "__main__":
    main(sys.argv[1:])