PhoenixStormJr commited on
Commit
430d30a
·
verified ·
1 Parent(s): fb17485

Update extract_feature_print.py

Browse files
Files changed (1) hide show
  1. extract_feature_print.py +123 -123
extract_feature_print.py CHANGED
@@ -1,123 +1,123 @@
1
- import os, sys, traceback
2
-
3
- # device=sys.argv[1]
4
- n_part = int(sys.argv[2])
5
- i_part = int(sys.argv[3])
6
- if len(sys.argv) == 5:
7
- exp_dir = sys.argv[4]
8
- version = sys.argv[5]
9
- else:
10
- i_gpu = sys.argv[4]
11
- exp_dir = sys.argv[5]
12
- os.environ["CUDA_VISIBLE_DEVICES"] = str(i_gpu)
13
- version = sys.argv[6]
14
- import torch
15
- import torch.nn.functional as F
16
- import soundfile as sf
17
- import numpy as np
18
- from fairseq import checkpoint_utils
19
-
20
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
-
22
- if torch.cuda.is_available():
23
- device = "cuda"
24
- elif torch.backends.mps.is_available():
25
- device = "mps"
26
- else:
27
- device = "cpu"
28
-
29
- f = open("%s/extract_f0_feature.log" % exp_dir, "a+")
30
-
31
-
32
- def printt(strr):
33
- print(strr)
34
- f.write("%s\n" % strr)
35
- f.flush()
36
-
37
-
38
- printt(sys.argv)
39
- model_path = "hubert_base.pt"
40
-
41
- printt(exp_dir)
42
- wavPath = "%s/1_16k_wavs" % exp_dir
43
- outPath = (
44
- "%s/3_feature256" % exp_dir if version == "v1" else "%s/3_feature768" % exp_dir
45
- )
46
- os.makedirs(outPath, exist_ok=True)
47
-
48
-
49
- # wave must be 16k, hop_size=320
50
- def readwave(wav_path, normalize=False):
51
- wav, sr = sf.read(wav_path)
52
- assert sr == 16000
53
- feats = torch.from_numpy(wav).float()
54
- if feats.dim() == 2: # double channels
55
- feats = feats.mean(-1)
56
- assert feats.dim() == 1, feats.dim()
57
- if normalize:
58
- with torch.no_grad():
59
- feats = F.layer_norm(feats, feats.shape)
60
- feats = feats.view(1, -1)
61
- return feats
62
-
63
-
64
- # HuBERT model
65
- printt("load model(s) from {}".format(model_path))
66
- # if hubert model is exist
67
- if os.access(model_path, os.F_OK) == False:
68
- printt(
69
- "Error: Extracting is shut down because %s does not exist, you may download it from https://huggingface.co/lj1995/VoiceConversionWebUI/tree/main"
70
- % model_path
71
- )
72
- exit(0)
73
- models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
74
- [model_path],
75
- suffix="",
76
- )
77
- model = models[0]
78
- model = model.to(device)
79
- printt("move model to %s" % device)
80
- if device not in ["mps", "cpu"]:
81
- model = model.half()
82
- model.eval()
83
-
84
- todo = sorted(list(os.listdir(wavPath)))[i_part::n_part]
85
- n = max(1, len(todo) // 10) # 最多打印十条
86
- if len(todo) == 0:
87
- printt("no-feature-todo")
88
- else:
89
- printt("all-feature-%s" % len(todo))
90
- for idx, file in enumerate(todo):
91
- try:
92
- if file.endswith(".wav"):
93
- wav_path = "%s/%s" % (wavPath, file)
94
- out_path = "%s/%s" % (outPath, file.replace("wav", "npy"))
95
-
96
- if os.path.exists(out_path):
97
- continue
98
-
99
- feats = readwave(wav_path, normalize=saved_cfg.task.normalize)
100
- padding_mask = torch.BoolTensor(feats.shape).fill_(False)
101
- inputs = {
102
- "source": feats.half().to(device)
103
- if device not in ["mps", "cpu"]
104
- else feats.to(device),
105
- "padding_mask": padding_mask.to(device),
106
- "output_layer": 9 if version == "v1" else 12, # layer 9
107
- }
108
- with torch.no_grad():
109
- logits = model.extract_features(**inputs)
110
- feats = (
111
- model.final_proj(logits[0]) if version == "v1" else logits[0]
112
- )
113
-
114
- feats = feats.squeeze(0).float().cpu().numpy()
115
- if np.isnan(feats).sum() == 0:
116
- np.save(out_path, feats, allow_pickle=False)
117
- else:
118
- printt("%s-contains nan" % file)
119
- if idx % n == 0:
120
- printt("now-%s,all-%s,%s,%s" % (idx, len(todo), file, feats.shape))
121
- except:
122
- printt(traceback.format_exc())
123
- printt("all-feature-done")
 
1
+ import os, sys, traceback
2
+
3
+ # device=sys.argv[1]
4
+ n_part = int(sys.argv[2])
5
+ i_part = int(sys.argv[3])
6
+ if len(sys.argv) == 5:
7
+ exp_dir = sys.argv[4]
8
+ version = sys.argv[5]
9
+ else:
10
+ i_gpu = sys.argv[4]
11
+ exp_dir = sys.argv[5]
12
+ os.environ["CUDA_VISIBLE_DEVICES"] = str(i_gpu)
13
+ version = sys.argv[6]
14
+ import torch
15
+ import torch.nn.functional as F
16
+ import soundfile as sf
17
+ import numpy as np
18
+ from fairseq import checkpoint_utils
19
+
20
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
+
22
+ if torch.cuda.is_available():
23
+ device = "cuda"
24
+ elif torch.backends.mps.is_available():
25
+ device = "mps"
26
+ else:
27
+ device = "cpu"
28
+
29
+ f = open("%s/extract_f0_feature.log" % exp_dir, "a+")
30
+
31
+
32
+ def printt(strr):
33
+ print(strr)
34
+ f.write("%s\n" % strr)
35
+ f.flush()
36
+
37
+
38
+ printt(sys.argv)
39
+ model_path = "hubert_base.pt"
40
+
41
+ printt(exp_dir)
42
+ wavPath = "%s/1_16k_wavs" % exp_dir
43
+ outPath = (
44
+ "%s/3_feature256" % exp_dir if version == "v1" else "%s/3_feature768" % exp_dir
45
+ )
46
+ os.makedirs(outPath, exist_ok=True)
47
+
48
+
49
+ # wave must be 16k, hop_size=320
50
+ def readwave(wav_path, normalize=False):
51
+ wav, sr = sf.read(wav_path)
52
+ assert sr == 16000
53
+ feats = torch.from_numpy(wav).float()
54
+ if feats.dim() == 2: # double channels
55
+ feats = feats.mean(-1)
56
+ assert feats.dim() == 1, feats.dim()
57
+ if normalize:
58
+ with torch.no_grad():
59
+ feats = F.layer_norm(feats, feats.shape)
60
+ feats = feats.view(1, -1)
61
+ return feats
62
+
63
+
64
+ # HuBERT model
65
+ printt("load model(s) from {}".format(model_path))
66
+ # if hubert model is exist
67
+ if os.access(model_path, os.F_OK) == False:
68
+ printt(
69
+ "Error: Extracting is shut down because %s does not exist, you may download it from https://huggingface.co/lj1995/VoiceConversionWebUI/tree/main"
70
+ % model_path
71
+ )
72
+ exit(0)
73
+ models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
74
+ [model_path],
75
+ suffix="",
76
+ )
77
+ model = models[0]
78
+ model = model.to(device)
79
+ printt("move model to %s" % device)
80
+ if device not in ["mps", "cpu"]:
81
+ model = model.half()
82
+ model.eval()
83
+
84
+ todo = sorted(list(os.listdir(wavPath)))[i_part::n_part]
85
+ n = max(1, len(todo) // 10) # Print up to ten items
86
+ if len(todo) == 0:
87
+ printt("no-feature-todo")
88
+ else:
89
+ printt("all-feature-%s" % len(todo))
90
+ for idx, file in enumerate(todo):
91
+ try:
92
+ if file.endswith(".wav"):
93
+ wav_path = "%s/%s" % (wavPath, file)
94
+ out_path = "%s/%s" % (outPath, file.replace("wav", "npy"))
95
+
96
+ if os.path.exists(out_path):
97
+ continue
98
+
99
+ feats = readwave(wav_path, normalize=saved_cfg.task.normalize)
100
+ padding_mask = torch.BoolTensor(feats.shape).fill_(False)
101
+ inputs = {
102
+ "source": feats.half().to(device)
103
+ if device not in ["mps", "cpu"]
104
+ else feats.to(device),
105
+ "padding_mask": padding_mask.to(device),
106
+ "output_layer": 9 if version == "v1" else 12, # layer 9
107
+ }
108
+ with torch.no_grad():
109
+ logits = model.extract_features(**inputs)
110
+ feats = (
111
+ model.final_proj(logits[0]) if version == "v1" else logits[0]
112
+ )
113
+
114
+ feats = feats.squeeze(0).float().cpu().numpy()
115
+ if np.isnan(feats).sum() == 0:
116
+ np.save(out_path, feats, allow_pickle=False)
117
+ else:
118
+ printt("%s-contains nan" % file)
119
+ if idx % n == 0:
120
+ printt("now-%s,all-%s,%s,%s" % (idx, len(todo), file, feats.shape))
121
+ except:
122
+ printt(traceback.format_exc())
123
+ printt("all-feature-done")