breastmnist2 / dataset.py
sanaa13's picture
Create dataset.py
29b1a23 verified
import numpy as np
import datasets
class BreastMNIST(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features({
"image": datasets.Array3D(shape=(28, 28, 1), dtype="uint8"), # Adjust shape if necessary
"label": datasets.ClassLabel(names=["benign", "malignant"]) # Adjust based on your labels
}),
description="BreastMNIST dataset containing medical imaging data",
supervised_keys=("image", "label"),
)
def _split_generators(self, dl_manager):
# Provide the URL for downloading your dataset
downloaded_file = dl_manager.download_and_extract({
"dataset": "https://huggingface.co/datasets/sanaa13/breastmnist1/raw/main/breastmnist.npz"
})
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_file["dataset"], "split": "train"}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_file["dataset"], "split": "val"}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_file["dataset"], "split": "test"}),
]
def _generate_examples(self, filepath, split):
# Load the .npz file
data = np.load(filepath)
if split == "train":
images = data['train_images']
labels = data['train_labels']
elif split == "val":
images = data['val_images']
labels = data['val_labels']
elif split == "test":
images = data['test_images']
labels = data['test_labels']
# Yield examples in index: {image, label} format
for idx, (image, label) in enumerate(zip(images, labels)):
yield idx, {
"image": image,
"label": int(label),
}