Upload modeling_rvc.py with huggingface_hub
Browse files- modeling_rvc.py +63 -0
modeling_rvc.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
class ResidualBlock(nn.Module):
|
| 8 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.convs1 = nn.ModuleList([
|
| 11 |
+
nn.Conv1d(channels, channels, kernel_size, 1, dilation=d, padding=d)
|
| 12 |
+
for d in dilation
|
| 13 |
+
])
|
| 14 |
+
self.convs2 = nn.ModuleList([
|
| 15 |
+
nn.Conv1d(channels, channels, kernel_size, 1, dilation=1, padding=1)
|
| 16 |
+
for _ in dilation
|
| 17 |
+
])
|
| 18 |
+
|
| 19 |
+
def forward(self, x):
|
| 20 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
| 21 |
+
xt = F.leaky_relu(x, 0.1)
|
| 22 |
+
xt = c1(xt)
|
| 23 |
+
xt = F.leaky_relu(xt, 0.1)
|
| 24 |
+
xt = c2(xt)
|
| 25 |
+
x = xt + x
|
| 26 |
+
return x
|
| 27 |
+
|
| 28 |
+
class RVCModel(nn.Module):
|
| 29 |
+
def __init__(self, config):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.config = config
|
| 32 |
+
model_cfg = config["model"]
|
| 33 |
+
|
| 34 |
+
self.encoder = nn.Sequential(
|
| 35 |
+
nn.Conv1d(128, model_cfg["upsample_initial_channel"], 7, 1, 3),
|
| 36 |
+
*[ResidualBlock(model_cfg["upsample_initial_channel"]) for _ in range(3)]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
self.decoder = nn.Sequential(
|
| 40 |
+
nn.Conv1d(model_cfg["upsample_initial_channel"], 128, 7, 1, 3),
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def forward(self, x):
|
| 44 |
+
encoded = self.encoder(x)
|
| 45 |
+
decoded = self.decoder(encoded)
|
| 46 |
+
return decoded
|
| 47 |
+
|
| 48 |
+
def convert_voice(self, audio_path):
|
| 49 |
+
return audio_path
|
| 50 |
+
|
| 51 |
+
@classmethod
|
| 52 |
+
def from_pretrained(cls, model_path):
|
| 53 |
+
config_path = os.path.join(model_path, "config.json")
|
| 54 |
+
with open(config_path, "r") as f:
|
| 55 |
+
config = json.load(f)
|
| 56 |
+
|
| 57 |
+
model = cls(config)
|
| 58 |
+
|
| 59 |
+
model_file = os.path.join(model_path, "model.pth")
|
| 60 |
+
if os.path.exists(model_file):
|
| 61 |
+
model.load_state_dict(torch.load(model_file, map_location="cpu"))
|
| 62 |
+
|
| 63 |
+
return model
|