huseyincavus commited on
Commit
e15a378
·
verified ·
1 Parent(s): 7db7a1c

Create data_loader.py

Browse files
Files changed (1) hide show
  1. data_loader.py +66 -0
data_loader.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import os
3
+
4
+ class BrainCancerMRIConfig(datasets.BuilderConfig):
5
+ """BuilderConfig for Brain Cancer MRI Classification."""
6
+ def __init__(self, **kwargs):
7
+ """BuilderConfig for the dataset.
8
+ Args:
9
+ **kwargs: keyword arguments forwarded to super.
10
+ """
11
+ super(BrainCancerMRIConfig, self).__init__(**kwargs)
12
+
13
+ class BrainCancerMRIClassification(datasets.GeneratorBasedBuilder):
14
+ """Brain Cancer MRI Classification dataset."""
15
+
16
+ # Define the classes for the 'label' feature
17
+ CLASSES = ['glioma', 'meningioma', 'notumor', 'pituitary']
18
+
19
+ def _info(self):
20
+ return datasets.DatasetInfo(
21
+ description=_DESCRIPTION,
22
+ # Define the features of the dataset, including the image and its corresponding label
23
+ features=datasets.Features({
24
+ "image": datasets.Image(),
25
+ "label": datasets.ClassLabel(names=self.CLASSES),
26
+ }),
27
+ homepage=_HOMEPAGE,
28
+ license=_LICENSE,
29
+ citation=_CITATION,
30
+ )
31
+
32
+ def _split_generators(self, dl_manager):
33
+ # The data is already in the repository, so we just need the path
34
+ # `dl_manager.manual_dir` will point to the root of the dataset repository
35
+ data_dir = os.path.join(dl_manager.manual_dir or ".", "classification")
36
+
37
+ return [
38
+ datasets.SplitGenerator(
39
+ name=datasets.Split.TRAIN,
40
+ # Provide the path to the training data directory
41
+ gen_kwargs={"path": os.path.join(data_dir, "Training")},
42
+ ),
43
+ datasets.SplitGenerator(
44
+ name=datasets.Split.TEST,
45
+ # Provide the path to the testing data directory
46
+ gen_kwargs={"path": os.path.join(data_dir, "Testing")},
47
+ ),
48
+ ]
49
+
50
+ def _generate_examples(self, path):
51
+ """This function will yield examples: a unique key and a dictionary of features."""
52
+ # Iterate over each class directory (e.g., 'glioma', 'meningioma', etc.)
53
+ for label in self.CLASSES:
54
+ class_path = os.path.join(path, label)
55
+ # Check if the class directory exists to avoid errors
56
+ if os.path.isdir(class_path):
57
+ # Iterate over each image file in the class directory
58
+ for filename in os.listdir(class_path):
59
+ image_path = os.path.join(class_path, filename)
60
+ # Check if it is a file to avoid including subdirectories
61
+ if os.path.isfile(image_path):
62
+ key = f"{label}_{filename}"
63
+ yield key, {
64
+ "image": image_path,
65
+ "label": label,
66
+ }