Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New code #1

Open
wants to merge 2 commits into
base: old_code
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/kss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ model:
gmm: 10
---
data:
path: 'KSS'
path: ''
extension: '*.wav'
---
audio:
Expand Down
9 changes: 6 additions & 3 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from utils.hparams import HParam
from model.model import MelNet


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, required=True,
Expand Down Expand Up @@ -45,9 +44,13 @@
spectrogram = plot_spectrogram_to_numpy(generated[0].cpu().detach().numpy())
plt.imsave(os.path.join('temp', args.name + '.png'), spectrogram.transpose((1, 2, 0)))

waveform = Reconstruct(hp).inverse(generated[0]).unsqueeze(-1)
waveform, wavespec = Reconstruct(hp).inverse(generated[0])
wavespec = plot_spectrogram_to_numpy(wavespec.cpu().detach().numpy())
plt.imsave(os.path.join('temp', 'Final ' + args.name + '.png'), wavespec.transpose((1, 2, 0)))

waveform = waveform.unsqueeze(-1)
waveform = waveform.cpu().detach().numpy()
waveform *= 32768
waveform *= 32768 / waveform.max()
waveform = waveform.astype(np.int16)
audio = audiosegment.from_numpy_array(
waveform,
Expand Down
10 changes: 7 additions & 3 deletions model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ def forward(self, x, tier_num):
return self.tiers[tier_num](x)

def sample(self, condition):
x = torch.zeros(1, self.n_mels // self.f_div, self.args.timestep // self.t_div).cuda()
x = None
seq = torch.from_numpy(text_to_sequence(condition)).long().unsqueeze(0)
input_lengths = torch.LongTensor([seq[0].shape[0]]).cuda()

## Tier 1 ##
tqdm.write('Tier 1')
for t in tqdm(range(x.size(2))):
for m in tqdm(range(x.size(1))):
for t in tqdm(range(self.args.timestep // self.t_div)):
if x is None:
x = torch.zeros((1, self.n_mels // self.f_div, 1)).cuda()
else:
x = torch.cat([x, torch.zeros((1, self.n_mels // self.f_div, 1)).cuda()], dim=-1)
for m in tqdm(range(self.n_mels // self.f_div)):
torch.cuda.synchronize()
if self.infer_hp.conditional:
mu, std, pi, _ = self.tiers[1](x, seq, input_lengths)
Expand Down
15 changes: 11 additions & 4 deletions utils/reconstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ def __init__(self, hp):
self.hp = hp
self.window = torch.hann_window(window_length=hp.audio.win_length).cuda()
self.mel_basis = librosa.filters.mel(
sr=hp.audio.sr, n_fft=hp.audio.n_fft, n_mels=hp.audio.n_mels)
self.mel_basis = \
torch.from_numpy(self.mel_basis).float().cuda() # [n_mels, n_fft//2+1]
sr=hp.audio.sr,
n_fft=hp.audio.n_fft,
n_mels=hp.audio.n_mels
)
self.mel_basis = torch.from_numpy(self.mel_basis).cuda() # [n_mels, n_fft//2+1]
self.criterion = torch.nn.MSELoss()

def get_mel(self, x):
Expand All @@ -30,6 +32,11 @@ def post_spec(self, x):
x = (x - 1) * -self.hp.audio.min_level_db + self.hp.audio.ref_level_db
x = torch.pow(10, x / 10)
return x

def pre_spec(self, x):
x = torch.log10(x) * 10
x = (x - self.hp.audio.ref_level_db) / -self.hp.audio.min_level_db + 1
return x

def inverse(self, melspectrogram, iters=1000):
x = torch.normal(0, 1e-6, size=((melspectrogram.size(1) - 1) * self.hp.audio.hop_length, )).cuda().requires_grad_()
Expand All @@ -48,4 +55,4 @@ def closure():
optimizer.step(closure=closure)
pbar.set_postfix(loss=self.criterion(self.get_mel(x), melspectrogram).item())

return x
return x, self.pre_spec(self.get_mel(x))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'inverse' function now returns two values instead of one. Please ensure that this doesn't affect the functionality of the code where this function is called.

5 changes: 2 additions & 3 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ def get_commit_hash():
def read_wav_np(wavpath, sample_rate):
file_format = wavpath.split('.')[-1]
audio = audiosegment.from_file(wavpath).resample(sample_rate_Hz=sample_rate)
data = audio.raw_data
wav = np.frombuffer(data, dtype=np.int16)
wav = audio.to_numpy_array()

if len(wav.shape) == 2:
wav = wav[:, 0]
wav = wav.T.flatten()

if wav.dtype == np.int16:
wav = wav / 32768.0
Expand Down