axiong commited on
Commit
33bd5b5
1 Parent(s): 6f2a1e0

Create pmc_oa.py

Browse files
Files changed (1) hide show
  1. pmc_oa.py +85 -0
pmc_oa.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """PMC-OA Dataset"""
2
+
3
+ import os
4
+ import jsonlines
5
+
6
+ import datasets
7
+
8
+
9
+ logger = datasets.logging.get_logger(__name__)
10
+
11
+
12
+ _CITATION = """\
13
+ @article{lin2023pmc,
14
+ title={PMC-CLIP: Contrastive Language-Image Pre-training using Biomedical Documents},
15
+ author={Lin, Weixiong and Zhao, Ziheng and Zhang, Xiaoman and Wu, Chaoyi and Zhang, Ya and Wang, Yanfeng and Xie, Weidi},
16
+ journal={arXiv preprint arXiv:2303.07240},
17
+ year={2023}
18
+ }
19
+ """
20
+
21
+ _DESCRIPTION = """\
22
+ 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.
23
+ 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.
24
+ PMC-OA covers diverse modalities or diseases, with majority of the image-caption samples aligned at finer-grained level, i.e., subfigure and subcaption.
25
+ While pretraining a CLIP-style model on PMC-OA, our model named PMC-CLIP achieves state-of-the-art results on various downstream tasks,
26
+ 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.
27
+ """
28
+
29
+ _HOMEPAGE = "https://weixionglin.github.io/PMC-CLIP/"
30
+
31
+ _URL = "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/"
32
+ _URLS = {
33
+ "images": _URL + "images.zip",
34
+ # "train": _URL + "train.jsonl",
35
+ "valid": _URL + "valid.jsonl",
36
+ "test": _URL + "test.jsonl"
37
+ }
38
+
39
+
40
+ class PMC_OA(datasets.GeneratorBasedBuilder):
41
+ """PMC_OA Dataset."""
42
+
43
+ def _info(self):
44
+ return datasets.DatasetInfo(
45
+ description=_DESCRIPTION,
46
+ features=datasets.Features(
47
+ {
48
+ # "image_name": datasets.Value("string"),
49
+ "image": datasets.Image(),
50
+ "caption": datasets.Value("string"),
51
+ }
52
+ ),
53
+ supervised_keys=None,
54
+ homepage=_HOMEPAGE,
55
+ citation=_CITATION,
56
+ )
57
+
58
+ def _split_generators(self, dl_manager):
59
+ data_dir = dl_manager.download_and_extract(_URLS)
60
+
61
+ return [
62
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(data_dir, "train.jsonl")}),
63
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(data_dir, "valid.jsonl")}),
64
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(data_dir, "test.jsonl")}),
65
+ ]
66
+
67
+ def _generate_examples(self, filepath):
68
+ """This function returns the examples in the raw (text) form."""
69
+ logger.info("generating examples from = %s", filepath)
70
+
71
+ with jsonlines.open(filepath) as reader:
72
+ _id = 0
73
+ for obj in reader:
74
+ relative_image_path = obj['image']
75
+ image_path = os.path.join("images", relative_image_path)
76
+ caption = obj['caption']
77
+ yield _id, {
78
+ "image": {
79
+ "path": image_path,
80
+ "bytes": open(image_path, "rb").read(),
81
+ },
82
+ "caption": caption,
83
+ }
84
+ _id += 1
85
+