Open-Sora: Democratizing Efficient Video Production for All
Python
update csvutil for vidprom
Zangwei Zheng24c707bfb941
diff --git a/tools/datasets/README.md b/tools/datasets/README.mdindex 0118c8f..ffa9590 100644--- a/tools/datasets/README.md+++ b/tools/datasets/README.md@@ -13,6 +13,12 @@ The training data should be provided in a CSV file with the following format: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.+## VidProM++```bash+python -m tools.datasets.convert_dataset vidprom VIDPROM_FOLDER --info VidProM_semantic_unique.csv+```+## Demo DatasetYou 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:diff --git a/tools/datasets/convert_dataset.py b/tools/datasets/convert_dataset.pyindex 4ff904f..4fa8719 100644--- a/tools/datasets/convert_dataset.py+++ b/tools/datasets/convert_dataset.py@@ -51,16 +51,38 @@ def process_ucf101(root, split):print(f"Saved {len(samples)} samples to {output}.")+def process_vidprom(root, info):+ root = os.path.expanduser(root)+ video_lists = get_filelist(root)+ video_set = set(video_lists)+ # read info csv+ with open(info, "r") as f:+ reader = csv.reader(f)+ lines = list(reader)+ infos = [(os.path.join(root, f"pika-{x[0]}.mp4"), x[1]) for x in lines[1:]]+ samples = [x for x in infos if x[0] in video_set]+ output = "vidprom.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("dataset", type=str, choices=["imagenet", "ucf101", "vidprom"])parser.add_argument("root", type=str)parser.add_argument("--split", type=str, default="train")+ parser.add_argument("--info", type=str, default=None)args = parser.parse_args()if args.dataset == "imagenet":process_imagenet(args.root, args.split)elif args.dataset == "ucf101":process_ucf101(args.root, args.split)+ elif args.dataset == "vidprom":+ process_vidprom(args.root, args.info)else:raise ValueError("Invalid dataset")diff --git a/tools/datasets/csvutil.py b/tools/datasets/csvutil.pyindex 4bbd22d..990f665 100644--- a/tools/datasets/csvutil.py+++ b/tools/datasets/csvutil.py@@ -24,6 +24,21 @@ def get_video_length(path):return int(cap.get(cv2.CAP_PROP_FRAME_COUNT))+def shard_csv(data, shard, input_path):+ n = len(data)+ num_shard = n // shard+ for i in range(shard):+ start = i * num_shard+ end = (i + 1) * num_shard if i != shard - 1 else n+ file_name, ext = os.path.splitext(input_path)+ output_path = os.path.join(os.path.join(os.path.dirname(file_name)), f"{os.path.basename(file_name)}_{i}{ext}")+ with open(output_path, "w") as f:+ writer = csv.writer(f)+ writer.writerows(data[start:end])+ print(f"Saved {end - start} samples to {output_path}.")+ print("Done.")+2+def main(args):input_path = args.inputoutput_path = args.output@@ -49,11 +64,16 @@ def main(args):data = list(reader)print("Number of videos before filtering:", len(data))+ if args.shard is not None:+ shard_csv(data, args.shard, input_path)+ return+data_new = []for i, row in tqdm(enumerate(data)):path = row[0]caption = row[1]- n_frames = int(row[2])+ if len(row) >= 3:+ n_frames = int(row[2])if args.fmin is not None and n_frames < args.fmin:continueif args.fmax is not None and n_frames > args.fmax:@@ -72,7 +92,10 @@ def main(args):row[0] = os.path.join(args.root, path)if args.relength:n_frames = get_video_length(row[0])- row[2] = n_frames+ if len(row) < 3:+ row.append(n_frames)+ else:+ row[2] = n_framesdata_new.append(row)print("Number of videos after filtering:", len(data_new))@@ -92,5 +115,6 @@ if __name__ == "__main__":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")+ parser.add_argument("--shard", type=int, default=None)args = parser.parse_args()main(args)