| |
| """Build MVEB DukeMTMC subset from a precomputed train/test split JSON.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import shutil |
| import sys |
| from pathlib import Path |
| from typing import Dict, List, Tuple |
|
|
| _SCRIPTS_ROOT = Path(__file__).resolve().parent.parent |
| if str(_SCRIPTS_ROOT) not in sys.path: |
| sys.path.insert(0, str(_SCRIPTS_ROOT)) |
| from pack_media_parquet import pack_dataset_with_media, resolve_split_output_dir |
|
|
| QUERY_INSTRUCTION = "Represent the person with the following text." |
| QUERY_TEXT = "Re-identify the person in the given image." |
| CANDIDATE_INSTRUCTION = "Represent the person with the following text." |
| CANDIDATE_TEXT = "Re-identify the person in the given image." |
|
|
|
|
| def _pid_from_name(rel_path: str) -> str: |
| name = Path(rel_path).name |
| if "_" not in name: |
| raise ValueError(f"Invalid DukeMTMC filename: {rel_path}") |
| return name.split("_", 1)[0] |
|
|
|
|
| def _filter_valid(paths: List[str]) -> List[str]: |
| uniq = sorted(set(paths)) |
| valid = [] |
| for p in uniq: |
| pid = _pid_from_name(p) |
| if pid == "-1": |
| continue |
| valid.append(p) |
| return valid |
|
|
|
|
| def _build_train_rows(train_paths: List[str]) -> Tuple[List[dict], List[dict]]: |
| candidate_paths = _filter_valid([p for p in train_paths if p.startswith("bounding_box_train/")]) |
| inst_to_ids: Dict[str, List[str]] = {} |
| candidate_rows: List[dict] = [] |
|
|
| for idx, rel_path in enumerate(candidate_paths): |
| pid = _pid_from_name(rel_path) |
| inst_to_ids.setdefault(pid, []).append(rel_path) |
| candidate_rows.append( |
| { |
| "id": rel_path, |
| "instance_id": pid, |
| "image_path": rel_path, |
| "instruction": CANDIDATE_INSTRUCTION, |
| "text": CANDIDATE_TEXT, |
| } |
| ) |
|
|
| query_rows: List[dict] = [] |
| for row in candidate_rows: |
| pos_ids = [x for x in inst_to_ids[row["instance_id"]] if x != row["id"]] |
| if not pos_ids: |
| continue |
| query_rows.append( |
| { |
| "id": row["id"], |
| "instance_id": row["instance_id"], |
| "image_path": row["image_path"], |
| "instruction": QUERY_INSTRUCTION, |
| "text": QUERY_TEXT, |
| "pos_ids": pos_ids, |
| } |
| ) |
| return query_rows, candidate_rows |
|
|
|
|
| def _build_test_rows(test_paths: List[str]) -> Tuple[List[dict], List[dict]]: |
| candidate_paths = _filter_valid([p for p in test_paths if p.startswith("bounding_box_test/")]) |
| query_paths = _filter_valid([p for p in test_paths if p.startswith("query/")]) |
| if not candidate_paths or not query_paths: |
| raise ValueError("test split must contain both 'bounding_box_test/' and 'query/' images") |
|
|
| inst_to_candidate_ids: Dict[str, List[str]] = {} |
| candidate_rows: List[dict] = [] |
| for idx, rel_path in enumerate(candidate_paths): |
| pid = _pid_from_name(rel_path) |
| inst_to_candidate_ids.setdefault(pid, []).append(rel_path) |
| candidate_rows.append( |
| { |
| "id": rel_path, |
| "instance_id": pid, |
| "image_path": rel_path, |
| "instruction": CANDIDATE_INSTRUCTION, |
| "text": CANDIDATE_TEXT, |
| } |
| ) |
|
|
| query_rows: List[dict] = [] |
| base = len(candidate_rows) |
| for q_idx, rel_path in enumerate(query_paths): |
| pid = _pid_from_name(rel_path) |
| pos_ids = inst_to_candidate_ids.get(pid, []) |
| if not pos_ids: |
| continue |
| query_rows.append( |
| { |
| "id": str(base + q_idx), |
| "instance_id": pid, |
| "image_path": rel_path, |
| "instruction": QUERY_INSTRUCTION, |
| "text": QUERY_TEXT, |
| "pos_ids": list(pos_ids), |
| } |
| ) |
|
|
| return query_rows, candidate_rows |
|
|
|
|
| def _process_one_split( |
| split_name: str, |
| rel_paths: List[str], |
| image_root: Path, |
| output_root: Path, |
| overwrite: bool, |
| media_rows_per_shard: int, |
| row_group_size: int, |
| num_workers: int, |
| ) -> None: |
| out_dir = resolve_split_output_dir(output_root, split_name, "DukeMTMC") |
| if overwrite and out_dir.exists(): |
| shutil.rmtree(out_dir) |
| out_dir.mkdir(parents=True, exist_ok=True) |
|
|
| if split_name == "train": |
| query_rows, candidate_rows = _build_train_rows(rel_paths) |
| elif split_name == "test": |
| query_rows, candidate_rows = _build_test_rows(rel_paths) |
| else: |
| raise ValueError(f"Unsupported split: {split_name}") |
|
|
| stats = pack_dataset_with_media( |
| query_annotations=query_rows, |
| candidate_annotations=candidate_rows, |
| image_dir=str(image_root), |
| output_dir=str(out_dir), |
| media_rows_per_shard=media_rows_per_shard, |
| row_group_size=row_group_size, |
| num_workers=num_workers, |
| dataset_name="DukeMTMC", |
| data_split=split_name, |
| write_subset_readme=True, |
| show_progress=True, |
| ) |
| print( |
| f"[{split_name}] done: media={stats['num_media']}, " |
| f"query={stats['num_query']}, candidate={stats['num_candidate']}, " |
| f"shards={stats['num_shards']} -> {out_dir}" |
| ) |
|
|
|
|
| def main() -> None: |
| script_dir = Path(__file__).resolve().parent |
| root_dir = script_dir.parent.parent.parent |
|
|
| parser = argparse.ArgumentParser(description="Process DukeMTMC split JSON to MVEB parquet format.") |
| parser.add_argument( |
| "--split-json", |
| type=Path, |
| default=script_dir / "train_test_split.json", |
| help="JSON containing train/test relative image paths.", |
| ) |
| parser.add_argument( |
| "--image-root", |
| type=Path, |
| default=root_dir / "source" / "dukemtmc", |
| help="Root directory containing bounding_box_train/bounding_box_test/query.", |
| ) |
| parser.add_argument( |
| "--output-root", |
| type=Path, |
| default=root_dir, |
| help="Output MVEB root directory (contains train/ and test/).", |
| ) |
| parser.add_argument("--overwrite", action="store_true", help="Delete existing output split dir before writing.") |
| parser.add_argument("--media-rows-per-shard", type=int, default=5000) |
| parser.add_argument("--row-group-size", type=int, default=100) |
| parser.add_argument("--num-workers", type=int, default=1) |
| args = parser.parse_args() |
|
|
| if not args.split_json.exists(): |
| raise FileNotFoundError(f"split json not found: {args.split_json}") |
| if not args.image_root.exists(): |
| raise FileNotFoundError(f"image root not found: {args.image_root}") |
|
|
| with args.split_json.open("r", encoding="utf-8") as f: |
| split_data = json.load(f) |
| for key in ("train", "test"): |
| if key not in split_data or not isinstance(split_data[key], list): |
| raise ValueError(f"split json must contain key {key!r} with a list value") |
|
|
| _process_one_split( |
| "train", |
| split_data["train"], |
| args.image_root, |
| args.output_root, |
| args.overwrite, |
| args.media_rows_per_shard, |
| args.row_group_size, |
| args.num_workers, |
| ) |
| _process_one_split( |
| "test", |
| split_data["test"], |
| args.image_root, |
| args.output_root, |
| args.overwrite, |
| args.media_rows_per_shard, |
| args.row_group_size, |
| args.num_workers, |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|