skytnt commited on
Commit
57a4371
β€’
1 Parent(s): efe10fa

use numba jit instead of cython

Browse files
MoeGoe.py DELETED
@@ -1,119 +0,0 @@
1
- import sys
2
- from torch import no_grad, LongTensor
3
- import logging
4
-
5
- logging.getLogger('numba').setLevel(logging.WARNING)
6
-
7
- import commons
8
- import utils
9
- from models import SynthesizerTrn
10
- from text import text_to_sequence
11
- from mel_processing import spectrogram_torch
12
-
13
- from scipy.io.wavfile import write
14
-
15
- def get_text(text, hps):
16
- text_norm = text_to_sequence(text, hps_ms.symbols, hps.data.text_cleaners)
17
- if hps.data.add_blank:
18
- text_norm = commons.intersperse(text_norm, 0)
19
- text_norm = LongTensor(text_norm)
20
- return text_norm
21
-
22
- def ask_if_continue():
23
- while True:
24
- answer = input('Continue? (y/n): ')
25
- if answer == 'y':
26
- break
27
- elif answer == 'n':
28
- sys.exit(0)
29
-
30
- def print_speakers(speakers):
31
- print('ID\tSpeaker')
32
- for id, name in enumerate(speakers):
33
- print(str(id) + '\t' + name)
34
-
35
- def get_speaker_id(message):
36
- speaker_id = input(message)
37
- try:
38
- speaker_id = int(speaker_id)
39
- except:
40
- print(str(speaker_id) + ' is not a valid ID!')
41
- sys.exit(1)
42
- return speaker_id
43
-
44
- if __name__ == '__main__':
45
- model = input('Path of a VITS model: ')
46
- config = input('Path of a config file: ')
47
- try:
48
- hps_ms = utils.get_hparams_from_file(config)
49
- net_g_ms = SynthesizerTrn(
50
- len(hps_ms.symbols),
51
- hps_ms.data.filter_length // 2 + 1,
52
- hps_ms.train.segment_size // hps_ms.data.hop_length,
53
- n_speakers=hps_ms.data.n_speakers,
54
- **hps_ms.model)
55
- _ = net_g_ms.eval()
56
- _ = utils.load_checkpoint(model, net_g_ms, None)
57
- except:
58
- print('Failed to load!')
59
- sys.exit(1)
60
-
61
- while True:
62
- choice = input('TTS or VC? (t/v):')
63
- if choice == 't':
64
- text = input('Text to read: ')
65
- try:
66
- stn_tst = get_text(text, hps_ms)
67
- except:
68
- print('Invalid text!')
69
- sys.exit(1)
70
-
71
- print_speakers(hps_ms.speakers)
72
- speaker_id = get_speaker_id('Speaker ID: ')
73
-
74
- out_path = input('Path to save: ')
75
-
76
- try:
77
- with no_grad():
78
- x_tst = stn_tst.unsqueeze(0)
79
- x_tst_lengths = LongTensor([stn_tst.size(0)])
80
- sid = LongTensor([speaker_id])
81
- audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.cpu().float().numpy()
82
- write(out_path, hps_ms.data.sampling_rate, audio)
83
- except:
84
- print('Failed to generate!')
85
- sys.exit(1)
86
-
87
- print('Successfully saved!')
88
- ask_if_continue()
89
-
90
-
91
- elif choice == 'v':
92
- wav_path = input('Path of a WAV file (22050 Hz, 16 bits, 1 channel) to convert:\n')
93
- print_speakers(hps_ms.speakers)
94
- audio, sampling_rate = utils.load_wav_to_torch(wav_path)
95
-
96
- originnal_id = get_speaker_id('Original speaker ID: ')
97
- target_id = get_speaker_id('Target speaker ID: ')
98
- out_path = input('Path to save: ')
99
-
100
- y = audio / hps_ms.data.max_wav_value
101
- y = y.unsqueeze(0)
102
-
103
- spec = spectrogram_torch(y, hps_ms.data.filter_length,
104
- hps_ms.data.sampling_rate, hps_ms.data.hop_length, hps_ms.data.win_length,
105
- center=False)
106
- spec_lengths = LongTensor([spec.size(-1)])
107
- sid_src = LongTensor([originnal_id])
108
-
109
- try:
110
- with no_grad():
111
- sid_tgt = LongTensor([target_id])
112
- audio = net_g_ms.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt)[0][0,0].data.cpu().float().numpy()
113
- write(out_path, hps_ms.data.sampling_rate, audio)
114
- except:
115
- print('Failed to generate!')
116
- sys.exit(1)
117
-
118
- print('Successfully saved!')
119
- ask_if_continue()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,8 +1,4 @@
1
  import json
2
- import os
3
-
4
- os.system('cd monotonic_align && python setup.py build_ext --inplace && cd ..')
5
-
6
  import librosa
7
  import numpy as np
8
  import torch
 
1
  import json
 
 
 
 
2
  import librosa
3
  import numpy as np
4
  import torch
monotonic_align/__init__.py CHANGED
@@ -1,19 +1,21 @@
1
- import numpy as np
2
- import torch
3
- from .monotonic_align.core import maximum_path_c
 
4
 
5
 
6
  def maximum_path(neg_cent, mask):
7
- """ Cython optimized version.
8
- neg_cent: [b, t_t, t_s]
9
- mask: [b, t_t, t_s]
10
- """
11
- device = neg_cent.device
12
- dtype = neg_cent.dtype
13
- neg_cent = neg_cent.data.cpu().numpy().astype(np.float32)
14
- path = np.zeros(neg_cent.shape, dtype=np.int32)
 
 
 
 
 
15
 
16
- t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(np.int32)
17
- t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(np.int32)
18
- maximum_path_c(path, neg_cent, t_t_max, t_s_max)
19
- return torch.from_numpy(path).to(device=device, dtype=dtype)
 
1
+ from numpy import zeros, int32, float32
2
+ from torch import from_numpy
3
+
4
+ from .core import maximum_path_jit
5
 
6
 
7
  def maximum_path(neg_cent, mask):
8
+ """ numba optimized version.
9
+ neg_cent: [b, t_t, t_s]
10
+ mask: [b, t_t, t_s]
11
+ """
12
+ device = neg_cent.device
13
+ dtype = neg_cent.dtype
14
+ neg_cent = neg_cent.data.cpu().numpy().astype(float32)
15
+ path = zeros(neg_cent.shape, dtype=int32)
16
+
17
+ t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(int32)
18
+ t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(int32)
19
+ maximum_path_jit(path, neg_cent, t_t_max, t_s_max)
20
+ return from_numpy(path).to(device=device, dtype=dtype)
21
 
 
 
 
 
monotonic_align/core.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numba
2
+
3
+
4
+ @numba.jit(numba.void(numba.int32[:, :, ::1], numba.float32[:, :, ::1], numba.int32[::1], numba.int32[::1]),
5
+ nopython=True, nogil=True)
6
+ def maximum_path_jit(paths, values, t_ys, t_xs):
7
+ b = paths.shape[0]
8
+ max_neg_val = -1e9
9
+ for i in range(int(b)):
10
+ path = paths[i]
11
+ value = values[i]
12
+ t_y = t_ys[i]
13
+ t_x = t_xs[i]
14
+
15
+ v_prev = v_cur = 0.0
16
+ index = t_x - 1
17
+
18
+ for y in range(t_y):
19
+ for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
20
+ if x == y:
21
+ v_cur = max_neg_val
22
+ else:
23
+ v_cur = value[y - 1, x]
24
+ if x == 0:
25
+ if y == 0:
26
+ v_prev = 0.
27
+ else:
28
+ v_prev = max_neg_val
29
+ else:
30
+ v_prev = value[y - 1, x - 1]
31
+ value[y, x] += max(v_prev, v_cur)
32
+
33
+ for y in range(t_y - 1, -1, -1):
34
+ path[y, index] = 1
35
+ if index != 0 and (index == y or value[y - 1, index] < value[y - 1, index - 1]):
36
+ index = index - 1
monotonic_align/core.pyx DELETED
@@ -1,42 +0,0 @@
1
- cimport cython
2
- from cython.parallel import prange
3
-
4
-
5
- @cython.boundscheck(False)
6
- @cython.wraparound(False)
7
- cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil:
8
- cdef int x
9
- cdef int y
10
- cdef float v_prev
11
- cdef float v_cur
12
- cdef float tmp
13
- cdef int index = t_x - 1
14
-
15
- for y in range(t_y):
16
- for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
17
- if x == y:
18
- v_cur = max_neg_val
19
- else:
20
- v_cur = value[y-1, x]
21
- if x == 0:
22
- if y == 0:
23
- v_prev = 0.
24
- else:
25
- v_prev = max_neg_val
26
- else:
27
- v_prev = value[y-1, x-1]
28
- value[y, x] += max(v_prev, v_cur)
29
-
30
- for y in range(t_y - 1, -1, -1):
31
- path[y, index] = 1
32
- if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]):
33
- index = index - 1
34
-
35
-
36
- @cython.boundscheck(False)
37
- @cython.wraparound(False)
38
- cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_ys, int[::1] t_xs) nogil:
39
- cdef int b = paths.shape[0]
40
- cdef int i
41
- for i in prange(b, nogil=True):
42
- maximum_path_each(paths[i], values[i], t_ys[i], t_xs[i])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
monotonic_align/setup.py DELETED
@@ -1,9 +0,0 @@
1
- from distutils.core import setup
2
- from Cython.Build import cythonize
3
- import numpy
4
-
5
- setup(
6
- name = 'monotonic_align',
7
- ext_modules = cythonize("core.pyx"),
8
- include_dirs=[numpy.get_include()]
9
- )