stanford-dogs / stanford-dogs.py
Alanox's picture
[FIX]Class label
6dd52df
raw
history blame
2.14 kB
"""stanford-dogs: The Stanford Dogs Dataset."""
from ast import literal_eval
from pathlib import Path
import datasets
import pandas as pd
logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """
The Stanford Dogs dataset contains images of 120 breeds of dogs from around the world. This dataset has been built using images and annotation from ImageNet for the task of fine-grained image categorization.
"""
_URL = "https://huggingface.co/datasets/Alanox/stanford-dogs"
_IMAGES = _URL + "/resolve/main/images.tar.gz"
_METADATA = _URL + "/resolve/main/metadata.csv"
class StanfordDogs(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"name": datasets.Value("string"),
"annotations": datasets.Array2D(shape=(None, 4), dtype="int32"),
"target": datasets.Value("string"),
"image": datasets.Image(),
}
),
homepage="https://huggingface.co/datasets/Alanox/stanford-dogs",
)
def _split_generators(self, dl_manager):
images_archive = dl_manager.download(_IMAGES)
images = dl_manager.iter_archive(images_archive)
metadata_csv = dl_manager.download(_METADATA)
metadata = pd.read_csv(metadata_csv, on_bad_lines="skip").set_index("name")
metadata["annotations"] = metadata["annotations"].apply(literal_eval)
return [
datasets.SplitGenerator(
name="full",
gen_kwargs={"images": images, "metadata": metadata},
),
]
def _generate_examples(self, images, metadata: pd.DataFrame):
for i, (filepath, image) in enumerate(images):
filename = Path(filepath).name
item = metadata.loc[filename]
yield i, {
"name": filename,
"image": {"path": filepath, "bytes": image.read()},
"annotations": item["annotations"],
"target": item["target"],
}