path-vqa / scripts /process_dataset.py
flaviagiammarino's picture
Upload process_dataset.py
711f25f
raw
history blame
2.55 kB
"""This script processes the dataset provided by the PathVQA authors. This script drops the duplicate
image-question-answer triplets, converts the images to bytes, and saves the dataset to a parquet file.
"""
import io
import pickle
import pandas as pd
from PIL import Image
from tqdm import tqdm
# loop across the splits
for split in ["train", "val", "test"]:
# load the image-question-answer triplets
data = pd.DataFrame(pickle.load(open(f"pvqa/qas/{split}/{split}_qa.pkl", "rb")))
print(f"Total number of triplets in {split} set: {format(data.shape[0], ',.0f')}")
# drop the duplicate image-question-answer triplets
data = data.drop_duplicates(ignore_index=True)
print(f"Unique number of triplets in {split} set: {format(data.shape[0], ',.0f')}")
# load the images as bytes
print(f"Loading {split} set images...")
images = {}
for image in tqdm(data["image"].unique()):
img = Image.open(f"pvqa/images/{split}/{image}.jpg")
byt = io.BytesIO()
img.save(byt, format="jpeg")
byt = byt.getvalue()
images[image] = {"path": None, "bytes": byt}
print(f"Unique number of images in {split} set: {format(len(images), ',.0f')}")
# save the data to a parquet file
print(f"Writing data to data/{split}.parquet...")
dataset = []
for _, row in data.iterrows():
dataset.append({
"image": images[row["image"]],
"question": row["question"],
"answer": row["answer"]
})
pd.DataFrame(dataset).to_parquet(f"data/{split}.parquet")
print("Done")
print("---------------------------------")
'''
Total number of triplets in train set: 19,755
Unique number of triplets in train set: 19,654
Loading train set images...
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2599/2599 [00:46<00:00, 56.27it/s]
Unique number of images in train set: 2,599
Writing data to data/train.parquet...
Done
---------------------------------
Total number of triplets in val set: 6,279
Unique number of triplets in val set: 6,259
Loading val set images...
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 832/832 [00:13<00:00, 59.49it/s]
Unique number of images in val set: 832
Writing data to data/val.parquet...
Done
---------------------------------
Total number of triplets in test set: 6,761
Unique number of triplets in test set: 6,719
Loading test set images...
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 858/858 [00:15<00:00, 53.93it/s]
Unique number of images in test set: 858
Writing data to data/test.parquet...
Done
'''