Create dataset.py
Browse files- dataset.py +47 -0
dataset.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import datasets
|
| 3 |
+
|
| 4 |
+
class BreastMNIST(datasets.GeneratorBasedBuilder):
|
| 5 |
+
VERSION = datasets.Version("1.0.0")
|
| 6 |
+
|
| 7 |
+
def _info(self):
|
| 8 |
+
return datasets.DatasetInfo(
|
| 9 |
+
features=datasets.Features({
|
| 10 |
+
"image": datasets.Array3D(shape=(28, 28, 1), dtype="uint8"), # Adjust shape if necessary
|
| 11 |
+
"label": datasets.ClassLabel(names=["benign", "malignant"]) # Adjust based on your labels
|
| 12 |
+
}),
|
| 13 |
+
description="BreastMNIST dataset containing medical imaging data",
|
| 14 |
+
supervised_keys=("image", "label"),
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def _split_generators(self, dl_manager):
|
| 18 |
+
# Provide the URL for downloading your dataset
|
| 19 |
+
downloaded_file = dl_manager.download_and_extract({
|
| 20 |
+
"dataset": "https://huggingface.co/datasets/sanaa13/breastmnist1/raw/main/breastmnist.npz"
|
| 21 |
+
})
|
| 22 |
+
return [
|
| 23 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_file["dataset"], "split": "train"}),
|
| 24 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_file["dataset"], "split": "val"}),
|
| 25 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_file["dataset"], "split": "test"}),
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
def _generate_examples(self, filepath, split):
|
| 29 |
+
# Load the .npz file
|
| 30 |
+
data = np.load(filepath)
|
| 31 |
+
|
| 32 |
+
if split == "train":
|
| 33 |
+
images = data['train_images']
|
| 34 |
+
labels = data['train_labels']
|
| 35 |
+
elif split == "val":
|
| 36 |
+
images = data['val_images']
|
| 37 |
+
labels = data['val_labels']
|
| 38 |
+
elif split == "test":
|
| 39 |
+
images = data['test_images']
|
| 40 |
+
labels = data['test_labels']
|
| 41 |
+
|
| 42 |
+
# Yield examples in index: {image, label} format
|
| 43 |
+
for idx, (image, label) in enumerate(zip(images, labels)):
|
| 44 |
+
yield idx, {
|
| 45 |
+
"image": image,
|
| 46 |
+
"label": int(label),
|
| 47 |
+
}
|