Kabilan108 commited on
Commit
aeef0fc
·
1 Parent(s): aa56e81

add dataset loading script

Browse files
Files changed (1) hide show
  1. spectrograms.py +62 -0
spectrograms.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import (
2
+ DatasetBuilder,
3
+ DownloadManager,
4
+ DatasetInfo,
5
+ Features,
6
+ Image,
7
+ ClassLabel,
8
+ Split,
9
+ SplitGenerator,
10
+ )
11
+ import pandas as pd
12
+
13
+ from pathlib import Path
14
+
15
+
16
+ _DESCRIPTION = """\
17
+ This dataset includes spectrograms of ~950 afrobeats songs and 1k rock songs.
18
+ The spectrograms were generated with `librosa`.
19
+ """
20
+
21
+ _NAMES = [fdr.name for fdr in Path("./data").glob("*/")]
22
+
23
+ _URLS = {
24
+ "afrobeats": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/afrobeats.zip",
25
+ "rock": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/rock.zip",
26
+ }
27
+
28
+
29
+ class SongSpectrograms(DatasetBuilder):
30
+ """ "Song-Spectrograms Images Dataset"""
31
+
32
+ def _info(self):
33
+ return DatasetInfo(
34
+ description=_DESCRIPTION,
35
+ features=Features({"image": Image(), "label": ClassLabel(names=_NAMES)}),
36
+ supervised_keys=("image", "label"),
37
+ )
38
+
39
+ def _split_generators(self, dl_manager: DownloadManager):
40
+ files = dl_manager.download_and_extract(_URLS)
41
+
42
+ return [
43
+ SplitGenerator(
44
+ name=Split.TRAIN,
45
+ gen_kwargs={
46
+ "afrobeats_dir": Path(files["afrobeats"]) / "afrobeats",
47
+ "rock_dir": Path(files["rock"]) / "rock",
48
+ },
49
+ )
50
+ ]
51
+
52
+ def _generate_examples(self, afrobeats_dir, rock_dir):
53
+ for genre_dir, label in [(afrobeats_dir, "afrobeats"), (rock_dir, "rock")]:
54
+ metadata = pd.read_csv(Path(genre_dir) / "metadata.tsv", sep="\t")
55
+
56
+ for _, row in metadata.iterrows():
57
+ path = Path(genre_dir) / row["file_name"]
58
+
59
+ yield (
60
+ f"{label}_{row['file_name'].replace('.png', '')}",
61
+ {"image": str(path), "label": label},
62
+ )