int8 build of OpenVoice V2
Browse files- int8/config.json +57 -0
- int8/converter.onnx +3 -0
- int8/pipeline.py +77 -0
- int8/ref_encoder.onnx +3 -0
int8/config.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_version_": "v2",
|
| 3 |
+
"data": {
|
| 4 |
+
"sampling_rate": 22050,
|
| 5 |
+
"filter_length": 1024,
|
| 6 |
+
"hop_length": 256,
|
| 7 |
+
"win_length": 1024,
|
| 8 |
+
"n_speakers": 0
|
| 9 |
+
},
|
| 10 |
+
"model": {
|
| 11 |
+
"zero_g": true,
|
| 12 |
+
"inter_channels": 192,
|
| 13 |
+
"hidden_channels": 192,
|
| 14 |
+
"filter_channels": 768,
|
| 15 |
+
"n_heads": 2,
|
| 16 |
+
"n_layers": 6,
|
| 17 |
+
"kernel_size": 3,
|
| 18 |
+
"p_dropout": 0.1,
|
| 19 |
+
"resblock": "1",
|
| 20 |
+
"resblock_kernel_sizes": [
|
| 21 |
+
3,
|
| 22 |
+
7,
|
| 23 |
+
11
|
| 24 |
+
],
|
| 25 |
+
"resblock_dilation_sizes": [
|
| 26 |
+
[
|
| 27 |
+
1,
|
| 28 |
+
3,
|
| 29 |
+
5
|
| 30 |
+
],
|
| 31 |
+
[
|
| 32 |
+
1,
|
| 33 |
+
3,
|
| 34 |
+
5
|
| 35 |
+
],
|
| 36 |
+
[
|
| 37 |
+
1,
|
| 38 |
+
3,
|
| 39 |
+
5
|
| 40 |
+
]
|
| 41 |
+
],
|
| 42 |
+
"upsample_rates": [
|
| 43 |
+
8,
|
| 44 |
+
8,
|
| 45 |
+
2,
|
| 46 |
+
2
|
| 47 |
+
],
|
| 48 |
+
"upsample_initial_channel": 512,
|
| 49 |
+
"upsample_kernel_sizes": [
|
| 50 |
+
16,
|
| 51 |
+
16,
|
| 52 |
+
4,
|
| 53 |
+
4
|
| 54 |
+
],
|
| 55 |
+
"gin_channels": 256
|
| 56 |
+
}
|
| 57 |
+
}
|
int8/converter.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:17c254cb33ca437f192d80e3bdc9cfdde39620aeb995a472345352e393955b64
|
| 3 |
+
size 32664984
|
int8/pipeline.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""OpenVoice V2 voice conversion (tone-colour converter) with ONNX Runtime: re-voice a recording into the voice of a
|
| 2 |
+
reference recording, keeping its words and timing. Follows the official `ToneColorConverter.extract_se` /
|
| 3 |
+
`convert(tau=0.3)` on whole files (the official `se_extractor.get_se` first splits the reference with a VAD; this
|
| 4 |
+
takes the reference in one piece), without the watermark. 22.05 kHz output. Needs numpy, onnxruntime, soundfile and
|
| 5 |
+
soxr.
|
| 6 |
+
|
| 7 |
+
python fp16/pipeline.py source.wav voice.wav out.wav [--cuda] [--seed 0]
|
| 8 |
+
|
| 9 |
+
The posterior encoder's sampling noise comes from numpy's generator (`seed`).
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
|
| 16 |
+
import numpy as np
|
| 17 |
+
|
| 18 |
+
SR, N_FFT, HOP = 22050, 1024, 256
|
| 19 |
+
TAU = 0.3
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def spectrogram(wav):
|
| 23 |
+
"""openvoice.mel_processing.spectrogram_torch(center=False): reflect padding of 384 each side, periodic Hann 1024,
|
| 24 |
+
hop 256, sqrt(re^2 + im^2 + 1e-6). Returns [1, 513, T]."""
|
| 25 |
+
x = np.pad(np.asarray(wav, np.float32), ((N_FFT - HOP) // 2, (N_FFT - HOP) // 2), mode="reflect")
|
| 26 |
+
win = (0.5 - 0.5 * np.cos(2 * np.pi * np.arange(N_FFT) / N_FFT)).astype(np.float32)
|
| 27 |
+
n = 1 + (len(x) - N_FFT) // HOP
|
| 28 |
+
idx = np.arange(N_FFT)[None, :] + HOP * np.arange(n)[:, None]
|
| 29 |
+
s = np.fft.rfft(x[idx] * win, axis=1)
|
| 30 |
+
return np.sqrt(s.real.astype(np.float64) ** 2 + s.imag.astype(np.float64) ** 2 + 1e-6).T[None].astype(np.float32)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class OpenVoiceV2:
|
| 34 |
+
def __init__(self, folder, provider="cpu", threads=4):
|
| 35 |
+
import onnxruntime as ort
|
| 36 |
+
folder = Path(folder)
|
| 37 |
+
if provider != "cpu" and hasattr(ort, "preload_dlls"):
|
| 38 |
+
try:
|
| 39 |
+
ort.preload_dlls()
|
| 40 |
+
except Exception:
|
| 41 |
+
pass
|
| 42 |
+
so = ort.SessionOptions()
|
| 43 |
+
so.intra_op_num_threads = threads
|
| 44 |
+
prov = ["CPUExecutionProvider"] if provider == "cpu" else [
|
| 45 |
+
("CUDAExecutionProvider", {"arena_extend_strategy": "kSameAsRequested"}), "CPUExecutionProvider"]
|
| 46 |
+
self.s = {n: ort.InferenceSession(str(folder / f"{n}.onnx"), so, providers=prov)
|
| 47 |
+
for n in ("ref_encoder", "converter")}
|
| 48 |
+
|
| 49 |
+
def embed(self, wav22):
|
| 50 |
+
"""extract_se for one file: the reference encoder on its spectrogram -> [1, 256, 1]."""
|
| 51 |
+
return self.s["ref_encoder"].run(None, {"spec": spectrogram(wav22).transpose(0, 2, 1).copy()})[0][..., None]
|
| 52 |
+
|
| 53 |
+
def __call__(self, src, src_sr, ref, ref_sr, seed=0):
|
| 54 |
+
import soxr
|
| 55 |
+
rs = lambda w, sr: soxr.resample(np.asarray(w, np.float32), sr, SR, quality="HQ").astype(np.float32) \
|
| 56 |
+
if sr != SR else np.asarray(w, np.float32)
|
| 57 |
+
src, ref = rs(src, src_sr), rs(ref, ref_sr)
|
| 58 |
+
spec = spectrogram(src)
|
| 59 |
+
noise = np.random.default_rng(seed).standard_normal((1, 192, spec.shape[2])).astype(np.float32)
|
| 60 |
+
y = self.s["converter"].run(None, {"spec": spec, "noise": noise, "src_se": self.embed(src),
|
| 61 |
+
"tgt_se": self.embed(ref)})[0]
|
| 62 |
+
return y[0, 0].astype(np.float32), SR
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
import argparse
|
| 67 |
+
import soundfile as sf
|
| 68 |
+
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
| 69 |
+
ap.add_argument("source"); ap.add_argument("voice"); ap.add_argument("output")
|
| 70 |
+
ap.add_argument("--dir", default=str(Path(__file__).resolve().parent))
|
| 71 |
+
ap.add_argument("--cuda", action="store_true"); ap.add_argument("--seed", type=int, default=0)
|
| 72 |
+
a = ap.parse_args()
|
| 73 |
+
s, ssr = sf.read(a.source, dtype="float32", always_2d=True)
|
| 74 |
+
r, rsr = sf.read(a.voice, dtype="float32", always_2d=True)
|
| 75 |
+
vc = OpenVoiceV2(a.dir, "cuda" if a.cuda else "cpu")
|
| 76 |
+
y, sr = vc(s.mean(1), ssr, r.mean(1), rsr, a.seed)
|
| 77 |
+
sf.write(a.output, y, sr)
|
int8/ref_encoder.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2e8b8b34c880b6f5759b31e7a8ee4ac0a5eeeca2362ec5d1270c10793703ad02
|
| 3 |
+
size 836521
|