Datasets:
Kurt Stolle
commited on
Commit
·
7f95801
1
Parent(s):
20e7eb7
Added manifest and download/prepare script
Browse files- manifest.csv +0 -0
- scripts/download.py +159 -0
manifest.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
scripts/download.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""
|
| 3 |
+
Downloads and extracts additional data from the Cityscapes dataset, moving the
|
| 4 |
+
required files to (existing) data directory.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import tempfile
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
import os
|
| 10 |
+
import re
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
import shutil
|
| 13 |
+
import zipfile
|
| 14 |
+
import argparse
|
| 15 |
+
import pandas as pd
|
| 16 |
+
|
| 17 |
+
from cityscapesscripts.download import downloader
|
| 18 |
+
|
| 19 |
+
PACKAGE_TO_VARIABLE = {
|
| 20 |
+
"vehicle_sequence": "vehicle",
|
| 21 |
+
"timestamp_sequence": "timestamp",
|
| 22 |
+
"leftImg8bit_sequence_trainvaltest": "image",
|
| 23 |
+
"camera_trainvaltest": "camera",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def parse_args() -> argparse.Namespace:
|
| 28 |
+
parser = argparse.ArgumentParser(
|
| 29 |
+
description="Download and extract the Cityscapes dataset."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
parser.add_argument(
|
| 33 |
+
"downloads_dir",
|
| 34 |
+
type=Path,
|
| 35 |
+
help="Path to the directory where ZIP files are/will be stored (e.g., 'downloads').",
|
| 36 |
+
)
|
| 37 |
+
parser.add_argument(
|
| 38 |
+
"data_dir",
|
| 39 |
+
type=Path,
|
| 40 |
+
help="Path to the directory where extracted files should be moved (e.g., 'data').",
|
| 41 |
+
)
|
| 42 |
+
parser.add_argument(
|
| 43 |
+
"manifest_file",
|
| 44 |
+
type=Path,
|
| 45 |
+
help="Path to the manifest file (e.g., 'manifest.csv').",
|
| 46 |
+
)
|
| 47 |
+
return parser.parse_args()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def read_manifest(manifest_file: str) -> pd.DataFrame:
|
| 51 |
+
"""
|
| 52 |
+
Read the manifest file and return a DataFrame.
|
| 53 |
+
"""
|
| 54 |
+
df = pd.read_csv(manifest_file, index_col="primary_key")
|
| 55 |
+
assert "split" in df.columns, "Missing 'split' column in manifest."
|
| 56 |
+
assert "sequence" in df.columns, "Missing 'sequence' column in manifest."
|
| 57 |
+
assert "frame" in df.columns, "Missing 'frame' column in manifest."
|
| 58 |
+
return df
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def download_files(downloads_dir: str, zip_files: list[str]):
|
| 62 |
+
if not zip_files:
|
| 63 |
+
print("No files to download.")
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
session = downloader.login()
|
| 67 |
+
|
| 68 |
+
downloader.download_packages(
|
| 69 |
+
session=session,
|
| 70 |
+
package_names=zip_files,
|
| 71 |
+
destination_path=downloads_dir,
|
| 72 |
+
resume=True,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def unzip_and_move(
|
| 77 |
+
pkg: str,
|
| 78 |
+
downloads_dir: Path,
|
| 79 |
+
data_dir: Path,
|
| 80 |
+
manifest: pd.DataFrame,
|
| 81 |
+
source_split: str,
|
| 82 |
+
):
|
| 83 |
+
"""
|
| 84 |
+
Unzip, rename, and move the requested split files for a single package.
|
| 85 |
+
"""
|
| 86 |
+
zip_path = downloads_dir / f"{pkg}.zip"
|
| 87 |
+
pkg_type = PACKAGE_TO_VARIABLE[pkg]
|
| 88 |
+
|
| 89 |
+
re_name = re.compile(r"([^_]*_[^_]*_[^_]*)_.*")
|
| 90 |
+
|
| 91 |
+
if not zip_path.is_file():
|
| 92 |
+
print(f"Warning: ZIP file not found => {zip_path}. Skipping...")
|
| 93 |
+
return
|
| 94 |
+
|
| 95 |
+
print(f"Processing: {zip_path}")
|
| 96 |
+
|
| 97 |
+
with tempfile.TemporaryDirectory() as td, zipfile.ZipFile(zip_path, "r") as zf:
|
| 98 |
+
names = zf.namelist()
|
| 99 |
+
for member in tqdm(names):
|
| 100 |
+
if f"/{source_split}/" not in member:
|
| 101 |
+
continue
|
| 102 |
+
|
| 103 |
+
# Extract the file and get its new path in the temporary directory
|
| 104 |
+
res_path = Path(zf.extract(member, td))
|
| 105 |
+
|
| 106 |
+
if res_path.is_dir():
|
| 107 |
+
continue
|
| 108 |
+
|
| 109 |
+
# Read the manifest to find the specification of this sample
|
| 110 |
+
primary_key = re_name.sub(r"\1", res_path.stem)
|
| 111 |
+
sample = manifest.loc[primary_key]
|
| 112 |
+
|
| 113 |
+
# New name is: split/<sequence>/<frame>.<type>.<ext>
|
| 114 |
+
new_path = (
|
| 115 |
+
data_dir
|
| 116 |
+
/ sample["split"]
|
| 117 |
+
/ "{:06d}".format(sample["sequence"])
|
| 118 |
+
/ "{:06d}.{:s}{:s}".format(sample["frame"], pkg_type, res_path.suffix)
|
| 119 |
+
)
|
| 120 |
+
new_path.parent.mkdir(parents=True, exist_ok=True)
|
| 121 |
+
|
| 122 |
+
shutil.move(res_path, new_path)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def main():
|
| 126 |
+
args = parse_args()
|
| 127 |
+
|
| 128 |
+
# Create directories if not exists
|
| 129 |
+
for dir in (args.downloads_dir, args.data_dir):
|
| 130 |
+
os.makedirs(dir, exist_ok=True)
|
| 131 |
+
|
| 132 |
+
# Read the manifest file
|
| 133 |
+
manifest = read_manifest(args.manifest_file)
|
| 134 |
+
|
| 135 |
+
# Prepare list of package zip files
|
| 136 |
+
packages = list(PACKAGE_TO_VARIABLE.keys())
|
| 137 |
+
zip_files = [f"{pkg}.zip" for pkg in packages]
|
| 138 |
+
|
| 139 |
+
# Filter out existing ZIPs
|
| 140 |
+
download_list = []
|
| 141 |
+
for zf in zip_files:
|
| 142 |
+
zf_path = os.path.join(args.downloads_dir, zf)
|
| 143 |
+
if os.path.isfile(zf_path):
|
| 144 |
+
print(f"Already downloaded: {zf_path}")
|
| 145 |
+
else:
|
| 146 |
+
download_list.append(zf)
|
| 147 |
+
|
| 148 |
+
# Download missing ZIP files using csDownload directly
|
| 149 |
+
download_files(args.downloads_dir, download_list)
|
| 150 |
+
|
| 151 |
+
# Unzip, rename, and move each package
|
| 152 |
+
for pkg in packages:
|
| 153 |
+
unzip_and_move(pkg, args.downloads_dir, args.data_dir, manifest, "val")
|
| 154 |
+
|
| 155 |
+
print(f"Successfully processed: {packages}")
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
if __name__ == "__main__":
|
| 159 |
+
main()
|