DurhamTrees / plantsdataset.py
Ziyuan111's picture
Upload plantsdataset.py
eb4d631 verified
raw
history blame
No virus
2.62 kB
# -*- coding: utf-8 -*-
"""PlantsDataset
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/13QeVLQP2QGaxkGcaoMi25geoBxHQ_3rQ
"""
from datasets import Dataset, DatasetInfo, Features, Value, Split, DownloadManager, GeneratorBasedBuilder
from datasets import SplitGenerator
# URL for the datasethttps://drive.google.com/file/d/1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH/view?usp=share_link
import os
from datasets import Dataset, DatasetInfo, Features, Value, Split, GeneratorBasedBuilder
# Google Drive ID for your ZIP file
_DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
_URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"
# Custom Dataset Class
class PlantsDataset(GeneratorBasedBuilder):
VERSION = "1.0.0"
def _info(self):
features = Features({
"image": Value("string"), # Replace with the actual datatype for your image data
"label": Value("int64"), # Replace with the actual datatype for your label data
})
return DatasetInfo(
description="Your dataset description",
features=features,
supervised_keys=("image", "label"),
homepage="Your dataset homepage",
citation="Citation for your dataset",
)
def _split_generators(self, dl_manager):
downloaded_file = dl_manager.download_and_extract(_URL)
return [
SplitGenerator(
name=Split.TRAIN,
gen_kwargs={
"data_folder": os.path.join(downloaded_file, "train"),
},
),
SplitGenerator(
name=Split.TEST,
gen_kwargs={
"data_folder": os.path.join(downloaded_file, "test"),
},
),
]
def _generate_examples(self, data_folder):
for label, subfolder in enumerate(["gigantic", "small"]):
subfolder_path = os.path.join(data_folder, subfolder)
for root, _, files in os.walk(subfolder_path):
for file_name in files:
file_path = os.path.join(root, file_name)
# Check if file_path is a file, not a directory
if os.path.isfile(file_path):
# Yield the example
yield {
"image": file_path,
"label": label,
}
# Create an instance of the PlantsDataset class
plants_dataset = PlantsDataset()
# Build and upload the dataset
plants_dataset.download_and_prepare()