Datasets:

Languages:
English
ArXiv:
License:
File size: 2,726 Bytes
a3f2883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import random
import datasets
from datasets.tasks import ImageClassification


_HOMEPAGE = (
    f"https://www.modelscope.cn/datasets/MuGeminorum/{os.path.basename(__file__)[:-3]}"
)

_URL = f"{_HOMEPAGE}/resolve/master/images.zip"

_NAMES = ["Centromere", "Golgi", "Homogeneous", "NuMem", "Nucleolar", "Speckled"]


class HEp2(datasets.GeneratorBasedBuilder):
    def _info(self):
        return datasets.DatasetInfo(
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.features.ClassLabel(names=_NAMES),
                }
            ),
            supervised_keys=("image", "label"),
            homepage=_HOMEPAGE,
            license="mit",
            version="0.0.1",
            task_templates=[
                ImageClassification(
                    task="image-classification",
                    image_column="image",
                    label_column="label",
                )
            ],
        )

    def _ground_truth(self, id):
        if id < 2495:
            return "Homogeneous"

        elif id < 5326:
            return "Speckled"

        elif id < 7924:
            return "Nucleolar"

        elif id < 10665:
            return "Centromere"

        elif id < 12873:
            return "NuMem"

        else:
            return "Golgi"

    def _split_generators(self, dl_manager):
        data_files = dl_manager.download_and_extract(_URL)
        files = dl_manager.iter_files([data_files])
        dataset = []
        for path in files:
            file_name = os.path.basename(path)
            if file_name.endswith(".png"):
                dataset.append(
                    {
                        "image": path,
                        "label": self._ground_truth(int(file_name.split(".")[0])),
                    }
                )

        random.shuffle(dataset)
        data_count = len(dataset)
        p80 = int(data_count * 0.8)
        p90 = int(data_count * 0.9)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "files": dataset[:p80],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "files": dataset[p80:p90],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "files": dataset[p90:],
                },
            ),
        ]

    def _generate_examples(self, files):
        for i, path in enumerate(files):
            yield i, path