Update breastmnist4.py
Browse files- breastmnist4.py +19 -61
breastmnist4.py
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
|
|
| 1 |
import datasets
|
| 2 |
-
import numpy as np
|
| 3 |
-
import numpy as np
|
| 4 |
-
import requests
|
| 5 |
-
import datasets
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
class BreastMNIST(datasets.GeneratorBasedBuilder):
|
| 9 |
VERSION = datasets.Version("1.0.0")
|
| 10 |
-
|
| 11 |
def _info(self):
|
| 12 |
return datasets.DatasetInfo(
|
| 13 |
features=datasets.Features({
|
| 14 |
-
"image": datasets.Array3D(shape=(28, 28, 1), dtype="
|
| 15 |
"label": datasets.ClassLabel(names=["benign", "malignant"]) # Adjust based on your labels
|
| 16 |
}),
|
| 17 |
description="BreastMNIST dataset containing medical imaging data",
|
|
@@ -19,60 +15,22 @@ class BreastMNIST(datasets.GeneratorBasedBuilder):
|
|
| 19 |
)
|
| 20 |
|
| 21 |
def _split_generators(self, dl_manager):
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
return [
|
| 24 |
-
datasets.SplitGenerator(
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
),
|
| 28 |
-
datasets.SplitGenerator(
|
| 29 |
-
name=datasets.Split.TEST,
|
| 30 |
-
gen_kwargs={"split": "test"},
|
| 31 |
-
),
|
| 32 |
-
datasets.SplitGenerator(
|
| 33 |
-
name=datasets.Split.VALIDATION,
|
| 34 |
-
gen_kwargs={"split": "validation"},
|
| 35 |
-
),
|
| 36 |
]
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def _generate_examples(self, filepath , split):
|
| 48 |
-
# Define the URL for the dataset
|
| 49 |
-
url =filepath
|
| 50 |
-
|
| 51 |
-
# Download the dataset
|
| 52 |
-
self._download_data(url)
|
| 53 |
-
|
| 54 |
-
# Load the dataset from the local file
|
| 55 |
-
data = np.load("breastmnist4.npz", allow_pickle=True)
|
| 56 |
-
|
| 57 |
-
# Yield examples based on the split
|
| 58 |
-
if split == "train":
|
| 59 |
-
for i in range(len(data['train_images'])):
|
| 60 |
-
yield i, {
|
| 61 |
-
"image": data['train_images'][i],
|
| 62 |
-
"label": data['train_labels'][i],
|
| 63 |
-
}
|
| 64 |
-
elif split == "test":
|
| 65 |
-
for i in range(len(data['test_images'])):
|
| 66 |
-
yield i, {
|
| 67 |
-
"image": data['test_images'][i],
|
| 68 |
-
"label": data['test_labels'][i],
|
| 69 |
-
}
|
| 70 |
-
elif split == "validation":
|
| 71 |
-
for i in range(len(data['val_images'])):
|
| 72 |
-
yield i, {
|
| 73 |
-
"image": data['val_images'][i],
|
| 74 |
-
"label": data['val_labels'][i],
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
# Optionally, remove the downloaded file after loading
|
| 78 |
-
os.remove("breastmnist4.npz")
|
|
|
|
| 1 |
+
import pandas as pd
|
| 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="float32"), # Use Array3D for image shapes
|
| 11 |
"label": datasets.ClassLabel(names=["benign", "malignant"]) # Adjust based on your labels
|
| 12 |
}),
|
| 13 |
description="BreastMNIST dataset containing medical imaging data",
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
def _split_generators(self, dl_manager):
|
| 18 |
+
train_data = "breastmnist_train.parquet"
|
| 19 |
+
test_data = "breastmnist_test.parquet"
|
| 20 |
+
val_data = "breastmnist_val.parquet"
|
| 21 |
+
|
| 22 |
return [
|
| 23 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_data}),
|
| 24 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_data}),
|
| 25 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": val_data}),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
]
|
| 27 |
|
| 28 |
+
def _generate_examples(self, filepath):
|
| 29 |
+
# Load the Parquet file
|
| 30 |
+
df = pd.read_parquet(filepath)
|
| 31 |
+
|
| 32 |
+
for idx, row in df.iterrows():
|
| 33 |
+
yield idx, {
|
| 34 |
+
"image": row["image"], # Ensure this matches the expected shape
|
| 35 |
+
"label": row["label"],
|
| 36 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|