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, }