pmc_oa / pmc_oa.py
axiong's picture
Update pmc_oa.py
561e21d
raw
history blame
3.32 kB
"""PMC-OA Dataset"""
import os
import jsonlines
import datasets
logger = datasets.logging.get_logger(__name__)
_CITATION = """\
@article{lin2023pmc,
title={PMC-CLIP: Contrastive Language-Image Pre-training using Biomedical Documents},
author={Lin, Weixiong and Zhao, Ziheng and Zhang, Xiaoman and Wu, Chaoyi and Zhang, Ya and Wang, Yanfeng and Xie, Weidi},
journal={arXiv preprint arXiv:2303.07240},
year={2023}
}
"""
_DESCRIPTION = """\
Foundation models trained on large-scale dataset gain a recent surge in CV and NLP. In contrast, development in biomedical domain lags far behind due to data scarcity.
To address this issue, we build and release PMC-OA, a biomedical dataset with 1.6M image-caption pairs collected from PubMedCentral's OpenAccess subset, which is 8 times larger than before.
PMC-OA covers diverse modalities or diseases, with majority of the image-caption samples aligned at finer-grained level, i.e., subfigure and subcaption.
While pretraining a CLIP-style model on PMC-OA, our model named PMC-CLIP achieves state-of-the-art results on various downstream tasks,
including image-text retrieval on ROCO, MedMNIST image classification, Medical VQA, i.e. +8.1% R@10 on image-text retrieval, +3.9% accuracy on image classification.
"""
_HOMEPAGE = "https://weixionglin.github.io/PMC-CLIP/"
_URL = "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/"
_URLS = {
"images": _URL + "images.zip",
"train": _URL + "train.jsonl",
"valid": _URL + "valid.jsonl",
"test": _URL + "test.jsonl"
}
class PMC_OA(datasets.GeneratorBasedBuilder):
"""PMC_OA Dataset."""
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
# "image_name": datasets.Value("string"),
"image": datasets.Image(),
"caption": datasets.Value("string"),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
data_dir = dl_manager.download_and_extract(_URLS)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(data_dir, "train.jsonl")}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(data_dir, "valid.jsonl")}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(data_dir, "test.jsonl")}),
]
def _generate_examples(self, filepath):
"""This function returns the examples in the raw (text) form."""
logger.info("generating examples from = %s", filepath)
with jsonlines.open(filepath) as reader:
_id = 0
for obj in reader:
relative_image_path = obj['image']
image_path = os.path.join("images", relative_image_path)
caption = obj['caption']
yield _id, {
"image": {
"path": image_path,
"bytes": open(image_path, "rb").read(),
},
"caption": caption,
}
_id += 1