food_vision_199_classes / food_vision_199_classes.py
mrdbourke's picture
add dataset loading scripts
2a92119
raw
history blame
2.49 kB
"""
Loading script for the Food Vision 199 classes dataset.
See the template: https://github.com/huggingface/datasets/blob/main/templates/new_dataset_script.py
See the example for Food101: https://huggingface.co/datasets/food101/blob/main/food101.py
See another example: https://huggingface.co/datasets/davanstrien/encyclopedia_britannica/blob/main/encyclopedia_britannica.py
"""
import datasets
import os
import pandas as pd
from datasets.tasks import ImageClassification
_HOMEPAGE = "https://www.nutrify.app"
_LICENSE = "TODO"
_CITATION = "TODO"
_DESCRIPTION = "Images of 199 food classes from the Nutrify app."
# Read lines of class_names.txt
with open("class_names.txt", "r") as f:
_NAMES = f.read().splitlines()
class Food199(datasets.GeneratorBasedBuilder):
"""Food199 Images dataset"""
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"image": datasets.Image(),
"label": datasets.ClassLabel(names=_NAMES)
}
),
supervised_keys=("image", "label"),
homepage=_HOMEPAGE,
citation=_CITATION,
license=_LICENSE,
task_templates=[ImageClassification(image_column="image", label_column="label")],
)
def _split_generators(self, dl_manager):
"""
This function returns the logic to split the dataset into different splits as well as labels.
"""
csv = dl_manager.download("annotations.csv")
df = pd.read_csv(csv)
df_train_annotations = df[df["split"] == "train"].to_dict(orient="records")
df_test_annotations = df[df["split"] == "test"].to_dict(orient="records")
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN,
gen_kwargs={
"annotations": df_train_annotations,
}),
datasets.SplitGenerator(name=datasets.Split.TEST,
gen_kwargs={
"annotations": df_test_annotations,
})]
def _generate_examples(self, annotations):
"""
This function takes in the kwargs from the _split_generators method and can then yield information from them.
"""
for id_, row in enumerate(annotations):
row["image"] = row.pop("filename")
row["label"] = row.pop("label")
yield id_, row