File size: 2,773 Bytes
e15a378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datasets
import os

class BrainCancerMRIConfig(datasets.BuilderConfig):
    """BuilderConfig for Brain Cancer MRI Classification."""
    def __init__(self, **kwargs):
        """BuilderConfig for the dataset.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(BrainCancerMRIConfig, self).__init__(**kwargs)

class BrainCancerMRIClassification(datasets.GeneratorBasedBuilder):
    """Brain Cancer MRI Classification dataset."""

    # Define the classes for the 'label' feature
    CLASSES = ['glioma', 'meningioma', 'notumor', 'pituitary']

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            # Define the features of the dataset, including the image and its corresponding label
            features=datasets.Features({
                "image": datasets.Image(),
                "label": datasets.ClassLabel(names=self.CLASSES),
            }),
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # The data is already in the repository, so we just need the path
        # `dl_manager.manual_dir` will point to the root of the dataset repository
        data_dir = os.path.join(dl_manager.manual_dir or ".", "classification")

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # Provide the path to the training data directory
                gen_kwargs={"path": os.path.join(data_dir, "Training")},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # Provide the path to the testing data directory
                gen_kwargs={"path": os.path.join(data_dir, "Testing")},
            ),
        ]

    def _generate_examples(self, path):
        """This function will yield examples: a unique key and a dictionary of features."""
        # Iterate over each class directory (e.g., 'glioma', 'meningioma', etc.)
        for label in self.CLASSES:
            class_path = os.path.join(path, label)
            # Check if the class directory exists to avoid errors
            if os.path.isdir(class_path):
                # Iterate over each image file in the class directory
                for filename in os.listdir(class_path):
                    image_path = os.path.join(class_path, filename)
                    # Check if it is a file to avoid including subdirectories
                    if os.path.isfile(image_path):
                        key = f"{label}_{filename}"
                        yield key, {
                            "image": image_path,
                            "label": label,
                        }