File size: 2,141 Bytes
ce90b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dd52df
ce90b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""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"],
            }