Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

add datasets doc

Zangwei Zhenge2856e639794
diff --git a/README.md b/README.md
index fe7483f..8b1a54c 100644
--- a/README.md
+++ b/README.md
@@ -136,13 +136,13 @@ torchrun --standalone --nproc_per_node 2 scripts/inference.py configs/opensora/i
The speed is tested on H800 GPUs. For inference with other models, see [here](docs/commands.md) for more instructions.
-## Data Processing (WIP)
+## Data Processing
-### Split video into clips
+High-quality Data is the key to high-quality models. Our used datasets and data collection plan is [here](/docs/datasets.md). We provide tools to process video data. Currently, our data processing pipeline includes the following steps:
-We provide code to split a long video into separate clips efficiently using `multiprocessing`. See `tools/data/scene_detect.py`.
-
-### Generate video caption
+1. Downloading datasets. [[docs](/tools/datasets/README.md)]
+2. Split videos into clips. [[docs](/tools/scenedetect/README.md)]
+3. Generate video captions. [[docs](/tools/caption/README.md)]
## Training
diff --git a/tools/datasets/README.md b/tools/datasets/README.md
index e69de29..0118c8f 100644
--- a/tools/datasets/README.md
+++ b/tools/datasets/README.md
@@ -0,0 +1,48 @@
+# Dataset Download and Management
+
+## Dataset Format
+
+The training data should be provided in a CSV file with the following format:
+
+```csv
+/absolute/path/to/image1.jpg, caption1, num_of_frames
+/absolute/path/to/image2.jpg, caption2, num_of_frames
+```
+
+## HD-VG-130M
+
+This dataset comprises 130M text-video pairs. You can download the dataset and prepare it for training according to [the dataset repository's instructions](https://github.com/daooshee/HD-VG-130M). There is a README.md file in the Google Drive link that provides instructions on how to download and cut the videos. For this version, we directly use the dataset provided by the authors.
+
+## Demo Dataset
+
+You can use ImageNet and UCF101 for a quick demo. After downloading the datasets, you can use the following command to prepare the csv file for the dataset:
+
+```bash
+# ImageNet
+python -m tools.datasets.convert_dataset imagenet IMAGENET_FOLDER --split train
+# UCF101
+python -m tools.datasets.convert_dataset ucf101 UCF101_FOLDER --split videos
+```
+
+## Manage datasets
+
+We provide `csvutils.py` to manage the CSV files. You can use the following commands to process the CSV files:
+
+```bash
+# generate DATA_fmin_128_fmax_256.csv with frames between 128 and 256
+python -m tools.datasets.csvutil DATA.csv --fmin 128 --fmax 256
+# generate DATA_root.csv with absolute path
+python -m tools.datasets.csvutil DATA.csv --root /absolute/path/to/dataset
+# remove videos with no captions
+python -m tools.datasets.csvutil DATA.csv --remove-empty-caption
+# compute the number of frames for each video
+python -m tools.datasets.csvutil DATA.csv --relength
+# remove caption prefix
+python -m tools.datasets.csvutil DATA.csv --remove-caption-prefix
+```
+
+To merge multiple CSV files, you can use the following command:
+
+```bash
+cat *csv > combined.csv
+```
diff --git a/tools/datasets/__init__.py b/tools/datasets/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tools/datasets/convert_dataset.py b/tools/datasets/convert_dataset.py
new file mode 100644
index 0000000..4ff904f
--- /dev/null
+++ b/tools/datasets/convert_dataset.py
@@ -0,0 +1,66 @@
+import argparse
+import csv
+import os
+
+from torchvision.datasets import ImageNet
+
+
+def get_filelist(file_path):
+ Filelist = []
+ for home, dirs, files in os.walk(file_path):
+ for filename in files:
+ Filelist.append(os.path.join(home, filename))
+ return Filelist
+
+
+def split_by_capital(name):
+ # BoxingPunchingBag -> Boxing Punching Bag
+ new_name = ""
+ for i in range(len(name)):
+ if name[i].isupper() and i != 0:
+ new_name += " "
+ new_name += name[i]
+ return new_name
+
+
+def process_imagenet(root, split):
+ root = os.path.expanduser(root)
+ data = ImageNet(root, split=split)
+ samples = [(path, data.classes[label][0]) for path, label in data.samples]
+ output = f"imagenet_{split}.csv"
+
+ with open(output, "w") as f:
+ writer = csv.writer(f)
+ writer.writerows(samples)
+
+ print(f"Saved {len(samples)} samples to {output}.")
+
+
+def process_ucf101(root, split):
+ root = os.path.expanduser(root)
+ video_lists = get_filelist(os.path.join(root, split))
+ classes = [x.split("/")[-2] for x in video_lists]
+ classes = [split_by_capital(x) for x in classes]
+ samples = list(zip(video_lists, classes))
+ output = f"ucf101_{split}.csv"
+
+ with open(output, "w") as f:
+ writer = csv.writer(f)
+ writer.writerows(samples)
+
+ print(f"Saved {len(samples)} samples to {output}.")
+
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser()
+ parser.add_argument("dataset", type=str, choices=["imagenet", "ucf101"])
+ parser.add_argument("root", type=str)
+ parser.add_argument("--split", type=str, default="train")
+ args = parser.parse_args()
+
+ if args.dataset == "imagenet":
+ process_imagenet(args.root, args.split)
+ elif args.dataset == "ucf101":
+ process_ucf101(args.root, args.split)
+ else:
+ raise ValueError("Invalid dataset")
diff --git a/tools/datasets/csvutil.py b/tools/datasets/csvutil.py
index 9bd5db2..4bbd22d 100644
--- a/tools/datasets/csvutil.py
+++ b/tools/datasets/csvutil.py
@@ -30,14 +30,14 @@ def main(args):
if output_path is None:
name = os.path.basename(input_path)
name, ext = os.path.splitext(name)
- if args.num_frames_min is not None:
- name += f"_fmin_{args.num_frames_min}"
- if args.num_frames_max is not None:
- name += f"_fmax_{args.num_frames_max}"
- if args.filter_null_text:
- name += "_fnt"
- if args.remove_prefix:
- name += "_rp"
+ if args.fmin is not None:
+ name += f"_fmin_{args.fmin}"
+ if args.fmax is not None:
+ name += f"_fmax_{args.fmax}"
+ if args.remove_empty_caption:
+ name += "_rec"
+ if args.remove_caption_prefix:
+ name += "_rcp"
if args.root is not None:
name += f"_root"
if args.relength:
@@ -54,13 +54,13 @@ def main(args):
path = row[0]
caption = row[1]
n_frames = int(row[2])
- if args.num_frames_min is not None and n_frames < args.num_frames_min:
+ if args.fmin is not None and n_frames < args.fmin:
continue
- if args.num_frames_max is not None and n_frames > args.num_frames_max:
+ if args.fmax is not None and n_frames > args.fmax:
continue
- if args.filter_null_text and len(caption) == 0:
+ if args.remove_empty_caption and len(caption) == 0:
continue
- if args.remove_prefix:
+ if args.remove_caption_prefix:
for prefix in PREFIX:
if caption.startswith(prefix):
caption = caption[len(prefix) :].strip()
@@ -84,13 +84,13 @@ def main(args):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
- parser.add_argument("--input", type=str, required=True)
+ parser.add_argument("input", type=str)
parser.add_argument("--output", type=str, default=None)
- parser.add_argument("--num_frames_min", type=int, default=None)
- parser.add_argument("--num_frames_max", type=int, default=None)
- parser.add_argument("--filter_null_text", action="store_true")
- parser.add_argument("--remove_prefix", action="store_true")
+ parser.add_argument("--fmin", type=int, default=None)
+ parser.add_argument("--fmax", type=int, default=None)
parser.add_argument("--root", type=str, default=None)
+ parser.add_argument("--remove-empty-caption", action="store_true")
+ parser.add_argument("--remove-caption-prefix", action="store_true")
parser.add_argument("--relength", action="store_true")
args = parser.parse_args()
main(args)
diff --git a/tools/datasets/to_csv_imagenet.py b/tools/datasets/to_csv_imagenet.py
deleted file mode 100644
index 8b18faf..0000000
--- a/tools/datasets/to_csv_imagenet.py
+++ /dev/null
@@ -1,17 +0,0 @@
-import csv
-import os
-
-from torchvision.datasets import ImageNet
-
-root = "~/data/imagenet"
-split = "train"
-
-root = os.path.expanduser(root)
-data = ImageNet(root, split=split)
-samples = [(path, data.classes[label][0]) for path, label in data.samples]
-
-with open(f"preprocess/imagenet_{split}.csv", "w") as f:
- writer = csv.writer(f)
- writer.writerows(samples)
-
-print(f"Saved {len(samples)} samples to preprocess/imagenet_{split}.csv.")
diff --git a/tools/datasets/to_csv_ucf101.py b/tools/datasets/to_csv_ucf101.py
deleted file mode 100644
index fd99b7e..0000000
--- a/tools/datasets/to_csv_ucf101.py
+++ /dev/null
@@ -1,36 +0,0 @@
-import csv
-import os
-
-
-def get_filelist(file_path):
- Filelist = []
- for home, dirs, files in os.walk(file_path):
- for filename in files:
- Filelist.append(os.path.join(home, filename))
- return Filelist
-
-
-def split_by_capital(name):
- # BoxingPunchingBag -> Boxing Punching Bag
- new_name = ""
- for i in range(len(name)):
- if name[i].isupper() and i != 0:
- new_name += " "
- new_name += name[i]
- return new_name
-
-
-root = "~/data/ucf101"
-split = "videos"
-
-root = os.path.expanduser(root)
-video_lists = get_filelist(os.path.join(root, split))
-classes = [x.split("/")[-2] for x in video_lists]
-classes = [split_by_capital(x) for x in classes]
-samples = list(zip(video_lists, classes))
-
-with open(f"preprocess/ucf101_{split}.csv", "w") as f:
- writer = csv.writer(f)
- writer.writerows(samples)
-
-print(f"Saved {len(samples)} samples to preprocess/ucf101_{split}.csv.")