blanchon commited on
Commit
4b3147f
1 Parent(s): 0ae5211

to python3.9

Browse files
Files changed (4) hide show
  1. best_model_gradio.ipynb +2 -2
  2. dataloading.py +7 -7
  3. gradio_utils.py +3 -5
  4. preprocessing.py +22 -22
best_model_gradio.ipynb CHANGED
@@ -59,14 +59,14 @@
59
  "torch.manual_seed(SEED)\n",
60
  "\n",
61
  "# Dataloading params\n",
62
- "PATHS: list[str] = [\n",
63
  " \"../data/\",\n",
64
  " \"../new_data/JulienNestor\",\n",
65
  " \"../new_data/classroom_data\",\n",
66
  " \"../new_data/class\",\n",
67
  " \"../new_data/JulienRaph\",\n",
68
  "]\n",
69
- "REMOVE_LABEL: list[str] = [\n",
70
  " \"penduleinverse\", \"pendule\", \n",
71
  " \"decollage\", \"atterrissage\",\n",
72
  " \"plushaut\", \"plusbas\",\n",
59
  "torch.manual_seed(SEED)\n",
60
  "\n",
61
  "# Dataloading params\n",
62
+ "PATHS: list = [\n",
63
  " \"../data/\",\n",
64
  " \"../new_data/JulienNestor\",\n",
65
  " \"../new_data/classroom_data\",\n",
66
  " \"../new_data/class\",\n",
67
  " \"../new_data/JulienRaph\",\n",
68
  "]\n",
69
+ "REMOVE_LABEL: list = [\n",
70
  " \"penduleinverse\", \"pendule\", \n",
71
  " \"decollage\", \"atterrissage\",\n",
72
  " \"plushaut\", \"plusbas\",\n",
dataloading.py CHANGED
@@ -7,11 +7,11 @@ from pathlib import Path
7
  from typing import Callable, Literal, Optional
8
 
9
  def load_dataset(
10
- paths: list[str],
11
- remove_label: list[str] = [""],
12
  sr: int = 22050,
13
- method : Literal["fix_length", "time_stretch"] = "fix_length",
14
- max_time: float = 4.0) -> tuple[pd.DataFrame, Callable[[np.ndarray, int], np.ndarray]]:
15
  """Folder dataset in memory loader (return fully loaded pandas dataframe).
16
  - For sklearn, load the whole dataset if possible otherwise use `proportion` to only load a part of the dataset.
17
  - For pytorch, load the whole dataset if possible otherwise use `proportion` to only load a part of the dataset.
@@ -31,7 +31,7 @@ def load_dataset(
31
 
32
  Args:
33
  paths (list[Path]): list of dataset directory to parse.
34
- remove_label (list[str], optional): list of label to remove. Defaults to None.. Defaults to [""].
35
  shuffle (bool, optional): True to suffle the dataframe. Defaults to True.
36
  proportion (float, optional): Proportion of file to load. Defaults to 1.0.
37
  sr (int, optional): Sample Rate to resample audio file. Defaults to 22050.
@@ -41,7 +41,7 @@ def load_dataset(
41
  Returns:
42
  df (pd.DataFrame): A pd.DataFrame with such define column:
43
  - absolute_path (str): file-system absolute path of the .wav file.
44
- - labels (list[str]): list of labels defining the sound file (ie, subdirectories and post _ filename).
45
  - ground_truth (str): ground_truth label meaning the last one after _ in the sound filename.
46
  - y_original_signal (np.ndarray): sound signal normalize as `float64` and resample with the given sr by `librosa.load`
47
  - y_original_duration (float): y_original_signal signal duration.
@@ -79,7 +79,7 @@ def load_dataset(
79
  def uniformize(
80
  audio: np.ndarray,
81
  sr: int,
82
- method: Literal["fix_length", "time_stretch"] = "fix_length",
83
  max_time: float = 4.0
84
  ):
85
  if method == "fix_length":
7
  from typing import Callable, Literal, Optional
8
 
9
  def load_dataset(
10
+ paths: list,
11
+ remove_label: list = [""],
12
  sr: int = 22050,
13
+ method = "fix_length",
14
+ max_time: float = 4.0):
15
  """Folder dataset in memory loader (return fully loaded pandas dataframe).
16
  - For sklearn, load the whole dataset if possible otherwise use `proportion` to only load a part of the dataset.
17
  - For pytorch, load the whole dataset if possible otherwise use `proportion` to only load a part of the dataset.
31
 
32
  Args:
33
  paths (list[Path]): list of dataset directory to parse.
34
+ remove_label (list, optional): list of label to remove. Defaults to None.. Defaults to [""].
35
  shuffle (bool, optional): True to suffle the dataframe. Defaults to True.
36
  proportion (float, optional): Proportion of file to load. Defaults to 1.0.
37
  sr (int, optional): Sample Rate to resample audio file. Defaults to 22050.
41
  Returns:
42
  df (pd.DataFrame): A pd.DataFrame with such define column:
43
  - absolute_path (str): file-system absolute path of the .wav file.
44
+ - labels (list): list of labels defining the sound file (ie, subdirectories and post _ filename).
45
  - ground_truth (str): ground_truth label meaning the last one after _ in the sound filename.
46
  - y_original_signal (np.ndarray): sound signal normalize as `float64` and resample with the given sr by `librosa.load`
47
  - y_original_duration (float): y_original_signal signal duration.
79
  def uniformize(
80
  audio: np.ndarray,
81
  sr: int,
82
+ method = "fix_length",
83
  max_time: float = 4.0
84
  ):
85
  if method == "fix_length":
gradio_utils.py CHANGED
@@ -3,17 +3,15 @@ from typing import Callable, Optional
3
 
4
  import numpy as np
5
 
6
-
7
-
8
  import librosa
9
 
10
  import gradio as gr
11
 
12
- def predict_gradio(data: tuple[int, np.ndarray],
13
- uniform_lambda: Callable[[np.ndarray, int], np.ndarray],
14
  sklearn_model,
15
  label_transform,
16
- target_sr: int = 22_050) -> Optional[dict]:
17
  if data is None:
18
  return
19
 
3
 
4
  import numpy as np
5
 
 
 
6
  import librosa
7
 
8
  import gradio as gr
9
 
10
+ def predict_gradio(data,
11
+ uniform_lambda,
12
  sklearn_model,
13
  label_transform,
14
+ target_sr: int = 22_050):
15
  if data is None:
16
  return
17
 
preprocessing.py CHANGED
@@ -6,18 +6,18 @@ from sklearn.base import BaseEstimator, TransformerMixin
6
  from typing import Callable, Optional
7
 
8
  class ReductionTransformer(BaseEstimator, TransformerMixin):
9
- def __init__(self, windows_number: int = 300, statistique: Callable[[np.ndarray], np.ndarray] = np.mean):
10
  self.windows_number = windows_number
11
  self.statistique = statistique
12
 
13
- def fit(self, X: np.ndarray, y: Optional[np.ndarray] = None):
14
  return self
15
 
16
- def fit_transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
17
  self.fit(X, y)
18
  return self.transform(X, y)
19
 
20
- def transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
21
  X_ = X.copy()
22
  *c_, size_ = X_.shape
23
  windows_size_ = size_//self.windows_number
@@ -34,14 +34,14 @@ class MeanTransformer(BaseEstimator, TransformerMixin):
34
  self.windows_number = windows_number
35
  self.windows_size = 0
36
 
37
- def fit(self, X: np.ndarray, y: Optional[np.ndarray] = None):
38
  return self
39
 
40
- def fit_transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
41
  self.fit(X, y)
42
  return self.transform(X, y)
43
 
44
- def transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
45
  X_ = X.copy()
46
  *c_, size_ = X_.shape
47
  windows_size_ = size_//self.windows_number
@@ -62,14 +62,14 @@ class StdTransformer(BaseEstimator, TransformerMixin):
62
  def __init__(self, windows_number: int = 300):
63
  self.windows_number = windows_number
64
 
65
- def fit(self, X: np.ndarray, y: Optional[np.ndarray] = None):
66
  return self
67
 
68
- def fit_transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
69
  self.fit(X, y)
70
  return self.transform(X, y)
71
 
72
- def transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
73
  X_ = X.copy()
74
  *c_, size_ = X_.shape
75
  windows_size_ = size_//self.windows_number
@@ -91,14 +91,14 @@ class MfccTransformer(BaseEstimator, TransformerMixin):
91
  c_, *_ = X_.shape
92
  return X_.reshape(c_, -1, self.N_MFCC)
93
 
94
- def fit(self, X: np.ndarray, y: Optional[np.ndarray] = None):
95
  return self
96
 
97
- def fit_transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
98
  self.fit(X, y)
99
  return self.transform(X, y)
100
 
101
- def transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
102
  X_ = X.copy()
103
  c_, *_ = X_.shape
104
  mfcc = librosa.feature.mfcc(y=X_,
@@ -130,14 +130,14 @@ class MelTransformer(BaseEstimator, TransformerMixin):
130
  c_, *_ = X_.shape
131
  return X_.reshape(c_, -1, self.N_MEL)
132
 
133
- def fit(self, X: np.ndarray, y: Optional[np.ndarray] = None):
134
  return self
135
 
136
- def fit_transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
137
  self.fit(X, y)
138
  return self.transform(X, y)
139
 
140
- def transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
141
  X_ = X.copy()
142
  c_, *_ = X_.shape
143
  mel = librosa.feature.melspectrogram(y=X,
@@ -162,14 +162,14 @@ class TorchTransform(BaseEstimator, TransformerMixin):
162
  def __init__(self):
163
  pass
164
 
165
- def fit(self, X: np.ndarray, y: Optional[np.ndarray] = None):
166
  return self
167
 
168
- def fit_transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> torch.Tensor:
169
  self.fit(X, y)
170
  return self.transform(X, y)
171
 
172
- def transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> torch.Tensor:
173
  return torch.tensor(X).unsqueeze(dim=1)
174
 
175
  def inverse_transform(self, X: torch.Tensor) -> np.ndarray:
@@ -179,14 +179,14 @@ class ShuffleTransformer(BaseEstimator, TransformerMixin):
179
  def __init__(self, p: float = 0.005):
180
  self.p = p
181
 
182
- def fit(self, X: np.ndarray, y: Optional[np.ndarray] = None):
183
  return self
184
 
185
- def fit_transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
186
  self.fit(X, y)
187
  return self.transform(X, y)
188
 
189
- def transform(self, X: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
190
  will_swap = np.random.choice(X.shape[0], int(self.p*X.shape[0]))
191
  will_swap_with = np.random.choice(X.shape[0], int(self.p*X.shape[0]))
192
  if hasattr(X, "copy"):
6
  from typing import Callable, Optional
7
 
8
  class ReductionTransformer(BaseEstimator, TransformerMixin):
9
+ def __init__(self, windows_number: int = 300, statistique = np.mean):
10
  self.windows_number = windows_number
11
  self.statistique = statistique
12
 
13
+ def fit(self, X: np.ndarray, y = None):
14
  return self
15
 
16
+ def fit_transform(self, X: np.ndarray, y = None) -> np.ndarray:
17
  self.fit(X, y)
18
  return self.transform(X, y)
19
 
20
+ def transform(self, X: np.ndarray, y = None) -> np.ndarray:
21
  X_ = X.copy()
22
  *c_, size_ = X_.shape
23
  windows_size_ = size_//self.windows_number
34
  self.windows_number = windows_number
35
  self.windows_size = 0
36
 
37
+ def fit(self, X: np.ndarray, y = None):
38
  return self
39
 
40
+ def fit_transform(self, X: np.ndarray, y = None) -> np.ndarray:
41
  self.fit(X, y)
42
  return self.transform(X, y)
43
 
44
+ def transform(self, X: np.ndarray, y = None) -> np.ndarray:
45
  X_ = X.copy()
46
  *c_, size_ = X_.shape
47
  windows_size_ = size_//self.windows_number
62
  def __init__(self, windows_number: int = 300):
63
  self.windows_number = windows_number
64
 
65
+ def fit(self, X: np.ndarray, y = None):
66
  return self
67
 
68
+ def fit_transform(self, X: np.ndarray, y = None) -> np.ndarray:
69
  self.fit(X, y)
70
  return self.transform(X, y)
71
 
72
+ def transform(self, X: np.ndarray, y = None) -> np.ndarray:
73
  X_ = X.copy()
74
  *c_, size_ = X_.shape
75
  windows_size_ = size_//self.windows_number
91
  c_, *_ = X_.shape
92
  return X_.reshape(c_, -1, self.N_MFCC)
93
 
94
+ def fit(self, X: np.ndarray, y = None):
95
  return self
96
 
97
+ def fit_transform(self, X: np.ndarray, y = None) -> np.ndarray:
98
  self.fit(X, y)
99
  return self.transform(X, y)
100
 
101
+ def transform(self, X: np.ndarray, y = None) -> np.ndarray:
102
  X_ = X.copy()
103
  c_, *_ = X_.shape
104
  mfcc = librosa.feature.mfcc(y=X_,
130
  c_, *_ = X_.shape
131
  return X_.reshape(c_, -1, self.N_MEL)
132
 
133
+ def fit(self, X: np.ndarray, y = None):
134
  return self
135
 
136
+ def fit_transform(self, X: np.ndarray, y = None) -> np.ndarray:
137
  self.fit(X, y)
138
  return self.transform(X, y)
139
 
140
+ def transform(self, X: np.ndarray, y = None) -> np.ndarray:
141
  X_ = X.copy()
142
  c_, *_ = X_.shape
143
  mel = librosa.feature.melspectrogram(y=X,
162
  def __init__(self):
163
  pass
164
 
165
+ def fit(self, X: np.ndarray, y = None):
166
  return self
167
 
168
+ def fit_transform(self, X: np.ndarray, y = None) -> torch.Tensor:
169
  self.fit(X, y)
170
  return self.transform(X, y)
171
 
172
+ def transform(self, X: np.ndarray, y = None) -> torch.Tensor:
173
  return torch.tensor(X).unsqueeze(dim=1)
174
 
175
  def inverse_transform(self, X: torch.Tensor) -> np.ndarray:
179
  def __init__(self, p: float = 0.005):
180
  self.p = p
181
 
182
+ def fit(self, X: np.ndarray, y = None):
183
  return self
184
 
185
+ def fit_transform(self, X: np.ndarray, y = None) -> np.ndarray:
186
  self.fit(X, y)
187
  return self.transform(X, y)
188
 
189
+ def transform(self, X: np.ndarray, y = None) -> np.ndarray:
190
  will_swap = np.random.choice(X.shape[0], int(self.p*X.shape[0]))
191
  will_swap_with = np.random.choice(X.shape[0], int(self.p*X.shape[0]))
192
  if hasattr(X, "copy"):