| 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.""" |
|
|
| |
| CLASSES = ['glioma', 'meningioma', 'notumor', 'pituitary'] |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| |
| features=datasets.Features({ |
| "image": datasets.Image(), |
| "label": datasets.ClassLabel(names=self.CLASSES), |
| }), |
| homepage=_HOMEPAGE, |
| license=_LICENSE, |
| citation=_CITATION, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| |
| |
| data_dir = os.path.join(dl_manager.manual_dir or ".", "classification") |
|
|
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| |
| gen_kwargs={"path": os.path.join(data_dir, "Training")}, |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.TEST, |
| |
| 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.""" |
| |
| for label in self.CLASSES: |
| class_path = os.path.join(path, label) |
| |
| if os.path.isdir(class_path): |
| |
| for filename in os.listdir(class_path): |
| image_path = os.path.join(class_path, filename) |
| |
| if os.path.isfile(image_path): |
| key = f"{label}_{filename}" |
| yield key, { |
| "image": image_path, |
| "label": label, |
| } |