Download recipe/scripts/train_vocoder_texture.py from gowombat/tiny-voca-da: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/scripts/train_vocoder_texture.py
- Command line
-
hf download hf://gowombat/tiny-voca-da/recipe/scripts/train_vocoder_texture.py
-
curl -L -o train_vocoder_texture.py https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/scripts/train_vocoder_texture.py
2.91 kB
| """tiny_voca.train_vocoder with a texture term that keeps vowel motion through the pairs fine-tune. | |
| Night of 2026-11-25/26: the texture voice models (T, T2) produce the real speaker's amount of | |
| frame-to-frame change above 1-2 kHz, and vocoder C passes most of it (T2 + C: 0.472 / 0.429 / | |
| 0.376 at 1-2 / 2-4 / 4-8 kHz, real 0.479 / 0.442 / 0.398). Fitting C to T's pairs (Ctex) cut | |
| the buzz but averaged the motion away (T + Ctex 0.387 / 0.340 at 2-4 / 4-8 kHz). In the pairs, | |
| the model's motion does not line up with the recording, so the frame-by-frame mel and | |
| distillation terms pull the vocoder towards the mean. | |
| This adds the voice model's texture idea to the vocoder. Per crop and per band above | |
| VOCODER_TEXTURE_LOW_HZ, it matches the AMOUNT of frame-to-frame change of the student's log | |
| magnitude to the teacher's (Vocos on the real recording), not the values. The bands are 16 | |
| linear bins (375 Hz at fft 1024 / 24 kHz). The term is added to the distillation magnitude | |
| part, so its effective weight is distill_weight x VOCODER_TEXTURE. | |
| VOCODER_TEXTURE=5 VOCODER_TEXTURE_LOW_HZ=1000 python scripts/train_vocoder_texture.py <train_vocoder args> | |
| Needs --teacher vocos --distill-mode spectrum (the only path that calls spectrum_distance). | |
| """ | |
| import math | |
| import os | |
| import sys | |
| import torch | |
| import torch.nn.functional as F | |
| from tiny_voca import train_vocoder | |
| WEIGHT = float(os.environ.get('VOCODER_TEXTURE', '0')) | |
| LOW_HZ = float(os.environ.get('VOCODER_TEXTURE_LOW_HZ', '1000')) | |
| POOL = 16 | |
| EVERY = int(os.environ.get('VOCODER_TEXTURE_PRINT_EVERY', '200')) | |
| FLOOR, CEILING = math.log(1e-5), math.log(1e2) | |
| _original = train_vocoder.spectrum_distance | |
| _window = {'sum': 0.0, 'n': 0} | |
| def _pooled(log_magnitude): | |
| bins = log_magnitude.shape[1] | |
| first = int(LOW_HZ / (12000.0 / (bins - 1))) | |
| magnitude = torch.exp(log_magnitude.clamp(FLOOR, CEILING))[:, first:] # [B, bins, frames] | |
| bands = F.avg_pool1d(magnitude.transpose(1, 2), POOL, POOL).transpose(1, 2) # [B, bands, frames] | |
| return torch.log(bands + 1e-5) | |
| def texture(student_log, teacher_log): | |
| change = lambda x: (x[..., 1:] - x[..., :-1]).abs().mean(-1) # [B, bands] | |
| return (change(_pooled(student_log)) - change(_pooled(teacher_log))).abs().mean() | |
| def spectrum_distance(student, teacher): | |
| magnitude, phase = _original(student, teacher) | |
| term = texture(student[0], teacher[0].detach()) | |
| _window['sum'] += float(term.detach()); _window['n'] += 1 | |
| if _window['n'] == EVERY: | |
| print(f'{{"vocoder_texture": {_window["sum"] / EVERY:.5f}}}', flush=True) | |
| _window['sum'], _window['n'] = 0.0, 0 | |
| return magnitude + WEIGHT * term, phase | |
| train_vocoder.spectrum_distance = spectrum_distance | |
| if __name__ == '__main__': | |
| print(f'vocoder texture weight {WEIGHT} from {LOW_HZ:.0f} Hz', flush=True) | |
| sys.argv[0] = 'tiny_voca.train_vocoder' | |
| train_vocoder.main() | |