benschill commited on
Commit
2ffa416
1 Parent(s): 26c4afb

Upload brain-tumor-collection.py

Browse files
Files changed (1) hide show
  1. brain-tumor-collection.py +129 -0
brain-tumor-collection.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Collection of brain xray images for fine-grain classification."""
15
+
16
+ import datasets
17
+ import numpy as np
18
+ import pandas as pd
19
+ from pathlib import Path
20
+ import os
21
+
22
+ _CITATION = """\
23
+ @misc{kaggle-brain-tumor-classification,
24
+ title={Kaggle: Brain Tumor Classification (MRI)},
25
+ howpublished={\\url{https://www.kaggle.com/datasets/sartajbhuvaji/brain-tumor-classification-mri?resource=download}},
26
+ note = {Accessed: 2022-06-30},
27
+ }
28
+ """
29
+
30
+ _DESCRIPTION = """\
31
+ This dataset is intended as a test case for classification tasks (4 different kinds of brain xrays). The dataset consists of almost 1400 JPEG images grouped into two splits - training and validation. Each split contains 4 categories labeled as n0~n3, each corresponding to a cancer result of the mrt.
32
+
33
+
34
+ | Label | Xray Category | Train Images | Validation Images |
35
+ | ----- | --------------------- | ------------ | ----------------- |
36
+ | n0 | glioma_tumor | 826 | 100 |
37
+ | n1 | meningioma_tumor | 822 | 115 |
38
+ | n2 | pituitary_tumor | 827 | 74 |
39
+ | n3 | no_tumor | 395 | 105 |
40
+
41
+
42
+ """
43
+
44
+ _HOMEPAGE = "https://www.kaggle.com/datasets/sartajbhuvaji/brain-tumor-classification-mri?resource=download"
45
+
46
+ _LICENSE = "cc0-1.0"
47
+
48
+ _URLS = {
49
+ "original": "https://ibm.ent.box.com/index.php?rm=box_download_shared_file&shared_name=nf6md3mxww5k9rw4ks1hwffuo5iez1pc&file_id=f_978363130854"
50
+ }
51
+
52
+ LABELS = [
53
+ "Glioma Tumor",
54
+ "Meningioma Tumor",
55
+ "Pituitary Tumor",
56
+ "No Tumor"
57
+ ]
58
+
59
+
60
+ class BrainTumorCollectionGenerator(datasets.GeneratorBasedBuilder):
61
+ """Collection of brain xray images for fine-grain classification."""
62
+
63
+ VERSION = datasets.Version("1.0.0")
64
+
65
+ BUILDER_CONFIGS = [
66
+ datasets.BuilderConfig(name="original", version=VERSION, description="Original JPEG files: images are 400x300 px or larger; ~550 MB"),
67
+ ]
68
+
69
+ DEFAULT_CONFIG_NAME = "original"
70
+
71
+ def _info(self):
72
+ features = datasets.Features(
73
+ {
74
+ "image": datasets.Image(),
75
+ "label": datasets.ClassLabel(names=LABELS)
76
+ }
77
+ )
78
+ supervised_keys = ("image", "label")
79
+
80
+ return datasets.DatasetInfo(
81
+ description=_DESCRIPTION,
82
+ features=features,
83
+ supervised_keys=supervised_keys,
84
+ homepage=_HOMEPAGE,
85
+ license=_LICENSE,
86
+ citation=_CITATION,
87
+ )
88
+
89
+ def _split_generators(self, dl_manager):
90
+ url = _URLS[self.config.name]
91
+ data_dir = dl_manager.download_and_extract(url)
92
+ print("Test"+data_dir)
93
+
94
+ return [
95
+ datasets.SplitGenerator(
96
+ name=datasets.Split.TRAIN,
97
+ gen_kwargs={
98
+ "filepath": os.path.join(data_dir, "xrays", "training", "training"),
99
+ "split": "train",
100
+ },
101
+ ),
102
+ datasets.SplitGenerator(
103
+ name=datasets.Split.TEST,
104
+ gen_kwargs={
105
+ "filepath": os.path.join(data_dir, "xrays", "validation", "validation"),
106
+ "split": "test",
107
+ },
108
+ ),
109
+ ]
110
+
111
+ def _generate_examples(self, filepath, split):
112
+ paths = list(Path(filepath).glob("**/*.jpg"))
113
+ data = []
114
+
115
+ for path in paths:
116
+ tumor_folder = str(path).split("/")[-2]
117
+ index = int(tumor_folder[1])
118
+ label = LABELS[index]
119
+ data.append({"file": str(path), "label": label})
120
+
121
+ df = pd.DataFrame(data)
122
+ print(df)
123
+ df.sort_values("file", inplace=True)
124
+
125
+ for idx_, row in df.iterrows():
126
+ yield idx_, {
127
+ "image": row["file"],
128
+ "label": row["label"]
129
+ }