Hev832 commited on
Commit
7048940
·
verified ·
1 Parent(s): d39b142

Create createfile.py

Browse files
Files changed (1) hide show
  1. createfile.py +133 -0
createfile.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import faiss
4
+ from sklearn.cluster import MiniBatchKMeans
5
+ import traceback
6
+
7
+ # Set the working directory
8
+ os.chdir('/content/RVC')
9
+
10
+ # Parameters
11
+ model_name = 'My-Voice'
12
+ dataset_folder = '/content/dataset'
13
+
14
+ def calculate_audio_duration(file_path):
15
+ # Placeholder function - replace with actual implementation
16
+ return 0
17
+
18
+ # Check cache status based on audio duration
19
+ try:
20
+ duration = calculate_audio_duration(dataset_folder)
21
+ cache = duration < 600
22
+ except:
23
+ cache = False
24
+
25
+ # Ensure dataset folder is not empty
26
+ while len(os.listdir(dataset_folder)) < 1:
27
+ input("Your dataset folder is empty.")
28
+
29
+ os.makedirs(f'./logs/{model_name}', exist_ok=True)
30
+
31
+ # Run the preprocessing script
32
+ os.system(f'python infer/modules/train/preprocess.py {dataset_folder} 32000 2 ./logs/{model_name} False 3.0 > /dev/null 2>&1')
33
+
34
+ with open(f'./logs/{model_name}/preprocess.log', 'r') as f:
35
+ if 'end preprocess' in f.read():
36
+ print("✔ Success")
37
+ else:
38
+ print("Error preprocessing data... Make sure your dataset folder is correct.")
39
+
40
+ f0method = "rmvpe_gpu"
41
+
42
+ # Run the feature extraction scripts
43
+ if f0method != "rmvpe_gpu":
44
+ os.system(f'python infer/modules/train/extract/extract_f0_print.py ./logs/{model_name} 2 {f0method}')
45
+ else:
46
+ os.system(f'python infer/modules/train/extract/extract_f0_rmvpe.py 1 0 0 ./logs/{model_name} True')
47
+
48
+ os.system(f'python infer/modules/train/extract_feature_print.py cuda:0 1 0 ./logs/{model_name} v2 True')
49
+
50
+ with open(f'./logs/{model_name}/extract_f0_feature.log', 'r') as f:
51
+ if 'all-feature-done' in f.read():
52
+ print("✔ Success")
53
+ else:
54
+ print("Error preprocessing data... Make sure your data was preprocessed.")
55
+
56
+ def train_index(exp_dir1, version19):
57
+ exp_dir = f"logs/{exp_dir1}"
58
+ os.makedirs(exp_dir, exist_ok=True)
59
+ feature_dir = f"{exp_dir}/3_feature256" if version19 == "v1" else f"{exp_dir}/3_feature768"
60
+
61
+ if not os.path.exists(feature_dir):
62
+ return "请先进行特征提取!"
63
+
64
+ listdir_res = list(os.listdir(feature_dir))
65
+ if len(listdir_res) == 0:
66
+ return "请先进行特征提取!"
67
+
68
+ infos = []
69
+ npys = []
70
+
71
+ for name in sorted(listdir_res):
72
+ phone = np.load(f"{feature_dir}/{name}")
73
+ npys.append(phone)
74
+
75
+ big_npy = np.concatenate(npys, 0)
76
+ big_npy_idx = np.arange(big_npy.shape[0])
77
+ np.random.shuffle(big_npy_idx)
78
+ big_npy = big_npy[big_npy_idx]
79
+
80
+ if big_npy.shape[0] > 2e5:
81
+ infos.append(f"Trying doing kmeans {big_npy.shape[0]} shape to 10k centers.")
82
+ yield "\n".join(infos)
83
+
84
+ try:
85
+ big_npy = MiniBatchKMeans(
86
+ n_clusters=10000,
87
+ verbose=True,
88
+ batch_size=256,
89
+ compute_labels=False,
90
+ init="random"
91
+ ).fit(big_npy).cluster_centers_
92
+ except:
93
+ info = traceback.format_exc()
94
+ infos.append(info)
95
+ yield "\n".join(infos)
96
+
97
+ np.save(f"{exp_dir}/total_fea.npy", big_npy)
98
+ n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)
99
+ infos.append(f"{big_npy.shape},{n_ivf}")
100
+ yield "\n".join(infos)
101
+
102
+ index = faiss.index_factory(256 if version19 == "v1" else 768, f"IVF{n_ivf},Flat")
103
+ infos.append("training")
104
+ yield "\n".join(infos)
105
+
106
+ index_ivf = faiss.extract_index_ivf(index)
107
+ index_ivf.nprobe = 1
108
+ index.train(big_npy)
109
+ faiss.write_index(
110
+ index,
111
+ f"{exp_dir}/trained_IVF{n_ivf}_Flat_nprobe_{index_ivf.nprobe}_{exp_dir1}_{version19}.index"
112
+ )
113
+
114
+ infos.append("adding")
115
+ yield "\n".join(infos)
116
+
117
+ batch_size_add = 8192
118
+ for i in range(0, big_npy.shape[0], batch_size_add):
119
+ index.add(big_npy[i: i + batch_size_add])
120
+
121
+ faiss.write_index(
122
+ index,
123
+ f"{exp_dir}/added_IVF{n_ivf}_Flat_nprobe_{index_ivf.nprobe}_{exp_dir1}_{version19}.index"
124
+ )
125
+
126
+ infos.append(f"成功构建索引,added_IVF{n_ivf}_Flat_nprobe_{index_ivf.nprobe}_{exp_dir1}_{version19}.index")
127
+
128
+ training_log = train_index(model_name, 'v2')
129
+
130
+ for line in training_log:
131
+ print(line)
132
+ if 'adding' in line:
133
+ print("✔ Success")