csvps / scripts /build_webdataset.py
Kurt Stolle
Updated examples and script organization
aad0211
raw
history blame
1.4 kB
#!/usr/bin/env python
r"""
Builds a WebDataset from the Cityscapes Video dataset.
"""
import argparse
import tarfile
from pathlib import Path
from tqdm import tqdm
def parse_args():
p = argparse.ArgumentParser(
description="Build a WebDataset from the Cityscapes Video dataset."
)
p.add_argument("--prefix", default="csvps", help="Prefix for the tar files.")
p.add_argument("data", type=Path, help="Path to the Cityscapes Video dataset.")
p.add_argument("output", type=Path, help="Path to the output directory.")
return p.parse_args()
def build_dataset(split: str, data_dir: Path, out_dir: Path, *, prefix: str = ""):
data_dir = data_dir / split
name = f"{split}.tar"
if prefix and prefix != "":
name = f"{prefix}-{name}"
tar_path = out_dir / name
if tar_path.exists():
print(f"Error: Tar archive already exists: {tar_path}")
with tarfile.open(tar_path, "w") as tar:
# Add the files to the tar archive
for file in tqdm(sorted(data_dir.glob("**/*")), desc=f"Building {tar_path}"):
tar.add(file, arcname=file.relative_to(data_dir))
def main():
args = parse_args()
# For each split, build a tar archive containing the sorted files
for split in ("train", "val", "test"):
build_dataset(split, args.data, args.output, prefix=args.prefix)
if __name__ == "__main__":
main()