Delete dreamvoice/src/model/.ipynb_checkpoints
Browse files
dreamvoice/src/model/.ipynb_checkpoints/model-checkpoint.py
DELETED
|
@@ -1,98 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
from diffusers import UNet2DModel, UNet2DConditionModel
|
| 4 |
-
import yaml
|
| 5 |
-
from einops import repeat, rearrange
|
| 6 |
-
|
| 7 |
-
from typing import Any
|
| 8 |
-
from torch import Tensor
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def rand_bool(shape: Any, proba: float, device: Any = None) -> Tensor:
|
| 12 |
-
if proba == 1:
|
| 13 |
-
return torch.ones(shape, device=device, dtype=torch.bool)
|
| 14 |
-
elif proba == 0:
|
| 15 |
-
return torch.zeros(shape, device=device, dtype=torch.bool)
|
| 16 |
-
else:
|
| 17 |
-
return torch.bernoulli(torch.full(shape, proba, device=device)).to(torch.bool)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
class DiffVC(nn.Module):
|
| 21 |
-
def __init__(self, config):
|
| 22 |
-
super().__init__()
|
| 23 |
-
self.config = config
|
| 24 |
-
self.unet = UNet2DModel(**self.config['unet'])
|
| 25 |
-
self.unet.set_use_memory_efficient_attention_xformers(True)
|
| 26 |
-
self.speaker_embedding = nn.Sequential(
|
| 27 |
-
nn.Linear(self.config['cls_embedding']['speaker_dim'], self.config['cls_embedding']['feature_dim']),
|
| 28 |
-
nn.SiLU(),
|
| 29 |
-
nn.Linear(self.config['cls_embedding']['feature_dim'], self.config['cls_embedding']['feature_dim']))
|
| 30 |
-
self.uncond = nn.Parameter(torch.randn(self.config['cls_embedding']['speaker_dim']) /
|
| 31 |
-
self.config['cls_embedding']['speaker_dim'] ** 0.5)
|
| 32 |
-
self.content_embedding = nn.Sequential(
|
| 33 |
-
nn.Linear(self.config['cls_embedding']['content_dim'], self.config['cls_embedding']['content_hidden']),
|
| 34 |
-
nn.SiLU(),
|
| 35 |
-
nn.Linear(self.config['cls_embedding']['content_hidden'], self.config['cls_embedding']['content_hidden']))
|
| 36 |
-
|
| 37 |
-
if self.config['cls_embedding']['use_pitch']:
|
| 38 |
-
self.pitch_control = True
|
| 39 |
-
self.pitch_embedding = nn.Sequential(
|
| 40 |
-
nn.Linear(self.config['cls_embedding']['pitch_dim'], self.config['cls_embedding']['pitch_hidden']),
|
| 41 |
-
nn.SiLU(),
|
| 42 |
-
nn.Linear(self.config['cls_embedding']['pitch_hidden'],
|
| 43 |
-
self.config['cls_embedding']['pitch_hidden']))
|
| 44 |
-
self.pitch_uncond = nn.Parameter(torch.randn(self.config['cls_embedding']['pitch_hidden']) /
|
| 45 |
-
self.config['cls_embedding']['pitch_hidden'] ** 0.5)
|
| 46 |
-
else:
|
| 47 |
-
print('no pitch module')
|
| 48 |
-
self.pitch_control = False
|
| 49 |
-
|
| 50 |
-
def forward(self, target, t, content, speaker, pitch,
|
| 51 |
-
train_cfg=False, speaker_cfg=0.0, pitch_cfg=0.0):
|
| 52 |
-
B, C, M, L = target.shape
|
| 53 |
-
content = self.content_embedding(content)
|
| 54 |
-
content = repeat(content, "b t c-> b c m t", m=M)
|
| 55 |
-
target = target.to(content.dtype)
|
| 56 |
-
x = torch.cat([target, content], dim=1)
|
| 57 |
-
|
| 58 |
-
if self.pitch_control:
|
| 59 |
-
if pitch is not None:
|
| 60 |
-
pitch = self.pitch_embedding(pitch)
|
| 61 |
-
else:
|
| 62 |
-
pitch = repeat(self.pitch_uncond, "c-> b t c", b=B, t=L).to(target.dtype)
|
| 63 |
-
|
| 64 |
-
if train_cfg:
|
| 65 |
-
uncond = repeat(self.uncond, "c-> b c", b=B).to(target.dtype)
|
| 66 |
-
batch_mask = rand_bool(shape=(B, 1), proba=speaker_cfg, device=target.device)
|
| 67 |
-
speaker = torch.where(batch_mask, uncond, speaker)
|
| 68 |
-
|
| 69 |
-
if self.pitch_control:
|
| 70 |
-
batch_mask = rand_bool(shape=(B, 1, 1), proba=pitch_cfg, device=target.device)
|
| 71 |
-
pitch_uncond = repeat(self.pitch_uncond, "c-> b t c", b=B, t=L).to(target.dtype)
|
| 72 |
-
pitch = torch.where(batch_mask, pitch_uncond, pitch)
|
| 73 |
-
|
| 74 |
-
speaker = self.speaker_embedding(speaker)
|
| 75 |
-
|
| 76 |
-
if self.pitch_control:
|
| 77 |
-
pitch = repeat(pitch, "b t c-> b c m t", m=M)
|
| 78 |
-
x = torch.cat([x, pitch], dim=1)
|
| 79 |
-
|
| 80 |
-
output = self.unet(sample=x, timestep=t, class_labels=speaker)['sample']
|
| 81 |
-
|
| 82 |
-
return output
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
if __name__ == "__main__":
|
| 86 |
-
with open('diffvc_base_pitch.yaml', 'r') as fp:
|
| 87 |
-
config = yaml.safe_load(fp)
|
| 88 |
-
device = 'cuda'
|
| 89 |
-
|
| 90 |
-
model = DiffVC(config['diffwrap']).to(device)
|
| 91 |
-
|
| 92 |
-
x = torch.rand((2, 1, 100, 256)).to(device)
|
| 93 |
-
y = torch.rand((2, 256, 768)).to(device)
|
| 94 |
-
p = torch.rand(2, 256, 1).to(device)
|
| 95 |
-
t = torch.randint(0, 1000, (2,)).long().to(device)
|
| 96 |
-
spk = torch.rand(2, 256).to(device)
|
| 97 |
-
|
| 98 |
-
output = model(x, t, y, spk, pitch=p, train_cfg=True, cfg_prob=0.25)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dreamvoice/src/model/.ipynb_checkpoints/model_cross-checkpoint.py
DELETED
|
@@ -1,116 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
from diffusers import UNet2DModel, UNet2DConditionModel
|
| 4 |
-
import yaml
|
| 5 |
-
from einops import repeat, rearrange
|
| 6 |
-
|
| 7 |
-
from typing import Any
|
| 8 |
-
from torch import Tensor
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def rand_bool(shape: Any, proba: float, device: Any = None) -> Tensor:
|
| 12 |
-
if proba == 1:
|
| 13 |
-
return torch.ones(shape, device=device, dtype=torch.bool)
|
| 14 |
-
elif proba == 0:
|
| 15 |
-
return torch.zeros(shape, device=device, dtype=torch.bool)
|
| 16 |
-
else:
|
| 17 |
-
return torch.bernoulli(torch.full(shape, proba, device=device)).to(torch.bool)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
class FixedEmbedding(nn.Module):
|
| 21 |
-
def __init__(self, features=128):
|
| 22 |
-
super().__init__()
|
| 23 |
-
self.embedding = nn.Embedding(1, features)
|
| 24 |
-
|
| 25 |
-
def forward(self, y):
|
| 26 |
-
B, L, C, device = y.shape[0], y.shape[-2], y.shape[-1], y.device
|
| 27 |
-
embed = self.embedding(torch.zeros(B, device=device).long())
|
| 28 |
-
fixed_embedding = repeat(embed, "b c -> b l c", l=L)
|
| 29 |
-
return fixed_embedding
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
class DiffVC_Cross(nn.Module):
|
| 33 |
-
def __init__(self, config):
|
| 34 |
-
super().__init__()
|
| 35 |
-
self.config = config
|
| 36 |
-
self.unet = UNet2DConditionModel(**self.config['unet'])
|
| 37 |
-
self.unet.set_use_memory_efficient_attention_xformers(True)
|
| 38 |
-
self.cfg_embedding = FixedEmbedding(self.config['unet']['cross_attention_dim'])
|
| 39 |
-
|
| 40 |
-
self.context_embedding = nn.Sequential(
|
| 41 |
-
nn.Linear(self.config['unet']['cross_attention_dim'], self.config['unet']['cross_attention_dim']),
|
| 42 |
-
nn.SiLU(),
|
| 43 |
-
nn.Linear(self.config['unet']['cross_attention_dim'], self.config['unet']['cross_attention_dim']))
|
| 44 |
-
|
| 45 |
-
self.content_embedding = nn.Sequential(
|
| 46 |
-
nn.Linear(self.config['cls_embedding']['content_dim'], self.config['cls_embedding']['content_hidden']),
|
| 47 |
-
nn.SiLU(),
|
| 48 |
-
nn.Linear(self.config['cls_embedding']['content_hidden'], self.config['cls_embedding']['content_hidden']))
|
| 49 |
-
|
| 50 |
-
if self.config['cls_embedding']['use_pitch']:
|
| 51 |
-
self.pitch_control = True
|
| 52 |
-
self.pitch_embedding = nn.Sequential(
|
| 53 |
-
nn.Linear(self.config['cls_embedding']['pitch_dim'], self.config['cls_embedding']['pitch_hidden']),
|
| 54 |
-
nn.SiLU(),
|
| 55 |
-
nn.Linear(self.config['cls_embedding']['pitch_hidden'],
|
| 56 |
-
self.config['cls_embedding']['pitch_hidden']))
|
| 57 |
-
|
| 58 |
-
self.pitch_uncond = nn.Parameter(torch.randn(self.config['cls_embedding']['pitch_hidden']) /
|
| 59 |
-
self.config['cls_embedding']['pitch_hidden'] ** 0.5)
|
| 60 |
-
else:
|
| 61 |
-
print('no pitch module')
|
| 62 |
-
self.pitch_control = False
|
| 63 |
-
|
| 64 |
-
def forward(self, target, t, content, prompt, prompt_mask=None, pitch=None,
|
| 65 |
-
train_cfg=False, speaker_cfg=0.0, pitch_cfg=0.0):
|
| 66 |
-
B, C, M, L = target.shape
|
| 67 |
-
content = self.content_embedding(content)
|
| 68 |
-
content = repeat(content, "b t c-> b c m t", m=M)
|
| 69 |
-
target = target.to(content.dtype)
|
| 70 |
-
x = torch.cat([target, content], dim=1)
|
| 71 |
-
|
| 72 |
-
if self.pitch_control:
|
| 73 |
-
if pitch is not None:
|
| 74 |
-
pitch = self.pitch_embedding(pitch)
|
| 75 |
-
else:
|
| 76 |
-
pitch = repeat(self.pitch_uncond, "c-> b t c", b=B, t=L).to(target.dtype)
|
| 77 |
-
|
| 78 |
-
if train_cfg:
|
| 79 |
-
# Randomly mask embedding
|
| 80 |
-
batch_mask = rand_bool(shape=(B, 1, 1), proba=speaker_cfg, device=target.device)
|
| 81 |
-
fixed_embedding = self.cfg_embedding(prompt).to(target.dtype)
|
| 82 |
-
prompt = torch.where(batch_mask, fixed_embedding, prompt)
|
| 83 |
-
|
| 84 |
-
if self.pitch_control:
|
| 85 |
-
batch_mask = rand_bool(shape=(B, 1, 1), proba=pitch_cfg, device=target.device)
|
| 86 |
-
pitch_uncond = repeat(self.pitch_uncond, "c-> b t c", b=B, t=L).to(target.dtype)
|
| 87 |
-
pitch = torch.where(batch_mask, pitch_uncond, pitch)
|
| 88 |
-
|
| 89 |
-
prompt = self.context_embedding(prompt)
|
| 90 |
-
|
| 91 |
-
if self.pitch_control:
|
| 92 |
-
pitch = repeat(pitch, "b t c-> b c m t", m=M)
|
| 93 |
-
x = torch.cat([x, pitch], dim=1)
|
| 94 |
-
|
| 95 |
-
output = self.unet(sample=x, timestep=t,
|
| 96 |
-
encoder_hidden_states=prompt,
|
| 97 |
-
encoder_attention_mask=prompt_mask)['sample']
|
| 98 |
-
|
| 99 |
-
return output
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
if __name__ == "__main__":
|
| 103 |
-
with open('diffvc_cross_pitch.yaml', 'r') as fp:
|
| 104 |
-
config = yaml.safe_load(fp)
|
| 105 |
-
device = 'cuda'
|
| 106 |
-
|
| 107 |
-
model = DiffVC_Cross(config['diffwrap']).to(device)
|
| 108 |
-
|
| 109 |
-
x = torch.rand((2, 1, 100, 256)).to(device)
|
| 110 |
-
y = torch.rand((2, 256, 768)).to(device)
|
| 111 |
-
t = torch.randint(0, 1000, (2,)).long().to(device)
|
| 112 |
-
prompt = torch.rand(2, 64, 768).to(device)
|
| 113 |
-
prompt_mask = torch.ones(2, 64).to(device)
|
| 114 |
-
p = torch.rand(2, 256, 1).to(device)
|
| 115 |
-
|
| 116 |
-
output = model(x, t, y, prompt, prompt_mask, p, train_cfg=True, speaker_cfg=0.25, pitch_cfg=0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dreamvoice/src/model/.ipynb_checkpoints/p2e_cross-checkpoint.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
from diffusers import UNet2DModel, UNet2DConditionModel
|
| 4 |
-
import yaml
|
| 5 |
-
from einops import repeat, rearrange
|
| 6 |
-
|
| 7 |
-
from typing import Any
|
| 8 |
-
from torch import Tensor
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def rand_bool(shape: Any, proba: float, device: Any = None) -> Tensor:
|
| 12 |
-
if proba == 1:
|
| 13 |
-
return torch.ones(shape, device=device, dtype=torch.bool)
|
| 14 |
-
elif proba == 0:
|
| 15 |
-
return torch.zeros(shape, device=device, dtype=torch.bool)
|
| 16 |
-
else:
|
| 17 |
-
return torch.bernoulli(torch.full(shape, proba, device=device)).to(torch.bool)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
class FixedEmbedding(nn.Module):
|
| 21 |
-
def __init__(self, features=128):
|
| 22 |
-
super().__init__()
|
| 23 |
-
self.embedding = nn.Embedding(1, features)
|
| 24 |
-
|
| 25 |
-
def forward(self, y):
|
| 26 |
-
B, L, C, device = y.shape[0], y.shape[-2], y.shape[-1], y.device
|
| 27 |
-
embed = self.embedding(torch.zeros(B, device=device).long())
|
| 28 |
-
fixed_embedding = repeat(embed, "b c -> b l c", l=L)
|
| 29 |
-
return fixed_embedding
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
class P2E_Cross(nn.Module):
|
| 33 |
-
def __init__(self, config):
|
| 34 |
-
super().__init__()
|
| 35 |
-
self.config = config
|
| 36 |
-
self.unet = UNet2DConditionModel(**self.config['unet'])
|
| 37 |
-
self.unet.set_use_memory_efficient_attention_xformers(True)
|
| 38 |
-
self.cfg_embedding = FixedEmbedding(self.config['unet']['cross_attention_dim'])
|
| 39 |
-
|
| 40 |
-
self.context_embedding = nn.Sequential(
|
| 41 |
-
nn.Linear(self.config['unet']['cross_attention_dim'], self.config['unet']['cross_attention_dim']),
|
| 42 |
-
nn.SiLU(),
|
| 43 |
-
nn.Linear(self.config['unet']['cross_attention_dim'], self.config['unet']['cross_attention_dim']))
|
| 44 |
-
|
| 45 |
-
def forward(self, target, t, prompt, prompt_mask=None,
|
| 46 |
-
train_cfg=False, cfg_prob=0.0):
|
| 47 |
-
B, C = target.shape
|
| 48 |
-
target = target.unsqueeze(-1).unsqueeze(-1)
|
| 49 |
-
|
| 50 |
-
if train_cfg:
|
| 51 |
-
if cfg_prob > 0.0:
|
| 52 |
-
# Randomly mask embedding
|
| 53 |
-
batch_mask = rand_bool(shape=(B, 1, 1), proba=cfg_prob, device=target.device)
|
| 54 |
-
fixed_embedding = self.cfg_embedding(prompt).to(target.dtype)
|
| 55 |
-
prompt = torch.where(batch_mask, fixed_embedding, prompt)
|
| 56 |
-
|
| 57 |
-
prompt = self.context_embedding(prompt)
|
| 58 |
-
# fix the bug that prompt will copy dtype from target in diffusers
|
| 59 |
-
target = target.to(prompt.dtype)
|
| 60 |
-
|
| 61 |
-
output = self.unet(sample=target, timestep=t,
|
| 62 |
-
encoder_hidden_states=prompt,
|
| 63 |
-
encoder_attention_mask=prompt_mask)['sample']
|
| 64 |
-
|
| 65 |
-
return output.squeeze(-1).squeeze(-1)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if __name__ == "__main__":
|
| 69 |
-
with open('p2e_cross.yaml', 'r') as fp:
|
| 70 |
-
config = yaml.safe_load(fp)
|
| 71 |
-
device = 'cuda'
|
| 72 |
-
|
| 73 |
-
model = P2E_Cross(config['diffwrap']).to(device)
|
| 74 |
-
|
| 75 |
-
x = torch.rand((2, 256)).to(device)
|
| 76 |
-
t = torch.randint(0, 1000, (2,)).long().to(device)
|
| 77 |
-
prompt = torch.rand(2, 64, 768).to(device)
|
| 78 |
-
prompt_mask = torch.ones(2, 64).to(device)
|
| 79 |
-
|
| 80 |
-
output = model(x, t, prompt, prompt_mask, train_cfg=True, cfg_prob=0.25)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|