Download scripts/data_preparation/assemble/apply_final_split.py from xiaohaoy/ViewingSoundPAQ: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/xiaohaoy/ViewingSoundPAQ/resolve/main/scripts/data_preparation/assemble/apply_final_split.py
- Command line
-
hf download hf://xiaohaoy/ViewingSoundPAQ/scripts/data_preparation/assemble/apply_final_split.py
-
curl -L -o apply_final_split.py https://huggingface.co/xiaohaoy/ViewingSoundPAQ/resolve/main/scripts/data_preparation/assemble/apply_final_split.py
3.65 kB
| #!/usr/bin/env python3 | |
| """Folds, training pairs and scored pairs of the final model for the available datasets. | |
| data/final_split/final_pairs.csv lists the final model's pairs of the public datasets with their CV folds. Pairs are | |
| matched to the training revision by dataset + photo file name; a pair is used when it is found, has the same paired | |
| clip and image-audio cosine >= 0.2. Datasets that are not available are left out. Writes next to the manifest: | |
| cv_folds_final_allscored.csv the used pairs with their folds (every pair trains in 9 folds and is tested once) | |
| train_exclude_final.csv every other pair of the revision (never trains) | |
| score_views_final.csv the used pairs (what the report counts) | |
| python scripts/data_preparation/assemble/apply_final_split.py --revision-dir <rev> | |
| """ | |
| import argparse | |
| import ntpath | |
| from pathlib import Path | |
| import pandas as pd | |
| def main(): | |
| p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| p.add_argument("--revision-dir", type=Path, required=True) | |
| p.add_argument("--split", type=Path, nargs="+", default=[Path("data/final_split/final_pairs.csv")]) | |
| p.add_argument("--min-train-pair-cosine", type=float, default=0.2) | |
| a = p.parse_args() | |
| m = pd.read_csv(a.revision_dir / "metadata_manifest.csv", low_memory=False) | |
| m = m[m.direct_aq_target_status.eq("ok") & m.split.isin(["train", "val", "test"])].copy() | |
| m["view_file"] = m.view_path.astype(str).map(lambda v: ntpath.basename(v.replace("/", "\\"))) | |
| m["dataset"] = m.training_dataset.astype(str) | |
| if m.duplicated(["dataset", "view_file"]).any(): | |
| raise SystemExit("photo file names are not unique within a dataset in this revision") | |
| s = pd.concat([pd.read_csv(x) for x in a.split], ignore_index=True) | |
| j = s.merge(m, on=["dataset", "view_file"], how="left", suffixes=("", "_rev"), indicator=True) | |
| found = j._merge.eq("both") | |
| clip = j.audio_path.astype(str).map(lambda v: ntpath.basename(v.replace("/", "\\"))) | |
| start = pd.to_numeric(j.clip_start_seconds, errors="coerce").round(3) | |
| same_clip = found & clip.eq(j.audio) & ((start == j.clip_start) | (start.isna() & j.clip_start.isna())) | |
| cos_ok = found & pd.to_numeric(j.image_audio_cosine, errors="coerce").ge(a.min_train_pair_cosine) | |
| use = same_clip & cos_ok | |
| print(pd.DataFrame({"dataset": j.dataset, "listed": 1, "found": found, "same_clip": same_clip, "cosine_ok": cos_ok, | |
| "used": use}).groupby("dataset").sum().to_string()) | |
| if (found & ~use).any(): | |
| j[found & ~use][["dataset", "view_file", "audio"]].to_csv(a.revision_dir / "final_split_differences.csv", index=False) | |
| print(f"{int((found & ~use).sum())} found pairs differ (other clip or cosine < 0.2) and are not used: " | |
| f"{a.revision_dir / 'final_split_differences.csv'}") | |
| k = j[use] | |
| pd.DataFrame({"view_id": k.view_id, "view_path": k.view_path, "dataset": k.dataset, "split": k.split, "group": k.group, | |
| "fold": k.fold.astype(int), "trainable_pair": True}).to_csv(a.revision_dir / "cv_folds_final_allscored.csv", index=False) | |
| pd.DataFrame({"view_id": sorted(set(m.view_id) - set(k.view_id))}).to_csv(a.revision_dir / "train_exclude_final.csv", index=False) | |
| pd.DataFrame({"view_id": k.view_id}).to_csv(a.revision_dir / "score_views_final.csv", index=False) | |
| print(f"{len(k)} pairs used, per fold {k.fold.value_counts().sort_index().to_dict()}; " | |
| f"wrote cv_folds_final_allscored.csv, train_exclude_final.csv, score_views_final.csv in {a.revision_dir}") | |
| if __name__ == "__main__": | |
| main() | |