Datasets:
ArXiv:
License:
Upload medianomaly.py
Browse files- medianomaly.py +93 -0
medianomaly.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
from PIL import Image
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
_DESCRIPTION = """\
|
7 |
+
MedIAnomaly is a benchmark for evaluating anomaly detection methods on seven diverse medical imaging datasets:
|
8 |
+
RSNA, VinCXR, BrainTumor, LAG, ISIC2018_Task3, Camelyon16, and BraTS2021. It supports both image-level
|
9 |
+
classification and pixel-level segmentation tasks.
|
10 |
+
|
11 |
+
All datasets follow a consistent one-class learning protocol: the training set contains only normal (non-anomalous)
|
12 |
+
images, while the test set includes both normal and abnormal cases. This setting is designed to reflect real-world
|
13 |
+
scenarios where anomalous samples are rare or unavailable during training. MedIAnomaly provides standardized preprocessing, train/test splits, and label formats to facilitate fair comparison
|
14 |
+
across methods.
|
15 |
+
"""
|
16 |
+
|
17 |
+
_HOMEPAGE = "https://github.com/caiyu6666/MedIAnomaly/tree/main"
|
18 |
+
|
19 |
+
_CITATION = """\
|
20 |
+
@article{cai2024medianomaly,
|
21 |
+
title={MedIAnomaly: A comparative study of anomaly detection in medical images},
|
22 |
+
author={Cai, Yu and Zhang, Weiwen and Chen, Hao and Cheng, Kwang-Ting},
|
23 |
+
journal={arXiv preprint arXiv:2404.04518},
|
24 |
+
year={2024}
|
25 |
+
}
|
26 |
+
"""
|
27 |
+
|
28 |
+
_BASE_URL = "https://huggingface.co/datasets/randall-lab/medianomaly/resolve/main"
|
29 |
+
|
30 |
+
_URLS = {
|
31 |
+
"rsna": f"{_BASE_URL}/rsna.tar",
|
32 |
+
"brats2021": f"{_BASE_URL}/brats2021.tar",
|
33 |
+
"braintumor": f"{_BASE_URL}/braintumor.tar",
|
34 |
+
"camelyon16": f"{_BASE_URL}/camelyon16.tar",
|
35 |
+
"isic2018": f"{_BASE_URL}/isic2018.tar",
|
36 |
+
"lag": f"{_BASE_URL}/lag.tar",
|
37 |
+
"vincxr": f"{_BASE_URL}/vincxr.tar",
|
38 |
+
}
|
39 |
+
|
40 |
+
class Medianomaly(datasets.GeneratorBasedBuilder):
|
41 |
+
BUILDER_CONFIGS = [
|
42 |
+
datasets.BuilderConfig(name="rsna", version=datasets.Version("1.0.0"), description="RSNA Pneumonia dataset."),
|
43 |
+
datasets.BuilderConfig(name="brats2021", version=datasets.Version("1.0.0"), description="BraTS2021 brain tumor dataset."),
|
44 |
+
datasets.BuilderConfig(name="braintumor", version=datasets.Version("1.0.0"), description="BrainTumor MRI dataset."),
|
45 |
+
datasets.BuilderConfig(name="camelyon16", version=datasets.Version("1.0.0"), description="Camelyon16 histopathology dataset."),
|
46 |
+
datasets.BuilderConfig(name="isic2018_task3", version=datasets.Version("1.0.0"), description="ISIC 2018 melanoma classification dataset."),
|
47 |
+
datasets.BuilderConfig(name="lag", version=datasets.Version("1.0.0"), description="LAG (glaucoma detection) fundus dataset."),
|
48 |
+
datasets.BuilderConfig(name="vincxr", version=datasets.Version("1.0.0"), description="VinCXR chest X-ray dataset."),
|
49 |
+
]
|
50 |
+
|
51 |
+
def _info(self):
|
52 |
+
return datasets.DatasetInfo(
|
53 |
+
description=_DESCRIPTION,
|
54 |
+
features=datasets.Features({
|
55 |
+
"image": datasets.Image(),
|
56 |
+
"label": datasets.ClassLabel(names=["normal", "abnormal"]),
|
57 |
+
}),
|
58 |
+
supervised_keys=("image", "label"),
|
59 |
+
homepage=_HOMEPAGE,
|
60 |
+
license="apache-2.0",
|
61 |
+
citation=_CITATION,
|
62 |
+
)
|
63 |
+
|
64 |
+
def _split_generators(self, dl_manager):
|
65 |
+
config_name = self.config.name.lower()
|
66 |
+
if config_name not in _URLS:
|
67 |
+
raise NotImplementedError(f"{config_name} is not implemented in Medianomaly.")
|
68 |
+
archive_path = dl_manager.download_and_extract(_URLS[config_name])
|
69 |
+
|
70 |
+
if config_name == "rsna":
|
71 |
+
data_dir = os.path.join(archive_path, config_name)
|
72 |
+
with open(os.path.join(data_dir, "data.json"), "r") as f:
|
73 |
+
metadata = json.load(f)
|
74 |
+
return [
|
75 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
|
76 |
+
"samples": metadata["train"], "base_dir": data_dir, "config": config_name
|
77 |
+
}),
|
78 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={
|
79 |
+
"samples": metadata["test"], "base_dir": data_dir, "config": config_name
|
80 |
+
}),
|
81 |
+
]
|
82 |
+
|
83 |
+
|
84 |
+
def _generate_examples(self, samples, base_dir, config):
|
85 |
+
if config == "rsna":
|
86 |
+
for label_str, items in samples.items(): # only "0" in train, "0"/"1" in test
|
87 |
+
label = int(label_str)
|
88 |
+
for idx, item in enumerate(items):
|
89 |
+
image_path = os.path.join(base_dir, item["image"])
|
90 |
+
yield idx, {
|
91 |
+
"image": image_path,
|
92 |
+
"label": label,
|
93 |
+
}
|