|
|
import os |
|
|
import csv |
|
|
import datasets |
|
|
from PIL import Image |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
class MyDataset(datasets.GeneratorBasedBuilder): |
|
|
def _info(self): |
|
|
return datasets.DatasetInfo( |
|
|
description="Dataset containing medical images with associated questions and answers.", |
|
|
features=datasets.Features({ |
|
|
"image": datasets.Image(), |
|
|
"source": datasets.Value("string"), |
|
|
"question": datasets.Value("string"), |
|
|
"answer": datasets.Value("string"), |
|
|
"img_id": datasets.Value("string"), }), |
|
|
supervised_keys=None, |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
df = pd.read_csv(os.path.join(self.config.data_dir, "gt.csv"), |
|
|
delimiter=";") |
|
|
return [ |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.VALIDATION, |
|
|
gen_kwargs={"df": df, |
|
|
"images_dir": os.path.join(self.config.data_dir, "data")} |
|
|
) |
|
|
] |
|
|
|
|
|
def _generate_examples(self, df, images_dir): |
|
|
|
|
|
for idx, row in df.iterrows(): |
|
|
img_id = row.get("img_id") |
|
|
if not img_id: |
|
|
continue |
|
|
image_path = os.path.join(images_dir, f"{img_id}") |
|
|
if not os.path.exists(image_path): |
|
|
continue |
|
|
yield idx, { |
|
|
"image": image_path, |
|
|
"source": row["source"], |
|
|
"question": row["question"], |
|
|
"answer": row["answer"], |
|
|
"img_id": img_id.split(".")[0], |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|