File size: 1,909 Bytes
aeef0fc
 
 
 
 
 
 
 
 
 
 
18a98fe
aeef0fc
 
 
 
 
 
 
 
 
68a9cd1
aeef0fc
 
 
 
 
 
 
18a98fe
 
 
aeef0fc
 
 
 
18a98fe
aeef0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import (
    DatasetBuilder,
    DownloadManager,
    DatasetInfo,
    Features,
    Image,
    ClassLabel,
    Split,
    SplitGenerator,
)
import pandas as pd
import datasets

from pathlib import Path


_DESCRIPTION = """\
This dataset includes spectrograms of ~950 afrobeats songs and 1k rock songs.
The spectrograms were generated with `librosa`.
"""

_NAMES = ["afrobeats", "rock"]

_URLS = {
    "afrobeats": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/afrobeats.zip",
    "rock": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/rock.zip",
}


# class Spectrograms(DatasetBuilder):
class Spectrograms(datasets.GeneratorBasedBuilder):
    """Spectrograms Images Dataset"""

    def _info(self):
        return DatasetInfo(
            description=_DESCRIPTION,
            features=Features({"image": datasets.Value("string"), "label": ClassLabel(names=_NAMES)}),
            supervised_keys=("image", "label"),
        )

    def _split_generators(self, dl_manager: DownloadManager):
        files = dl_manager.download_and_extract(_URLS)

        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={
                    "afrobeats_dir": Path(files["afrobeats"]) / "afrobeats",
                    "rock_dir": Path(files["rock"]) / "rock",
                },
            )
        ]

    def _generate_examples(self, afrobeats_dir, rock_dir):
        for genre_dir, label in [(afrobeats_dir, "afrobeats"), (rock_dir, "rock")]:
            metadata = pd.read_csv(Path(genre_dir) / "metadata.tsv", sep="\t")

            for _, row in metadata.iterrows():
                path = Path(genre_dir) / row["file_name"]

                yield (
                    f"{label}_{row['file_name'].replace('.png', '')}",
                    {"image": str(path), "label": label},
                )