Update extract_feature_print.py
Browse files- extract_feature_print.py +142 -6
extract_feature_print.py
CHANGED
|
@@ -3,6 +3,135 @@ from transformers import HubertModel
|
|
| 3 |
import os
|
| 4 |
from torch import nn
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
#HuggingFacePlaceHolder = None
|
| 7 |
class HubertModelWithFinalProj(HubertModel):
|
| 8 |
def __init__(self, config):
|
|
@@ -89,7 +218,8 @@ models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
|
|
| 89 |
if Custom_Embed == False:
|
| 90 |
model = models[0]
|
| 91 |
else:
|
| 92 |
-
|
|
|
|
| 93 |
model = model.to(device)
|
| 94 |
printt("move model to %s" % device)
|
| 95 |
if device not in ["mps", "cpu"]:
|
|
@@ -97,7 +227,7 @@ if device not in ["mps", "cpu"]:
|
|
| 97 |
model.eval()
|
| 98 |
|
| 99 |
todo = sorted(list(os.listdir(wavPath)))[i_part::n_part]
|
| 100 |
-
n = max(1, len(todo) // 10)
|
| 101 |
if len(todo) == 0:
|
| 102 |
printt("no-feature-todo")
|
| 103 |
else:
|
|
@@ -121,10 +251,16 @@ else:
|
|
| 121 |
"output_layer": 9 if version == "v1" else 12, # layer 9
|
| 122 |
}
|
| 123 |
with torch.no_grad():
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
feats = feats.squeeze(0).float().cpu().numpy()
|
| 130 |
if np.isnan(feats).sum() == 0:
|
|
|
|
| 3 |
import os
|
| 4 |
from torch import nn
|
| 5 |
|
| 6 |
+
import json
|
| 7 |
+
version_config_paths = [
|
| 8 |
+
os.path.join("/kaggle/working/Mangio-RVC-Fork/configs", "32k.json"),
|
| 9 |
+
os.path.join("/kaggle/working/Mangio-RVC-Fork/configs", "40k.json"),
|
| 10 |
+
os.path.join("/kaggle/working/Mangio-RVC-Fork/configs", "48k.json"),
|
| 11 |
+
os.path.join("/kaggle/working/Mangio-RVC-Fork/configs", "48k_v2.json"),
|
| 12 |
+
os.path.join("/kaggle/working/Mangio-RVC-Fork/configs", "40k.json"),
|
| 13 |
+
os.path.join("/kaggle/working/Mangio-RVC-Fork/configs", "32k_v2.json"),
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
class Config:
|
| 17 |
+
def __init__(self):
|
| 18 |
+
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
self.is_half = self.device != "cpu"
|
| 20 |
+
self.gpu_name = (
|
| 21 |
+
torch.cuda.get_device_name(int(self.device.split(":")[-1]))
|
| 22 |
+
if self.device.startswith("cuda")
|
| 23 |
+
else None
|
| 24 |
+
)
|
| 25 |
+
self.json_config = self.load_config_json()
|
| 26 |
+
self.gpu_mem = None
|
| 27 |
+
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
|
| 28 |
+
|
| 29 |
+
def load_config_json(self) -> dict:
|
| 30 |
+
configs = {}
|
| 31 |
+
for config_file in version_config_paths:
|
| 32 |
+
config_path = os.path.join("rvc", "configs", config_file)
|
| 33 |
+
with open(config_path, "r") as f:
|
| 34 |
+
configs[config_file] = json.load(f)
|
| 35 |
+
return configs
|
| 36 |
+
|
| 37 |
+
def has_mps(self) -> bool:
|
| 38 |
+
# Check if Metal Performance Shaders are available - for macOS 12.3+.
|
| 39 |
+
return torch.backends.mps.is_available()
|
| 40 |
+
|
| 41 |
+
def has_xpu(self) -> bool:
|
| 42 |
+
# Check if XPU is available.
|
| 43 |
+
return hasattr(torch, "xpu") and torch.xpu.is_available()
|
| 44 |
+
|
| 45 |
+
def set_precision(self, precision):
|
| 46 |
+
if precision not in ["fp32", "fp16"]:
|
| 47 |
+
raise ValueError("Invalid precision type. Must be 'fp32' or 'fp16'.")
|
| 48 |
+
|
| 49 |
+
fp16_run_value = precision == "fp16"
|
| 50 |
+
preprocess_target_version = "3.7" if precision == "fp16" else "3.0"
|
| 51 |
+
preprocess_path = os.path.join(
|
| 52 |
+
os.path.dirname(__file__),
|
| 53 |
+
os.pardir,
|
| 54 |
+
"rvc",
|
| 55 |
+
"train",
|
| 56 |
+
"preprocess",
|
| 57 |
+
"preprocess.py",
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
for config_path in version_config_paths:
|
| 61 |
+
full_config_path = os.path.join("rvc", "configs", config_path)
|
| 62 |
+
try:
|
| 63 |
+
with open(full_config_path, "r") as f:
|
| 64 |
+
config = json.load(f)
|
| 65 |
+
config["train"]["fp16_run"] = fp16_run_value
|
| 66 |
+
with open(full_config_path, "w") as f:
|
| 67 |
+
json.dump(config, f, indent=4)
|
| 68 |
+
except FileNotFoundError:
|
| 69 |
+
print(f"File not found: {full_config_path}")
|
| 70 |
+
|
| 71 |
+
if os.path.exists(preprocess_path):
|
| 72 |
+
with open(preprocess_path, "r") as f:
|
| 73 |
+
preprocess_content = f.read()
|
| 74 |
+
preprocess_content = preprocess_content.replace(
|
| 75 |
+
"3.0" if precision == "fp16" else "3.7", preprocess_target_version
|
| 76 |
+
)
|
| 77 |
+
with open(preprocess_path, "w") as f:
|
| 78 |
+
f.write(preprocess_content)
|
| 79 |
+
|
| 80 |
+
return f"Overwritten preprocess and config.json to use {precision}."
|
| 81 |
+
|
| 82 |
+
def get_precision(self):
|
| 83 |
+
if not version_config_paths:
|
| 84 |
+
raise FileNotFoundError("No configuration paths provided.")
|
| 85 |
+
|
| 86 |
+
full_config_path = os.path.join("rvc", "configs", version_config_paths[0])
|
| 87 |
+
try:
|
| 88 |
+
with open(full_config_path, "r") as f:
|
| 89 |
+
config = json.load(f)
|
| 90 |
+
fp16_run_value = config["train"].get("fp16_run", False)
|
| 91 |
+
precision = "fp16" if fp16_run_value else "fp32"
|
| 92 |
+
return precision
|
| 93 |
+
except FileNotFoundError:
|
| 94 |
+
print(f"File not found: {full_config_path}")
|
| 95 |
+
return None
|
| 96 |
+
|
| 97 |
+
def device_config(self) -> tuple:
|
| 98 |
+
if self.device.startswith("cuda"):
|
| 99 |
+
self.set_cuda_config()
|
| 100 |
+
elif self.has_mps():
|
| 101 |
+
self.device = "mps"
|
| 102 |
+
self.is_half = False
|
| 103 |
+
self.set_precision("fp32")
|
| 104 |
+
else:
|
| 105 |
+
self.device = "cpu"
|
| 106 |
+
self.is_half = False
|
| 107 |
+
self.set_precision("fp32")
|
| 108 |
+
|
| 109 |
+
# Configuration for 6GB GPU memory
|
| 110 |
+
x_pad, x_query, x_center, x_max = (
|
| 111 |
+
(3, 10, 60, 65) if self.is_half else (1, 6, 38, 41)
|
| 112 |
+
)
|
| 113 |
+
if self.gpu_mem is not None and self.gpu_mem <= 4:
|
| 114 |
+
# Configuration for 5GB GPU memory
|
| 115 |
+
x_pad, x_query, x_center, x_max = (1, 5, 30, 32)
|
| 116 |
+
|
| 117 |
+
return x_pad, x_query, x_center, x_max
|
| 118 |
+
|
| 119 |
+
def set_cuda_config(self):
|
| 120 |
+
i_device = int(self.device.split(":")[-1])
|
| 121 |
+
self.gpu_name = torch.cuda.get_device_name(i_device)
|
| 122 |
+
low_end_gpus = ["16", "P40", "P10", "1060", "1070", "1080"]
|
| 123 |
+
if (
|
| 124 |
+
any(gpu in self.gpu_name for gpu in low_end_gpus)
|
| 125 |
+
and "V100" not in self.gpu_name.upper()
|
| 126 |
+
):
|
| 127 |
+
self.is_half = False
|
| 128 |
+
self.set_precision("fp32")
|
| 129 |
+
|
| 130 |
+
self.gpu_mem = torch.cuda.get_device_properties(i_device).total_memory // (
|
| 131 |
+
1024**3
|
| 132 |
+
)
|
| 133 |
+
Config()
|
| 134 |
+
|
| 135 |
#HuggingFacePlaceHolder = None
|
| 136 |
class HubertModelWithFinalProj(HubertModel):
|
| 137 |
def __init__(self, config):
|
|
|
|
| 218 |
if Custom_Embed == False:
|
| 219 |
model = models[0]
|
| 220 |
else:
|
| 221 |
+
dtype = torch.float16 if config.is_half and "cuda" in device else torch.float32
|
| 222 |
+
model = HubertModelWithFinalProj.from_pretrained("/kaggle/working/Mangio-RVC-Fork/Custom/").to(dtype).to(device)
|
| 223 |
model = model.to(device)
|
| 224 |
printt("move model to %s" % device)
|
| 225 |
if device not in ["mps", "cpu"]:
|
|
|
|
| 227 |
model.eval()
|
| 228 |
|
| 229 |
todo = sorted(list(os.listdir(wavPath)))[i_part::n_part]
|
| 230 |
+
n = max(1, len(todo) // 10)
|
| 231 |
if len(todo) == 0:
|
| 232 |
printt("no-feature-todo")
|
| 233 |
else:
|
|
|
|
| 251 |
"output_layer": 9 if version == "v1" else 12, # layer 9
|
| 252 |
}
|
| 253 |
with torch.no_grad():
|
| 254 |
+
if Custom_Embed == False:
|
| 255 |
+
logits = model.extract_features(**inputs)
|
| 256 |
+
feats = (
|
| 257 |
+
model.final_proj(logits[0]) if version == "v1" else logits[0]
|
| 258 |
+
)
|
| 259 |
+
elif Custom_Embed == True:
|
| 260 |
+
feats = model(feats)["last_hidden_state"]
|
| 261 |
+
feats = (
|
| 262 |
+
model.final_proj(feats[0]).unsqueeze(0) if version == "v1" else feats
|
| 263 |
+
)
|
| 264 |
|
| 265 |
feats = feats.squeeze(0).float().cpu().numpy()
|
| 266 |
if np.isnan(feats).sum() == 0:
|