File size: 2,362 Bytes
f74dbd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24449b1
 
f74dbd6
 
 
 
 
24449b1
 
 
cfc7931
 
24449b1
 
 
 
 
 
 
0e15771
f74dbd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import datasets
from datasets import SplitGenerator, Split, ImageClassification

_CITATION = """\
@TECHREPORT{gpiosenka/100-bird-species,
    author = {gpiosenka},
    title = {BIRDS 525 SPECIES- IMAGE CLASSIFICATION},
    institution = {},
    year = {2023}
}
"""

_DESCRIPTION = """\
A dataset of bird species downloaded from kaggle. """

_HOMEPAGE = "https://www.kaggle.com/datasets/gpiosenka/100-bird-species/"

_DATA_DIR = 'data/'

_VERSION = "0.1.0"

def _CLASSES() -> list[str]:
    # reads from bird_labels.txt, line by line
    with open("birds_labels.txt") as f:
        return f.read().splitlines()

class BirdSpeciesDataset(datasets.GeneratorBasedBuilder):
    """DatasetBuilder for bird_species_dataset dataset."""

    DEFAULT_CONFIG_NAME = "bird_species_dataset"

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="bird_species_dataset",
            version=datasets.Version(_VERSION),
            description=_DESCRIPTION,
        )
    ]

    def _info(self):
        _NAMES = _CLASSES()

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.features.ClassLabel(names=_NAMES),
                }
            ),
            supervised_keys=("image", "label"),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            task_templates=ImageClassification(image_column="image", label_column="label"),
        )
    
    def _split_generators(self, dl_manager):
        data_dir = _DATA_DIR
        return [
            SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": os.path.join(data_dir, "train")}),
            SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": os.path.join(data_dir, "valid")}),
            SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": os.path.join(data_dir, "test")}),
        ]

    def _generate_examples(self, filepath):
        """Yields examples."""
        idx = 0
        for label in os.listdir(filepath):
            for f in os.listdir(os.path.join(filepath, label)):
                record = {
                    "image": os.path.join(filepath, label, f),
                    "label": label,
                }
                yield idx, record
                idx += 1