File size: 2,013 Bytes
f799bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""Rebuild the per-sequence tars from the original frame tree.

The tars that get uploaded to the Hub are derived data - they are deleted locally
once uploaded. This regenerates them byte-for-byte identically.

    python repack_for_upload.py --src ../../vqs_4k_kaggle --out ..

`metadata.csv` carries both ids, so the rename (legacy_seq_id -> seq_id) is applied
to the archive name AND to the paths inside it.
"""
import argparse
import csv
import os
import subprocess
from multiprocessing import Pool
from pathlib import Path


def repack(job):
    src, out, kind, old, new = job
    dst = Path(out) / kind / f"{new}.tar"
    if dst.exists():
        return None
    tmp = str(dst) + ".part"
    cmd = ["tar", "--sort=name", "--owner=0", "--group=0", "--mtime=UTC 2026-01-01",
           f"--transform=s|^{old}|{new}|", "-cf", tmp, "-C", f"{src}/{kind}", old]
    r = subprocess.run(cmd, capture_output=True)
    if r.returncode != 0:
        Path(tmp).exists() and os.remove(tmp)
        return f"FAIL {kind}/{old}: {r.stderr.decode()[:120]}"
    os.replace(tmp, dst)
    return None


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--src", required=True, help="original tree with images/<id>/ and masks/<id>/")
    p.add_argument("--out", default=".", help="where to write images/*.tar and masks/*.tar")
    p.add_argument("--workers", type=int, default=6)
    a = p.parse_args()

    meta = Path(a.out) / "metadata.csv"
    rows = list(csv.DictReader(open(meta)))
    for k in ("images", "masks"):
        (Path(a.out) / k).mkdir(parents=True, exist_ok=True)

    jobs = [(a.src, a.out, k, r["legacy_seq_id"], r["seq_id"])
            for k in ("masks", "images") for r in rows]
    print(f"packing {len(jobs)} archives from {a.src}")
    with Pool(a.workers) as pool:
        errs = [e for e in pool.imap_unordered(repack, jobs, chunksize=8) if e]
    print("errors:", len(errs))
    for e in errs[:5]:
        print(" ", e)


if __name__ == "__main__":
    main()