Ouzhang's picture
Add files using upload-large-folder tool
3bdb1d6 verified
Raw
History Blame Contribute Delete
4.29 kB
# Adopted from https://github.com/guandeh17/Self-Forcing
# SPDX-License-Identifier: Apache-2.0
from torch.utils.data import Dataset
import numpy as np
import torch
import lmdb
import json
from pathlib import Path
from PIL import Image
import os
import datasets
class TextDataset(Dataset):
def __init__(self, prompt_path, extended_prompt_path=None):
with open(prompt_path, encoding="utf-8") as f:
self.prompt_list = [line.rstrip() for line in f]
if extended_prompt_path is not None:
with open(extended_prompt_path, encoding="utf-8") as f:
self.extended_prompt_list = [line.rstrip() for line in f]
assert len(self.extended_prompt_list) == len(self.prompt_list)
else:
self.extended_prompt_list = None
def __len__(self):
return len(self.prompt_list)
def __getitem__(self, idx):
batch = {
"prompts": self.prompt_list[idx],
"idx": idx,
}
if self.extended_prompt_list is not None:
batch["extended_prompts"] = self.extended_prompt_list[idx]
return batch
class TwoTextDataset(Dataset):
"""Dataset that returns two text prompts per sample for prompt-switch training.
The dataset behaves similarly to :class:`TextDataset` but instead of a single
prompt, it provides *two* prompts – typically the first prompt is used for the
first segment of the video, and the second prompt is used after a temporal
switch during training.
Args:
prompt_path (str): Path to a text file containing the *first* prompt for
each sample. One prompt per line.
switch_prompt_path (str): Path to a text file containing the *second*
prompt for each sample. Must have the **same number of lines** as
``prompt_path`` so that prompts are paired 1-to-1.
"""
def __init__(self, prompt_path: str, switch_prompt_path: str):
# Load the first-segment prompts.
with open(prompt_path, encoding="utf-8") as f:
self.prompt_list = [line.rstrip() for line in f]
# Load the second-segment prompts.
with open(switch_prompt_path, encoding="utf-8") as f:
self.switch_prompt_list = [line.rstrip() for line in f]
assert len(self.switch_prompt_list) == len(self.prompt_list), (
"The two prompt files must contain the same number of lines so that "
"each first-segment prompt is paired with exactly one second-segment prompt."
)
def __len__(self):
return len(self.prompt_list)
def __getitem__(self, idx):
return {
"prompts": self.prompt_list[idx], # first-segment prompt
"switch_prompts": self.switch_prompt_list[idx], # second-segment prompt
"idx": idx,
}
class MultiTextDataset(Dataset):
"""Dataset for multi-segment prompts stored in a JSONL file.
Each line is a JSON object, e.g.
{"prompts": ["a cat", "a dog", "a bird"]}
Args
----
prompt_path : str
Path to the JSONL file
field : str
Name of the list-of-strings field, default "prompts"
cache_dir : str | None
``cache_dir`` passed to HF Datasets (optional)
"""
def __init__(self, prompt_path: str, field: str = "prompts", cache_dir: str | None = None):
self.ds = datasets.load_dataset(
"json",
data_files=prompt_path,
split="train",
cache_dir=cache_dir,
streaming=False,
)
assert len(self.ds) > 0, "JSONL is empty"
assert field in self.ds.column_names, f"Missing field '{field}'"
seg_len = len(self.ds[0][field])
for i, ex in enumerate(self.ds):
val = ex[field]
assert isinstance(val, list), f"Line {i} field '{field}' is not a list"
assert len(val) == seg_len, f"Line {i} list length mismatch"
self.field = field
def __len__(self):
return len(self.ds)
def __getitem__(self, idx: int):
return {
"idx": idx,
"prompts_list": self.ds[idx][self.field], # List[str]
}
def cycle(dl):
while True:
for data in dl:
yield data