add making code
Browse files- make_ds_coco.py +79 -0
make_ds_coco.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from tqdm import tqdm
|
| 4 |
+
|
| 5 |
+
from datasets import Dataset, load_dataset, Image
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def load_jsonl(file_path):
|
| 9 |
+
"""
|
| 10 |
+
Loads a JSONL file and returns a list of Python dictionaries.
|
| 11 |
+
Each dictionary represents a JSON object from a line in the file.
|
| 12 |
+
"""
|
| 13 |
+
data = []
|
| 14 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 15 |
+
for line in f:
|
| 16 |
+
try:
|
| 17 |
+
# Parse each line as a JSON object
|
| 18 |
+
json_object = json.loads(line.strip())
|
| 19 |
+
data.append(json_object)
|
| 20 |
+
except json.JSONDecodeError as e:
|
| 21 |
+
print(f"Error decoding JSON on line: {line.strip()} - {e}")
|
| 22 |
+
return data
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
dset = "val"
|
| 27 |
+
workdir = "./coco"
|
| 28 |
+
|
| 29 |
+
# Load Annotions from Official Coco
|
| 30 |
+
with open(
|
| 31 |
+
os.path.join(workdir, "annotations", f"captions_{dset}2017.json"),
|
| 32 |
+
"r",
|
| 33 |
+
encoding="utf-8"
|
| 34 |
+
) as reader:
|
| 35 |
+
data = json.load(reader)
|
| 36 |
+
|
| 37 |
+
# Format dict of image elements
|
| 38 |
+
images = {}
|
| 39 |
+
for item in tqdm(data["images"]):
|
| 40 |
+
_idx = item["id"]
|
| 41 |
+
images[_idx] = {
|
| 42 |
+
"file_name": item["file_name"],
|
| 43 |
+
"height": item["height"],
|
| 44 |
+
"width": item["width"],
|
| 45 |
+
"id": _idx,
|
| 46 |
+
"image": os.path.join(workdir, f"{dset}2017", item["file_name"]),
|
| 47 |
+
"captions": [],
|
| 48 |
+
"narratives": []
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# Assign official annotations
|
| 52 |
+
for item in tqdm(data["annotations"]):
|
| 53 |
+
_idx = item["image_id"]
|
| 54 |
+
images[_idx]["captions"].append(item["caption"])
|
| 55 |
+
|
| 56 |
+
# Load Narratives
|
| 57 |
+
data_narr = load_jsonl(os.path.join(workdir, "localized_narratives", f"coco_{dset}_captions.jsonl"))
|
| 58 |
+
for item in tqdm(data_narr):
|
| 59 |
+
_idx = int(item["image_id"])
|
| 60 |
+
images[_idx]["narratives"].append(item["caption"])
|
| 61 |
+
|
| 62 |
+
def gen():
|
| 63 |
+
for k, v in images.items():
|
| 64 |
+
yield v
|
| 65 |
+
|
| 66 |
+
ds = Dataset.from_generator(gen)
|
| 67 |
+
ds = ds.cast_column("image", Image())
|
| 68 |
+
ds.save_to_disk(f"coco/datasets/data/{dset}", max_shard_size="400MB")
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_coco():
|
| 73 |
+
ds = load_dataset("./coco/datasets", split="val")
|
| 74 |
+
print(ds.info)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
# main()
|
| 79 |
+
test_coco()
|