1- '''
1+ """
220416后的更新:
33 引入config中half
44 重建npy而不用填写
99 int16:
1010 增加无索引支持
1111 f0算法改harvest(怎么看就只有这个会影响CPU占用),但是不这么改效果不好
12- '''
12+ """
1313import os , sys , traceback
14+
1415now_dir = os .getcwd ()
1516sys .path .append (now_dir )
1617from config import Config
17- is_half = Config ().is_half
18+
19+ is_half = Config ().is_half
1820import PySimpleGUI as sg
1921import sounddevice as sd
2022import noisereduce as nr
2628import scipy .signal as signal
2729
2830# import matplotlib.pyplot as plt
29- from infer_pack .models import SynthesizerTrnMs256NSFsid , SynthesizerTrnMs256NSFsid_nono ,SynthesizerTrnMs768NSFsid ,SynthesizerTrnMs768NSFsid_nono
31+ from infer_pack .models import (
32+ SynthesizerTrnMs256NSFsid ,
33+ SynthesizerTrnMs256NSFsid_nono ,
34+ SynthesizerTrnMs768NSFsid ,
35+ SynthesizerTrnMs768NSFsid_nono ,
36+ )
3037from i18n import I18nAuto
3138
3239i18n = I18nAuto ()
@@ -63,7 +70,7 @@ def __init__(
6370 )
6471 self .model = models [0 ]
6572 self .model = self .model .to (device )
66- if ( is_half == True ) :
73+ if is_half == True :
6774 self .model = self .model .half ()
6875 else :
6976 self .model = self .model .float ()
@@ -75,21 +82,25 @@ def __init__(
7582 self .version = cpt .get ("version" , "v1" )
7683 if version == "v1" :
7784 if if_f0 == 1 :
78- self .net_g = SynthesizerTrnMs256NSFsid (* cpt ["config" ], is_half = config .is_half )
85+ self .net_g = SynthesizerTrnMs256NSFsid (
86+ * cpt ["config" ], is_half = config .is_half
87+ )
7988 else :
8089 self .net_g = SynthesizerTrnMs256NSFsid_nono (* cpt ["config" ])
8190 elif version == "v2" :
8291 if if_f0 == 1 :
83- self .net_g = SynthesizerTrnMs768NSFsid (* cpt ["config" ], is_half = config .is_half )
92+ self .net_g = SynthesizerTrnMs768NSFsid (
93+ * cpt ["config" ], is_half = config .is_half
94+ )
8495 else :
8596 self .net_g = SynthesizerTrnMs768NSFsid_nono (* cpt ["config" ])
8697 del self .net_g .enc_q
8798 print (self .net_g .load_state_dict (cpt ["weight" ], strict = False ))
8899 self .net_g .eval ().to (device )
89- if ( is_half == True ) :
90- self .net_g = self .net_g .half ()
100+ if is_half == True :
101+ self .net_g = self .net_g .half ()
91102 else :
92- self .net_g = self .net_g .float ()
103+ self .net_g = self .net_g .float ()
93104 except :
94105 print (traceback .format_exc ())
95106
@@ -151,15 +162,18 @@ def infer(self, feats: torch.Tensor) -> np.ndarray:
151162
152163 ####索引优化
153164 try :
154- if hasattr (self , "index" ) and hasattr (self , "big_npy" ) and self .index_rate != 0 :
165+ if (
166+ hasattr (self , "index" )
167+ and hasattr (self , "big_npy" )
168+ and self .index_rate != 0
169+ ):
155170 npy = feats [0 ].cpu ().numpy ().astype ("float32" )
156171 score , ix = self .index .search (npy , k = 8 )
157172 weight = np .square (1 / score )
158173 weight /= weight .sum (axis = 1 , keepdims = True )
159- npy = np .sum (
160- self .big_npy [ix ] * np .expand_dims (weight , axis = 2 ), axis = 1
161- )
162- if (is_half == True ):npy = npy .astype ("float16" )
174+ npy = np .sum (self .big_npy [ix ] * np .expand_dims (weight , axis = 2 ), axis = 1 )
175+ if is_half == True :
176+ npy = npy .astype ("float16" )
163177 feats = (
164178 torch .from_numpy (npy ).unsqueeze (0 ).to (device ) * self .index_rate
165179 + (1 - self .index_rate ) * feats
0 commit comments