| """ |
| =============================================================================== |
| features/augmentation.py β Data Augmentation for Training |
| =============================================================================== |
| |
| """ |
|
|
| import numpy as np |
| from config import FREQ_MASK_PARAM, TIME_MASK_PARAM, NOISE_STD, AUGMENT_PROB |
|
|
| AUGMENT_PROB = 0.5 |
| FREQ_MASK_PARAM = 24 |
| TIME_MASK_PARAM = 40 |
| NOISE_STD = 0.05 |
|
|
| |
| |
|
|
| def apply_augmentation(mel_spec): |
| """ |
| Apply data augmentation to a mel spectrogram (training only). |
| |
| Notes |
| ----- |
| - This function creates a COPY of the input β the original is not modified. |
| - Each call produces a DIFFERENT augmentation due to randomness. |
| So each epoch, the same sample looks slightly different β diversity. |
| - The augmentation is seeded by numpy's global RNG. For reproducibility |
| across runs, set np.random.seed() in the training script. |
| |
| """ |
| augmented = mel_spec.copy() |
| n_mels, time_frames = augmented.shape |
|
|
| |
| if np.random.random() < AUGMENT_PROB: |
| f = np.random.randint(0, FREQ_MASK_PARAM) |
| f0 = np.random.randint(0, n_mels - f) |
| augmented[f0:f0 + f, :] = 0.0 |
|
|
| |
| if np.random.random() < AUGMENT_PROB: |
| t = np.random.randint(0, TIME_MASK_PARAM) |
| t0 = np.random.randint(0, time_frames - t) |
| augmented[:, t0:t0 + t] = 0.0 |
|
|
| |
| if np.random.random() < AUGMENT_PROB: |
| noise = np.random.normal(0, NOISE_STD, augmented.shape) |
| augmented = augmented + noise |
| |
| |
| augmented = np.clip(augmented, 0.0, 1.0) |
|
|
| return augmented |