11import os
22import shutil
33import sys
4+
45now_dir = os .getcwd ()
56sys .path .append (now_dir )
6- import traceback ,pdb
7+ import traceback , pdb
78import warnings
89
910import numpy as np
@@ -396,7 +397,7 @@ def uvr(model_name, inp_root, save_root_vocal, paths, save_root_ins, agg, format
396397
397398
398399# 一个选项卡全局只能有一个音色
399- def get_vc (sid ,to_return_protect0 ,to_return_protect1 ):
400+ def get_vc (sid , to_return_protect0 , to_return_protect1 ):
400401 global n_spk , tgt_sr , net_g , vc , cpt , version
401402 if sid == "" or sid == []:
402403 global hubert_model
@@ -434,11 +435,23 @@ def get_vc(sid,to_return_protect0,to_return_protect1):
434435 tgt_sr = cpt ["config" ][- 1 ]
435436 cpt ["config" ][- 3 ] = cpt ["weight" ]["emb_g.weight" ].shape [0 ] # n_spk
436437 if_f0 = cpt .get ("f0" , 1 )
437- if (if_f0 == 0 ):
438- to_return_protect0 = to_return_protect1 = {"visible" : False , "value" : 0.5 , "__type__" : "update" }
438+ if if_f0 == 0 :
439+ to_return_protect0 = to_return_protect1 = {
440+ "visible" : False ,
441+ "value" : 0.5 ,
442+ "__type__" : "update" ,
443+ }
439444 else :
440- to_return_protect0 = {"visible" : True , "value" : to_return_protect0 , "__type__" : "update" }
441- to_return_protect1 = {"visible" : True , "value" : to_return_protect1 , "__type__" : "update" }
445+ to_return_protect0 = {
446+ "visible" : True ,
447+ "value" : to_return_protect0 ,
448+ "__type__" : "update" ,
449+ }
450+ to_return_protect1 = {
451+ "visible" : True ,
452+ "value" : to_return_protect1 ,
453+ "__type__" : "update" ,
454+ }
442455 version = cpt .get ("version" , "v1" )
443456 if version == "v1" :
444457 if if_f0 == 1 :
@@ -459,7 +472,11 @@ def get_vc(sid,to_return_protect0,to_return_protect1):
459472 net_g = net_g .float ()
460473 vc = VC (tgt_sr , config )
461474 n_spk = cpt ["config" ][- 3 ]
462- return {"visible" : True , "maximum" : n_spk , "__type__" : "update" },to_return_protect0 ,to_return_protect1
475+ return (
476+ {"visible" : True , "maximum" : n_spk , "__type__" : "update" },
477+ to_return_protect0 ,
478+ to_return_protect1 ,
479+ )
463480
464481
465482def change_choices ():
@@ -665,8 +682,13 @@ def change_sr2(sr2, if_f0_3, version19):
665682
666683def change_version19 (sr2 , if_f0_3 , version19 ):
667684 path_str = "" if version19 == "v1" else "_v2"
668- if (sr2 == "32k" and version19 == "v1" ):sr2 = "40k"
669- to_return_sr2 = {"choices" : ["40k" ,"48k" ], "__type__" : "update" } if version19 == "v1" else {"choices" : ["32k" ,"40k" ,"48k" ], "__type__" : "update" }
685+ if sr2 == "32k" and version19 == "v1" :
686+ sr2 = "40k"
687+ to_return_sr2 = (
688+ {"choices" : ["40k" , "48k" ], "__type__" : "update" }
689+ if version19 == "v1"
690+ else {"choices" : ["32k" , "40k" , "48k" ], "__type__" : "update" }
691+ )
670692 f0_str = "f0" if if_f0_3 else ""
671693 if_pretrained_generator_exist = os .access (
672694 "pretrained%s/%sG%s.pth" % (path_str , f0_str , sr2 ), os .F_OK
@@ -691,7 +713,7 @@ def change_version19(sr2, if_f0_3, version19):
691713 "pretrained%s/%sD%s.pth" % (path_str , f0_str , sr2 )
692714 if if_pretrained_discriminator_exist
693715 else "" ,
694- to_return_sr2
716+ to_return_sr2 ,
695717 )
696718
697719
@@ -893,14 +915,24 @@ def train_index(exp_dir1, version19):
893915 big_npy_idx = np .arange (big_npy .shape [0 ])
894916 np .random .shuffle (big_npy_idx )
895917 big_npy = big_npy [big_npy_idx ]
896- if ( big_npy .shape [0 ]> 2e5 ) :
897- # if(1):
898- infos .append ("Trying doing kmeans %s shape to 10k centers." % big_npy .shape [0 ])
918+ if big_npy .shape [0 ] > 2e5 :
919+ # if(1):
920+ infos .append ("Trying doing kmeans %s shape to 10k centers." % big_npy .shape [0 ])
899921 yield "\n " .join (infos )
900922 try :
901- big_npy = MiniBatchKMeans (n_clusters = 10000 , verbose = True , batch_size = 256 * config .n_cpu , compute_labels = False , init = "random" ).fit (big_npy ).cluster_centers_
923+ big_npy = (
924+ MiniBatchKMeans (
925+ n_clusters = 10000 ,
926+ verbose = True ,
927+ batch_size = 256 * config .n_cpu ,
928+ compute_labels = False ,
929+ init = "random" ,
930+ )
931+ .fit (big_npy )
932+ .cluster_centers_
933+ )
902934 except :
903- info = traceback .format_exc ()
935+ info = traceback .format_exc ()
904936 print (info )
905937 infos .append (info )
906938 yield "\n " .join (infos )
@@ -1147,15 +1179,25 @@ def get_info_str(strr):
11471179 np .random .shuffle (big_npy_idx )
11481180 big_npy = big_npy [big_npy_idx ]
11491181
1150- if ( big_npy .shape [0 ]> 2e5 ) :
1151- # if(1):
1152- info = "Trying doing kmeans %s shape to 10k centers." % big_npy .shape [0 ]
1182+ if big_npy .shape [0 ] > 2e5 :
1183+ # if(1):
1184+ info = "Trying doing kmeans %s shape to 10k centers." % big_npy .shape [0 ]
11531185 print (info )
11541186 yield get_info_str (info )
11551187 try :
1156- big_npy = MiniBatchKMeans (n_clusters = 10000 , verbose = True , batch_size = 256 * config .n_cpu , compute_labels = False , init = "random" ).fit (big_npy ).cluster_centers_
1188+ big_npy = (
1189+ MiniBatchKMeans (
1190+ n_clusters = 10000 ,
1191+ verbose = True ,
1192+ batch_size = 256 * config .n_cpu ,
1193+ compute_labels = False ,
1194+ init = "random" ,
1195+ )
1196+ .fit (big_npy )
1197+ .cluster_centers_
1198+ )
11571199 except :
1158- info = traceback .format_exc ()
1200+ info = traceback .format_exc ()
11591201 print (info )
11601202 yield get_info_str (info )
11611203
@@ -1207,11 +1249,10 @@ def change_info_(ckpt_path):
12071249 return {"__type__" : "update" }, {"__type__" : "update" }, {"__type__" : "update" }
12081250
12091251
1210-
12111252def export_onnx (ModelPath , ExportedPath ):
12121253 cpt = torch .load (ModelPath , map_location = "cpu" )
12131254 cpt ["config" ][- 3 ] = cpt ["weight" ]["emb_g.weight" ].shape [0 ]
1214- vec_channels = 256 if cpt .get ("version" ,"v1" )== "v1" else 768
1255+ vec_channels = 256 if cpt .get ("version" , "v1" ) == "v1" else 768
12151256
12161257 test_phone = torch .rand (1 , 200 , vec_channels ) # hidden unit
12171258 test_phone_lengths = torch .tensor ([200 ]).long () # hidden unit 长度(貌似没啥用)
@@ -1223,7 +1264,7 @@ def export_onnx(ModelPath, ExportedPath):
12231264 device = "cpu" # 导出时设备(不影响使用模型)
12241265
12251266 net_g = SynthesizerTrnMsNSFsidM (
1226- * cpt ["config" ], is_half = False ,version = cpt .get ("version" ,"v1" )
1267+ * cpt ["config" ], is_half = False , version = cpt .get ("version" , "v1" )
12271268 ) # fp32导出(C++要支持fp16必须手动将内存重新排列所以暂时不用fp16)
12281269 net_g .load_state_dict (cpt ["weight" ], strict = False )
12291270 input_names = ["phone" , "phone_lengths" , "pitch" , "pitchf" , "ds" , "rnd" ]
@@ -1504,8 +1545,8 @@ def export_onnx(ModelPath, ExportedPath):
15041545 )
15051546 sid0 .change (
15061547 fn = get_vc ,
1507- inputs = [sid0 ,protect0 ,protect1 ],
1508- outputs = [spk_item ,protect0 ,protect1 ],
1548+ inputs = [sid0 , protect0 , protect1 ],
1549+ outputs = [spk_item , protect0 , protect1 ],
15091550 )
15101551 with gr .TabItem (i18n ("伴奏人声分离&去混响&去回声" )):
15111552 with gr .Group ():
@@ -1604,7 +1645,7 @@ def export_onnx(ModelPath, ExportedPath):
16041645 maximum = config .n_cpu ,
16051646 step = 1 ,
16061647 label = i18n ("提取音高和处理数据使用的CPU进程数" ),
1607- value = int (np .ceil (config .n_cpu / 1.5 )),
1648+ value = int (np .ceil (config .n_cpu / 1.5 )),
16081649 interactive = True ,
16091650 )
16101651 with gr .Group (): # 暂时单人的, 后面支持最多4人的#数据处理
@@ -1722,7 +1763,7 @@ def export_onnx(ModelPath, ExportedPath):
17221763 version19 .change (
17231764 change_version19 ,
17241765 [sr2 , if_f0_3 , version19 ],
1725- [pretrained_G14 , pretrained_D15 ,sr2 ],
1766+ [pretrained_G14 , pretrained_D15 , sr2 ],
17261767 )
17271768 if_f0_3 .change (
17281769 change_f0 ,
@@ -1915,7 +1956,7 @@ def export_onnx(ModelPath, ExportedPath):
19151956 [ckpt_path2 , save_name , sr__ , if_f0__ , info___ , version_1 ],
19161957 info7 ,
19171958 )
1918-
1959+
19191960 with gr .TabItem (i18n ("Onnx导出" )):
19201961 with gr .Row ():
19211962 ckpt_dir = gr .Textbox (label = i18n ("RVC模型路径" ), value = "" , interactive = True )
0 commit comments