forked from mrq/DL-Art-School
All the stuff needed for cheater latent generation
This commit is contained in:
parent
c5ea2bee52
commit
a659cd865c
|
@ -95,6 +95,8 @@ def create_dataset(dataset_opt, return_collate=False):
|
|||
from data.audio.unsupervised_audio_dataset import UnsupervisedAudioDataset as D
|
||||
elif mode == 'unsupervised_audio_with_noise':
|
||||
from data.audio.audio_with_noise_dataset import AudioWithNoiseDataset as D
|
||||
elif mode == 'preprocessed_mel':
|
||||
from data.audio.preprocessed_mel_dataset import PreprocessedMelDataset as D
|
||||
elif mode == 'grand_conjoined_voice':
|
||||
from data.audio.grand_conjoined_dataset import GrandConjoinedDataset as D
|
||||
from data.zero_pad_dict_collate import ZeroPadDictCollate as C
|
||||
|
|
71
codes/data/audio/preprocessed_mel_dataset.py
Normal file
71
codes/data/audio/preprocessed_mel_dataset.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
import torch.utils.data
|
||||
import torchaudio
|
||||
import torchvision
|
||||
from tqdm import tqdm
|
||||
|
||||
from utils.util import opt_get
|
||||
|
||||
|
||||
class PreprocessedMelDataset(torch.utils.data.Dataset):
|
||||
|
||||
def __init__(self, opt):
|
||||
path = opt['path']
|
||||
cache_path = opt['cache_path'] # Will fail when multiple paths specified, must be specified in this case.
|
||||
if os.path.exists(cache_path):
|
||||
self.paths = torch.load(cache_path)
|
||||
else:
|
||||
path = Path(path)
|
||||
self.paths = [str(p) for p in path.rglob("*.npz")]
|
||||
torch.save(self.paths, cache_path)
|
||||
self.pad_to = opt_get(opt, ['pad_to_samples'], 10336)
|
||||
|
||||
def __getitem__(self, index):
|
||||
with np.load(self.paths[index]) as npz_file:
|
||||
mel = torch.tensor(npz_file['arr_0'])
|
||||
assert mel.shape[-1] <= self.pad_to
|
||||
padding_needed = self.pad_to - mel.shape[-1]
|
||||
mask = torch.zeros_like(mel)
|
||||
if padding_needed > 0:
|
||||
mel = F.pad(mel, (0,padding_needed))
|
||||
mask = F.pad(mask, (0,padding_needed), value=1)
|
||||
|
||||
output = {
|
||||
'mel': mel,
|
||||
'mel_lengths': torch.tensor(mel.shape[-1]),
|
||||
'mask': mask,
|
||||
'mask_lengths': torch.tensor(mask.shape[-1]),
|
||||
'path': self.paths[index],
|
||||
}
|
||||
return output
|
||||
|
||||
def __len__(self):
|
||||
return len(self.paths)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
params = {
|
||||
'mode': 'preprocessed_mel',
|
||||
'path': 'Y:\\separated\\large_mels',
|
||||
'cache_path': 'Y:\\separated\\large_mels.pth',
|
||||
'pad_to_samples': 10336,
|
||||
'phase': 'train',
|
||||
'n_workers': 0,
|
||||
'batch_size': 16,
|
||||
}
|
||||
from data import create_dataset, create_dataloader
|
||||
|
||||
ds = create_dataset(params)
|
||||
dl = create_dataloader(ds, params)
|
||||
i = 0
|
||||
for b in tqdm(dl):
|
||||
#pass
|
||||
torchvision.utils.save_image((b['mel'].unsqueeze(1)+1)/2, f'{i}.png')
|
||||
i += 1
|
||||
if i > 20:
|
||||
break
|
|
@ -0,0 +1,272 @@
|
|||
import itertools
|
||||
from time import time
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
from models.arch_util import ResBlock, AttentionBlock
|
||||
from models.audio.music.gpt_music2 import UpperEncoder, GptMusicLower
|
||||
from models.audio.music.music_quantizer2 import MusicQuantizer2
|
||||
from models.audio.tts.lucidrains_dvae import DiscreteVAE
|
||||
from models.diffusion.nn import timestep_embedding, normalization, zero_module, conv_nd, linear
|
||||
from models.diffusion.unet_diffusion import TimestepBlock
|
||||
from models.lucidrains.x_transformers import Encoder, Attention, RMSScaleShiftNorm, RotaryEmbedding, \
|
||||
FeedForward
|
||||
from trainer.networks import register_model
|
||||
from utils.util import checkpoint, print_network
|
||||
|
||||
|
||||
def is_latent(t):
|
||||
return t.dtype == torch.float
|
||||
|
||||
def is_sequence(t):
|
||||
return t.dtype == torch.long
|
||||
|
||||
|
||||
class MultiGroupEmbedding(nn.Module):
|
||||
def __init__(self, tokens, groups, dim):
|
||||
super().__init__()
|
||||
self.m = nn.ModuleList([nn.Embedding(tokens, dim // groups) for _ in range(groups)])
|
||||
|
||||
def forward(self, x):
|
||||
h = [embedding(x[:, :, i]) for i, embedding in enumerate(self.m)]
|
||||
return torch.cat(h, dim=-1)
|
||||
|
||||
|
||||
class TimestepRotaryEmbedSequential(nn.Sequential, TimestepBlock):
|
||||
def forward(self, x, emb, rotary_emb):
|
||||
for layer in self:
|
||||
if isinstance(layer, TimestepBlock):
|
||||
x = layer(x, emb, rotary_emb)
|
||||
else:
|
||||
x = layer(x, rotary_emb)
|
||||
return x
|
||||
|
||||
|
||||
class SubBlock(nn.Module):
|
||||
def __init__(self, inp_dim, contraction_dim, heads, dropout):
|
||||
super().__init__()
|
||||
self.attn = Attention(inp_dim, out_dim=contraction_dim, heads=heads, dim_head=contraction_dim//heads, causal=False, dropout=dropout)
|
||||
self.attnorm = nn.LayerNorm(contraction_dim)
|
||||
self.ff = FeedForward(inp_dim+contraction_dim, dim_out=contraction_dim, mult=2, dropout=dropout)
|
||||
self.ffnorm = nn.LayerNorm(contraction_dim)
|
||||
|
||||
def forward(self, x, rotary_emb):
|
||||
ah, _, _, _ = checkpoint(self.attn, x, None, None, None, None, None, rotary_emb)
|
||||
ah = F.gelu(self.attnorm(ah))
|
||||
h = torch.cat([ah, x], dim=-1)
|
||||
hf = checkpoint(self.ff, h)
|
||||
hf = F.gelu(self.ffnorm(hf))
|
||||
h = torch.cat([h, hf], dim=-1)
|
||||
return h
|
||||
|
||||
|
||||
class ConcatAttentionBlock(TimestepBlock):
|
||||
def __init__(self, trunk_dim, contraction_dim, time_embed_dim, heads, dropout):
|
||||
super().__init__()
|
||||
self.prenorm = RMSScaleShiftNorm(trunk_dim, embed_dim=time_embed_dim, bias=False)
|
||||
self.block1 = SubBlock(trunk_dim, contraction_dim, heads, dropout)
|
||||
self.block2 = SubBlock(trunk_dim+contraction_dim*2, contraction_dim, heads, dropout)
|
||||
self.out = nn.Linear(contraction_dim*4, trunk_dim, bias=False)
|
||||
self.out.weight.data.zero_()
|
||||
|
||||
def forward(self, x, conditioning, timestep_emb, rotary_emb):
|
||||
h = self.prenorm(x, norm_scale_shift_inp=timestep_emb)
|
||||
h = torch.cat([conditioning, h], dim=1)
|
||||
h = self.block1(h, rotary_emb)
|
||||
h = self.block2(h, rotary_emb)
|
||||
h = self.out(h[:,:,x.shape[-1]:])
|
||||
return h[:, 1:] + x
|
||||
|
||||
|
||||
class TransformerDiffusion(nn.Module):
|
||||
"""
|
||||
A diffusion model composed entirely of stacks of transformer layers. Why would you do it any other way?
|
||||
"""
|
||||
def __init__(
|
||||
self,
|
||||
in_channels=256,
|
||||
out_channels=512, # mean and variance
|
||||
model_channels=1024,
|
||||
contraction_dim=256,
|
||||
time_embed_dim=256,
|
||||
num_layers=8,
|
||||
rotary_emb_dim=32,
|
||||
input_cond_dim=1024,
|
||||
num_heads=8,
|
||||
dropout=0,
|
||||
use_fp16=False,
|
||||
# Parameters for regularization.
|
||||
unconditioned_percentage=.1, # This implements a mechanism similar to what is used in classifier-free training.
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.in_channels = in_channels
|
||||
self.model_channels = model_channels
|
||||
self.time_embed_dim = time_embed_dim
|
||||
self.out_channels = out_channels
|
||||
self.dropout = dropout
|
||||
self.unconditioned_percentage = unconditioned_percentage
|
||||
self.enable_fp16 = use_fp16
|
||||
|
||||
self.inp_block = conv_nd(1, in_channels, model_channels, 3, 1, 1)
|
||||
|
||||
self.time_embed = nn.Sequential(
|
||||
linear(time_embed_dim, time_embed_dim),
|
||||
nn.SiLU(),
|
||||
linear(time_embed_dim, time_embed_dim),
|
||||
)
|
||||
|
||||
self.conditioner = nn.Linear(input_cond_dim, model_channels) if input_cond_dim != model_channels else nn.Identity()
|
||||
self.unconditioned_embedding = nn.Parameter(torch.randn(1,1,model_channels))
|
||||
self.rotary_embeddings = RotaryEmbedding(rotary_emb_dim)
|
||||
self.layers = TimestepRotaryEmbedSequential(*[ConcatAttentionBlock(model_channels, contraction_dim, time_embed_dim, num_heads, dropout) for _ in range(num_layers)])
|
||||
|
||||
self.out = nn.Sequential(
|
||||
normalization(model_channels),
|
||||
nn.SiLU(),
|
||||
zero_module(conv_nd(1, model_channels, out_channels, 3, padding=1)),
|
||||
)
|
||||
|
||||
self.debug_codes = {}
|
||||
|
||||
def get_grad_norm_parameter_groups(self):
|
||||
attn1 = list(itertools.chain.from_iterable([lyr.block1.attn.parameters() for lyr in self.layers]))
|
||||
attn2 = list(itertools.chain.from_iterable([lyr.block2.attn.parameters() for lyr in self.layers]))
|
||||
ff1 = list(itertools.chain.from_iterable([lyr.block1.ff.parameters() for lyr in self.layers]))
|
||||
ff2 = list(itertools.chain.from_iterable([lyr.block2.ff.parameters() for lyr in self.layers]))
|
||||
blkout_layers = list(itertools.chain.from_iterable([lyr.out.parameters() for lyr in self.layers]))
|
||||
groups = {
|
||||
'prenorms': list(itertools.chain.from_iterable([lyr.prenorm.parameters() for lyr in self.layers])),
|
||||
'blk1_attention_layers': attn1,
|
||||
'blk2_attention_layers': attn2,
|
||||
'attention_layers': attn1 + attn2,
|
||||
'blk1_ff_layers': ff1,
|
||||
'blk2_ff_layers': ff2,
|
||||
'ff_layers': ff1 + ff2,
|
||||
'block_out_layers': blkout_layers,
|
||||
'rotary_embeddings': list(self.rotary_embeddings.parameters()),
|
||||
'out': list(self.out.parameters()),
|
||||
'x_proj': list(self.inp_block.parameters()),
|
||||
'layers': list(self.layers.parameters()),
|
||||
'time_embed': list(self.time_embed.parameters()),
|
||||
}
|
||||
return groups
|
||||
|
||||
def forward(self, x, timesteps, conditioning_input, conditioning_free=False):
|
||||
unused_params = []
|
||||
if conditioning_free:
|
||||
cond = self.unconditioned_embedding.repeat(x.shape[0], x.shape[-1], 1)
|
||||
else:
|
||||
cond = self.conditioner(conditioning_input)
|
||||
# Mask out the conditioning branch for whole batch elements, implementing something similar to classifier-free guidance.
|
||||
if self.training and self.unconditioned_percentage > 0:
|
||||
unconditioned_batches = torch.rand((cond.shape[0], 1, 1),
|
||||
device=cond.device) < self.unconditioned_percentage
|
||||
cond = torch.where(unconditioned_batches, self.unconditioned_embedding.repeat(cond.shape[0], 1, 1),
|
||||
cond)
|
||||
unused_params.append(self.unconditioned_embedding)
|
||||
|
||||
with torch.autocast(x.device.type, enabled=self.enable_fp16):
|
||||
blk_emb = self.time_embed(timestep_embedding(timesteps, self.time_embed_dim))
|
||||
x = self.inp_block(x).permute(0,2,1)
|
||||
|
||||
rotary_pos_emb = self.rotary_embeddings(x.shape[1]+1, x.device)
|
||||
for layer in self.layers:
|
||||
x = checkpoint(layer, x, cond, blk_emb, rotary_pos_emb)
|
||||
|
||||
x = x.float().permute(0,2,1)
|
||||
out = self.out(x)
|
||||
|
||||
# Involve probabilistic or possibly unused parameters in loss so we don't get DDP errors.
|
||||
extraneous_addition = 0
|
||||
for p in unused_params:
|
||||
extraneous_addition = extraneous_addition + p.mean()
|
||||
out = out + extraneous_addition * 0
|
||||
|
||||
return out
|
||||
|
||||
|
||||
class ConditioningEncoder(nn.Module):
|
||||
def __init__(self,
|
||||
cond_dim,
|
||||
embedding_dim,
|
||||
attn_blocks=6,
|
||||
num_attn_heads=8,
|
||||
do_checkpointing=False):
|
||||
super().__init__()
|
||||
attn = []
|
||||
self.init = nn.Conv1d(cond_dim, embedding_dim, kernel_size=1)
|
||||
for a in range(attn_blocks):
|
||||
attn.append(AttentionBlock(embedding_dim, num_attn_heads, do_checkpoint=do_checkpointing))
|
||||
self.attn = nn.Sequential(*attn)
|
||||
self.dim = embedding_dim
|
||||
self.do_checkpointing = do_checkpointing
|
||||
|
||||
def forward(self, x):
|
||||
h = self.init(x)
|
||||
h = self.attn(h)
|
||||
return h.mean(dim=2).unsqueeze(1)
|
||||
|
||||
|
||||
class TransformerDiffusionWithConditioningEncoder(nn.Module):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__()
|
||||
self.internal_step = 0
|
||||
self.diff = TransformerDiffusion(**kwargs)
|
||||
self.conditioning_encoder = ConditioningEncoder(256, kwargs['model_channels'])
|
||||
self.encoder = UpperEncoder(256, 1024, 256).eval()
|
||||
for p in self.encoder.parameters():
|
||||
p.DO_NOT_TRAIN = True
|
||||
p.requires_grad = False
|
||||
|
||||
def forward(self, x, timesteps, true_cheater, conditioning_input=None, disable_diversity=False, conditioning_free=False):
|
||||
cond = self.conditioning_encoder(true_cheater)
|
||||
diff = self.diff(x, timesteps, conditioning_input=cond, conditioning_free=conditioning_free)
|
||||
return diff
|
||||
|
||||
def get_debug_values(self, step, __):
|
||||
self.internal_step = step
|
||||
return {}
|
||||
|
||||
def get_grad_norm_parameter_groups(self):
|
||||
groups = self.diff.get_grad_norm_parameter_groups()
|
||||
groups['conditioning_encoder'] = list(self.conditioning_encoder.parameters())
|
||||
return
|
||||
|
||||
def before_step(self, step):
|
||||
scaled_grad_parameters = list(itertools.chain.from_iterable([lyr.out.parameters() for lyr in self.diff.layers])) + \
|
||||
list(itertools.chain.from_iterable([lyr.prenorm.parameters() for lyr in self.diff.layers]))
|
||||
# Scale back the gradients of the blkout and prenorm layers by a constant factor. These get two orders of magnitudes
|
||||
# higher gradients. Ideally we would use parameter groups, but ZeroRedundancyOptimizer makes this trickier than
|
||||
# directly fiddling with the gradients.
|
||||
for p in scaled_grad_parameters:
|
||||
if hasattr(p, 'grad') and p.grad is not None:
|
||||
p.grad *= .2
|
||||
|
||||
|
||||
@register_model
|
||||
def register_transformer_diffusion_with_point_conditioning(opt_net, opt):
|
||||
return TransformerDiffusion(**opt_net['kwargs'])
|
||||
|
||||
|
||||
@register_model
|
||||
def register_tfdpc_with_conditioning_encoder(opt_net, opt):
|
||||
return TransformerDiffusionWithConditioningEncoder(**opt_net['kwargs'])
|
||||
|
||||
|
||||
def test_cheater_model():
|
||||
clip = torch.randn(2, 256, 400)
|
||||
cl = torch.randn(2, 1, 400)
|
||||
ts = torch.LongTensor([600, 600])
|
||||
|
||||
# For music:
|
||||
model = TransformerDiffusionWithConditioningEncoder(model_channels=1024)
|
||||
print_network(model)
|
||||
o = model(clip, ts, cl)
|
||||
pg = model.get_grad_norm_parameter_groups()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_cheater_model()
|
|
@ -339,7 +339,7 @@ class Trainer:
|
|||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-opt', type=str, help='Path to option YAML file.', default='../options/train_music_gpt.yml')
|
||||
parser.add_argument('-opt', type=str, help='Path to option YAML file.', default='../options/train_music_cheater_gen.yml')
|
||||
parser.add_argument('--launcher', choices=['none', 'pytorch'], default='none', help='job launcher')
|
||||
args = parser.parse_args()
|
||||
opt = option.parse(args.opt, is_train=True)
|
||||
|
|
|
@ -378,5 +378,20 @@ class ChannelClipInjector(Injector):
|
|||
return {self.output: inp[:,self.lo:self.hi]}
|
||||
|
||||
|
||||
class MusicCheaterLatentInjector(Injector):
|
||||
def __init__(self, opt, env):
|
||||
super().__init__(opt, env)
|
||||
from models.audio.music.gpt_music2 import UpperEncoder
|
||||
self.encoder = UpperEncoder(256, 1024, 256).eval()
|
||||
self.encoder.load_state_dict(torch.load('../experiments/music_cheater_encoder_256.pth', map_location=torch.device('cpu')))
|
||||
|
||||
def forward(self, state):
|
||||
with torch.no_grad():
|
||||
mel = state[self.input]
|
||||
self.encoder = self.encoder.to(mel.device)
|
||||
proj = self.encoder(mel)
|
||||
return {self.output: proj}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('hi')
|
Loading…
Reference in New Issue
Block a user