Update pmc_oa.py
Browse files
pmc_oa.py
CHANGED
@@ -5,8 +5,10 @@ import jsonlines
|
|
5 |
|
6 |
import datasets
|
7 |
|
|
|
8 |
logger = datasets.logging.get_logger(__name__)
|
9 |
|
|
|
10 |
_CITATION = """\
|
11 |
@article{lin2023pmc,
|
12 |
title={PMC-CLIP: Contrastive Language-Image Pre-training using Biomedical Documents},
|
@@ -26,116 +28,57 @@ including image-text retrieval on ROCO, MedMNIST image classification, Medical V
|
|
26 |
|
27 |
_HOMEPAGE = "https://weixionglin.github.io/PMC-CLIP/"
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
"
|
32 |
-
"
|
|
|
|
|
33 |
}
|
34 |
|
35 |
|
36 |
-
class PMC_OA_Config(datasets.BuilderConfig):
|
37 |
-
"""BuilderConfig for PMC_OA"""
|
38 |
-
|
39 |
-
def __init__(self, **kwargs):
|
40 |
-
"""
|
41 |
-
Args:
|
42 |
-
**kwargs: keyword arguments forwarded to super.
|
43 |
-
"""
|
44 |
-
super(PMC_OA_Config, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
|
45 |
-
|
46 |
-
|
47 |
class PMC_OA(datasets.GeneratorBasedBuilder):
|
48 |
-
"""PMC_OA Dataset"""
|
49 |
-
|
50 |
-
VERSION = datasets.Version("1.0.0")
|
51 |
-
BUILDER_CONFIGS = [
|
52 |
-
PMC_OA_Config(
|
53 |
-
name="pmc_oa_beta",
|
54 |
-
description="<subfigure, caption> pairs. Subfigures detected by a DETR model.",
|
55 |
-
),
|
56 |
-
PMC_OA_Config(
|
57 |
-
name="pmc_oa",
|
58 |
-
description="<subfigure, subcaption> pairs. Subfigures detected by a DETR model. Subcaptions detected by ChatGPT and aligned with subfigures.",
|
59 |
-
),
|
60 |
-
]
|
61 |
|
62 |
def _info(self):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
)
|
76 |
-
elif self.config.name == "pmc_oa":
|
77 |
-
return datasets.DatasetInfo(
|
78 |
-
description=_DESCRIPTION,
|
79 |
-
features=datasets.Features(
|
80 |
-
{
|
81 |
-
"image": datasets.Value("string"),
|
82 |
-
"caption": datasets.Value("string"),
|
83 |
-
"alignment_type": datasets.Value("string"),
|
84 |
-
"alignment_score": datasets.Value("float"),
|
85 |
-
}
|
86 |
-
),
|
87 |
-
supervised_keys=None,
|
88 |
-
citation=_CITATION,
|
89 |
-
homepage=_HOMEPAGE,
|
90 |
-
)
|
91 |
|
92 |
def _split_generators(self, dl_manager):
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
)
|
102 |
-
]
|
103 |
-
elif self.config.name == "pmc_oa":
|
104 |
-
return [
|
105 |
-
datasets.SplitGenerator(
|
106 |
-
name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["pmc_oa"], "image_dir": downloaded_files['images']}
|
107 |
-
)
|
108 |
-
]
|
109 |
|
110 |
def _generate_examples(self, filepath, image_dir):
|
111 |
-
"""
|
112 |
logger.info("generating examples from = %s", filepath)
|
113 |
-
|
114 |
with jsonlines.open(filepath) as reader:
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
relative_image_path = obj['image']
|
129 |
-
image_path = os.path.join(image_dir, "caption_T060_filtered_top4_sep_v0_subfigures", relative_image_path)
|
130 |
-
caption = obj['caption']
|
131 |
-
alignment_type = obj['alignment_type']
|
132 |
-
alignment_score = obj['alignment_score']
|
133 |
-
yield _id, {
|
134 |
-
"image": {
|
135 |
-
"path": image_path,
|
136 |
-
"bytes": open(image_path, "rb").read(),
|
137 |
-
},
|
138 |
-
"caption": caption,
|
139 |
-
"alignment_type": alignment_type,
|
140 |
-
"alignment_score": alignment_score,
|
141 |
-
}
|
|
|
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},
|
|
|
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": datasets.Image(),
|
49 |
+
"caption": datasets.Value("string"),
|
50 |
+
}
|
51 |
+
),
|
52 |
+
supervised_keys=None,
|
53 |
+
homepage=_HOMEPAGE,
|
54 |
+
citation=_CITATION,
|
55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
def _split_generators(self, dl_manager):
|
58 |
+
data_dir = dl_manager.download_and_extract(_URLS)
|
59 |
+
raise RuntimeError(data_dir)
|
60 |
+
|
61 |
+
return [
|
62 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_dir['train'], "image_dir": data_dir['images']}),
|
63 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_dir['valid'], "image_dir": data_dir['images']}),
|
64 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_dir['test'], "image_dir": data_dir['images']}),
|
65 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
def _generate_examples(self, filepath, image_dir):
|
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(image_dir, "caption_T060_filtered_top4_sep_v0_subfigures", 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|