Datasets:
ArXiv:
License:
Update medianomaly.py
Browse files- medianomaly.py +144 -13
medianomaly.py
CHANGED
@@ -53,17 +53,54 @@ class Medianomaly(datasets.GeneratorBasedBuilder):
|
|
53 |
]
|
54 |
|
55 |
def _info(self):
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
def _split_generators(self, dl_manager):
|
69 |
config_name = self.config.name.lower()
|
@@ -83,7 +120,26 @@ class Medianomaly(datasets.GeneratorBasedBuilder):
|
|
83 |
"samples": metadata["test"], "base_dir": data_dir, "config": config_name
|
84 |
}),
|
85 |
]
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
def _generate_examples(self, samples, base_dir, config):
|
89 |
if config in ["rsna", "vincxr", "braintumor", "lag"]:
|
@@ -95,4 +151,79 @@ class Medianomaly(datasets.GeneratorBasedBuilder):
|
|
95 |
yield idx, {
|
96 |
"image": image_path,
|
97 |
"label": label,
|
98 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
]
|
54 |
|
55 |
def _info(self):
|
56 |
+
config_name = self.config.name.lower()
|
57 |
+
if config_name in ["rsna", "vincxr", "braintumor", "lag", "camelyon16"]:
|
58 |
+
return datasets.DatasetInfo(
|
59 |
+
description=_DESCRIPTION,
|
60 |
+
features=datasets.Features({
|
61 |
+
"image": datasets.Image(),
|
62 |
+
"label": datasets.ClassLabel(names=["normal", "abnormal"]),
|
63 |
+
}),
|
64 |
+
supervised_keys=("image", "label"),
|
65 |
+
homepage=_HOMEPAGE,
|
66 |
+
license="apache-2.0",
|
67 |
+
citation=_CITATION,
|
68 |
+
)
|
69 |
+
elif config_name == "brats2021":
|
70 |
+
return datasets.DatasetInfo(
|
71 |
+
description=_DESCRIPTION,
|
72 |
+
features=datasets.Features({
|
73 |
+
"image": datasets.Image(),
|
74 |
+
"label": datasets.ClassLabel(names=["normal", "abnormal"]),
|
75 |
+
"annotation": datasets.Image(),
|
76 |
+
}),
|
77 |
+
supervised_keys=("image", "label"),
|
78 |
+
homepage=_HOMEPAGE,
|
79 |
+
license="apache-2.0",
|
80 |
+
citation=_CITATION,
|
81 |
+
)
|
82 |
+
elif config_name == "isic2018_task3":
|
83 |
+
return datasets.DatasetInfo(
|
84 |
+
description=_DESCRIPTION,
|
85 |
+
features=datasets.Features({
|
86 |
+
"image": datasets.Image(),
|
87 |
+
"labels": datasets.Sequence(datasets.Value("int32")),
|
88 |
+
"MEL": datasets.ClassLabel(names=["melanoma", "non-melanoma"]),
|
89 |
+
"NV": datasets.ClassLabel(names=["nevus", "non-nevus"]),
|
90 |
+
"BCC": datasets.ClassLabel(names=["basal cell carcinoma", "non-basal cell carcinoma"]),
|
91 |
+
"AKIEC": datasets.ClassLabel(names=["actinic keratosis", "non-actinic keratosis"]),
|
92 |
+
"BKL": datasets.ClassLabel(names=["benign keratosis", "non-benign keratosis"]),
|
93 |
+
"VASC": datasets.ClassLabel(names=["vascular lesion", "non-vascular lesion"]),
|
94 |
+
"DF": datasets.ClassLabel(names=["dermatofibroma", "non-dermatofibroma"]),
|
95 |
+
}),
|
96 |
+
supervised_keys=("image", "labels"),
|
97 |
+
homepage=_HOMEPAGE,
|
98 |
+
license="apache-2.0",
|
99 |
+
citation=_CITATION,
|
100 |
+
)
|
101 |
+
else:
|
102 |
+
raise NotImplementedError(f"{config_name} is not implemented in Medianomaly.")
|
103 |
+
|
104 |
|
105 |
def _split_generators(self, dl_manager):
|
106 |
config_name = self.config.name.lower()
|
|
|
120 |
"samples": metadata["test"], "base_dir": data_dir, "config": config_name
|
121 |
}),
|
122 |
]
|
123 |
+
elif config_name == "brats2021":
|
124 |
+
data_dir = os.path.join(archive_path, config_names[config_name])
|
125 |
+
return [
|
126 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
|
127 |
+
"samples": "train", "base_dir": data_dir, "config": config_name
|
128 |
+
}),
|
129 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={
|
130 |
+
"samples": "test", "base_dir": data_dir, "config": config_name
|
131 |
+
}),
|
132 |
+
]
|
133 |
+
elif config_name == "camelyon16":
|
134 |
+
data_dir = os.path.join(archive_path, config_names[config_name])
|
135 |
+
return [
|
136 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
|
137 |
+
"samples": "train", "base_dir": data_dir, "config": config_name
|
138 |
+
}),
|
139 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={
|
140 |
+
"samples": "test", "base_dir": data_dir, "config": config_name
|
141 |
+
}),
|
142 |
+
]
|
143 |
|
144 |
def _generate_examples(self, samples, base_dir, config):
|
145 |
if config in ["rsna", "vincxr", "braintumor", "lag"]:
|
|
|
151 |
yield idx, {
|
152 |
"image": image_path,
|
153 |
"label": label,
|
154 |
+
}
|
155 |
+
|
156 |
+
elif config == "brats2021":
|
157 |
+
if samples == "train":
|
158 |
+
base_dir = os.path.join(base_dir, "train")
|
159 |
+
for idx, item in enumerate(os.listdir(base_dir)):
|
160 |
+
image_path = os.path.join(base_dir, item)
|
161 |
+
yield idx, {
|
162 |
+
"image": image_path,
|
163 |
+
"label": 0, # All training images are normal
|
164 |
+
}
|
165 |
+
elif samples == "test":
|
166 |
+
image_dir_normal = os.path.join(base_dir, "test", "normal")
|
167 |
+
image_dir_tumor = os.path.join(base_dir, "test", "tumor")
|
168 |
+
annot_dir = os.path.join(base_dir, "test", "annotation")
|
169 |
+
|
170 |
+
idx = 0
|
171 |
+
for fname in os.listdir(image_dir_normal):
|
172 |
+
if fname.endswith(".png"):
|
173 |
+
image_path = os.path.join(image_dir_normal, fname)
|
174 |
+
yield idx, {
|
175 |
+
"image": image_path,
|
176 |
+
"label": 0,
|
177 |
+
"annotation": None,
|
178 |
+
}
|
179 |
+
idx += 1
|
180 |
+
|
181 |
+
for fname in os.listdir(image_dir_tumor):
|
182 |
+
if fname.endswith(".png"):
|
183 |
+
image_path = os.path.join(image_dir_tumor, fname)
|
184 |
+
annot_name = fname.replace("flair", "seg")
|
185 |
+
annot_path = os.path.join(annot_dir, annot_name)
|
186 |
+
|
187 |
+
yield idx, {
|
188 |
+
"image": image_path,
|
189 |
+
"label": 1,
|
190 |
+
"annotation": annot_path,
|
191 |
+
}
|
192 |
+
idx += 1
|
193 |
+
|
194 |
+
elif config == "camelyon16":
|
195 |
+
if samples == "train":
|
196 |
+
base_dir = os.path.join(base_dir, "train")
|
197 |
+
base_dir = os.path.join(base_dir, "good")
|
198 |
+
for idx, item in enumerate(os.listdir(base_dir)):
|
199 |
+
image_path = os.path.join(base_dir, item)
|
200 |
+
yield idx, {
|
201 |
+
"image": image_path,
|
202 |
+
"label": 0, # All training images are normal
|
203 |
+
}
|
204 |
+
elif samples == "test":
|
205 |
+
base_dir = os.path.join(base_dir, "test")
|
206 |
+
good_dir = os.path.join(base_dir, "good")
|
207 |
+
ungood_dir = os.path.join(base_dir, "ungood")
|
208 |
+
|
209 |
+
idx = 0
|
210 |
+
for item in os.listdir(good_dir):
|
211 |
+
if item.endswith(".png"):
|
212 |
+
image_path = os.path.join(base_dir, item)
|
213 |
+
yield idx, {
|
214 |
+
"image": image_path,
|
215 |
+
"label": 0,
|
216 |
+
}
|
217 |
+
idx += 1
|
218 |
+
|
219 |
+
for item in os.listdir(ungood_dir):
|
220 |
+
if item.endswith(".png"):
|
221 |
+
image_path = os.path.join(base_dir, item)
|
222 |
+
yield idx, {
|
223 |
+
"image": image_path,
|
224 |
+
"label": 1,
|
225 |
+
}
|
226 |
+
idx += 1
|
227 |
+
|
228 |
+
|
229 |
+
|