import pandas as pd import datasets import numpy as np from PIL import Image class InvasivePlantsConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(InvasivePlantsConfig, self).__init__(**kwargs) class InvasivePlantsDataset(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ InvasivePlantsConfig( name="full", version=datasets.Version("1.0.0"), description="Full dataset, combining both systematically and opportunistically sampled leaves" ), InvasivePlantsConfig( name="systematic", version=datasets.Version("1.0.0"), description="Subset containing only systematically sampled leaves" ), InvasivePlantsConfig( name="opportunistic", version=datasets.Version("1.0.0"), description="Subset containing only opportunistically sampled leaves" ) ] DEFAULT_CONFIG_NAME = "full" def _info(self): #expert_other_insect,expert_mechanical_damage,expert_confidence,expert_other_remarks,expert_notes,sampling_type return datasets.DatasetInfo( description="Invasive Plants Datasets", features=datasets.Features( { "session": datasets.Value("string"), "filename": datasets.Value("string"), "sampling_type" : datasets.Value("string"), "image": datasets.Image(), "site": datasets.Value("int32"), "day": datasets.Value("int32"), "plant": datasets.Value("string"), "level": datasets.Value("string"), "view": datasets.Value("string"), "healthy": datasets.Value("string"), "rust" : datasets.Value("string"), "leaf_miner" : datasets.Value("string"), "other_insect" : datasets.Value("string"), "mechanical_damage" : datasets.Value("string"), "other_remarks" : datasets.Value("string"), "expert_healthy" : datasets.Value("string"), "expert_rust" : datasets.Value("string"), "expert_leaf_miner" : datasets.Value("string"), "expert_other_insect" : datasets.Value("string"), "expert_mechanical_damage" : datasets.Value("string"), "expert_confidence" : datasets.Value("string"), "expert_other_remarks" : datasets.Value("string"), "expert_notes" : datasets.Value("string") } ), supervised_keys=None, homepage="https://huggingface.co/datasets/imageomics/invasive_plants_hawaii", license="MIT", ) def _split_generators(self, dl_manager): data_dir = "images" if self.config.name == "full": full_csv = "metadata/full_dataset.csv" dorsal_csv = "metadata/full_dataset_dorsal.csv" ventral_csv = "metadata/full_dataset_ventral.csv" elif self.config.name == "systematic": full_csv = "metadata/full_systematic_dataset.csv" dorsal_csv = "metadata/systematic_dataset_dorsal.csv" ventral_csv = "metadata/systematic_dataset_ventral.csv" elif self.config.name == "opportunistic": full_csv = "metadata/full_opportunistic_dataset.csv" dorsal_csv = "metadata/opportunistic_dataset_dorsal.csv" ventral_csv = "metadata/opportunistic_dataset_ventral.csv" else: raise ValueError(f"Unknown config: {self.config.name}") return [ datasets.SplitGenerator( name="dorsal", gen_kwargs={ "metadata_path": dorsal_csv, }, ), datasets.SplitGenerator( name="ventral", gen_kwargs={ "metadata_path": ventral_csv, }, ), datasets.SplitGenerator( name="both", gen_kwargs={ "metadata_path": full_csv, }, ), ] def _generate_examples(self, metadata_path): img_dir = "images" df_metadata = pd.read_csv(metadata_path) for idx in range(len(df_metadata)): row = df_metadata.iloc[idx] img_path = f"{img_dir}/{row["session"]}/{row["filename"]}" img_pillow = Image.open(img_path) yield idx, { "session": row["session"], "filename": row["filename"], "sampling_type" : row["sampling_type"], "image": img_pillow, "site": row["site"], "day": row["day"], "plant": row["plant"], "level": row["level"], "view": row["view"], "healthy": row["healthy"], "rust" : row["rust"], "leaf_miner" : row["leaf_miner"], "other_insect" : row["other_insect"], "mechanical_damage" : row["mechanical_damage"], "other_remarks" : row["other_remarks"], "expert_healthy" : row["expert_healthy"], "expert_rust" : row["expert_rust"], "expert_leaf_miner" : row["expert_leaf_miner"], "expert_other_insect" : row["expert_other_insect"], "expert_mechanical_damage" : row["expert_mechanical_damage"], "expert_confidence" : row["expert_confidence"], "expert_other_remarks" : row["expert_other_remarks"], "expert_notes" : row["expert_notes"] }