Create beans.py
Browse files
beans.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datasets
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import zipfile
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Beans(datasets.GeneratorBasedBuilder):
|
| 7 |
+
"""Bean disease dataset for classification of three classes: Angular Leaf Spot, Bean Rust, and Healthy leaves."""
|
| 8 |
+
|
| 9 |
+
VERSION = datasets.Version("1.0.0")
|
| 10 |
+
|
| 11 |
+
def _info(self):
|
| 12 |
+
return datasets.DatasetInfo(
|
| 13 |
+
description="""The IBeans dataset contains leaf images representing three classes:
|
| 14 |
+
1) Healthy leaves, 2) Angular Leaf Spot, and 3) Bean Rust. Images are collected in Uganda for disease
|
| 15 |
+
classification in the field.""",
|
| 16 |
+
features=datasets.Features(
|
| 17 |
+
{
|
| 18 |
+
"image": datasets.Image(),
|
| 19 |
+
"label": datasets.ClassLabel(
|
| 20 |
+
names=["healthy", "angular_leaf_spot", "bean_rust"]
|
| 21 |
+
),
|
| 22 |
+
}
|
| 23 |
+
),
|
| 24 |
+
supervised_keys=("image", "label"),
|
| 25 |
+
license="MIT License",
|
| 26 |
+
citation="""@misc{makerere2020beans,
|
| 27 |
+
author = "{Makerere AI Lab}",
|
| 28 |
+
title = "{Bean Disease Dataset}",
|
| 29 |
+
year = "2020",
|
| 30 |
+
month = "January",
|
| 31 |
+
url = "https://github.com/AI-Lab-Makerere/ibean/"}"""
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
def _split_generators(self, dl_manager):
|
| 35 |
+
urls = {
|
| 36 |
+
"train": "https://storage.googleapis.com/ibeans/train.zip",
|
| 37 |
+
"test": "https://storage.googleapis.com/ibeans/test.zip",
|
| 38 |
+
"validation": "https://storage.googleapis.com/ibeans/validation.zip",
|
| 39 |
+
}
|
| 40 |
+
downloaded_files = dl_manager.download(urls)
|
| 41 |
+
|
| 42 |
+
return [
|
| 43 |
+
datasets.SplitGenerator(
|
| 44 |
+
name=datasets.Split.TRAIN,
|
| 45 |
+
gen_kwargs={"zip_path": downloaded_files["train"]},
|
| 46 |
+
),
|
| 47 |
+
datasets.SplitGenerator(
|
| 48 |
+
name=datasets.Split.TEST,
|
| 49 |
+
gen_kwargs={"zip_path": downloaded_files["test"]},
|
| 50 |
+
),
|
| 51 |
+
datasets.SplitGenerator(
|
| 52 |
+
name=datasets.Split.VALIDATION,
|
| 53 |
+
gen_kwargs={"zip_path": downloaded_files["validation"]},
|
| 54 |
+
),
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
def _generate_examples(self, zip_path):
|
| 58 |
+
with zipfile.ZipFile(zip_path, "r") as archive:
|
| 59 |
+
for file_name in archive.namelist():
|
| 60 |
+
if file_name.endswith(".jpg"):
|
| 61 |
+
with archive.open(file_name) as file:
|
| 62 |
+
image_data = Image.open(file)
|
| 63 |
+
label_name = file_name.split("/")[1]
|
| 64 |
+
label = self.info.features["label"].str2int(label_name)
|
| 65 |
+
yield file_name, {"image": image_data, "label": label}
|