1
1
import os
2
2
import shutil
3
3
import sys
4
+
4
5
now_dir = os .getcwd ()
5
6
sys .path .append (now_dir )
6
- import traceback ,pdb
7
+ import traceback , pdb
7
8
import warnings
8
9
9
10
import numpy as np
@@ -396,7 +397,7 @@ def uvr(model_name, inp_root, save_root_vocal, paths, save_root_ins, agg, format
396
397
397
398
398
399
# 一个选项卡全局只能有一个音色
399
- def get_vc (sid ,to_return_protect0 ,to_return_protect1 ):
400
+ def get_vc (sid , to_return_protect0 , to_return_protect1 ):
400
401
global n_spk , tgt_sr , net_g , vc , cpt , version
401
402
if sid == "" or sid == []:
402
403
global hubert_model
@@ -434,11 +435,23 @@ def get_vc(sid,to_return_protect0,to_return_protect1):
434
435
tgt_sr = cpt ["config" ][- 1 ]
435
436
cpt ["config" ][- 3 ] = cpt ["weight" ]["emb_g.weight" ].shape [0 ] # n_spk
436
437
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
+ }
439
444
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
+ }
442
455
version = cpt .get ("version" , "v1" )
443
456
if version == "v1" :
444
457
if if_f0 == 1 :
@@ -459,7 +472,11 @@ def get_vc(sid,to_return_protect0,to_return_protect1):
459
472
net_g = net_g .float ()
460
473
vc = VC (tgt_sr , config )
461
474
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
+ )
463
480
464
481
465
482
def change_choices ():
@@ -665,8 +682,13 @@ def change_sr2(sr2, if_f0_3, version19):
665
682
666
683
def change_version19 (sr2 , if_f0_3 , version19 ):
667
684
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
+ )
670
692
f0_str = "f0" if if_f0_3 else ""
671
693
if_pretrained_generator_exist = os .access (
672
694
"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):
691
713
"pretrained%s/%sD%s.pth" % (path_str , f0_str , sr2 )
692
714
if if_pretrained_discriminator_exist
693
715
else "" ,
694
- to_return_sr2
716
+ to_return_sr2 ,
695
717
)
696
718
697
719
@@ -893,14 +915,24 @@ def train_index(exp_dir1, version19):
893
915
big_npy_idx = np .arange (big_npy .shape [0 ])
894
916
np .random .shuffle (big_npy_idx )
895
917
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 ])
899
921
yield "\n " .join (infos )
900
922
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
+ )
902
934
except :
903
- info = traceback .format_exc ()
935
+ info = traceback .format_exc ()
904
936
print (info )
905
937
infos .append (info )
906
938
yield "\n " .join (infos )
@@ -1147,15 +1179,25 @@ def get_info_str(strr):
1147
1179
np .random .shuffle (big_npy_idx )
1148
1180
big_npy = big_npy [big_npy_idx ]
1149
1181
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 ]
1153
1185
print (info )
1154
1186
yield get_info_str (info )
1155
1187
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
+ )
1157
1199
except :
1158
- info = traceback .format_exc ()
1200
+ info = traceback .format_exc ()
1159
1201
print (info )
1160
1202
yield get_info_str (info )
1161
1203
@@ -1207,11 +1249,10 @@ def change_info_(ckpt_path):
1207
1249
return {"__type__" : "update" }, {"__type__" : "update" }, {"__type__" : "update" }
1208
1250
1209
1251
1210
-
1211
1252
def export_onnx (ModelPath , ExportedPath ):
1212
1253
cpt = torch .load (ModelPath , map_location = "cpu" )
1213
1254
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
1215
1256
1216
1257
test_phone = torch .rand (1 , 200 , vec_channels ) # hidden unit
1217
1258
test_phone_lengths = torch .tensor ([200 ]).long () # hidden unit 长度(貌似没啥用)
@@ -1223,7 +1264,7 @@ def export_onnx(ModelPath, ExportedPath):
1223
1264
device = "cpu" # 导出时设备(不影响使用模型)
1224
1265
1225
1266
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" )
1227
1268
) # fp32导出(C++要支持fp16必须手动将内存重新排列所以暂时不用fp16)
1228
1269
net_g .load_state_dict (cpt ["weight" ], strict = False )
1229
1270
input_names = ["phone" , "phone_lengths" , "pitch" , "pitchf" , "ds" , "rnd" ]
@@ -1504,8 +1545,8 @@ def export_onnx(ModelPath, ExportedPath):
1504
1545
)
1505
1546
sid0 .change (
1506
1547
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 ],
1509
1550
)
1510
1551
with gr .TabItem (i18n ("伴奏人声分离&去混响&去回声" )):
1511
1552
with gr .Group ():
@@ -1604,7 +1645,7 @@ def export_onnx(ModelPath, ExportedPath):
1604
1645
maximum = config .n_cpu ,
1605
1646
step = 1 ,
1606
1647
label = i18n ("提取音高和处理数据使用的CPU进程数" ),
1607
- value = int (np .ceil (config .n_cpu / 1.5 )),
1648
+ value = int (np .ceil (config .n_cpu / 1.5 )),
1608
1649
interactive = True ,
1609
1650
)
1610
1651
with gr .Group (): # 暂时单人的, 后面支持最多4人的#数据处理
@@ -1722,7 +1763,7 @@ def export_onnx(ModelPath, ExportedPath):
1722
1763
version19 .change (
1723
1764
change_version19 ,
1724
1765
[sr2 , if_f0_3 , version19 ],
1725
- [pretrained_G14 , pretrained_D15 ,sr2 ],
1766
+ [pretrained_G14 , pretrained_D15 , sr2 ],
1726
1767
)
1727
1768
if_f0_3 .change (
1728
1769
change_f0 ,
@@ -1915,7 +1956,7 @@ def export_onnx(ModelPath, ExportedPath):
1915
1956
[ckpt_path2 , save_name , sr__ , if_f0__ , info___ , version_1 ],
1916
1957
info7 ,
1917
1958
)
1918
-
1959
+
1919
1960
with gr .TabItem (i18n ("Onnx导出" )):
1920
1961
with gr .Row ():
1921
1962
ckpt_dir = gr .Textbox (label = i18n ("RVC模型路径" ), value = "" , interactive = True )
0 commit comments