Alexander Suslov
commited on
Commit
·
fe0f47c
1
Parent(s):
71156c8
added mvtec_capsule.py
Browse files- mvtec_capsule.py +127 -0
mvtec_capsule.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from enum import Enum
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import datasets
|
| 6 |
+
from pandas import DataFrame
|
| 7 |
+
|
| 8 |
+
_HOMEPAGE = "https://www.mvtec.com/company/research/datasets/mvtec-ad"
|
| 9 |
+
_LICENSE = "cc-by-nc-sa-4.0"
|
| 10 |
+
_CITATION = """\
|
| 11 |
+
@misc{
|
| 12 |
+
the-mvtec-anomaly-detection-dataset,
|
| 13 |
+
title = { The MVTec Anomaly Detection Dataset: A Comprehensive Real-World Dataset for Unsupervised Anomaly Detection },
|
| 14 |
+
type = { Open Source Dataset },
|
| 15 |
+
author = { Paul Bergmann, Kilian Batzner, Michael Fauser, David Sattlegger, Carsten Steger },
|
| 16 |
+
howpublished = { \\url{ https://link.springer.com/article/10.1007%2Fs11263-020-01400-4 } },
|
| 17 |
+
url = { https://link.springer.com/article/10.1007%2Fs11263-020-01400-4 },
|
| 18 |
+
}
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class LabelName(int, Enum):
|
| 23 |
+
NORMAL = 0
|
| 24 |
+
ABNORMAL = 1
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class MVTECCapsule(datasets.GeneratorBasedBuilder):
|
| 28 |
+
"""satellite-building-segmentation instance segmentation dataset"""
|
| 29 |
+
|
| 30 |
+
VERSION = datasets.Version("1.0.0")
|
| 31 |
+
_URL = "https://huggingface.co/datasets/alexsu52/mvtec_capsule/resolve/main/capsule.tar.xz"
|
| 32 |
+
|
| 33 |
+
def _info(self):
|
| 34 |
+
features = datasets.Features(
|
| 35 |
+
{
|
| 36 |
+
"image": datasets.Image(),
|
| 37 |
+
"mask": datasets.Image(),
|
| 38 |
+
"label": datasets.ClassLabel(names=["normal", "abnormal"]),
|
| 39 |
+
}
|
| 40 |
+
)
|
| 41 |
+
return datasets.DatasetInfo(
|
| 42 |
+
features=features,
|
| 43 |
+
homepage=_HOMEPAGE,
|
| 44 |
+
citation=_CITATION,
|
| 45 |
+
license=_LICENSE,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def _split_generators(self, dl_manager):
|
| 49 |
+
folder_dir = dl_manager.download_and_extract(self._URL)
|
| 50 |
+
category_dir = os.path.join(folder_dir, "capsule")
|
| 51 |
+
return [
|
| 52 |
+
datasets.SplitGenerator(
|
| 53 |
+
name=datasets.Split.TRAIN,
|
| 54 |
+
gen_kwargs={
|
| 55 |
+
"category_dir": category_dir,
|
| 56 |
+
"split": "train",
|
| 57 |
+
},
|
| 58 |
+
),
|
| 59 |
+
datasets.SplitGenerator(
|
| 60 |
+
name=datasets.Split.TEST,
|
| 61 |
+
gen_kwargs={
|
| 62 |
+
"category_dir": category_dir,
|
| 63 |
+
"split": "test",
|
| 64 |
+
},
|
| 65 |
+
),
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
def _generate_examples(self, category_dir, split):
|
| 69 |
+
extensions = (".png", ".PNG")
|
| 70 |
+
|
| 71 |
+
root = Path(category_dir)
|
| 72 |
+
samples_list = [(str(root),) + f.parts[-3:] for f in root.glob(r"**/*") if f.suffix in extensions]
|
| 73 |
+
if not samples_list:
|
| 74 |
+
raise RuntimeError(f"Found 0 images in {root}")
|
| 75 |
+
|
| 76 |
+
samples = DataFrame(samples_list, columns=["path", "split", "label", "image_path"])
|
| 77 |
+
|
| 78 |
+
# Modify image_path column by converting to absolute path
|
| 79 |
+
samples["image_path"] = samples.path + "/" + samples.split + "/" + samples.label + "/" + samples.image_path
|
| 80 |
+
|
| 81 |
+
# Create label index for normal (0) and anomalous (1) images.
|
| 82 |
+
samples.loc[(samples.label == "good"), "label_index"] = LabelName.NORMAL
|
| 83 |
+
samples.loc[(samples.label != "good"), "label_index"] = LabelName.ABNORMAL
|
| 84 |
+
samples.label_index = samples.label_index.astype(int)
|
| 85 |
+
|
| 86 |
+
# separate masks from samples
|
| 87 |
+
mask_samples = samples.loc[samples.split == "ground_truth"].sort_values(by="image_path", ignore_index=True)
|
| 88 |
+
samples = samples[samples.split != "ground_truth"].sort_values(by="image_path", ignore_index=True)
|
| 89 |
+
|
| 90 |
+
# assign mask paths to anomalous test images
|
| 91 |
+
samples["mask_path"] = ""
|
| 92 |
+
samples.loc[
|
| 93 |
+
(samples.split == "test") & (samples.label_index == LabelName.ABNORMAL), "mask_path"
|
| 94 |
+
] = mask_samples.image_path.values
|
| 95 |
+
|
| 96 |
+
# assert that the right mask files are associated with the right test images
|
| 97 |
+
if len(samples.loc[samples.label_index == LabelName.ABNORMAL]):
|
| 98 |
+
assert (
|
| 99 |
+
samples.loc[samples.label_index == LabelName.ABNORMAL]
|
| 100 |
+
.apply(lambda x: Path(x.image_path).stem in Path(x.mask_path).stem, axis=1)
|
| 101 |
+
.all()
|
| 102 |
+
), "Mismatch between anomalous images and ground truth masks. Make sure the mask files in 'ground_truth' \
|
| 103 |
+
folder follow the same naming convention as the anomalous images in the dataset (e.g. image: \
|
| 104 |
+
'000.png', mask: '000.png' or '000_mask.png')."
|
| 105 |
+
|
| 106 |
+
if split:
|
| 107 |
+
samples = samples[samples.split == split].reset_index(drop=True)
|
| 108 |
+
|
| 109 |
+
for idx in range(len(samples)):
|
| 110 |
+
image_path = samples.iloc[idx].image_path
|
| 111 |
+
mask_path = samples.iloc[idx].mask_path
|
| 112 |
+
label_index = samples.iloc[idx].label_index
|
| 113 |
+
|
| 114 |
+
with open(image_path, "rb") as f:
|
| 115 |
+
image_bytes = f.read()
|
| 116 |
+
|
| 117 |
+
if mask_path:
|
| 118 |
+
with open(mask_path, "rb") as f:
|
| 119 |
+
mask_bytes = f.read()
|
| 120 |
+
else:
|
| 121 |
+
mask_bytes = bytes(len(image_bytes))
|
| 122 |
+
|
| 123 |
+
yield idx, {
|
| 124 |
+
"image": {"path": image_path, "bytes": image_bytes},
|
| 125 |
+
"mask": {"path": mask_path, "bytes": mask_bytes},
|
| 126 |
+
"label": label_index,
|
| 127 |
+
}
|