#!/usr/bin/env python3 """YFCC non-geotagged expansion: pick videos for TRAINING ONLY (street-view-like new places). Input: data/yfcc_nogeo_manifest.csv (prepare_yfcc_nogeo_videos.py). Videos have no coordinates, so leakage control is by uploader: a video is excluded if its uploader has validation/test videos in the original YFCC pool (data/yfcc_street_videos.csv). Videos already in the original pool or the geotagged expansion list are skipped; keyword-flagged videos and videos shorter than 10 s are dropped. Order: videos with street keywords in tags/title/description first (more keywords first), then the rest in random order. --maximum-videos takes the first N of that order (pilot / batches); --offset skips the first N (next batch). The YFCC prepare scripts run unchanged on the output. Limitation: without coordinates, a different uploader could have filmed a validation/test place. """ from __future__ import annotations import sys as _sys from pathlib import Path as _Path _SCRIPTS = next(_p for _p in _Path(__file__).resolve().parents if _p.name == "scripts") _sys.path[:0] = [str(_SCRIPTS.parent), str(_SCRIPTS.parent / "models")] + [str(_d) for _d in sorted(_SCRIPTS.rglob("*")) if _d.is_dir() and _d.name != "__pycache__"] import argparse import json from pathlib import Path import numpy as np import pandas as pd def parse_args(): p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) p.add_argument("--manifest", type=Path, default=Path("data/yfcc_nogeo_manifest.csv")) p.add_argument("--existing", type=Path, nargs="+", default=[Path("data/yfcc_street_videos.csv"), Path("data/yfcc_expansion_videos.csv")]) p.add_argument("--output", type=Path, default=Path("data/yfcc_nogeo_videos.csv")) p.add_argument("--clip-seconds", type=float, default=10.0) p.add_argument("--maximum-videos", type=int, default=0) p.add_argument("--offset", type=int, default=0) p.add_argument("--seed", type=int, default=42) return p.parse_args() def main(): a = parse_args() m = pd.read_csv(a.manifest, low_memory=False, dtype={"uid": str, "hash": str}) counts = {"nogeo_with_mp4": int(len(m))} olds = [pd.read_csv(p, low_memory=False, dtype={"uid": str, "hash": str}) for p in a.existing if p.exists()] known = set().union(*[set(o.hash) for o in olds]) if olds else set() m = m[~m.hash.isin(known)] counts["not_in_existing_lists"] = int(len(m)) m = m[m.text_flag.astype(int) == 0] counts["no_text_flag"] = int(len(m)) m["duration_s"] = pd.to_numeric(m.duration_s, errors="coerce") m = m[m.duration_s >= a.clip_seconds].drop_duplicates("hash").reset_index(drop=True) counts["at_least_one_segment"] = int(len(m)) m["video_id"] = "yfcc_" + m.photoid.astype(str) m["split_group"] = "yfcc_uid:" + m.uid.astype(str) held = set() for o in olds: if "split" in o: held |= set(("yfcc_uid:" + o.loc[o.split.isin(["val", "test"]), "uid"].astype(str))) m["split"] = np.where(m.split_group.isin(held), "excluded_uploader", "train") counts["excluded_uploader"] = int((m.split == "excluded_uploader").sum()) rng = np.random.default_rng(a.seed) m["_r"] = rng.random(len(m)) m = m.sort_values(["split", "street_keyword", "_r"], ascending=[False, False, True]).drop(columns="_r").reset_index(drop=True) train = m.index[m.split.eq("train")] chosen = train[a.offset:a.offset + a.maximum_videos] if a.maximum_videos else train[a.offset:] m.loc[m.split.eq("train") & ~m.index.isin(chosen), "split"] = "excluded_batch" m["segments_available"] = (m.duration_s // a.clip_seconds).astype(int) m["expansion"] = "nogeo" a.output.parent.mkdir(parents=True, exist_ok=True) m.to_csv(a.output, index=False) used = m[m.split.eq("train")] report = {"filters": counts, "split_videos": m.split.value_counts().to_dict(), "train_with_street_keyword": int((used.street_keyword > 0).sum()), "pool_with_street_keyword": int(((m.split != "excluded_uploader") & (m.street_keyword > 0)).sum()), "train_uploaders": int(used.split_group.nunique()), "train_hours": round(float(used.duration_s.sum()) / 3600, 1), "offset": a.offset, "maximum_videos": a.maximum_videos} a.output.with_suffix(".quality.json").write_text(json.dumps(report, indent=2), encoding="utf-8") print(json.dumps(report, indent=2)) print(f"Wrote {a.output}") if __name__ == "__main__": main()