Commit ·
eca30bf
1
Parent(s): b46762c
updated master parquet script.
Browse files
tools/build_master_parquet.py
CHANGED
|
@@ -36,30 +36,50 @@ def main():
|
|
| 36 |
ap.add_argument("--src_root", required=True, help="dir with metadata.csv world_annotations.json world_images/")
|
| 37 |
ap.add_argument("--out_dir", required=True, help="hf_repo/data/master")
|
| 38 |
ap.add_argument("--rows_per_shard", type=int, default=256)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
args = ap.parse_args()
|
| 40 |
|
| 41 |
src = Path(args.src_root)
|
| 42 |
out = Path(args.out_dir)
|
| 43 |
out.mkdir(parents=True, exist_ok=True)
|
| 44 |
|
| 45 |
-
meta = pd.read_csv(src / "metadata.csv")
|
| 46 |
coco = read_json(src / "world_annotations.json")
|
| 47 |
images_by_filename, annos_by_image_id, categories = build_indices(coco)
|
| 48 |
|
| 49 |
missing = set(meta["filename"]) - set(images_by_filename.keys())
|
| 50 |
if missing:
|
| 51 |
ex = next(iter(missing))
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
rows = []
|
| 55 |
shard_idx = 0
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
for r in tqdm(meta.itertuples(index=False), total=len(meta), desc="Building master parquet"):
|
| 58 |
filename = r.filename
|
|
|
|
|
|
|
|
|
|
| 59 |
img_info = images_by_filename[filename]
|
| 60 |
image_id = int(img_info["id"])
|
| 61 |
img_path = src / "world_images" / filename
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
b = img_path.read_bytes()
|
| 64 |
annos = annos_by_image_id.get(image_id, [])
|
| 65 |
|
|
@@ -76,6 +96,7 @@ def main():
|
|
| 76 |
"coco_annotations": orjson.dumps(annos).decode("utf-8"),
|
| 77 |
"coco_categories": orjson.dumps(categories).decode("utf-8"),
|
| 78 |
})
|
|
|
|
| 79 |
|
| 80 |
if len(rows) >= args.rows_per_shard:
|
| 81 |
shard_path = out / f"all-{shard_idx:05d}.parquet"
|
|
@@ -87,7 +108,10 @@ def main():
|
|
| 87 |
shard_path = out / f"all-{shard_idx:05d}.parquet"
|
| 88 |
write_shard(rows, shard_path)
|
| 89 |
|
| 90 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
|
|
|
| 36 |
ap.add_argument("--src_root", required=True, help="dir with metadata.csv world_annotations.json world_images/")
|
| 37 |
ap.add_argument("--out_dir", required=True, help="hf_repo/data/master")
|
| 38 |
ap.add_argument("--rows_per_shard", type=int, default=256)
|
| 39 |
+
ap.add_argument(
|
| 40 |
+
"--strict_filenames",
|
| 41 |
+
action="store_true",
|
| 42 |
+
help="if set, fail when metadata filenames are missing from COCO/images; default is to skip",
|
| 43 |
+
)
|
| 44 |
args = ap.parse_args()
|
| 45 |
|
| 46 |
src = Path(args.src_root)
|
| 47 |
out = Path(args.out_dir)
|
| 48 |
out.mkdir(parents=True, exist_ok=True)
|
| 49 |
|
| 50 |
+
meta = pd.read_csv(src / "metadata.csv", low_memory=False)
|
| 51 |
coco = read_json(src / "world_annotations.json")
|
| 52 |
images_by_filename, annos_by_image_id, categories = build_indices(coco)
|
| 53 |
|
| 54 |
missing = set(meta["filename"]) - set(images_by_filename.keys())
|
| 55 |
if missing:
|
| 56 |
ex = next(iter(missing))
|
| 57 |
+
if args.strict_filenames:
|
| 58 |
+
raise RuntimeError(f"{len(missing)} filenames in metadata missing from COCO images[]. Example: {ex}")
|
| 59 |
+
print(
|
| 60 |
+
f"Warning: {len(missing)} filenames in metadata missing from COCO images[]; "
|
| 61 |
+
f"they will be skipped. Example: {ex}"
|
| 62 |
+
)
|
| 63 |
|
| 64 |
rows = []
|
| 65 |
shard_idx = 0
|
| 66 |
|
| 67 |
+
skipped_not_in_coco = 0
|
| 68 |
+
skipped_missing_image_file = 0
|
| 69 |
+
written_rows = 0
|
| 70 |
+
|
| 71 |
for r in tqdm(meta.itertuples(index=False), total=len(meta), desc="Building master parquet"):
|
| 72 |
filename = r.filename
|
| 73 |
+
if filename in missing:
|
| 74 |
+
skipped_not_in_coco += 1
|
| 75 |
+
continue
|
| 76 |
img_info = images_by_filename[filename]
|
| 77 |
image_id = int(img_info["id"])
|
| 78 |
img_path = src / "world_images" / filename
|
| 79 |
|
| 80 |
+
if not img_path.exists():
|
| 81 |
+
skipped_missing_image_file += 1
|
| 82 |
+
continue
|
| 83 |
b = img_path.read_bytes()
|
| 84 |
annos = annos_by_image_id.get(image_id, [])
|
| 85 |
|
|
|
|
| 96 |
"coco_annotations": orjson.dumps(annos).decode("utf-8"),
|
| 97 |
"coco_categories": orjson.dumps(categories).decode("utf-8"),
|
| 98 |
})
|
| 99 |
+
written_rows += 1
|
| 100 |
|
| 101 |
if len(rows) >= args.rows_per_shard:
|
| 102 |
shard_path = out / f"all-{shard_idx:05d}.parquet"
|
|
|
|
| 108 |
shard_path = out / f"all-{shard_idx:05d}.parquet"
|
| 109 |
write_shard(rows, shard_path)
|
| 110 |
|
| 111 |
+
print(
|
| 112 |
+
f"Done. Wrote master shards to {out} | written={written_rows} "
|
| 113 |
+
f"skipped_not_in_coco={skipped_not_in_coco} skipped_missing_image_file={skipped_missing_image_file}"
|
| 114 |
+
)
|
| 115 |
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|