initial upload attempt

initial upload attempt
This commit is contained in:
erew123 committed 2024-06-06 21:40:12 +01:00
1 parent 84c19ed5a9
commit 9fd928f6e7
527 files changed
+37359

No files matched your search

@@ -0,0 +1,14 @@
import os
import torch
def change_info(path, info, name):
try:
ckpt = torch.load(path, map_location="cpu")
ckpt["info"] = info
if name == "":
name = os.path.basename(path)
torch.save(ckpt, f"logs/{name}/{name}")
return "Success."
except Exception as error:
print(error)
@@ -0,0 +1,88 @@
import os
import sys
import faiss
import numpy as np
from sklearn.cluster import MiniBatchKMeans
from multiprocessing import cpu_count
exp_dir = sys.argv[1]
version = sys.argv[2]
try:
if version == "v1":
feature_dir = os.path.join(exp_dir, "3_feature256")
elif version == "v2":
feature_dir = os.path.join(exp_dir, "3_feature768")
npys = []
listdir_res = sorted(os.listdir(feature_dir))
for name in listdir_res:
file_path = os.path.join(feature_dir, name)
phone = np.load(file_path)
npys.append(phone)
big_npy = np.concatenate(npys, axis=0)
big_npy_idx = np.arange(big_npy.shape[0])
np.random.shuffle(big_npy_idx)
big_npy = big_npy[big_npy_idx]
if big_npy.shape[0] > 2e5:
big_npy = (
MiniBatchKMeans(
n_clusters=10000,
verbose=True,
batch_size=256 * cpu_count(),
compute_labels=False,
init="random",
)
.fit(big_npy)
.cluster_centers_
)
np.save(os.path.join(exp_dir, "total_fea.npy"), big_npy)
n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)
# index_trained
index_trained = faiss.index_factory(
256 if version == "v1" else 768, f"IVF{n_ivf},Flat"
)
index_ivf_trained = faiss.extract_index_ivf(index_trained)
index_ivf_trained.nprobe = 1
index_trained.train(big_npy)
index_filename_trained = (
f"trained_IVF{n_ivf}_Flat_nprobe_{index_ivf_trained.nprobe}_{version}.index"
)
index_filepath_trained = os.path.join(exp_dir, index_filename_trained)
faiss.write_index(index_trained, index_filepath_trained)
# index_added
index_added = faiss.index_factory(
256 if version == "v1" else 768, f"IVF{n_ivf},Flat"
)
index_ivf_added = faiss.extract_index_ivf(index_added)
index_ivf_added.nprobe = 1
index_added.train(big_npy)
index_filename_added = (
f"added_IVF{n_ivf}_Flat_nprobe_{index_ivf_added.nprobe}_{version}.index"
)
index_filepath_added = os.path.join(exp_dir, index_filename_added)
batch_size_add = 8192
for i in range(0, big_npy.shape[0], batch_size_add):
index_added.add(big_npy[i : i + batch_size_add])
faiss.write_index(index_added, index_filepath_added)
print(f"Saved index file '{index_filepath_added}'")
except Exception as error:
print(f"Failed to train index: {error}")
if "one array to concatenate" in str(error):
print(
"If you are running this code in a virtual environment, make sure you have enough GPU available to generate the Index file."
)
@@ -0,0 +1,82 @@
import os
import torch
import hashlib
import datetime
from collections import OrderedDict
def replace_keys_in_dict(d, old_key_part, new_key_part):
if isinstance(d, OrderedDict):
updated_dict = OrderedDict()
else:
updated_dict = {}
for key, value in d.items():
new_key = key.replace(old_key_part, new_key_part)
if isinstance(value, dict):
value = replace_keys_in_dict(value, old_key_part, new_key_part)
updated_dict[new_key] = value
return updated_dict
def extract_model(ckpt, sr, if_f0, name, model_dir, epoch, step, version, hps):
try:
print(f"Saved model '{model_dir}' (epoch {epoch} and step {step})")
pth_file = f"{name}_{epoch}e_{step}s.pth"
pth_file_old_version_path = os.path.join(
model_dir, f"{pth_file}_old_version.pth"
)
opt = OrderedDict(
weight={
key: value.half() for key, value in ckpt.items() if "enc_q" not in key
}
)
opt["config"] = [
hps.data.filter_length // 2 + 1,
32,
hps.model.inter_channels,
hps.model.hidden_channels,
hps.model.filter_channels,
hps.model.n_heads,
hps.model.n_layers,
hps.model.kernel_size,
hps.model.p_dropout,
hps.model.resblock,
hps.model.resblock_kernel_sizes,
hps.model.resblock_dilation_sizes,
hps.model.upsample_rates,
hps.model.upsample_initial_channel,
hps.model.upsample_kernel_sizes,
hps.model.spk_embed_dim,
hps.model.gin_channels,
hps.data.sampling_rate,
]
opt["epoch"] = epoch
opt["step"] = step
opt["sr"] = sr
opt["f0"] = if_f0
opt["version"] = version
opt["creation_date"] = datetime.datetime.now().isoformat()
hash_input = f"{str(ckpt)} {epoch} {step} {datetime.datetime.now().isoformat()}"
model_hash = hashlib.sha256(hash_input.encode()).hexdigest()
opt["model_hash"] = model_hash
torch.save(opt, model_dir)
model = torch.load(model_dir, map_location=torch.device("cpu"))
torch.save(
replace_keys_in_dict(
replace_keys_in_dict(
model, ".parametrizations.weight.original1", ".weight_v"
),
".parametrizations.weight.original0",
".weight_g",
),
pth_file_old_version_path,
)
os.remove(model_dir)
os.rename(pth_file_old_version_path, model_dir)
except Exception as error:
print(error)
@@ -0,0 +1,175 @@
import os
import torch
import hashlib
import datetime
from collections import OrderedDict
def replace_keys_in_dict(d, old_key_part, new_key_part):
# Use OrderedDict if the original is an OrderedDict
if isinstance(d, OrderedDict):
updated_dict = OrderedDict()
else:
updated_dict = {}
for key, value in d.items():
# Replace the key part if found
new_key = key.replace(old_key_part, new_key_part)
# If the value is a dictionary, apply the function recursively
if isinstance(value, dict):
value = replace_keys_in_dict(value, old_key_part, new_key_part)
updated_dict[new_key] = value
return updated_dict
def extract_small_model(path, name, sr, if_f0, version, epoch, step):
try:
ckpt = torch.load(path, map_location="cpu")
pth_file = f"{name}.pth"
pth_file_old_version_path = os.path.join("logs", f"{pth_file}_old_version.pth")
opt = OrderedDict(
weight={
key: value.half() for key, value in ckpt.items() if "enc_q" not in key
}
)
if "model" in ckpt:
ckpt = ckpt["model"]
opt = OrderedDict()
opt["weight"] = {}
for key in ckpt.keys():
if "enc_q" in key:
continue
opt["weight"][key] = ckpt[key].half()
if sr == "40000":
opt["config"] = [
1025,
32,
192,
192,
768,
2,
6,
3,
0,
"1",
[3, 7, 11],
[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
[10, 10, 2, 2],
512,
[16, 16, 4, 4],
109,
256,
40000,
]
elif sr == "48000":
if version == "v1":
opt["config"] = [
1025,
32,
192,
192,
768,
2,
6,
3,
0,
"1",
[3, 7, 11],
[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
[10, 6, 2, 2, 2],
512,
[16, 16, 4, 4, 4],
109,
256,
48000,
]
else:
opt["config"] = [
1025,
32,
192,
192,
768,
2,
6,
3,
0,
"1",
[3, 7, 11],
[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
[12, 10, 2, 2],
512,
[24, 20, 4, 4],
109,
256,
48000,
]
elif sr == "32000":
if version == "v1":
opt["config"] = [
513,
32,
192,
192,
768,
2,
6,
3,
0,
"1",
[3, 7, 11],
[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
[10, 4, 2, 2, 2],
512,
[16, 16, 4, 4, 4],
109,
256,
32000,
]
else:
opt["config"] = [
513,
32,
192,
192,
768,
2,
6,
3,
0,
"1",
[3, 7, 11],
[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
[10, 8, 2, 2],
512,
[20, 16, 4, 4],
109,
256,
32000,
]
opt["epoch"] = epoch
opt["step"] = step
opt["sr"] = sr
opt["f0"] = int(if_f0)
opt["version"] = version
opt["creation_date"] = datetime.datetime.now().isoformat()
hash_input = f"{str(ckpt)} {epoch} {step} {datetime.datetime.now().isoformat()}"
model_hash = hashlib.sha256(hash_input.encode()).hexdigest()
opt["model_hash"] = model_hash
model = torch.load(pth_file_old_version_path, map_location=torch.device("cpu"))
torch.save(
replace_keys_in_dict(
replace_keys_in_dict(
model, ".parametrizations.weight.original1", ".weight_v"
),
".parametrizations.weight.original0",
".weight_g",
),
pth_file_old_version_path,
)
os.remove(pth_file_old_version_path)
os.rename(pth_file_old_version_path, pth_file)
except Exception as error:
print(error)
@@ -0,0 +1,63 @@
import os
import torch
from collections import OrderedDict
def extract(ckpt):
a = ckpt["model"]
opt = OrderedDict()
opt["weight"] = {}
for key in a.keys():
if "enc_q" in key:
continue
opt["weight"][key] = a[key]
return opt
def model_blender(name, path1, path2, ratio):
try:
message = f"Model {path1} and {path2} are merged with alpha {ratio}."
ckpt1 = torch.load(path1, map_location="cpu")
ckpt2 = torch.load(path2, map_location="cpu")
cfg = ckpt1["config"]
cfg_f0 = ckpt1["f0"]
cfg_version = ckpt1["version"]
if "model" in ckpt1:
ckpt1 = extract(ckpt1)
else:
ckpt1 = ckpt1["weight"]
if "model" in ckpt2:
ckpt2 = extract(ckpt2)
else:
ckpt2 = ckpt2["weight"]
if sorted(list(ckpt1.keys())) != sorted(list(ckpt2.keys())):
return "Fail to merge the models. The model architectures are not the same."
opt = OrderedDict()
opt["weight"] = {}
for key in ckpt1.keys():
if key == "emb_g.weight" and ckpt1[key].shape != ckpt2[key].shape:
min_shape0 = min(ckpt1[key].shape[0], ckpt2[key].shape[0])
opt["weight"][key] = (
ratio * (ckpt1[key][:min_shape0].float())
+ (1 - ratio) * (ckpt2[key][:min_shape0].float())
).half()
else:
opt["weight"][key] = (
ratio * (ckpt1[key].float()) + (1 - ratio) * (ckpt2[key].float())
).half()
opt["config"] = cfg
opt["sr"] = message
opt["f0"] = cfg_f0
opt["version"] = cfg_version
opt["info"] = message
torch.save(opt, os.path.join("logs", "%s.pth" % name))
print(message)
return message, os.path.join("logs", "%s.pth" % name)
except Exception as error:
print(error)
return error
@@ -0,0 +1,40 @@
import torch
from datetime import datetime
def prettify_date(date_str):
if date_str is None:
return "None"
try:
date_time_obj = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%f")
return date_time_obj.strftime("%Y-%m-%d %H:%M:%S")
except ValueError:
return "Invalid date format"
def model_information(path):
model_data = torch.load(path, map_location="cpu")
print(f"Loaded model from {path}")
epochs = model_data.get("epoch", "None")
steps = model_data.get("step", "None")
sr = model_data.get("sr", "None")
f0 = model_data.get("f0", "None")
version = model_data.get("version", "None")
creation_date = model_data.get("creation_date", "None")
model_hash = model_data.get("model_hash", None)
pitch_guidance = "True" if f0 == 1 else "False"
creation_date_str = prettify_date(creation_date) if creation_date else "None"
return (
f"Epochs: {epochs}\n"
f"Steps: {steps}\n"
f"RVC Version: {version}\n"
f"Sampling Rate: {sr}\n"
f"Pitch Guidance: {pitch_guidance}\n"
f"Creation Date: {creation_date_str}\n"
f"Hash (ID): {model_hash}"
)