sebchw commited on
Commit
f8a36a6
1 Parent(s): ea33064

add: mean and std

Browse files
Files changed (1) hide show
  1. musdb18.py +46 -21
musdb18.py CHANGED
@@ -36,50 +36,73 @@ class Musdb18Dataset(datasets.GeneratorBasedBuilder):
36
  def _info(self):
37
  return datasets.DatasetInfo(
38
  description=_DESCRIPTION,
39
- features=datasets.Features({
40
- "name": datasets.Value("string"),
41
- "n_window": datasets.Value("int16"),
42
- **{name: datasets.Audio(sampling_rate=self.SAMPLING_RATE, mono=False)
43
- for name in self.INSTRUMENT_NAMES}
44
- })
 
 
 
 
 
 
 
 
45
  )
46
 
47
  def _split_generators(self, dl_manager):
48
  #! you must have your folder locally!
49
  archive_path = dl_manager.download_and_extract(
50
- "https://zenodo.org/record/1117372/files/musdb18.zip?download=1")
 
51
 
52
  return [
53
  datasets.SplitGenerator(
54
  name=datasets.Split.TRAIN,
55
- gen_kwargs={
56
- "audio_path": f"{archive_path}/train"}
57
  ),
58
  datasets.SplitGenerator(
59
  name=datasets.Split.TEST,
60
- gen_kwargs={
61
- "audio_path": f"{archive_path}/test"
62
- }
63
- )
64
  ]
65
 
66
  def _generate_stem_dict(self, S, song_name, end):
67
- return {name: {"path": f"{song_name}/{name}",
68
- "array": S[i, end-self.WINDOW_SIZE:end, :], "sampling_rate": self.SAMPLING_RATE}
69
- for i, name in enumerate(self.INSTRUMENT_NAMES)}
 
 
 
 
 
70
 
71
  def _generate_examples(self, audio_path):
72
  id_ = 0
73
  for stems_path in Path(audio_path).iterdir():
74
  song_name = stems_path.stem
75
  S, sr = stempeg.read_stems(
76
- str(stems_path), dtype=np.float32, multiprocess=False)
 
 
 
 
 
 
 
 
77
 
78
- for idx, end in enumerate(range(self.WINDOW_SIZE, S.shape[1], self.WINDOW_SIZE)):
 
 
79
  yield id_, {
80
  "name": song_name,
81
  "n_window": idx,
82
- **self._generate_stem_dict(S, song_name, end)
 
 
83
  }
84
 
85
  id_ += 1
@@ -87,8 +110,10 @@ class Musdb18Dataset(datasets.GeneratorBasedBuilder):
87
  # It's very rare for song to have exactly 3 minutes
88
  yield id_, {
89
  "name": song_name,
90
- "n_window": idx+1,
91
- **self._generate_stem_dict(S, song_name, end=S.shape[1])
 
 
92
  }
93
 
94
  id_ += 1
 
36
  def _info(self):
37
  return datasets.DatasetInfo(
38
  description=_DESCRIPTION,
39
+ features=datasets.Features(
40
+ {
41
+ "name": datasets.Value("string"),
42
+ "n_window": datasets.Value("int16"),
43
+ **{
44
+ name: datasets.Audio(
45
+ sampling_rate=self.SAMPLING_RATE, mono=False
46
+ )
47
+ for name in self.INSTRUMENT_NAMES
48
+ },
49
+ "mean": datasets.Value("float"),
50
+ "std": datasets.Value("float"),
51
+ }
52
+ ),
53
  )
54
 
55
  def _split_generators(self, dl_manager):
56
  #! you must have your folder locally!
57
  archive_path = dl_manager.download_and_extract(
58
+ "https://zenodo.org/record/1117372/files/musdb18.zip?download=1"
59
+ )
60
 
61
  return [
62
  datasets.SplitGenerator(
63
  name=datasets.Split.TRAIN,
64
+ gen_kwargs={"audio_path": f"{archive_path}/train"},
 
65
  ),
66
  datasets.SplitGenerator(
67
  name=datasets.Split.TEST,
68
+ gen_kwargs={"audio_path": f"{archive_path}/test"},
69
+ ),
 
 
70
  ]
71
 
72
  def _generate_stem_dict(self, S, song_name, end):
73
+ return {
74
+ name: {
75
+ "path": f"{song_name}/{name}",
76
+ "array": S[i, end - self.WINDOW_SIZE : end, :],
77
+ "sampling_rate": self.SAMPLING_RATE,
78
+ }
79
+ for i, name in enumerate(self.INSTRUMENT_NAMES)
80
+ }
81
 
82
  def _generate_examples(self, audio_path):
83
  id_ = 0
84
  for stems_path in Path(audio_path).iterdir():
85
  song_name = stems_path.stem
86
  S, sr = stempeg.read_stems(
87
+ str(stems_path), dtype=np.float32, multiprocess=False
88
+ )
89
+
90
+ mixture = S.sum(axis=0).T
91
+ assert mixture.shape[0] == 2
92
+ # from (n_instr, n_chann, n_samp) -> (n_chann, n_samp)
93
+ mixture = mixture.mean(0) # channel_wise mean -> (n_samples,)
94
+ mean = mixture.mean().item()
95
+ std = mixture.std().item()
96
 
97
+ for idx, end in enumerate(
98
+ range(self.WINDOW_SIZE, S.shape[1], self.WINDOW_SIZE)
99
+ ):
100
  yield id_, {
101
  "name": song_name,
102
  "n_window": idx,
103
+ **self._generate_stem_dict(S, song_name, end),
104
+ "mean": mean,
105
+ "std": std,
106
  }
107
 
108
  id_ += 1
 
110
  # It's very rare for song to have exactly 3 minutes
111
  yield id_, {
112
  "name": song_name,
113
+ "n_window": idx + 1,
114
+ **self._generate_stem_dict(S, song_name, end=S.shape[1]),
115
+ "mean": mean,
116
+ "std": std,
117
  }
118
 
119
  id_ += 1