Plana-Archive commited on
Commit
347d7a2
·
verified ·
1 Parent(s): 917e90a

Delete Blue-Archive/Library-VITS

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. Blue-Archive/Library-VITS/Arknights/LICENSE +0 -21
  2. Blue-Archive/Library-VITS/Arknights/cluster/__init__.py +0 -29
  3. Blue-Archive/Library-VITS/Arknights/cluster/train_cluster.py +0 -89
  4. Blue-Archive/Library-VITS/Arknights/configs_template/config_template.json +0 -65
  5. Blue-Archive/Library-VITS/Arknights/data_utils.py +0 -142
  6. Blue-Archive/Library-VITS/Arknights/flask_api.py +0 -56
  7. Blue-Archive/Library-VITS/Arknights/flask_api_full_song.py +0 -55
  8. Blue-Archive/Library-VITS/Arknights/hubert/__init__.py +0 -0
  9. Blue-Archive/Library-VITS/Arknights/hubert/checkpoint_best_legacy_500.pt +0 -3
  10. Blue-Archive/Library-VITS/Arknights/hubert/hubert_model.py +0 -222
  11. Blue-Archive/Library-VITS/Arknights/hubert/hubert_model_onnx.py +0 -217
  12. Blue-Archive/Library-VITS/Arknights/hubert/put_hubert_ckpt_here +0 -0
  13. Blue-Archive/Library-VITS/Arknights/inference/__init__.py +0 -0
  14. Blue-Archive/Library-VITS/Arknights/inference/infer_tool.py +0 -251
  15. Blue-Archive/Library-VITS/Arknights/inference/infer_tool_grad.py +0 -160
  16. Blue-Archive/Library-VITS/Arknights/inference/slicer.py +0 -142
  17. Blue-Archive/Library-VITS/Arknights/inference_main.py +0 -101
  18. Blue-Archive/Library-VITS/Arknights/models.py +0 -420
  19. Blue-Archive/Library-VITS/Arknights/models/Arknights/G_10400.pth +0 -3
  20. Blue-Archive/Library-VITS/Arknights/models/Arknights/config.json +0 -97
  21. Blue-Archive/Library-VITS/Arknights/modules/__init__.py +0 -0
  22. Blue-Archive/Library-VITS/Arknights/modules/attentions.py +0 -349
  23. Blue-Archive/Library-VITS/Arknights/modules/commons.py +0 -188
  24. Blue-Archive/Library-VITS/Arknights/modules/losses.py +0 -61
  25. Blue-Archive/Library-VITS/Arknights/modules/mel_processing.py +0 -112
  26. Blue-Archive/Library-VITS/Arknights/modules/modules.py +0 -342
  27. Blue-Archive/Library-VITS/Arknights/onnx_export.py +0 -53
  28. Blue-Archive/Library-VITS/Arknights/onnxexport/model_onnx.py +0 -335
  29. Blue-Archive/Library-VITS/Arknights/preprocess_flist_config.py +0 -83
  30. Blue-Archive/Library-VITS/Arknights/preprocess_hubert_f0.py +0 -62
  31. Blue-Archive/Library-VITS/Arknights/requirements_win.txt +0 -21
  32. Blue-Archive/Library-VITS/Arknights/resample.py +0 -48
  33. Blue-Archive/Library-VITS/Arknights/spec_gen.py +0 -22
  34. Blue-Archive/Library-VITS/Arknights/train.py +0 -310
  35. Blue-Archive/Library-VITS/Arknights/utils.py +0 -502
  36. Blue-Archive/Library-VITS/Arknights/vdecoder/__init__.py +0 -0
  37. Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/env.py +0 -15
  38. Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/models.py +0 -503
  39. Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/nvSTFT.py +0 -111
  40. Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/utils.py +0 -68
  41. Blue-Archive/Library-VITS/Arknights/wav_upload.py +0 -23
  42. Blue-Archive/Library-VITS/BanG-Dream/.pre-commit-config.yaml +0 -25
  43. Blue-Archive/Library-VITS/BanG-Dream/LICENSE +0 -661
  44. Blue-Archive/Library-VITS/BanG-Dream/attentions.py +0 -464
  45. Blue-Archive/Library-VITS/BanG-Dream/attentions_onnx.py +0 -378
  46. Blue-Archive/Library-VITS/BanG-Dream/bert_gen.py +0 -81
  47. Blue-Archive/Library-VITS/BanG-Dream/clap_gen.py +0 -64
  48. Blue-Archive/Library-VITS/BanG-Dream/clap_wrapper.py +0 -49
  49. Blue-Archive/Library-VITS/BanG-Dream/commons.py +0 -158
  50. Blue-Archive/Library-VITS/BanG-Dream/compress_model.py +0 -89
Blue-Archive/Library-VITS/Arknights/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Jingyi Li
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/cluster/__init__.py DELETED
@@ -1,29 +0,0 @@
1
- import numpy as np
2
- import torch
3
- from sklearn.cluster import KMeans
4
-
5
- def get_cluster_model(ckpt_path):
6
- checkpoint = torch.load(ckpt_path)
7
- kmeans_dict = {}
8
- for spk, ckpt in checkpoint.items():
9
- km = KMeans(ckpt["n_features_in_"])
10
- km.__dict__["n_features_in_"] = ckpt["n_features_in_"]
11
- km.__dict__["_n_threads"] = ckpt["_n_threads"]
12
- km.__dict__["cluster_centers_"] = ckpt["cluster_centers_"]
13
- kmeans_dict[spk] = km
14
- return kmeans_dict
15
-
16
- def get_cluster_result(model, x, speaker):
17
- """
18
- x: np.array [t, 256]
19
- return cluster class result
20
- """
21
- return model[speaker].predict(x)
22
-
23
- def get_cluster_center_result(model, x,speaker):
24
- """x: np.array [t, 256]"""
25
- predict = model[speaker].predict(x)
26
- return model[speaker].cluster_centers_[predict]
27
-
28
- def get_center(model, x,speaker):
29
- return model[speaker].cluster_centers_[x]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/cluster/train_cluster.py DELETED
@@ -1,89 +0,0 @@
1
- import os
2
- from glob import glob
3
- from pathlib import Path
4
- import torch
5
- import logging
6
- import argparse
7
- import torch
8
- import numpy as np
9
- from sklearn.cluster import KMeans, MiniBatchKMeans
10
- import tqdm
11
- logging.basicConfig(level=logging.INFO)
12
- logger = logging.getLogger(__name__)
13
- import time
14
- import random
15
-
16
- def train_cluster(in_dir, n_clusters, use_minibatch=True, verbose=False):
17
-
18
- logger.info(f"Loading features from {in_dir}")
19
- features = []
20
- nums = 0
21
- for path in tqdm.tqdm(in_dir.glob("*.soft.pt")):
22
- features.append(torch.load(path).squeeze(0).numpy().T)
23
- # print(features[-1].shape)
24
- features = np.concatenate(features, axis=0)
25
- print(nums, features.nbytes/ 1024**2, "MB , shape:",features.shape, features.dtype)
26
- features = features.astype(np.float32)
27
- logger.info(f"Clustering features of shape: {features.shape}")
28
- t = time.time()
29
- if use_minibatch:
30
- kmeans = MiniBatchKMeans(n_clusters=n_clusters,verbose=verbose, batch_size=4096, max_iter=80).fit(features)
31
- else:
32
- kmeans = KMeans(n_clusters=n_clusters,verbose=verbose).fit(features)
33
- print(time.time()-t, "s")
34
-
35
- x = {
36
- "n_features_in_": kmeans.n_features_in_,
37
- "_n_threads": kmeans._n_threads,
38
- "cluster_centers_": kmeans.cluster_centers_,
39
- }
40
- print("end")
41
-
42
- return x
43
-
44
-
45
- if __name__ == "__main__":
46
-
47
- parser = argparse.ArgumentParser()
48
- parser.add_argument('--dataset', type=Path, default="./dataset/44k",
49
- help='path of training data directory')
50
- parser.add_argument('--output', type=Path, default="logs/44k",
51
- help='path of model output directory')
52
-
53
- args = parser.parse_args()
54
-
55
- checkpoint_dir = args.output
56
- dataset = args.dataset
57
- n_clusters = 10000
58
-
59
- ckpt = {}
60
- for spk in os.listdir(dataset):
61
- if os.path.isdir(dataset/spk):
62
- print(f"train kmeans for {spk}...")
63
- in_dir = dataset/spk
64
- x = train_cluster(in_dir, n_clusters, verbose=False)
65
- ckpt[spk] = x
66
-
67
- checkpoint_path = checkpoint_dir / f"kmeans_{n_clusters}.pt"
68
- checkpoint_path.parent.mkdir(exist_ok=True, parents=True)
69
- torch.save(
70
- ckpt,
71
- checkpoint_path,
72
- )
73
-
74
-
75
- # import cluster
76
- # for spk in tqdm.tqdm(os.listdir("dataset")):
77
- # if os.path.isdir(f"dataset/{spk}"):
78
- # print(f"start kmeans inference for {spk}...")
79
- # for feature_path in tqdm.tqdm(glob(f"dataset/{spk}/*.discrete.npy", recursive=True)):
80
- # mel_path = feature_path.replace(".discrete.npy",".mel.npy")
81
- # mel_spectrogram = np.load(mel_path)
82
- # feature_len = mel_spectrogram.shape[-1]
83
- # c = np.load(feature_path)
84
- # c = utils.tools.repeat_expand_2d(torch.FloatTensor(c), feature_len).numpy()
85
- # feature = c.T
86
- # feature_class = cluster.get_cluster_result(feature, spk)
87
- # np.save(feature_path.replace(".discrete.npy", ".discrete_class.npy"), feature_class)
88
-
89
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/configs_template/config_template.json DELETED
@@ -1,65 +0,0 @@
1
- {
2
- "train": {
3
- "log_interval": 200,
4
- "eval_interval": 800,
5
- "seed": 1234,
6
- "epochs": 10000,
7
- "learning_rate": 0.0001,
8
- "betas": [
9
- 0.8,
10
- 0.99
11
- ],
12
- "eps": 1e-09,
13
- "batch_size": 6,
14
- "fp16_run": false,
15
- "lr_decay": 0.999875,
16
- "segment_size": 10240,
17
- "init_lr_ratio": 1,
18
- "warmup_epochs": 0,
19
- "c_mel": 45,
20
- "c_kl": 1.0,
21
- "use_sr": true,
22
- "max_speclen": 512,
23
- "port": "8001",
24
- "keep_ckpts": 3
25
- },
26
- "data": {
27
- "training_files": "filelists/train.txt",
28
- "validation_files": "filelists/val.txt",
29
- "max_wav_value": 32768.0,
30
- "sampling_rate": 44100,
31
- "filter_length": 2048,
32
- "hop_length": 512,
33
- "win_length": 2048,
34
- "n_mel_channels": 80,
35
- "mel_fmin": 0.0,
36
- "mel_fmax": 22050
37
- },
38
- "model": {
39
- "inter_channels": 192,
40
- "hidden_channels": 192,
41
- "filter_channels": 768,
42
- "n_heads": 2,
43
- "n_layers": 6,
44
- "kernel_size": 3,
45
- "p_dropout": 0.1,
46
- "resblock": "1",
47
- "resblock_kernel_sizes": [3,7,11],
48
- "resblock_dilation_sizes": [[1,3,5], [1,3,5], [1,3,5]],
49
- "upsample_rates": [ 8, 8, 2, 2, 2],
50
- "upsample_initial_channel": 512,
51
- "upsample_kernel_sizes": [16,16, 4, 4, 4],
52
- "n_layers_q": 3,
53
- "use_spectral_norm": false,
54
- "gin_channels": 256,
55
- "ssl_dim": 256,
56
- "n_speakers": 200
57
- },
58
- "spk": {
59
- "nyaru": 0,
60
- "huiyu": 1,
61
- "nen": 2,
62
- "paimon": 3,
63
- "yunhao": 4
64
- }
65
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/data_utils.py DELETED
@@ -1,142 +0,0 @@
1
- import time
2
- import os
3
- import random
4
- import numpy as np
5
- import torch
6
- import torch.utils.data
7
-
8
- import modules.commons as commons
9
- import utils
10
- from modules.mel_processing import spectrogram_torch, spec_to_mel_torch
11
- from utils import load_wav_to_torch, load_filepaths_and_text
12
-
13
- # import h5py
14
-
15
-
16
- """Multi speaker version"""
17
-
18
-
19
- class TextAudioSpeakerLoader(torch.utils.data.Dataset):
20
- """
21
- 1) loads audio, speaker_id, text pairs
22
- 2) normalizes text and converts them to sequences of integers
23
- 3) computes spectrograms from audio files.
24
- """
25
-
26
- def __init__(self, audiopaths, hparams):
27
- self.audiopaths = load_filepaths_and_text(audiopaths)
28
- self.max_wav_value = hparams.data.max_wav_value
29
- self.sampling_rate = hparams.data.sampling_rate
30
- self.filter_length = hparams.data.filter_length
31
- self.hop_length = hparams.data.hop_length
32
- self.win_length = hparams.data.win_length
33
- self.sampling_rate = hparams.data.sampling_rate
34
- self.use_sr = hparams.train.use_sr
35
- self.spec_len = hparams.train.max_speclen
36
- self.spk_map = hparams.spk
37
-
38
- random.seed(1234)
39
- random.shuffle(self.audiopaths)
40
-
41
- def get_audio(self, filename):
42
- filename = filename.replace("\\", "/")
43
- audio, sampling_rate = load_wav_to_torch(filename)
44
- if sampling_rate != self.sampling_rate:
45
- raise ValueError("{} SR doesn't match target {} SR".format(
46
- sampling_rate, self.sampling_rate))
47
- audio_norm = audio / self.max_wav_value
48
- audio_norm = audio_norm.unsqueeze(0)
49
- spec_filename = filename.replace(".wav", ".spec.pt")
50
- if os.path.exists(spec_filename):
51
- spec = torch.load(spec_filename)
52
- else:
53
- spec = spectrogram_torch(audio_norm, self.filter_length,
54
- self.sampling_rate, self.hop_length, self.win_length,
55
- center=False)
56
- spec = torch.squeeze(spec, 0)
57
- torch.save(spec, spec_filename)
58
-
59
- spk = filename.split("/")[-2]
60
- spk = torch.LongTensor([self.spk_map[spk]])
61
-
62
- f0 = np.load(filename + ".f0.npy")
63
- f0, uv = utils.interpolate_f0(f0)
64
- f0 = torch.FloatTensor(f0)
65
- uv = torch.FloatTensor(uv)
66
-
67
- c = torch.load(filename+ ".soft.pt")
68
- c = utils.repeat_expand_2d(c.squeeze(0), f0.shape[0])
69
-
70
-
71
- lmin = min(c.size(-1), spec.size(-1))
72
- assert abs(c.size(-1) - spec.size(-1)) < 3, (c.size(-1), spec.size(-1), f0.shape, filename)
73
- assert abs(audio_norm.shape[1]-lmin * self.hop_length) < 3 * self.hop_length
74
- spec, c, f0, uv = spec[:, :lmin], c[:, :lmin], f0[:lmin], uv[:lmin]
75
- audio_norm = audio_norm[:, :lmin * self.hop_length]
76
- # if spec.shape[1] < 30:
77
- # print("skip too short audio:", filename)
78
- # return None
79
- if spec.shape[1] > 800:
80
- start = random.randint(0, spec.shape[1]-800)
81
- end = start + 790
82
- spec, c, f0, uv = spec[:, start:end], c[:, start:end], f0[start:end], uv[start:end]
83
- audio_norm = audio_norm[:, start * self.hop_length : end * self.hop_length]
84
-
85
- return c, f0, spec, audio_norm, spk, uv
86
-
87
- def __getitem__(self, index):
88
- return self.get_audio(self.audiopaths[index][0])
89
-
90
- def __len__(self):
91
- return len(self.audiopaths)
92
-
93
-
94
- class TextAudioCollate:
95
-
96
- def __call__(self, batch):
97
- batch = [b for b in batch if b is not None]
98
-
99
- input_lengths, ids_sorted_decreasing = torch.sort(
100
- torch.LongTensor([x[0].shape[1] for x in batch]),
101
- dim=0, descending=True)
102
-
103
- max_c_len = max([x[0].size(1) for x in batch])
104
- max_wav_len = max([x[3].size(1) for x in batch])
105
-
106
- lengths = torch.LongTensor(len(batch))
107
-
108
- c_padded = torch.FloatTensor(len(batch), batch[0][0].shape[0], max_c_len)
109
- f0_padded = torch.FloatTensor(len(batch), max_c_len)
110
- spec_padded = torch.FloatTensor(len(batch), batch[0][2].shape[0], max_c_len)
111
- wav_padded = torch.FloatTensor(len(batch), 1, max_wav_len)
112
- spkids = torch.LongTensor(len(batch), 1)
113
- uv_padded = torch.FloatTensor(len(batch), max_c_len)
114
-
115
- c_padded.zero_()
116
- spec_padded.zero_()
117
- f0_padded.zero_()
118
- wav_padded.zero_()
119
- uv_padded.zero_()
120
-
121
- for i in range(len(ids_sorted_decreasing)):
122
- row = batch[ids_sorted_decreasing[i]]
123
-
124
- c = row[0]
125
- c_padded[i, :, :c.size(1)] = c
126
- lengths[i] = c.size(1)
127
-
128
- f0 = row[1]
129
- f0_padded[i, :f0.size(0)] = f0
130
-
131
- spec = row[2]
132
- spec_padded[i, :, :spec.size(1)] = spec
133
-
134
- wav = row[3]
135
- wav_padded[i, :, :wav.size(1)] = wav
136
-
137
- spkids[i, 0] = row[4]
138
-
139
- uv = row[5]
140
- uv_padded[i, :uv.size(0)] = uv
141
-
142
- return c_padded, f0_padded, spec_padded, wav_padded, spkids, lengths, uv_padded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/flask_api.py DELETED
@@ -1,56 +0,0 @@
1
- import io
2
- import logging
3
-
4
- import soundfile
5
- import torch
6
- import torchaudio
7
- from flask import Flask, request, send_file
8
- from flask_cors import CORS
9
-
10
- from inference.infer_tool import Svc, RealTimeVC
11
-
12
- app = Flask(__name__)
13
-
14
- CORS(app)
15
-
16
- logging.getLogger('numba').setLevel(logging.WARNING)
17
-
18
-
19
- @app.route("/voiceChangeModel", methods=["POST"])
20
- def voice_change_model():
21
- request_form = request.form
22
- wave_file = request.files.get("sample", None)
23
- # 变调信息
24
- f_pitch_change = float(request_form.get("fPitchChange", 0))
25
- # DAW所需的采样率
26
- daw_sample = int(float(request_form.get("sampleRate", 0)))
27
- speaker_id = int(float(request_form.get("sSpeakId", 0)))
28
- # http获得wav文件并转换
29
- input_wav_path = io.BytesIO(wave_file.read())
30
-
31
- # 模型推理
32
- if raw_infer:
33
- out_audio, out_sr = svc_model.infer(speaker_id, f_pitch_change, input_wav_path)
34
- tar_audio = torchaudio.functional.resample(out_audio, svc_model.target_sample, daw_sample)
35
- else:
36
- out_audio = svc.process(svc_model, speaker_id, f_pitch_change, input_wav_path)
37
- tar_audio = torchaudio.functional.resample(torch.from_numpy(out_audio), svc_model.target_sample, daw_sample)
38
- # 返回音频
39
- out_wav_path = io.BytesIO()
40
- soundfile.write(out_wav_path, tar_audio.cpu().numpy(), daw_sample, format="wav")
41
- out_wav_path.seek(0)
42
- return send_file(out_wav_path, download_name="temp.wav", as_attachment=True)
43
-
44
-
45
- if __name__ == '__main__':
46
- # 启用则为直接切片合成,False为交叉淡化方式
47
- # vst插件调整0.3-0.5s切片时间可以降低延迟,直接切片方法会有连接处爆音、交叉淡化会有轻微重叠声音
48
- # 自行选择能接受的方法,或将vst最大切片时间调整为1s,此处设为Ture,延迟大音质稳定一些
49
- raw_infer = True
50
- # 每个模型和config是唯一对应的
51
- model_name = "logs/32k/G_174000-Copy1.pth"
52
- config_name = "configs/config.json"
53
- svc_model = Svc(model_name, config_name)
54
- svc = RealTimeVC()
55
- # 此处与vst插件对应,不建议更改
56
- app.run(port=6842, host="0.0.0.0", debug=False, threaded=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/flask_api_full_song.py DELETED
@@ -1,55 +0,0 @@
1
- import io
2
- import numpy as np
3
- import soundfile
4
- from flask import Flask, request, send_file
5
-
6
- from inference import infer_tool
7
- from inference import slicer
8
-
9
- app = Flask(__name__)
10
-
11
-
12
- @app.route("/wav2wav", methods=["POST"])
13
- def wav2wav():
14
- request_form = request.form
15
- audio_path = request_form.get("audio_path", None) # wav文件地址
16
- tran = int(float(request_form.get("tran", 0))) # 音调
17
- spk = request_form.get("spk", 0) # 说话人(id或者name都可以,具体看你的config)
18
- wav_format = request_form.get("wav_format", 'wav') # 范围文件格式
19
- infer_tool.format_wav(audio_path)
20
- chunks = slicer.cut(audio_path, db_thresh=-40)
21
- audio_data, audio_sr = slicer.chunks2audio(audio_path, chunks)
22
-
23
- audio = []
24
- for (slice_tag, data) in audio_data:
25
- print(f'#=====segment start, {round(len(data) / audio_sr, 3)}s======')
26
-
27
- length = int(np.ceil(len(data) / audio_sr * svc_model.target_sample))
28
- if slice_tag:
29
- print('jump empty segment')
30
- _audio = np.zeros(length)
31
- else:
32
- # padd
33
- pad_len = int(audio_sr * 0.5)
34
- data = np.concatenate([np.zeros([pad_len]), data, np.zeros([pad_len])])
35
- raw_path = io.BytesIO()
36
- soundfile.write(raw_path, data, audio_sr, format="wav")
37
- raw_path.seek(0)
38
- out_audio, out_sr = svc_model.infer(spk, tran, raw_path)
39
- svc_model.clear_empty()
40
- _audio = out_audio.cpu().numpy()
41
- pad_len = int(svc_model.target_sample * 0.5)
42
- _audio = _audio[pad_len:-pad_len]
43
-
44
- audio.extend(list(infer_tool.pad_array(_audio, length)))
45
- out_wav_path = io.BytesIO()
46
- soundfile.write(out_wav_path, audio, svc_model.target_sample, format=wav_format)
47
- out_wav_path.seek(0)
48
- return send_file(out_wav_path, download_name=f"temp.{wav_format}", as_attachment=True)
49
-
50
-
51
- if __name__ == '__main__':
52
- model_name = "logs/44k/G_60000.pth" # 模型地址
53
- config_name = "configs/config.json" # config地址
54
- svc_model = infer_tool.Svc(model_name, config_name)
55
- app.run(port=1145, host="0.0.0.0", debug=False, threaded=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/hubert/__init__.py DELETED
File without changes
Blue-Archive/Library-VITS/Arknights/hubert/checkpoint_best_legacy_500.pt DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:60d936ec5a566776fc392e69ad8b630d14eb588111233fe313436e200a7b187b
3
- size 1330114945
 
 
 
 
Blue-Archive/Library-VITS/Arknights/hubert/hubert_model.py DELETED
@@ -1,222 +0,0 @@
1
- import copy
2
- import random
3
- from typing import Optional, Tuple
4
-
5
- import torch
6
- import torch.nn as nn
7
- import torch.nn.functional as t_func
8
- from torch.nn.modules.utils import consume_prefix_in_state_dict_if_present
9
-
10
-
11
- class Hubert(nn.Module):
12
- def __init__(self, num_label_embeddings: int = 100, mask: bool = True):
13
- super().__init__()
14
- self._mask = mask
15
- self.feature_extractor = FeatureExtractor()
16
- self.feature_projection = FeatureProjection()
17
- self.positional_embedding = PositionalConvEmbedding()
18
- self.norm = nn.LayerNorm(768)
19
- self.dropout = nn.Dropout(0.1)
20
- self.encoder = TransformerEncoder(
21
- nn.TransformerEncoderLayer(
22
- 768, 12, 3072, activation="gelu", batch_first=True
23
- ),
24
- 12,
25
- )
26
- self.proj = nn.Linear(768, 256)
27
-
28
- self.masked_spec_embed = nn.Parameter(torch.FloatTensor(768).uniform_())
29
- self.label_embedding = nn.Embedding(num_label_embeddings, 256)
30
-
31
- def mask(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
32
- mask = None
33
- if self.training and self._mask:
34
- mask = _compute_mask((x.size(0), x.size(1)), 0.8, 10, x.device, 2)
35
- x[mask] = self.masked_spec_embed.to(x.dtype)
36
- return x, mask
37
-
38
- def encode(
39
- self, x: torch.Tensor, layer: Optional[int] = None
40
- ) -> Tuple[torch.Tensor, torch.Tensor]:
41
- x = self.feature_extractor(x)
42
- x = self.feature_projection(x.transpose(1, 2))
43
- x, mask = self.mask(x)
44
- x = x + self.positional_embedding(x)
45
- x = self.dropout(self.norm(x))
46
- x = self.encoder(x, output_layer=layer)
47
- return x, mask
48
-
49
- def logits(self, x: torch.Tensor) -> torch.Tensor:
50
- logits = torch.cosine_similarity(
51
- x.unsqueeze(2),
52
- self.label_embedding.weight.unsqueeze(0).unsqueeze(0),
53
- dim=-1,
54
- )
55
- return logits / 0.1
56
-
57
- def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
58
- x, mask = self.encode(x)
59
- x = self.proj(x)
60
- logits = self.logits(x)
61
- return logits, mask
62
-
63
-
64
- class HubertSoft(Hubert):
65
- def __init__(self):
66
- super().__init__()
67
-
68
- @torch.inference_mode()
69
- def units(self, wav: torch.Tensor) -> torch.Tensor:
70
- wav = t_func.pad(wav, ((400 - 320) // 2, (400 - 320) // 2))
71
- x, _ = self.encode(wav)
72
- return self.proj(x)
73
-
74
-
75
- class FeatureExtractor(nn.Module):
76
- def __init__(self):
77
- super().__init__()
78
- self.conv0 = nn.Conv1d(1, 512, 10, 5, bias=False)
79
- self.norm0 = nn.GroupNorm(512, 512)
80
- self.conv1 = nn.Conv1d(512, 512, 3, 2, bias=False)
81
- self.conv2 = nn.Conv1d(512, 512, 3, 2, bias=False)
82
- self.conv3 = nn.Conv1d(512, 512, 3, 2, bias=False)
83
- self.conv4 = nn.Conv1d(512, 512, 3, 2, bias=False)
84
- self.conv5 = nn.Conv1d(512, 512, 2, 2, bias=False)
85
- self.conv6 = nn.Conv1d(512, 512, 2, 2, bias=False)
86
-
87
- def forward(self, x: torch.Tensor) -> torch.Tensor:
88
- x = t_func.gelu(self.norm0(self.conv0(x)))
89
- x = t_func.gelu(self.conv1(x))
90
- x = t_func.gelu(self.conv2(x))
91
- x = t_func.gelu(self.conv3(x))
92
- x = t_func.gelu(self.conv4(x))
93
- x = t_func.gelu(self.conv5(x))
94
- x = t_func.gelu(self.conv6(x))
95
- return x
96
-
97
-
98
- class FeatureProjection(nn.Module):
99
- def __init__(self):
100
- super().__init__()
101
- self.norm = nn.LayerNorm(512)
102
- self.projection = nn.Linear(512, 768)
103
- self.dropout = nn.Dropout(0.1)
104
-
105
- def forward(self, x: torch.Tensor) -> torch.Tensor:
106
- x = self.norm(x)
107
- x = self.projection(x)
108
- x = self.dropout(x)
109
- return x
110
-
111
-
112
- class PositionalConvEmbedding(nn.Module):
113
- def __init__(self):
114
- super().__init__()
115
- self.conv = nn.Conv1d(
116
- 768,
117
- 768,
118
- kernel_size=128,
119
- padding=128 // 2,
120
- groups=16,
121
- )
122
- self.conv = nn.utils.weight_norm(self.conv, name="weight", dim=2)
123
-
124
- def forward(self, x: torch.Tensor) -> torch.Tensor:
125
- x = self.conv(x.transpose(1, 2))
126
- x = t_func.gelu(x[:, :, :-1])
127
- return x.transpose(1, 2)
128
-
129
-
130
- class TransformerEncoder(nn.Module):
131
- def __init__(
132
- self, encoder_layer: nn.TransformerEncoderLayer, num_layers: int
133
- ) -> None:
134
- super(TransformerEncoder, self).__init__()
135
- self.layers = nn.ModuleList(
136
- [copy.deepcopy(encoder_layer) for _ in range(num_layers)]
137
- )
138
- self.num_layers = num_layers
139
-
140
- def forward(
141
- self,
142
- src: torch.Tensor,
143
- mask: torch.Tensor = None,
144
- src_key_padding_mask: torch.Tensor = None,
145
- output_layer: Optional[int] = None,
146
- ) -> torch.Tensor:
147
- output = src
148
- for layer in self.layers[:output_layer]:
149
- output = layer(
150
- output, src_mask=mask, src_key_padding_mask=src_key_padding_mask
151
- )
152
- return output
153
-
154
-
155
- def _compute_mask(
156
- shape: Tuple[int, int],
157
- mask_prob: float,
158
- mask_length: int,
159
- device: torch.device,
160
- min_masks: int = 0,
161
- ) -> torch.Tensor:
162
- batch_size, sequence_length = shape
163
-
164
- if mask_length < 1:
165
- raise ValueError("`mask_length` has to be bigger than 0.")
166
-
167
- if mask_length > sequence_length:
168
- raise ValueError(
169
- f"`mask_length` has to be smaller than `sequence_length`, but got `mask_length`: {mask_length} and `sequence_length`: {sequence_length}`"
170
- )
171
-
172
- # compute number of masked spans in batch
173
- num_masked_spans = int(mask_prob * sequence_length / mask_length + random.random())
174
- num_masked_spans = max(num_masked_spans, min_masks)
175
-
176
- # make sure num masked indices <= sequence_length
177
- if num_masked_spans * mask_length > sequence_length:
178
- num_masked_spans = sequence_length // mask_length
179
-
180
- # SpecAugment mask to fill
181
- mask = torch.zeros((batch_size, sequence_length), device=device, dtype=torch.bool)
182
-
183
- # uniform distribution to sample from, make sure that offset samples are < sequence_length
184
- uniform_dist = torch.ones(
185
- (batch_size, sequence_length - (mask_length - 1)), device=device
186
- )
187
-
188
- # get random indices to mask
189
- mask_indices = torch.multinomial(uniform_dist, num_masked_spans)
190
-
191
- # expand masked indices to masked spans
192
- mask_indices = (
193
- mask_indices.unsqueeze(dim=-1)
194
- .expand((batch_size, num_masked_spans, mask_length))
195
- .reshape(batch_size, num_masked_spans * mask_length)
196
- )
197
- offsets = (
198
- torch.arange(mask_length, device=device)[None, None, :]
199
- .expand((batch_size, num_masked_spans, mask_length))
200
- .reshape(batch_size, num_masked_spans * mask_length)
201
- )
202
- mask_idxs = mask_indices + offsets
203
-
204
- # scatter indices to mask
205
- mask = mask.scatter(1, mask_idxs, True)
206
-
207
- return mask
208
-
209
-
210
- def hubert_soft(
211
- path: str,
212
- ) -> HubertSoft:
213
- r"""HuBERT-Soft from `"A Comparison of Discrete and Soft Speech Units for Improved Voice Conversion"`.
214
- Args:
215
- path (str): path of a pretrained model
216
- """
217
- hubert = HubertSoft()
218
- checkpoint = torch.load(path)
219
- consume_prefix_in_state_dict_if_present(checkpoint, "module.")
220
- hubert.load_state_dict(checkpoint)
221
- hubert.eval()
222
- return hubert
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/hubert/hubert_model_onnx.py DELETED
@@ -1,217 +0,0 @@
1
- import copy
2
- import random
3
- from typing import Optional, Tuple
4
-
5
- import torch
6
- import torch.nn as nn
7
- import torch.nn.functional as t_func
8
- from torch.nn.modules.utils import consume_prefix_in_state_dict_if_present
9
-
10
-
11
- class Hubert(nn.Module):
12
- def __init__(self, num_label_embeddings: int = 100, mask: bool = True):
13
- super().__init__()
14
- self._mask = mask
15
- self.feature_extractor = FeatureExtractor()
16
- self.feature_projection = FeatureProjection()
17
- self.positional_embedding = PositionalConvEmbedding()
18
- self.norm = nn.LayerNorm(768)
19
- self.dropout = nn.Dropout(0.1)
20
- self.encoder = TransformerEncoder(
21
- nn.TransformerEncoderLayer(
22
- 768, 12, 3072, activation="gelu", batch_first=True
23
- ),
24
- 12,
25
- )
26
- self.proj = nn.Linear(768, 256)
27
-
28
- self.masked_spec_embed = nn.Parameter(torch.FloatTensor(768).uniform_())
29
- self.label_embedding = nn.Embedding(num_label_embeddings, 256)
30
-
31
- def mask(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
32
- mask = None
33
- if self.training and self._mask:
34
- mask = _compute_mask((x.size(0), x.size(1)), 0.8, 10, x.device, 2)
35
- x[mask] = self.masked_spec_embed.to(x.dtype)
36
- return x, mask
37
-
38
- def encode(
39
- self, x: torch.Tensor, layer: Optional[int] = None
40
- ) -> Tuple[torch.Tensor, torch.Tensor]:
41
- x = self.feature_extractor(x)
42
- x = self.feature_projection(x.transpose(1, 2))
43
- x, mask = self.mask(x)
44
- x = x + self.positional_embedding(x)
45
- x = self.dropout(self.norm(x))
46
- x = self.encoder(x, output_layer=layer)
47
- return x, mask
48
-
49
- def logits(self, x: torch.Tensor) -> torch.Tensor:
50
- logits = torch.cosine_similarity(
51
- x.unsqueeze(2),
52
- self.label_embedding.weight.unsqueeze(0).unsqueeze(0),
53
- dim=-1,
54
- )
55
- return logits / 0.1
56
-
57
-
58
- class HubertSoft(Hubert):
59
- def __init__(self):
60
- super().__init__()
61
-
62
- def units(self, wav: torch.Tensor) -> torch.Tensor:
63
- wav = t_func.pad(wav, ((400 - 320) // 2, (400 - 320) // 2))
64
- x, _ = self.encode(wav)
65
- return self.proj(x)
66
-
67
- def forward(self, x):
68
- return self.units(x)
69
-
70
- class FeatureExtractor(nn.Module):
71
- def __init__(self):
72
- super().__init__()
73
- self.conv0 = nn.Conv1d(1, 512, 10, 5, bias=False)
74
- self.norm0 = nn.GroupNorm(512, 512)
75
- self.conv1 = nn.Conv1d(512, 512, 3, 2, bias=False)
76
- self.conv2 = nn.Conv1d(512, 512, 3, 2, bias=False)
77
- self.conv3 = nn.Conv1d(512, 512, 3, 2, bias=False)
78
- self.conv4 = nn.Conv1d(512, 512, 3, 2, bias=False)
79
- self.conv5 = nn.Conv1d(512, 512, 2, 2, bias=False)
80
- self.conv6 = nn.Conv1d(512, 512, 2, 2, bias=False)
81
-
82
- def forward(self, x: torch.Tensor) -> torch.Tensor:
83
- x = t_func.gelu(self.norm0(self.conv0(x)))
84
- x = t_func.gelu(self.conv1(x))
85
- x = t_func.gelu(self.conv2(x))
86
- x = t_func.gelu(self.conv3(x))
87
- x = t_func.gelu(self.conv4(x))
88
- x = t_func.gelu(self.conv5(x))
89
- x = t_func.gelu(self.conv6(x))
90
- return x
91
-
92
-
93
- class FeatureProjection(nn.Module):
94
- def __init__(self):
95
- super().__init__()
96
- self.norm = nn.LayerNorm(512)
97
- self.projection = nn.Linear(512, 768)
98
- self.dropout = nn.Dropout(0.1)
99
-
100
- def forward(self, x: torch.Tensor) -> torch.Tensor:
101
- x = self.norm(x)
102
- x = self.projection(x)
103
- x = self.dropout(x)
104
- return x
105
-
106
-
107
- class PositionalConvEmbedding(nn.Module):
108
- def __init__(self):
109
- super().__init__()
110
- self.conv = nn.Conv1d(
111
- 768,
112
- 768,
113
- kernel_size=128,
114
- padding=128 // 2,
115
- groups=16,
116
- )
117
- self.conv = nn.utils.weight_norm(self.conv, name="weight", dim=2)
118
-
119
- def forward(self, x: torch.Tensor) -> torch.Tensor:
120
- x = self.conv(x.transpose(1, 2))
121
- x = t_func.gelu(x[:, :, :-1])
122
- return x.transpose(1, 2)
123
-
124
-
125
- class TransformerEncoder(nn.Module):
126
- def __init__(
127
- self, encoder_layer: nn.TransformerEncoderLayer, num_layers: int
128
- ) -> None:
129
- super(TransformerEncoder, self).__init__()
130
- self.layers = nn.ModuleList(
131
- [copy.deepcopy(encoder_layer) for _ in range(num_layers)]
132
- )
133
- self.num_layers = num_layers
134
-
135
- def forward(
136
- self,
137
- src: torch.Tensor,
138
- mask: torch.Tensor = None,
139
- src_key_padding_mask: torch.Tensor = None,
140
- output_layer: Optional[int] = None,
141
- ) -> torch.Tensor:
142
- output = src
143
- for layer in self.layers[:output_layer]:
144
- output = layer(
145
- output, src_mask=mask, src_key_padding_mask=src_key_padding_mask
146
- )
147
- return output
148
-
149
-
150
- def _compute_mask(
151
- shape: Tuple[int, int],
152
- mask_prob: float,
153
- mask_length: int,
154
- device: torch.device,
155
- min_masks: int = 0,
156
- ) -> torch.Tensor:
157
- batch_size, sequence_length = shape
158
-
159
- if mask_length < 1:
160
- raise ValueError("`mask_length` has to be bigger than 0.")
161
-
162
- if mask_length > sequence_length:
163
- raise ValueError(
164
- f"`mask_length` has to be smaller than `sequence_length`, but got `mask_length`: {mask_length} and `sequence_length`: {sequence_length}`"
165
- )
166
-
167
- # compute number of masked spans in batch
168
- num_masked_spans = int(mask_prob * sequence_length / mask_length + random.random())
169
- num_masked_spans = max(num_masked_spans, min_masks)
170
-
171
- # make sure num masked indices <= sequence_length
172
- if num_masked_spans * mask_length > sequence_length:
173
- num_masked_spans = sequence_length // mask_length
174
-
175
- # SpecAugment mask to fill
176
- mask = torch.zeros((batch_size, sequence_length), device=device, dtype=torch.bool)
177
-
178
- # uniform distribution to sample from, make sure that offset samples are < sequence_length
179
- uniform_dist = torch.ones(
180
- (batch_size, sequence_length - (mask_length - 1)), device=device
181
- )
182
-
183
- # get random indices to mask
184
- mask_indices = torch.multinomial(uniform_dist, num_masked_spans)
185
-
186
- # expand masked indices to masked spans
187
- mask_indices = (
188
- mask_indices.unsqueeze(dim=-1)
189
- .expand((batch_size, num_masked_spans, mask_length))
190
- .reshape(batch_size, num_masked_spans * mask_length)
191
- )
192
- offsets = (
193
- torch.arange(mask_length, device=device)[None, None, :]
194
- .expand((batch_size, num_masked_spans, mask_length))
195
- .reshape(batch_size, num_masked_spans * mask_length)
196
- )
197
- mask_idxs = mask_indices + offsets
198
-
199
- # scatter indices to mask
200
- mask = mask.scatter(1, mask_idxs, True)
201
-
202
- return mask
203
-
204
-
205
- def hubert_soft(
206
- path: str,
207
- ) -> HubertSoft:
208
- r"""HuBERT-Soft from `"A Comparison of Discrete and Soft Speech Units for Improved Voice Conversion"`.
209
- Args:
210
- path (str): path of a pretrained model
211
- """
212
- hubert = HubertSoft()
213
- checkpoint = torch.load(path)
214
- consume_prefix_in_state_dict_if_present(checkpoint, "module.")
215
- hubert.load_state_dict(checkpoint)
216
- hubert.eval()
217
- return hubert
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/hubert/put_hubert_ckpt_here DELETED
File without changes
Blue-Archive/Library-VITS/Arknights/inference/__init__.py DELETED
File without changes
Blue-Archive/Library-VITS/Arknights/inference/infer_tool.py DELETED
@@ -1,251 +0,0 @@
1
- import hashlib
2
- import io
3
- import json
4
- import logging
5
- import os
6
- import time
7
- from pathlib import Path
8
- from inference import slicer
9
-
10
- import librosa
11
- import numpy as np
12
- # import onnxruntime
13
- import parselmouth
14
- import soundfile
15
- import torch
16
- import torchaudio
17
-
18
- import cluster
19
- from hubert import hubert_model
20
- import utils
21
- from models import SynthesizerTrn
22
-
23
- logging.getLogger('matplotlib').setLevel(logging.WARNING)
24
-
25
-
26
- def read_temp(file_name):
27
- if not os.path.exists(file_name):
28
- with open(file_name, "w") as f:
29
- f.write(json.dumps({"info": "temp_dict"}))
30
- return {}
31
- else:
32
- try:
33
- with open(file_name, "r") as f:
34
- data = f.read()
35
- data_dict = json.loads(data)
36
- if os.path.getsize(file_name) > 50 * 1024 * 1024:
37
- f_name = file_name.replace("\\", "/").split("/")[-1]
38
- print(f"clean {f_name}")
39
- for wav_hash in list(data_dict.keys()):
40
- if int(time.time()) - int(data_dict[wav_hash]["time"]) > 14 * 24 * 3600:
41
- del data_dict[wav_hash]
42
- except Exception as e:
43
- print(e)
44
- print(f"{file_name} error,auto rebuild file")
45
- data_dict = {"info": "temp_dict"}
46
- return data_dict
47
-
48
-
49
- def write_temp(file_name, data):
50
- with open(file_name, "w") as f:
51
- f.write(json.dumps(data))
52
-
53
-
54
- def timeit(func):
55
- def run(*args, **kwargs):
56
- t = time.time()
57
- res = func(*args, **kwargs)
58
- print('executing \'%s\' costed %.3fs' % (func.__name__, time.time() - t))
59
- return res
60
-
61
- return run
62
-
63
-
64
- def format_wav(audio_path):
65
- if Path(audio_path).suffix == '.wav':
66
- return
67
- raw_audio, raw_sample_rate = librosa.load(audio_path, mono=True, sr=None)
68
- soundfile.write(Path(audio_path).with_suffix(".wav"), raw_audio, raw_sample_rate)
69
-
70
-
71
- def get_end_file(dir_path, end):
72
- file_lists = []
73
- for root, dirs, files in os.walk(dir_path):
74
- files = [f for f in files if f[0] != '.']
75
- dirs[:] = [d for d in dirs if d[0] != '.']
76
- for f_file in files:
77
- if f_file.endswith(end):
78
- file_lists.append(os.path.join(root, f_file).replace("\\", "/"))
79
- return file_lists
80
-
81
-
82
- def get_md5(content):
83
- return hashlib.new("md5", content).hexdigest()
84
-
85
- def fill_a_to_b(a, b):
86
- if len(a) < len(b):
87
- for _ in range(0, len(b) - len(a)):
88
- a.append(a[0])
89
-
90
- def mkdir(paths: list):
91
- for path in paths:
92
- if not os.path.exists(path):
93
- os.mkdir(path)
94
-
95
- def pad_array(arr, target_length):
96
- current_length = arr.shape[0]
97
- if current_length >= target_length:
98
- return arr
99
- else:
100
- pad_width = target_length - current_length
101
- pad_left = pad_width // 2
102
- pad_right = pad_width - pad_left
103
- padded_arr = np.pad(arr, (pad_left, pad_right), 'constant', constant_values=(0, 0))
104
- return padded_arr
105
-
106
-
107
- class Svc(object):
108
- def __init__(self, net_g_path, config_path,
109
- device=None,
110
- cluster_model_path="logs/44k/kmeans_10000.pt"):
111
- self.net_g_path = net_g_path
112
- if device is None:
113
- self.dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
114
- else:
115
- self.dev = torch.device(device)
116
- self.net_g_ms = None
117
- self.hps_ms = utils.get_hparams_from_file(config_path)
118
- self.target_sample = self.hps_ms.data.sampling_rate
119
- self.hop_size = self.hps_ms.data.hop_length
120
- self.spk2id = self.hps_ms.spk
121
- # 加载hubert
122
- self.hubert_model = utils.get_hubert_model().to(self.dev)
123
- self.load_model()
124
- if os.path.exists(cluster_model_path):
125
- self.cluster_model = cluster.get_cluster_model(cluster_model_path)
126
-
127
- def load_model(self):
128
- # 获取模型配置
129
- self.net_g_ms = SynthesizerTrn(
130
- self.hps_ms.data.filter_length // 2 + 1,
131
- self.hps_ms.train.segment_size // self.hps_ms.data.hop_length,
132
- **self.hps_ms.model)
133
- _ = utils.load_checkpoint(self.net_g_path, self.net_g_ms, None)
134
- if "half" in self.net_g_path and torch.cuda.is_available():
135
- _ = self.net_g_ms.half().eval().to(self.dev)
136
- else:
137
- _ = self.net_g_ms.eval().to(self.dev)
138
-
139
-
140
-
141
- def get_unit_f0(self, in_path, tran, cluster_infer_ratio, speaker):
142
-
143
- wav, sr = librosa.load(in_path, sr=self.target_sample)
144
-
145
- f0 = utils.compute_f0_parselmouth(wav, sampling_rate=self.target_sample, hop_length=self.hop_size)
146
- f0, uv = utils.interpolate_f0(f0)
147
- f0 = torch.FloatTensor(f0)
148
- uv = torch.FloatTensor(uv)
149
- f0 = f0 * 2 ** (tran / 12)
150
- f0 = f0.unsqueeze(0).to(self.dev)
151
- uv = uv.unsqueeze(0).to(self.dev)
152
-
153
- wav16k = librosa.resample(wav, orig_sr=self.target_sample, target_sr=16000)
154
- wav16k = torch.from_numpy(wav16k).to(self.dev)
155
- c = utils.get_hubert_content(self.hubert_model, wav_16k_tensor=wav16k)
156
- c = utils.repeat_expand_2d(c.squeeze(0), f0.shape[1])
157
-
158
- if cluster_infer_ratio !=0:
159
- cluster_c = cluster.get_cluster_center_result(self.cluster_model, c.cpu().numpy().T, speaker).T
160
- cluster_c = torch.FloatTensor(cluster_c).to(self.dev)
161
- c = cluster_infer_ratio * cluster_c + (1 - cluster_infer_ratio) * c
162
-
163
- c = c.unsqueeze(0)
164
- return c, f0, uv
165
-
166
- def infer(self, speaker, tran, raw_path,
167
- cluster_infer_ratio=0,
168
- auto_predict_f0=False,
169
- noice_scale=0.4):
170
- speaker_id = self.spk2id.__dict__.get(speaker)
171
- if not speaker_id and type(speaker) is int:
172
- if len(self.spk2id.__dict__) >= speaker:
173
- speaker_id = speaker
174
- sid = torch.LongTensor([int(speaker_id)]).to(self.dev).unsqueeze(0)
175
- c, f0, uv = self.get_unit_f0(raw_path, tran, cluster_infer_ratio, speaker)
176
- if "half" in self.net_g_path and torch.cuda.is_available():
177
- c = c.half()
178
- with torch.no_grad():
179
- start = time.time()
180
- audio = self.net_g_ms.infer(c, f0=f0, g=sid, uv=uv, predict_f0=auto_predict_f0, noice_scale=noice_scale)[0,0].data.float()
181
- use_time = time.time() - start
182
- print("vits use time:{}".format(use_time))
183
- return audio, audio.shape[-1]
184
-
185
- def clear_empty(self):
186
- # 清理显存
187
- torch.cuda.empty_cache()
188
-
189
- def slice_inference(self,raw_audio_path, spk, tran, slice_db,cluster_infer_ratio, auto_predict_f0,noice_scale, pad_seconds=0.5):
190
- wav_path = raw_audio_path
191
- chunks = slicer.cut(wav_path, db_thresh=slice_db)
192
- audio_data, audio_sr = slicer.chunks2audio(wav_path, chunks)
193
-
194
- audio = []
195
- for (slice_tag, data) in audio_data:
196
- print(f'#=====segment start, {round(len(data) / audio_sr, 3)}s======')
197
- # padd
198
- pad_len = int(audio_sr * pad_seconds)
199
- data = np.concatenate([np.zeros([pad_len]), data, np.zeros([pad_len])])
200
- length = int(np.ceil(len(data) / audio_sr * self.target_sample))
201
- raw_path = io.BytesIO()
202
- soundfile.write(raw_path, data, audio_sr, format="wav")
203
- raw_path.seek(0)
204
- if slice_tag:
205
- print('jump empty segment')
206
- _audio = np.zeros(length)
207
- else:
208
- out_audio, out_sr = self.infer(spk, tran, raw_path,
209
- cluster_infer_ratio=cluster_infer_ratio,
210
- auto_predict_f0=auto_predict_f0,
211
- noice_scale=noice_scale
212
- )
213
- _audio = out_audio.cpu().numpy()
214
-
215
- pad_len = int(self.target_sample * pad_seconds)
216
- _audio = _audio[pad_len:-pad_len]
217
- audio.extend(list(_audio))
218
- return np.array(audio)
219
-
220
-
221
- class RealTimeVC:
222
- def __init__(self):
223
- self.last_chunk = None
224
- self.last_o = None
225
- self.chunk_len = 16000 # 区块长度
226
- self.pre_len = 3840 # 交叉淡化长度,640的倍数
227
-
228
- """输入输出都是1维numpy 音频波形数组"""
229
-
230
- def process(self, svc_model, speaker_id, f_pitch_change, input_wav_path):
231
- import maad
232
- audio, sr = torchaudio.load(input_wav_path)
233
- audio = audio.cpu().numpy()[0]
234
- temp_wav = io.BytesIO()
235
- if self.last_chunk is None:
236
- input_wav_path.seek(0)
237
- audio, sr = svc_model.infer(speaker_id, f_pitch_change, input_wav_path)
238
- audio = audio.cpu().numpy()
239
- self.last_chunk = audio[-self.pre_len:]
240
- self.last_o = audio
241
- return audio[-self.chunk_len:]
242
- else:
243
- audio = np.concatenate([self.last_chunk, audio])
244
- soundfile.write(temp_wav, audio, sr, format="wav")
245
- temp_wav.seek(0)
246
- audio, sr = svc_model.infer(speaker_id, f_pitch_change, temp_wav)
247
- audio = audio.cpu().numpy()
248
- ret = maad.util.crossfade(self.last_o, audio, self.pre_len)
249
- self.last_chunk = audio[-self.pre_len:]
250
- self.last_o = audio
251
- return ret[self.chunk_len:2 * self.chunk_len]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/inference/infer_tool_grad.py DELETED
@@ -1,160 +0,0 @@
1
- import hashlib
2
- import json
3
- import logging
4
- import os
5
- import time
6
- from pathlib import Path
7
- import io
8
- import librosa
9
- import maad
10
- import numpy as np
11
- from inference import slicer
12
- import parselmouth
13
- import soundfile
14
- import torch
15
- import torchaudio
16
-
17
- from hubert import hubert_model
18
- import utils
19
- from models import SynthesizerTrn
20
- logging.getLogger('numba').setLevel(logging.WARNING)
21
- logging.getLogger('matplotlib').setLevel(logging.WARNING)
22
-
23
- def resize2d_f0(x, target_len):
24
- source = np.array(x)
25
- source[source < 0.001] = np.nan
26
- target = np.interp(np.arange(0, len(source) * target_len, len(source)) / target_len, np.arange(0, len(source)),
27
- source)
28
- res = np.nan_to_num(target)
29
- return res
30
-
31
- def get_f0(x, p_len,f0_up_key=0):
32
-
33
- time_step = 160 / 16000 * 1000
34
- f0_min = 50
35
- f0_max = 1100
36
- f0_mel_min = 1127 * np.log(1 + f0_min / 700)
37
- f0_mel_max = 1127 * np.log(1 + f0_max / 700)
38
-
39
- f0 = parselmouth.Sound(x, 16000).to_pitch_ac(
40
- time_step=time_step / 1000, voicing_threshold=0.6,
41
- pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency']
42
-
43
- pad_size=(p_len - len(f0) + 1) // 2
44
- if(pad_size>0 or p_len - len(f0) - pad_size>0):
45
- f0 = np.pad(f0,[[pad_size,p_len - len(f0) - pad_size]], mode='constant')
46
-
47
- f0 *= pow(2, f0_up_key / 12)
48
- f0_mel = 1127 * np.log(1 + f0 / 700)
49
- f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (f0_mel_max - f0_mel_min) + 1
50
- f0_mel[f0_mel <= 1] = 1
51
- f0_mel[f0_mel > 255] = 255
52
- f0_coarse = np.rint(f0_mel).astype(np.int)
53
- return f0_coarse, f0
54
-
55
- def clean_pitch(input_pitch):
56
- num_nan = np.sum(input_pitch == 1)
57
- if num_nan / len(input_pitch) > 0.9:
58
- input_pitch[input_pitch != 1] = 1
59
- return input_pitch
60
-
61
-
62
- def plt_pitch(input_pitch):
63
- input_pitch = input_pitch.astype(float)
64
- input_pitch[input_pitch == 1] = np.nan
65
- return input_pitch
66
-
67
-
68
- def f0_to_pitch(ff):
69
- f0_pitch = 69 + 12 * np.log2(ff / 440)
70
- return f0_pitch
71
-
72
-
73
- def fill_a_to_b(a, b):
74
- if len(a) < len(b):
75
- for _ in range(0, len(b) - len(a)):
76
- a.append(a[0])
77
-
78
-
79
- def mkdir(paths: list):
80
- for path in paths:
81
- if not os.path.exists(path):
82
- os.mkdir(path)
83
-
84
-
85
- class VitsSvc(object):
86
- def __init__(self):
87
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
88
- self.SVCVITS = None
89
- self.hps = None
90
- self.speakers = None
91
- self.hubert_soft = utils.get_hubert_model()
92
-
93
- def set_device(self, device):
94
- self.device = torch.device(device)
95
- self.hubert_soft.to(self.device)
96
- if self.SVCVITS != None:
97
- self.SVCVITS.to(self.device)
98
-
99
- def loadCheckpoint(self, path):
100
- self.hps = utils.get_hparams_from_file(f"checkpoints/{path}/config.json")
101
- self.SVCVITS = SynthesizerTrn(
102
- self.hps.data.filter_length // 2 + 1,
103
- self.hps.train.segment_size // self.hps.data.hop_length,
104
- **self.hps.model)
105
- _ = utils.load_checkpoint(f"checkpoints/{path}/model.pth", self.SVCVITS, None)
106
- _ = self.SVCVITS.eval().to(self.device)
107
- self.speakers = self.hps.spk
108
-
109
- def get_units(self, source, sr):
110
- source = source.unsqueeze(0).to(self.device)
111
- with torch.inference_mode():
112
- units = self.hubert_soft.units(source)
113
- return units
114
-
115
-
116
- def get_unit_pitch(self, in_path, tran):
117
- source, sr = torchaudio.load(in_path)
118
- source = torchaudio.functional.resample(source, sr, 16000)
119
- if len(source.shape) == 2 and source.shape[1] >= 2:
120
- source = torch.mean(source, dim=0).unsqueeze(0)
121
- soft = self.get_units(source, sr).squeeze(0).cpu().numpy()
122
- f0_coarse, f0 = get_f0(source.cpu().numpy()[0], soft.shape[0]*2, tran)
123
- return soft, f0
124
-
125
- def infer(self, speaker_id, tran, raw_path):
126
- speaker_id = self.speakers[speaker_id]
127
- sid = torch.LongTensor([int(speaker_id)]).to(self.device).unsqueeze(0)
128
- soft, pitch = self.get_unit_pitch(raw_path, tran)
129
- f0 = torch.FloatTensor(clean_pitch(pitch)).unsqueeze(0).to(self.device)
130
- stn_tst = torch.FloatTensor(soft)
131
- with torch.no_grad():
132
- x_tst = stn_tst.unsqueeze(0).to(self.device)
133
- x_tst = torch.repeat_interleave(x_tst, repeats=2, dim=1).transpose(1, 2)
134
- audio = self.SVCVITS.infer(x_tst, f0=f0, g=sid)[0,0].data.float()
135
- return audio, audio.shape[-1]
136
-
137
- def inference(self,srcaudio,chara,tran,slice_db):
138
- sampling_rate, audio = srcaudio
139
- audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
140
- if len(audio.shape) > 1:
141
- audio = librosa.to_mono(audio.transpose(1, 0))
142
- if sampling_rate != 16000:
143
- audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
144
- soundfile.write("tmpwav.wav", audio, 16000, format="wav")
145
- chunks = slicer.cut("tmpwav.wav", db_thresh=slice_db)
146
- audio_data, audio_sr = slicer.chunks2audio("tmpwav.wav", chunks)
147
- audio = []
148
- for (slice_tag, data) in audio_data:
149
- length = int(np.ceil(len(data) / audio_sr * self.hps.data.sampling_rate))
150
- raw_path = io.BytesIO()
151
- soundfile.write(raw_path, data, audio_sr, format="wav")
152
- raw_path.seek(0)
153
- if slice_tag:
154
- _audio = np.zeros(length)
155
- else:
156
- out_audio, out_sr = self.infer(chara, tran, raw_path)
157
- _audio = out_audio.cpu().numpy()
158
- audio.extend(list(_audio))
159
- audio = (np.array(audio) * 32768.0).astype('int16')
160
- return (self.hps.data.sampling_rate,audio)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/inference/slicer.py DELETED
@@ -1,142 +0,0 @@
1
- import librosa
2
- import torch
3
- import torchaudio
4
-
5
-
6
- class Slicer:
7
- def __init__(self,
8
- sr: int,
9
- threshold: float = -40.,
10
- min_length: int = 5000,
11
- min_interval: int = 300,
12
- hop_size: int = 20,
13
- max_sil_kept: int = 5000):
14
- if not min_length >= min_interval >= hop_size:
15
- raise ValueError('The following condition must be satisfied: min_length >= min_interval >= hop_size')
16
- if not max_sil_kept >= hop_size:
17
- raise ValueError('The following condition must be satisfied: max_sil_kept >= hop_size')
18
- min_interval = sr * min_interval / 1000
19
- self.threshold = 10 ** (threshold / 20.)
20
- self.hop_size = round(sr * hop_size / 1000)
21
- self.win_size = min(round(min_interval), 4 * self.hop_size)
22
- self.min_length = round(sr * min_length / 1000 / self.hop_size)
23
- self.min_interval = round(min_interval / self.hop_size)
24
- self.max_sil_kept = round(sr * max_sil_kept / 1000 / self.hop_size)
25
-
26
- def _apply_slice(self, waveform, begin, end):
27
- if len(waveform.shape) > 1:
28
- return waveform[:, begin * self.hop_size: min(waveform.shape[1], end * self.hop_size)]
29
- else:
30
- return waveform[begin * self.hop_size: min(waveform.shape[0], end * self.hop_size)]
31
-
32
- # @timeit
33
- def slice(self, waveform):
34
- if len(waveform.shape) > 1:
35
- samples = librosa.to_mono(waveform)
36
- else:
37
- samples = waveform
38
- if samples.shape[0] <= self.min_length:
39
- return {"0": {"slice": False, "split_time": f"0,{len(waveform)}"}}
40
- rms_list = librosa.feature.rms(y=samples, frame_length=self.win_size, hop_length=self.hop_size).squeeze(0)
41
- sil_tags = []
42
- silence_start = None
43
- clip_start = 0
44
- for i, rms in enumerate(rms_list):
45
- # Keep looping while frame is silent.
46
- if rms < self.threshold:
47
- # Record start of silent frames.
48
- if silence_start is None:
49
- silence_start = i
50
- continue
51
- # Keep looping while frame is not silent and silence start has not been recorded.
52
- if silence_start is None:
53
- continue
54
- # Clear recorded silence start if interval is not enough or clip is too short
55
- is_leading_silence = silence_start == 0 and i > self.max_sil_kept
56
- need_slice_middle = i - silence_start >= self.min_interval and i - clip_start >= self.min_length
57
- if not is_leading_silence and not need_slice_middle:
58
- silence_start = None
59
- continue
60
- # Need slicing. Record the range of silent frames to be removed.
61
- if i - silence_start <= self.max_sil_kept:
62
- pos = rms_list[silence_start: i + 1].argmin() + silence_start
63
- if silence_start == 0:
64
- sil_tags.append((0, pos))
65
- else:
66
- sil_tags.append((pos, pos))
67
- clip_start = pos
68
- elif i - silence_start <= self.max_sil_kept * 2:
69
- pos = rms_list[i - self.max_sil_kept: silence_start + self.max_sil_kept + 1].argmin()
70
- pos += i - self.max_sil_kept
71
- pos_l = rms_list[silence_start: silence_start + self.max_sil_kept + 1].argmin() + silence_start
72
- pos_r = rms_list[i - self.max_sil_kept: i + 1].argmin() + i - self.max_sil_kept
73
- if silence_start == 0:
74
- sil_tags.append((0, pos_r))
75
- clip_start = pos_r
76
- else:
77
- sil_tags.append((min(pos_l, pos), max(pos_r, pos)))
78
- clip_start = max(pos_r, pos)
79
- else:
80
- pos_l = rms_list[silence_start: silence_start + self.max_sil_kept + 1].argmin() + silence_start
81
- pos_r = rms_list[i - self.max_sil_kept: i + 1].argmin() + i - self.max_sil_kept
82
- if silence_start == 0:
83
- sil_tags.append((0, pos_r))
84
- else:
85
- sil_tags.append((pos_l, pos_r))
86
- clip_start = pos_r
87
- silence_start = None
88
- # Deal with trailing silence.
89
- total_frames = rms_list.shape[0]
90
- if silence_start is not None and total_frames - silence_start >= self.min_interval:
91
- silence_end = min(total_frames, silence_start + self.max_sil_kept)
92
- pos = rms_list[silence_start: silence_end + 1].argmin() + silence_start
93
- sil_tags.append((pos, total_frames + 1))
94
- # Apply and return slices.
95
- if len(sil_tags) == 0:
96
- return {"0": {"slice": False, "split_time": f"0,{len(waveform)}"}}
97
- else:
98
- chunks = []
99
- # 第一段静音并非从头开始,补上有声片段
100
- if sil_tags[0][0]:
101
- chunks.append(
102
- {"slice": False, "split_time": f"0,{min(waveform.shape[0], sil_tags[0][0] * self.hop_size)}"})
103
- for i in range(0, len(sil_tags)):
104
- # 标识有声片段(跳过第一段)
105
- if i:
106
- chunks.append({"slice": False,
107
- "split_time": f"{sil_tags[i - 1][1] * self.hop_size},{min(waveform.shape[0], sil_tags[i][0] * self.hop_size)}"})
108
- # 标识所有静音片段
109
- chunks.append({"slice": True,
110
- "split_time": f"{sil_tags[i][0] * self.hop_size},{min(waveform.shape[0], sil_tags[i][1] * self.hop_size)}"})
111
- # 最后一段静音并非结尾,补上结尾片段
112
- if sil_tags[-1][1] * self.hop_size < len(waveform):
113
- chunks.append({"slice": False, "split_time": f"{sil_tags[-1][1] * self.hop_size},{len(waveform)}"})
114
- chunk_dict = {}
115
- for i in range(len(chunks)):
116
- chunk_dict[str(i)] = chunks[i]
117
- return chunk_dict
118
-
119
-
120
- def cut(audio_path, db_thresh=-30, min_len=5000):
121
- audio, sr = librosa.load(audio_path, sr=None)
122
- slicer = Slicer(
123
- sr=sr,
124
- threshold=db_thresh,
125
- min_length=min_len
126
- )
127
- chunks = slicer.slice(audio)
128
- return chunks
129
-
130
-
131
- def chunks2audio(audio_path, chunks):
132
- chunks = dict(chunks)
133
- audio, sr = torchaudio.load(audio_path)
134
- if len(audio.shape) == 2 and audio.shape[1] >= 2:
135
- audio = torch.mean(audio, dim=0).unsqueeze(0)
136
- audio = audio.cpu().numpy()[0]
137
- result = []
138
- for k, v in chunks.items():
139
- tag = v["split_time"].split(",")
140
- if tag[0] != tag[1]:
141
- result.append((v["slice"], audio[int(tag[0]):int(tag[1])]))
142
- return result, sr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/inference_main.py DELETED
@@ -1,101 +0,0 @@
1
- import io
2
- import logging
3
- import time
4
- from pathlib import Path
5
-
6
- import librosa
7
- import matplotlib.pyplot as plt
8
- import numpy as np
9
- import soundfile
10
-
11
- from inference import infer_tool
12
- from inference import slicer
13
- from inference.infer_tool import Svc
14
-
15
- logging.getLogger('numba').setLevel(logging.WARNING)
16
- chunks_dict = infer_tool.read_temp("inference/chunks_temp.json")
17
-
18
-
19
-
20
- def main():
21
- import argparse
22
-
23
- parser = argparse.ArgumentParser(description='sovits4 inference')
24
-
25
- # 一定要设置的部分
26
- parser.add_argument('-m', '--model_path', type=str, default="logs/44k/G_0.pth", help='模型路径')
27
- parser.add_argument('-c', '--config_path', type=str, default="configs/config.json", help='配置文件路径')
28
- parser.add_argument('-n', '--clean_names', type=str, nargs='+', default=["君の知らない物語-src.wav"], help='wav文件名列表,放在raw文件夹下')
29
- parser.add_argument('-t', '--trans', type=int, nargs='+', default=[0], help='音高调整,支持正负(半音)')
30
- parser.add_argument('-s', '--spk_list', type=str, nargs='+', default=['nen'], help='合成目标说话人名称')
31
-
32
- # 可选项部分
33
- parser.add_argument('-a', '--auto_predict_f0', action='store_true', default=False,
34
- help='语音转换自动预测音高,转换歌声时不要打开这个会严重跑调')
35
- parser.add_argument('-cm', '--cluster_model_path', type=str, default="logs/44k/kmeans_10000.pt", help='聚类模型路径,如果没有训练聚类则随便填')
36
- parser.add_argument('-cr', '--cluster_infer_ratio', type=float, default=0, help='聚类方案占比,范围0-1,若没有训练聚类模型则填0即可')
37
-
38
- # 不用动的部分
39
- parser.add_argument('-sd', '--slice_db', type=int, default=-40, help='默认-40,嘈杂的音频可以-30,干声保留呼吸可以-50')
40
- parser.add_argument('-d', '--device', type=str, default=None, help='推理设备,None则为自动选择cpu和gpu')
41
- parser.add_argument('-ns', '--noice_scale', type=float, default=0.4, help='噪音级别,会影响咬字和音质,较为玄学')
42
- parser.add_argument('-p', '--pad_seconds', type=float, default=0.5, help='推理音频pad秒数,由于未知原因开头结尾会有异响,pad一小段静音段后就不会出现')
43
- parser.add_argument('-wf', '--wav_format', type=str, default='flac', help='音频输出格式')
44
-
45
- args = parser.parse_args()
46
-
47
- svc_model = Svc(args.model_path, args.config_path, args.device, args.cluster_model_path)
48
- infer_tool.mkdir(["raw", "results"])
49
- clean_names = args.clean_names
50
- trans = args.trans
51
- spk_list = args.spk_list
52
- slice_db = args.slice_db
53
- wav_format = args.wav_format
54
- auto_predict_f0 = args.auto_predict_f0
55
- cluster_infer_ratio = args.cluster_infer_ratio
56
- noice_scale = args.noice_scale
57
- pad_seconds = args.pad_seconds
58
-
59
- infer_tool.fill_a_to_b(trans, clean_names)
60
- for clean_name, tran in zip(clean_names, trans):
61
- raw_audio_path = f"raw/{clean_name}"
62
- if "." not in raw_audio_path:
63
- raw_audio_path += ".wav"
64
- infer_tool.format_wav(raw_audio_path)
65
- wav_path = Path(raw_audio_path).with_suffix('.wav')
66
- chunks = slicer.cut(wav_path, db_thresh=slice_db)
67
- audio_data, audio_sr = slicer.chunks2audio(wav_path, chunks)
68
-
69
- for spk in spk_list:
70
- audio = []
71
- for (slice_tag, data) in audio_data:
72
- print(f'#=====segment start, {round(len(data) / audio_sr, 3)}s======')
73
-
74
- length = int(np.ceil(len(data) / audio_sr * svc_model.target_sample))
75
- if slice_tag:
76
- print('jump empty segment')
77
- _audio = np.zeros(length)
78
- else:
79
- # padd
80
- pad_len = int(audio_sr * pad_seconds)
81
- data = np.concatenate([np.zeros([pad_len]), data, np.zeros([pad_len])])
82
- raw_path = io.BytesIO()
83
- soundfile.write(raw_path, data, audio_sr, format="wav")
84
- raw_path.seek(0)
85
- out_audio, out_sr = svc_model.infer(spk, tran, raw_path,
86
- cluster_infer_ratio=cluster_infer_ratio,
87
- auto_predict_f0=auto_predict_f0,
88
- noice_scale=noice_scale
89
- )
90
- _audio = out_audio.cpu().numpy()
91
- pad_len = int(svc_model.target_sample * pad_seconds)
92
- _audio = _audio[pad_len:-pad_len]
93
-
94
- audio.extend(list(infer_tool.pad_array(_audio, length)))
95
- key = "auto" if auto_predict_f0 else f"{tran}key"
96
- cluster_name = "" if cluster_infer_ratio == 0 else f"_{cluster_infer_ratio}"
97
- res_path = f'./results/{clean_name}_{key}_{spk}{cluster_name}.{wav_format}'
98
- soundfile.write(res_path, audio, svc_model.target_sample, format=wav_format)
99
-
100
- if __name__ == '__main__':
101
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/models.py DELETED
@@ -1,420 +0,0 @@
1
- import copy
2
- import math
3
- import torch
4
- from torch import nn
5
- from torch.nn import functional as F
6
-
7
- import modules.attentions as attentions
8
- import modules.commons as commons
9
- import modules.modules as modules
10
-
11
- from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
12
- from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
13
-
14
- import utils
15
- from modules.commons import init_weights, get_padding
16
- from vdecoder.hifigan.models import Generator
17
- from utils import f0_to_coarse
18
-
19
- class ResidualCouplingBlock(nn.Module):
20
- def __init__(self,
21
- channels,
22
- hidden_channels,
23
- kernel_size,
24
- dilation_rate,
25
- n_layers,
26
- n_flows=4,
27
- gin_channels=0):
28
- super().__init__()
29
- self.channels = channels
30
- self.hidden_channels = hidden_channels
31
- self.kernel_size = kernel_size
32
- self.dilation_rate = dilation_rate
33
- self.n_layers = n_layers
34
- self.n_flows = n_flows
35
- self.gin_channels = gin_channels
36
-
37
- self.flows = nn.ModuleList()
38
- for i in range(n_flows):
39
- self.flows.append(modules.ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels, mean_only=True))
40
- self.flows.append(modules.Flip())
41
-
42
- def forward(self, x, x_mask, g=None, reverse=False):
43
- if not reverse:
44
- for flow in self.flows:
45
- x, _ = flow(x, x_mask, g=g, reverse=reverse)
46
- else:
47
- for flow in reversed(self.flows):
48
- x = flow(x, x_mask, g=g, reverse=reverse)
49
- return x
50
-
51
-
52
- class Encoder(nn.Module):
53
- def __init__(self,
54
- in_channels,
55
- out_channels,
56
- hidden_channels,
57
- kernel_size,
58
- dilation_rate,
59
- n_layers,
60
- gin_channels=0):
61
- super().__init__()
62
- self.in_channels = in_channels
63
- self.out_channels = out_channels
64
- self.hidden_channels = hidden_channels
65
- self.kernel_size = kernel_size
66
- self.dilation_rate = dilation_rate
67
- self.n_layers = n_layers
68
- self.gin_channels = gin_channels
69
-
70
- self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
71
- self.enc = modules.WN(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels)
72
- self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
73
-
74
- def forward(self, x, x_lengths, g=None):
75
- # print(x.shape,x_lengths.shape)
76
- x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
77
- x = self.pre(x) * x_mask
78
- x = self.enc(x, x_mask, g=g)
79
- stats = self.proj(x) * x_mask
80
- m, logs = torch.split(stats, self.out_channels, dim=1)
81
- z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
82
- return z, m, logs, x_mask
83
-
84
-
85
- class TextEncoder(nn.Module):
86
- def __init__(self,
87
- out_channels,
88
- hidden_channels,
89
- kernel_size,
90
- n_layers,
91
- gin_channels=0,
92
- filter_channels=None,
93
- n_heads=None,
94
- p_dropout=None):
95
- super().__init__()
96
- self.out_channels = out_channels
97
- self.hidden_channels = hidden_channels
98
- self.kernel_size = kernel_size
99
- self.n_layers = n_layers
100
- self.gin_channels = gin_channels
101
- self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
102
- self.f0_emb = nn.Embedding(256, hidden_channels)
103
-
104
- self.enc_ = attentions.Encoder(
105
- hidden_channels,
106
- filter_channels,
107
- n_heads,
108
- n_layers,
109
- kernel_size,
110
- p_dropout)
111
-
112
- def forward(self, x, x_mask, f0=None, noice_scale=1):
113
- x = x + self.f0_emb(f0).transpose(1,2)
114
- x = self.enc_(x * x_mask, x_mask)
115
- stats = self.proj(x) * x_mask
116
- m, logs = torch.split(stats, self.out_channels, dim=1)
117
- z = (m + torch.randn_like(m) * torch.exp(logs) * noice_scale) * x_mask
118
-
119
- return z, m, logs, x_mask
120
-
121
-
122
-
123
- class DiscriminatorP(torch.nn.Module):
124
- def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
125
- super(DiscriminatorP, self).__init__()
126
- self.period = period
127
- self.use_spectral_norm = use_spectral_norm
128
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
129
- self.convs = nn.ModuleList([
130
- norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
131
- norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
132
- norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
133
- norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
134
- norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(get_padding(kernel_size, 1), 0))),
135
- ])
136
- self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
137
-
138
- def forward(self, x):
139
- fmap = []
140
-
141
- # 1d to 2d
142
- b, c, t = x.shape
143
- if t % self.period != 0: # pad first
144
- n_pad = self.period - (t % self.period)
145
- x = F.pad(x, (0, n_pad), "reflect")
146
- t = t + n_pad
147
- x = x.view(b, c, t // self.period, self.period)
148
-
149
- for l in self.convs:
150
- x = l(x)
151
- x = F.leaky_relu(x, modules.LRELU_SLOPE)
152
- fmap.append(x)
153
- x = self.conv_post(x)
154
- fmap.append(x)
155
- x = torch.flatten(x, 1, -1)
156
-
157
- return x, fmap
158
-
159
-
160
- class DiscriminatorS(torch.nn.Module):
161
- def __init__(self, use_spectral_norm=False):
162
- super(DiscriminatorS, self).__init__()
163
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
164
- self.convs = nn.ModuleList([
165
- norm_f(Conv1d(1, 16, 15, 1, padding=7)),
166
- norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
167
- norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
168
- norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
169
- norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
170
- norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
171
- ])
172
- self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
173
-
174
- def forward(self, x):
175
- fmap = []
176
-
177
- for l in self.convs:
178
- x = l(x)
179
- x = F.leaky_relu(x, modules.LRELU_SLOPE)
180
- fmap.append(x)
181
- x = self.conv_post(x)
182
- fmap.append(x)
183
- x = torch.flatten(x, 1, -1)
184
-
185
- return x, fmap
186
-
187
-
188
- class MultiPeriodDiscriminator(torch.nn.Module):
189
- def __init__(self, use_spectral_norm=False):
190
- super(MultiPeriodDiscriminator, self).__init__()
191
- periods = [2,3,5,7,11]
192
-
193
- discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
194
- discs = discs + [DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods]
195
- self.discriminators = nn.ModuleList(discs)
196
-
197
- def forward(self, y, y_hat):
198
- y_d_rs = []
199
- y_d_gs = []
200
- fmap_rs = []
201
- fmap_gs = []
202
- for i, d in enumerate(self.discriminators):
203
- y_d_r, fmap_r = d(y)
204
- y_d_g, fmap_g = d(y_hat)
205
- y_d_rs.append(y_d_r)
206
- y_d_gs.append(y_d_g)
207
- fmap_rs.append(fmap_r)
208
- fmap_gs.append(fmap_g)
209
-
210
- return y_d_rs, y_d_gs, fmap_rs, fmap_gs
211
-
212
-
213
- class SpeakerEncoder(torch.nn.Module):
214
- def __init__(self, mel_n_channels=80, model_num_layers=3, model_hidden_size=256, model_embedding_size=256):
215
- super(SpeakerEncoder, self).__init__()
216
- self.lstm = nn.LSTM(mel_n_channels, model_hidden_size, model_num_layers, batch_first=True)
217
- self.linear = nn.Linear(model_hidden_size, model_embedding_size)
218
- self.relu = nn.ReLU()
219
-
220
- def forward(self, mels):
221
- self.lstm.flatten_parameters()
222
- _, (hidden, _) = self.lstm(mels)
223
- embeds_raw = self.relu(self.linear(hidden[-1]))
224
- return embeds_raw / torch.norm(embeds_raw, dim=1, keepdim=True)
225
-
226
- def compute_partial_slices(self, total_frames, partial_frames, partial_hop):
227
- mel_slices = []
228
- for i in range(0, total_frames-partial_frames, partial_hop):
229
- mel_range = torch.arange(i, i+partial_frames)
230
- mel_slices.append(mel_range)
231
-
232
- return mel_slices
233
-
234
- def embed_utterance(self, mel, partial_frames=128, partial_hop=64):
235
- mel_len = mel.size(1)
236
- last_mel = mel[:,-partial_frames:]
237
-
238
- if mel_len > partial_frames:
239
- mel_slices = self.compute_partial_slices(mel_len, partial_frames, partial_hop)
240
- mels = list(mel[:,s] for s in mel_slices)
241
- mels.append(last_mel)
242
- mels = torch.stack(tuple(mels), 0).squeeze(1)
243
-
244
- with torch.no_grad():
245
- partial_embeds = self(mels)
246
- embed = torch.mean(partial_embeds, axis=0).unsqueeze(0)
247
- #embed = embed / torch.linalg.norm(embed, 2)
248
- else:
249
- with torch.no_grad():
250
- embed = self(last_mel)
251
-
252
- return embed
253
-
254
- class F0Decoder(nn.Module):
255
- def __init__(self,
256
- out_channels,
257
- hidden_channels,
258
- filter_channels,
259
- n_heads,
260
- n_layers,
261
- kernel_size,
262
- p_dropout,
263
- spk_channels=0):
264
- super().__init__()
265
- self.out_channels = out_channels
266
- self.hidden_channels = hidden_channels
267
- self.filter_channels = filter_channels
268
- self.n_heads = n_heads
269
- self.n_layers = n_layers
270
- self.kernel_size = kernel_size
271
- self.p_dropout = p_dropout
272
- self.spk_channels = spk_channels
273
-
274
- self.prenet = nn.Conv1d(hidden_channels, hidden_channels, 3, padding=1)
275
- self.decoder = attentions.FFT(
276
- hidden_channels,
277
- filter_channels,
278
- n_heads,
279
- n_layers,
280
- kernel_size,
281
- p_dropout)
282
- self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
283
- self.f0_prenet = nn.Conv1d(1, hidden_channels , 3, padding=1)
284
- self.cond = nn.Conv1d(spk_channels, hidden_channels, 1)
285
-
286
- def forward(self, x, norm_f0, x_mask, spk_emb=None):
287
- x = torch.detach(x)
288
- if (spk_emb is not None):
289
- x = x + self.cond(spk_emb)
290
- x += self.f0_prenet(norm_f0)
291
- x = self.prenet(x) * x_mask
292
- x = self.decoder(x * x_mask, x_mask)
293
- x = self.proj(x) * x_mask
294
- return x
295
-
296
-
297
- class SynthesizerTrn(nn.Module):
298
- """
299
- Synthesizer for Training
300
- """
301
-
302
- def __init__(self,
303
- spec_channels,
304
- segment_size,
305
- inter_channels,
306
- hidden_channels,
307
- filter_channels,
308
- n_heads,
309
- n_layers,
310
- kernel_size,
311
- p_dropout,
312
- resblock,
313
- resblock_kernel_sizes,
314
- resblock_dilation_sizes,
315
- upsample_rates,
316
- upsample_initial_channel,
317
- upsample_kernel_sizes,
318
- gin_channels,
319
- ssl_dim,
320
- n_speakers,
321
- sampling_rate=44100,
322
- **kwargs):
323
-
324
- super().__init__()
325
- self.spec_channels = spec_channels
326
- self.inter_channels = inter_channels
327
- self.hidden_channels = hidden_channels
328
- self.filter_channels = filter_channels
329
- self.n_heads = n_heads
330
- self.n_layers = n_layers
331
- self.kernel_size = kernel_size
332
- self.p_dropout = p_dropout
333
- self.resblock = resblock
334
- self.resblock_kernel_sizes = resblock_kernel_sizes
335
- self.resblock_dilation_sizes = resblock_dilation_sizes
336
- self.upsample_rates = upsample_rates
337
- self.upsample_initial_channel = upsample_initial_channel
338
- self.upsample_kernel_sizes = upsample_kernel_sizes
339
- self.segment_size = segment_size
340
- self.gin_channels = gin_channels
341
- self.ssl_dim = ssl_dim
342
- self.emb_g = nn.Embedding(n_speakers, gin_channels)
343
-
344
- self.pre = nn.Conv1d(ssl_dim, hidden_channels, kernel_size=5, padding=2)
345
-
346
- self.enc_p = TextEncoder(
347
- inter_channels,
348
- hidden_channels,
349
- filter_channels=filter_channels,
350
- n_heads=n_heads,
351
- n_layers=n_layers,
352
- kernel_size=kernel_size,
353
- p_dropout=p_dropout
354
- )
355
- hps = {
356
- "sampling_rate": sampling_rate,
357
- "inter_channels": inter_channels,
358
- "resblock": resblock,
359
- "resblock_kernel_sizes": resblock_kernel_sizes,
360
- "resblock_dilation_sizes": resblock_dilation_sizes,
361
- "upsample_rates": upsample_rates,
362
- "upsample_initial_channel": upsample_initial_channel,
363
- "upsample_kernel_sizes": upsample_kernel_sizes,
364
- "gin_channels": gin_channels,
365
- }
366
- self.dec = Generator(h=hps)
367
- self.enc_q = Encoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16, gin_channels=gin_channels)
368
- self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels)
369
- self.f0_decoder = F0Decoder(
370
- 1,
371
- hidden_channels,
372
- filter_channels,
373
- n_heads,
374
- n_layers,
375
- kernel_size,
376
- p_dropout,
377
- spk_channels=gin_channels
378
- )
379
- self.emb_uv = nn.Embedding(2, hidden_channels)
380
-
381
- def forward(self, c, f0, uv, spec, g=None, c_lengths=None, spec_lengths=None):
382
- g = self.emb_g(g).transpose(1,2)
383
- # ssl prenet
384
- x_mask = torch.unsqueeze(commons.sequence_mask(c_lengths, c.size(2)), 1).to(c.dtype)
385
- x = self.pre(c) * x_mask + self.emb_uv(uv.long()).transpose(1,2)
386
-
387
- # f0 predict
388
- lf0 = 2595. * torch.log10(1. + f0.unsqueeze(1) / 700.) / 500
389
- norm_lf0 = utils.normalize_f0(lf0, x_mask, uv)
390
- pred_lf0 = self.f0_decoder(x, norm_lf0, x_mask, spk_emb=g)
391
-
392
- # encoder
393
- z_ptemp, m_p, logs_p, _ = self.enc_p(x, x_mask, f0=f0_to_coarse(f0))
394
- z, m_q, logs_q, spec_mask = self.enc_q(spec, spec_lengths, g=g)
395
-
396
- # flow
397
- z_p = self.flow(z, spec_mask, g=g)
398
- z_slice, pitch_slice, ids_slice = commons.rand_slice_segments_with_pitch(z, f0, spec_lengths, self.segment_size)
399
-
400
- # nsf decoder
401
- o = self.dec(z_slice, g=g, f0=pitch_slice)
402
-
403
- return o, ids_slice, spec_mask, (z, z_p, m_p, logs_p, m_q, logs_q), pred_lf0, norm_lf0, lf0
404
-
405
- def infer(self, c, f0, uv, g=None, noice_scale=0.35, predict_f0=False):
406
- c_lengths = (torch.ones(c.size(0)) * c.size(-1)).to(c.device)
407
- g = self.emb_g(g).transpose(1,2)
408
- x_mask = torch.unsqueeze(commons.sequence_mask(c_lengths, c.size(2)), 1).to(c.dtype)
409
- x = self.pre(c) * x_mask + self.emb_uv(uv.long()).transpose(1,2)
410
-
411
- if predict_f0:
412
- lf0 = 2595. * torch.log10(1. + f0.unsqueeze(1) / 700.) / 500
413
- norm_lf0 = utils.normalize_f0(lf0, x_mask, uv, random_scale=False)
414
- pred_lf0 = self.f0_decoder(x, norm_lf0, x_mask, spk_emb=g)
415
- f0 = (700 * (torch.pow(10, pred_lf0 * 500 / 2595) - 1)).squeeze(1)
416
-
417
- z_p, m_p, logs_p, c_mask = self.enc_p(x, x_mask, f0=f0_to_coarse(f0), noice_scale=noice_scale)
418
- z = self.flow(z_p, c_mask, g=g, reverse=True)
419
- o = self.dec(z * c_mask, g=g, f0=f0)
420
- return o
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/models/Arknights/G_10400.pth DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:010b66cbfc075149f1493381c1c50780d2e4ddbbfd73f072b5e136fad2f23d07
3
- size 542789405
 
 
 
 
Blue-Archive/Library-VITS/Arknights/models/Arknights/config.json DELETED
@@ -1,97 +0,0 @@
1
- {
2
- "train": {
3
- "log_interval": 200,
4
- "eval_interval": 800,
5
- "seed": 1234,
6
- "epochs": 10000,
7
- "learning_rate": 0.0001,
8
- "betas": [
9
- 0.8,
10
- 0.99
11
- ],
12
- "eps": 1e-09,
13
- "batch_size": 10,
14
- "fp16_run": false,
15
- "lr_decay": 0.999875,
16
- "segment_size": 10240,
17
- "init_lr_ratio": 1,
18
- "warmup_epochs": 0,
19
- "c_mel": 45,
20
- "c_kl": 1.0,
21
- "use_sr": true,
22
- "max_speclen": 512,
23
- "port": "8001",
24
- "keep_ckpts": 3
25
- },
26
- "data": {
27
- "training_files": "filelists/train.txt",
28
- "validation_files": "filelists/val.txt",
29
- "max_wav_value": 32768.0,
30
- "sampling_rate": 44100,
31
- "filter_length": 2048,
32
- "hop_length": 512,
33
- "win_length": 2048,
34
- "n_mel_channels": 80,
35
- "mel_fmin": 0.0,
36
- "mel_fmax": 22050
37
- },
38
- "model": {
39
- "inter_channels": 192,
40
- "hidden_channels": 192,
41
- "filter_channels": 768,
42
- "n_heads": 2,
43
- "n_layers": 6,
44
- "kernel_size": 3,
45
- "p_dropout": 0.1,
46
- "resblock": "1",
47
- "resblock_kernel_sizes": [
48
- 3,
49
- 7,
50
- 11
51
- ],
52
- "resblock_dilation_sizes": [
53
- [
54
- 1,
55
- 3,
56
- 5
57
- ],
58
- [
59
- 1,
60
- 3,
61
- 5
62
- ],
63
- [
64
- 1,
65
- 3,
66
- 5
67
- ]
68
- ],
69
- "upsample_rates": [
70
- 8,
71
- 8,
72
- 2,
73
- 2,
74
- 2
75
- ],
76
- "upsample_initial_channel": 512,
77
- "upsample_kernel_sizes": [
78
- 16,
79
- 16,
80
- 4,
81
- 4,
82
- 4
83
- ],
84
- "n_layers_q": 3,
85
- "use_spectral_norm": false,
86
- "gin_channels": 256,
87
- "ssl_dim": 256,
88
- "n_speakers": 200
89
- },
90
- "spk": {
91
- "Ifrit": 0,
92
- "Kaltsit": 1,
93
- "Myrtle": 2,
94
- "Saria": 3,
95
- "Skadi": 4
96
- }
97
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/modules/__init__.py DELETED
File without changes
Blue-Archive/Library-VITS/Arknights/modules/attentions.py DELETED
@@ -1,349 +0,0 @@
1
- import copy
2
- import math
3
- import numpy as np
4
- import torch
5
- from torch import nn
6
- from torch.nn import functional as F
7
-
8
- import modules.commons as commons
9
- import modules.modules as modules
10
- from modules.modules import LayerNorm
11
-
12
-
13
- class FFT(nn.Module):
14
- def __init__(self, hidden_channels, filter_channels, n_heads, n_layers=1, kernel_size=1, p_dropout=0.,
15
- proximal_bias=False, proximal_init=True, **kwargs):
16
- super().__init__()
17
- self.hidden_channels = hidden_channels
18
- self.filter_channels = filter_channels
19
- self.n_heads = n_heads
20
- self.n_layers = n_layers
21
- self.kernel_size = kernel_size
22
- self.p_dropout = p_dropout
23
- self.proximal_bias = proximal_bias
24
- self.proximal_init = proximal_init
25
-
26
- self.drop = nn.Dropout(p_dropout)
27
- self.self_attn_layers = nn.ModuleList()
28
- self.norm_layers_0 = nn.ModuleList()
29
- self.ffn_layers = nn.ModuleList()
30
- self.norm_layers_1 = nn.ModuleList()
31
- for i in range(self.n_layers):
32
- self.self_attn_layers.append(
33
- MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout, proximal_bias=proximal_bias,
34
- proximal_init=proximal_init))
35
- self.norm_layers_0.append(LayerNorm(hidden_channels))
36
- self.ffn_layers.append(
37
- FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout, causal=True))
38
- self.norm_layers_1.append(LayerNorm(hidden_channels))
39
-
40
- def forward(self, x, x_mask):
41
- """
42
- x: decoder input
43
- h: encoder output
44
- """
45
- self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(device=x.device, dtype=x.dtype)
46
- x = x * x_mask
47
- for i in range(self.n_layers):
48
- y = self.self_attn_layers[i](x, x, self_attn_mask)
49
- y = self.drop(y)
50
- x = self.norm_layers_0[i](x + y)
51
-
52
- y = self.ffn_layers[i](x, x_mask)
53
- y = self.drop(y)
54
- x = self.norm_layers_1[i](x + y)
55
- x = x * x_mask
56
- return x
57
-
58
-
59
- class Encoder(nn.Module):
60
- def __init__(self, hidden_channels, filter_channels, n_heads, n_layers, kernel_size=1, p_dropout=0., window_size=4, **kwargs):
61
- super().__init__()
62
- self.hidden_channels = hidden_channels
63
- self.filter_channels = filter_channels
64
- self.n_heads = n_heads
65
- self.n_layers = n_layers
66
- self.kernel_size = kernel_size
67
- self.p_dropout = p_dropout
68
- self.window_size = window_size
69
-
70
- self.drop = nn.Dropout(p_dropout)
71
- self.attn_layers = nn.ModuleList()
72
- self.norm_layers_1 = nn.ModuleList()
73
- self.ffn_layers = nn.ModuleList()
74
- self.norm_layers_2 = nn.ModuleList()
75
- for i in range(self.n_layers):
76
- self.attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout, window_size=window_size))
77
- self.norm_layers_1.append(LayerNorm(hidden_channels))
78
- self.ffn_layers.append(FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout))
79
- self.norm_layers_2.append(LayerNorm(hidden_channels))
80
-
81
- def forward(self, x, x_mask):
82
- attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
83
- x = x * x_mask
84
- for i in range(self.n_layers):
85
- y = self.attn_layers[i](x, x, attn_mask)
86
- y = self.drop(y)
87
- x = self.norm_layers_1[i](x + y)
88
-
89
- y = self.ffn_layers[i](x, x_mask)
90
- y = self.drop(y)
91
- x = self.norm_layers_2[i](x + y)
92
- x = x * x_mask
93
- return x
94
-
95
-
96
- class Decoder(nn.Module):
97
- def __init__(self, hidden_channels, filter_channels, n_heads, n_layers, kernel_size=1, p_dropout=0., proximal_bias=False, proximal_init=True, **kwargs):
98
- super().__init__()
99
- self.hidden_channels = hidden_channels
100
- self.filter_channels = filter_channels
101
- self.n_heads = n_heads
102
- self.n_layers = n_layers
103
- self.kernel_size = kernel_size
104
- self.p_dropout = p_dropout
105
- self.proximal_bias = proximal_bias
106
- self.proximal_init = proximal_init
107
-
108
- self.drop = nn.Dropout(p_dropout)
109
- self.self_attn_layers = nn.ModuleList()
110
- self.norm_layers_0 = nn.ModuleList()
111
- self.encdec_attn_layers = nn.ModuleList()
112
- self.norm_layers_1 = nn.ModuleList()
113
- self.ffn_layers = nn.ModuleList()
114
- self.norm_layers_2 = nn.ModuleList()
115
- for i in range(self.n_layers):
116
- self.self_attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout, proximal_bias=proximal_bias, proximal_init=proximal_init))
117
- self.norm_layers_0.append(LayerNorm(hidden_channels))
118
- self.encdec_attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout))
119
- self.norm_layers_1.append(LayerNorm(hidden_channels))
120
- self.ffn_layers.append(FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout, causal=True))
121
- self.norm_layers_2.append(LayerNorm(hidden_channels))
122
-
123
- def forward(self, x, x_mask, h, h_mask):
124
- """
125
- x: decoder input
126
- h: encoder output
127
- """
128
- self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(device=x.device, dtype=x.dtype)
129
- encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
130
- x = x * x_mask
131
- for i in range(self.n_layers):
132
- y = self.self_attn_layers[i](x, x, self_attn_mask)
133
- y = self.drop(y)
134
- x = self.norm_layers_0[i](x + y)
135
-
136
- y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
137
- y = self.drop(y)
138
- x = self.norm_layers_1[i](x + y)
139
-
140
- y = self.ffn_layers[i](x, x_mask)
141
- y = self.drop(y)
142
- x = self.norm_layers_2[i](x + y)
143
- x = x * x_mask
144
- return x
145
-
146
-
147
- class MultiHeadAttention(nn.Module):
148
- def __init__(self, channels, out_channels, n_heads, p_dropout=0., window_size=None, heads_share=True, block_length=None, proximal_bias=False, proximal_init=False):
149
- super().__init__()
150
- assert channels % n_heads == 0
151
-
152
- self.channels = channels
153
- self.out_channels = out_channels
154
- self.n_heads = n_heads
155
- self.p_dropout = p_dropout
156
- self.window_size = window_size
157
- self.heads_share = heads_share
158
- self.block_length = block_length
159
- self.proximal_bias = proximal_bias
160
- self.proximal_init = proximal_init
161
- self.attn = None
162
-
163
- self.k_channels = channels // n_heads
164
- self.conv_q = nn.Conv1d(channels, channels, 1)
165
- self.conv_k = nn.Conv1d(channels, channels, 1)
166
- self.conv_v = nn.Conv1d(channels, channels, 1)
167
- self.conv_o = nn.Conv1d(channels, out_channels, 1)
168
- self.drop = nn.Dropout(p_dropout)
169
-
170
- if window_size is not None:
171
- n_heads_rel = 1 if heads_share else n_heads
172
- rel_stddev = self.k_channels**-0.5
173
- self.emb_rel_k = nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev)
174
- self.emb_rel_v = nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev)
175
-
176
- nn.init.xavier_uniform_(self.conv_q.weight)
177
- nn.init.xavier_uniform_(self.conv_k.weight)
178
- nn.init.xavier_uniform_(self.conv_v.weight)
179
- if proximal_init:
180
- with torch.no_grad():
181
- self.conv_k.weight.copy_(self.conv_q.weight)
182
- self.conv_k.bias.copy_(self.conv_q.bias)
183
-
184
- def forward(self, x, c, attn_mask=None):
185
- q = self.conv_q(x)
186
- k = self.conv_k(c)
187
- v = self.conv_v(c)
188
-
189
- x, self.attn = self.attention(q, k, v, mask=attn_mask)
190
-
191
- x = self.conv_o(x)
192
- return x
193
-
194
- def attention(self, query, key, value, mask=None):
195
- # reshape [b, d, t] -> [b, n_h, t, d_k]
196
- b, d, t_s, t_t = (*key.size(), query.size(2))
197
- query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
198
- key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
199
- value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
200
-
201
- scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
202
- if self.window_size is not None:
203
- assert t_s == t_t, "Relative attention is only available for self-attention."
204
- key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
205
- rel_logits = self._matmul_with_relative_keys(query /math.sqrt(self.k_channels), key_relative_embeddings)
206
- scores_local = self._relative_position_to_absolute_position(rel_logits)
207
- scores = scores + scores_local
208
- if self.proximal_bias:
209
- assert t_s == t_t, "Proximal bias is only available for self-attention."
210
- scores = scores + self._attention_bias_proximal(t_s).to(device=scores.device, dtype=scores.dtype)
211
- if mask is not None:
212
- scores = scores.masked_fill(mask == 0, -1e4)
213
- if self.block_length is not None:
214
- assert t_s == t_t, "Local attention is only available for self-attention."
215
- block_mask = torch.ones_like(scores).triu(-self.block_length).tril(self.block_length)
216
- scores = scores.masked_fill(block_mask == 0, -1e4)
217
- p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
218
- p_attn = self.drop(p_attn)
219
- output = torch.matmul(p_attn, value)
220
- if self.window_size is not None:
221
- relative_weights = self._absolute_position_to_relative_position(p_attn)
222
- value_relative_embeddings = self._get_relative_embeddings(self.emb_rel_v, t_s)
223
- output = output + self._matmul_with_relative_values(relative_weights, value_relative_embeddings)
224
- output = output.transpose(2, 3).contiguous().view(b, d, t_t) # [b, n_h, t_t, d_k] -> [b, d, t_t]
225
- return output, p_attn
226
-
227
- def _matmul_with_relative_values(self, x, y):
228
- """
229
- x: [b, h, l, m]
230
- y: [h or 1, m, d]
231
- ret: [b, h, l, d]
232
- """
233
- ret = torch.matmul(x, y.unsqueeze(0))
234
- return ret
235
-
236
- def _matmul_with_relative_keys(self, x, y):
237
- """
238
- x: [b, h, l, d]
239
- y: [h or 1, m, d]
240
- ret: [b, h, l, m]
241
- """
242
- ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
243
- return ret
244
-
245
- def _get_relative_embeddings(self, relative_embeddings, length):
246
- max_relative_position = 2 * self.window_size + 1
247
- # Pad first before slice to avoid using cond ops.
248
- pad_length = max(length - (self.window_size + 1), 0)
249
- slice_start_position = max((self.window_size + 1) - length, 0)
250
- slice_end_position = slice_start_position + 2 * length - 1
251
- if pad_length > 0:
252
- padded_relative_embeddings = F.pad(
253
- relative_embeddings,
254
- commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]))
255
- else:
256
- padded_relative_embeddings = relative_embeddings
257
- used_relative_embeddings = padded_relative_embeddings[:,slice_start_position:slice_end_position]
258
- return used_relative_embeddings
259
-
260
- def _relative_position_to_absolute_position(self, x):
261
- """
262
- x: [b, h, l, 2*l-1]
263
- ret: [b, h, l, l]
264
- """
265
- batch, heads, length, _ = x.size()
266
- # Concat columns of pad to shift from relative to absolute indexing.
267
- x = F.pad(x, commons.convert_pad_shape([[0,0],[0,0],[0,0],[0,1]]))
268
-
269
- # Concat extra elements so to add up to shape (len+1, 2*len-1).
270
- x_flat = x.view([batch, heads, length * 2 * length])
271
- x_flat = F.pad(x_flat, commons.convert_pad_shape([[0,0],[0,0],[0,length-1]]))
272
-
273
- # Reshape and slice out the padded elements.
274
- x_final = x_flat.view([batch, heads, length+1, 2*length-1])[:, :, :length, length-1:]
275
- return x_final
276
-
277
- def _absolute_position_to_relative_position(self, x):
278
- """
279
- x: [b, h, l, l]
280
- ret: [b, h, l, 2*l-1]
281
- """
282
- batch, heads, length, _ = x.size()
283
- # padd along column
284
- x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length-1]]))
285
- x_flat = x.view([batch, heads, length**2 + length*(length -1)])
286
- # add 0's in the beginning that will skew the elements after reshape
287
- x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
288
- x_final = x_flat.view([batch, heads, length, 2*length])[:,:,:,1:]
289
- return x_final
290
-
291
- def _attention_bias_proximal(self, length):
292
- """Bias for self-attention to encourage attention to close positions.
293
- Args:
294
- length: an integer scalar.
295
- Returns:
296
- a Tensor with shape [1, 1, length, length]
297
- """
298
- r = torch.arange(length, dtype=torch.float32)
299
- diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
300
- return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
301
-
302
-
303
- class FFN(nn.Module):
304
- def __init__(self, in_channels, out_channels, filter_channels, kernel_size, p_dropout=0., activation=None, causal=False):
305
- super().__init__()
306
- self.in_channels = in_channels
307
- self.out_channels = out_channels
308
- self.filter_channels = filter_channels
309
- self.kernel_size = kernel_size
310
- self.p_dropout = p_dropout
311
- self.activation = activation
312
- self.causal = causal
313
-
314
- if causal:
315
- self.padding = self._causal_padding
316
- else:
317
- self.padding = self._same_padding
318
-
319
- self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
320
- self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
321
- self.drop = nn.Dropout(p_dropout)
322
-
323
- def forward(self, x, x_mask):
324
- x = self.conv_1(self.padding(x * x_mask))
325
- if self.activation == "gelu":
326
- x = x * torch.sigmoid(1.702 * x)
327
- else:
328
- x = torch.relu(x)
329
- x = self.drop(x)
330
- x = self.conv_2(self.padding(x * x_mask))
331
- return x * x_mask
332
-
333
- def _causal_padding(self, x):
334
- if self.kernel_size == 1:
335
- return x
336
- pad_l = self.kernel_size - 1
337
- pad_r = 0
338
- padding = [[0, 0], [0, 0], [pad_l, pad_r]]
339
- x = F.pad(x, commons.convert_pad_shape(padding))
340
- return x
341
-
342
- def _same_padding(self, x):
343
- if self.kernel_size == 1:
344
- return x
345
- pad_l = (self.kernel_size - 1) // 2
346
- pad_r = self.kernel_size // 2
347
- padding = [[0, 0], [0, 0], [pad_l, pad_r]]
348
- x = F.pad(x, commons.convert_pad_shape(padding))
349
- return x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/modules/commons.py DELETED
@@ -1,188 +0,0 @@
1
- import math
2
- import numpy as np
3
- import torch
4
- from torch import nn
5
- from torch.nn import functional as F
6
-
7
- def slice_pitch_segments(x, ids_str, segment_size=4):
8
- ret = torch.zeros_like(x[:, :segment_size])
9
- for i in range(x.size(0)):
10
- idx_str = ids_str[i]
11
- idx_end = idx_str + segment_size
12
- ret[i] = x[i, idx_str:idx_end]
13
- return ret
14
-
15
- def rand_slice_segments_with_pitch(x, pitch, x_lengths=None, segment_size=4):
16
- b, d, t = x.size()
17
- if x_lengths is None:
18
- x_lengths = t
19
- ids_str_max = x_lengths - segment_size + 1
20
- ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
21
- ret = slice_segments(x, ids_str, segment_size)
22
- ret_pitch = slice_pitch_segments(pitch, ids_str, segment_size)
23
- return ret, ret_pitch, ids_str
24
-
25
- def init_weights(m, mean=0.0, std=0.01):
26
- classname = m.__class__.__name__
27
- if classname.find("Conv") != -1:
28
- m.weight.data.normal_(mean, std)
29
-
30
-
31
- def get_padding(kernel_size, dilation=1):
32
- return int((kernel_size*dilation - dilation)/2)
33
-
34
-
35
- def convert_pad_shape(pad_shape):
36
- l = pad_shape[::-1]
37
- pad_shape = [item for sublist in l for item in sublist]
38
- return pad_shape
39
-
40
-
41
- def intersperse(lst, item):
42
- result = [item] * (len(lst) * 2 + 1)
43
- result[1::2] = lst
44
- return result
45
-
46
-
47
- def kl_divergence(m_p, logs_p, m_q, logs_q):
48
- """KL(P||Q)"""
49
- kl = (logs_q - logs_p) - 0.5
50
- kl += 0.5 * (torch.exp(2. * logs_p) + ((m_p - m_q)**2)) * torch.exp(-2. * logs_q)
51
- return kl
52
-
53
-
54
- def rand_gumbel(shape):
55
- """Sample from the Gumbel distribution, protect from overflows."""
56
- uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
57
- return -torch.log(-torch.log(uniform_samples))
58
-
59
-
60
- def rand_gumbel_like(x):
61
- g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
62
- return g
63
-
64
-
65
- def slice_segments(x, ids_str, segment_size=4):
66
- ret = torch.zeros_like(x[:, :, :segment_size])
67
- for i in range(x.size(0)):
68
- idx_str = ids_str[i]
69
- idx_end = idx_str + segment_size
70
- ret[i] = x[i, :, idx_str:idx_end]
71
- return ret
72
-
73
-
74
- def rand_slice_segments(x, x_lengths=None, segment_size=4):
75
- b, d, t = x.size()
76
- if x_lengths is None:
77
- x_lengths = t
78
- ids_str_max = x_lengths - segment_size + 1
79
- ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
80
- ret = slice_segments(x, ids_str, segment_size)
81
- return ret, ids_str
82
-
83
-
84
- def rand_spec_segments(x, x_lengths=None, segment_size=4):
85
- b, d, t = x.size()
86
- if x_lengths is None:
87
- x_lengths = t
88
- ids_str_max = x_lengths - segment_size
89
- ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
90
- ret = slice_segments(x, ids_str, segment_size)
91
- return ret, ids_str
92
-
93
-
94
- def get_timing_signal_1d(
95
- length, channels, min_timescale=1.0, max_timescale=1.0e4):
96
- position = torch.arange(length, dtype=torch.float)
97
- num_timescales = channels // 2
98
- log_timescale_increment = (
99
- math.log(float(max_timescale) / float(min_timescale)) /
100
- (num_timescales - 1))
101
- inv_timescales = min_timescale * torch.exp(
102
- torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment)
103
- scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
104
- signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
105
- signal = F.pad(signal, [0, 0, 0, channels % 2])
106
- signal = signal.view(1, channels, length)
107
- return signal
108
-
109
-
110
- def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
111
- b, channels, length = x.size()
112
- signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
113
- return x + signal.to(dtype=x.dtype, device=x.device)
114
-
115
-
116
- def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
117
- b, channels, length = x.size()
118
- signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
119
- return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
120
-
121
-
122
- def subsequent_mask(length):
123
- mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
124
- return mask
125
-
126
-
127
- @torch.jit.script
128
- def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
129
- n_channels_int = n_channels[0]
130
- in_act = input_a + input_b
131
- t_act = torch.tanh(in_act[:, :n_channels_int, :])
132
- s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
133
- acts = t_act * s_act
134
- return acts
135
-
136
-
137
- def convert_pad_shape(pad_shape):
138
- l = pad_shape[::-1]
139
- pad_shape = [item for sublist in l for item in sublist]
140
- return pad_shape
141
-
142
-
143
- def shift_1d(x):
144
- x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
145
- return x
146
-
147
-
148
- def sequence_mask(length, max_length=None):
149
- if max_length is None:
150
- max_length = length.max()
151
- x = torch.arange(max_length, dtype=length.dtype, device=length.device)
152
- return x.unsqueeze(0) < length.unsqueeze(1)
153
-
154
-
155
- def generate_path(duration, mask):
156
- """
157
- duration: [b, 1, t_x]
158
- mask: [b, 1, t_y, t_x]
159
- """
160
- device = duration.device
161
-
162
- b, _, t_y, t_x = mask.shape
163
- cum_duration = torch.cumsum(duration, -1)
164
-
165
- cum_duration_flat = cum_duration.view(b * t_x)
166
- path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
167
- path = path.view(b, t_x, t_y)
168
- path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
169
- path = path.unsqueeze(1).transpose(2,3) * mask
170
- return path
171
-
172
-
173
- def clip_grad_value_(parameters, clip_value, norm_type=2):
174
- if isinstance(parameters, torch.Tensor):
175
- parameters = [parameters]
176
- parameters = list(filter(lambda p: p.grad is not None, parameters))
177
- norm_type = float(norm_type)
178
- if clip_value is not None:
179
- clip_value = float(clip_value)
180
-
181
- total_norm = 0
182
- for p in parameters:
183
- param_norm = p.grad.data.norm(norm_type)
184
- total_norm += param_norm.item() ** norm_type
185
- if clip_value is not None:
186
- p.grad.data.clamp_(min=-clip_value, max=clip_value)
187
- total_norm = total_norm ** (1. / norm_type)
188
- return total_norm
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/modules/losses.py DELETED
@@ -1,61 +0,0 @@
1
- import torch
2
- from torch.nn import functional as F
3
-
4
- import modules.commons as commons
5
-
6
-
7
- def feature_loss(fmap_r, fmap_g):
8
- loss = 0
9
- for dr, dg in zip(fmap_r, fmap_g):
10
- for rl, gl in zip(dr, dg):
11
- rl = rl.float().detach()
12
- gl = gl.float()
13
- loss += torch.mean(torch.abs(rl - gl))
14
-
15
- return loss * 2
16
-
17
-
18
- def discriminator_loss(disc_real_outputs, disc_generated_outputs):
19
- loss = 0
20
- r_losses = []
21
- g_losses = []
22
- for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
23
- dr = dr.float()
24
- dg = dg.float()
25
- r_loss = torch.mean((1-dr)**2)
26
- g_loss = torch.mean(dg**2)
27
- loss += (r_loss + g_loss)
28
- r_losses.append(r_loss.item())
29
- g_losses.append(g_loss.item())
30
-
31
- return loss, r_losses, g_losses
32
-
33
-
34
- def generator_loss(disc_outputs):
35
- loss = 0
36
- gen_losses = []
37
- for dg in disc_outputs:
38
- dg = dg.float()
39
- l = torch.mean((1-dg)**2)
40
- gen_losses.append(l)
41
- loss += l
42
-
43
- return loss, gen_losses
44
-
45
-
46
- def kl_loss(z_p, logs_q, m_p, logs_p, z_mask):
47
- """
48
- z_p, logs_q: [b, h, t_t]
49
- m_p, logs_p: [b, h, t_t]
50
- """
51
- z_p = z_p.float()
52
- logs_q = logs_q.float()
53
- m_p = m_p.float()
54
- logs_p = logs_p.float()
55
- z_mask = z_mask.float()
56
- #print(logs_p)
57
- kl = logs_p - logs_q - 0.5
58
- kl += 0.5 * ((z_p - m_p)**2) * torch.exp(-2. * logs_p)
59
- kl = torch.sum(kl * z_mask)
60
- l = kl / torch.sum(z_mask)
61
- return l
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/modules/mel_processing.py DELETED
@@ -1,112 +0,0 @@
1
- import math
2
- import os
3
- import random
4
- import torch
5
- from torch import nn
6
- import torch.nn.functional as F
7
- import torch.utils.data
8
- import numpy as np
9
- import librosa
10
- import librosa.util as librosa_util
11
- from librosa.util import normalize, pad_center, tiny
12
- from scipy.signal import get_window
13
- from scipy.io.wavfile import read
14
- from librosa.filters import mel as librosa_mel_fn
15
-
16
- MAX_WAV_VALUE = 32768.0
17
-
18
-
19
- def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
20
- """
21
- PARAMS
22
- ------
23
- C: compression factor
24
- """
25
- return torch.log(torch.clamp(x, min=clip_val) * C)
26
-
27
-
28
- def dynamic_range_decompression_torch(x, C=1):
29
- """
30
- PARAMS
31
- ------
32
- C: compression factor used to compress
33
- """
34
- return torch.exp(x) / C
35
-
36
-
37
- def spectral_normalize_torch(magnitudes):
38
- output = dynamic_range_compression_torch(magnitudes)
39
- return output
40
-
41
-
42
- def spectral_de_normalize_torch(magnitudes):
43
- output = dynamic_range_decompression_torch(magnitudes)
44
- return output
45
-
46
-
47
- mel_basis = {}
48
- hann_window = {}
49
-
50
-
51
- def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):
52
- if torch.min(y) < -1.:
53
- print('min value is ', torch.min(y))
54
- if torch.max(y) > 1.:
55
- print('max value is ', torch.max(y))
56
-
57
- global hann_window
58
- dtype_device = str(y.dtype) + '_' + str(y.device)
59
- wnsize_dtype_device = str(win_size) + '_' + dtype_device
60
- if wnsize_dtype_device not in hann_window:
61
- hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)
62
-
63
- y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')
64
- y = y.squeeze(1)
65
-
66
- spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],
67
- center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=False)
68
-
69
- spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
70
- return spec
71
-
72
-
73
- def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):
74
- global mel_basis
75
- dtype_device = str(spec.dtype) + '_' + str(spec.device)
76
- fmax_dtype_device = str(fmax) + '_' + dtype_device
77
- if fmax_dtype_device not in mel_basis:
78
- mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax)
79
- mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=spec.dtype, device=spec.device)
80
- spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
81
- spec = spectral_normalize_torch(spec)
82
- return spec
83
-
84
-
85
- def mel_spectrogram_torch(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False):
86
- if torch.min(y) < -1.:
87
- print('min value is ', torch.min(y))
88
- if torch.max(y) > 1.:
89
- print('max value is ', torch.max(y))
90
-
91
- global mel_basis, hann_window
92
- dtype_device = str(y.dtype) + '_' + str(y.device)
93
- fmax_dtype_device = str(fmax) + '_' + dtype_device
94
- wnsize_dtype_device = str(win_size) + '_' + dtype_device
95
- if fmax_dtype_device not in mel_basis:
96
- mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax)
97
- mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=y.dtype, device=y.device)
98
- if wnsize_dtype_device not in hann_window:
99
- hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)
100
-
101
- y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')
102
- y = y.squeeze(1)
103
-
104
- spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],
105
- center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=False)
106
-
107
- spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
108
-
109
- spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
110
- spec = spectral_normalize_torch(spec)
111
-
112
- return spec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/modules/modules.py DELETED
@@ -1,342 +0,0 @@
1
- import copy
2
- import math
3
- import numpy as np
4
- import scipy
5
- import torch
6
- from torch import nn
7
- from torch.nn import functional as F
8
-
9
- from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
10
- from torch.nn.utils import weight_norm, remove_weight_norm
11
-
12
- import modules.commons as commons
13
- from modules.commons import init_weights, get_padding
14
-
15
-
16
- LRELU_SLOPE = 0.1
17
-
18
-
19
- class LayerNorm(nn.Module):
20
- def __init__(self, channels, eps=1e-5):
21
- super().__init__()
22
- self.channels = channels
23
- self.eps = eps
24
-
25
- self.gamma = nn.Parameter(torch.ones(channels))
26
- self.beta = nn.Parameter(torch.zeros(channels))
27
-
28
- def forward(self, x):
29
- x = x.transpose(1, -1)
30
- x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
31
- return x.transpose(1, -1)
32
-
33
-
34
- class ConvReluNorm(nn.Module):
35
- def __init__(self, in_channels, hidden_channels, out_channels, kernel_size, n_layers, p_dropout):
36
- super().__init__()
37
- self.in_channels = in_channels
38
- self.hidden_channels = hidden_channels
39
- self.out_channels = out_channels
40
- self.kernel_size = kernel_size
41
- self.n_layers = n_layers
42
- self.p_dropout = p_dropout
43
- assert n_layers > 1, "Number of layers should be larger than 0."
44
-
45
- self.conv_layers = nn.ModuleList()
46
- self.norm_layers = nn.ModuleList()
47
- self.conv_layers.append(nn.Conv1d(in_channels, hidden_channels, kernel_size, padding=kernel_size//2))
48
- self.norm_layers.append(LayerNorm(hidden_channels))
49
- self.relu_drop = nn.Sequential(
50
- nn.ReLU(),
51
- nn.Dropout(p_dropout))
52
- for _ in range(n_layers-1):
53
- self.conv_layers.append(nn.Conv1d(hidden_channels, hidden_channels, kernel_size, padding=kernel_size//2))
54
- self.norm_layers.append(LayerNorm(hidden_channels))
55
- self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
56
- self.proj.weight.data.zero_()
57
- self.proj.bias.data.zero_()
58
-
59
- def forward(self, x, x_mask):
60
- x_org = x
61
- for i in range(self.n_layers):
62
- x = self.conv_layers[i](x * x_mask)
63
- x = self.norm_layers[i](x)
64
- x = self.relu_drop(x)
65
- x = x_org + self.proj(x)
66
- return x * x_mask
67
-
68
-
69
- class DDSConv(nn.Module):
70
- """
71
- Dialted and Depth-Separable Convolution
72
- """
73
- def __init__(self, channels, kernel_size, n_layers, p_dropout=0.):
74
- super().__init__()
75
- self.channels = channels
76
- self.kernel_size = kernel_size
77
- self.n_layers = n_layers
78
- self.p_dropout = p_dropout
79
-
80
- self.drop = nn.Dropout(p_dropout)
81
- self.convs_sep = nn.ModuleList()
82
- self.convs_1x1 = nn.ModuleList()
83
- self.norms_1 = nn.ModuleList()
84
- self.norms_2 = nn.ModuleList()
85
- for i in range(n_layers):
86
- dilation = kernel_size ** i
87
- padding = (kernel_size * dilation - dilation) // 2
88
- self.convs_sep.append(nn.Conv1d(channels, channels, kernel_size,
89
- groups=channels, dilation=dilation, padding=padding
90
- ))
91
- self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
92
- self.norms_1.append(LayerNorm(channels))
93
- self.norms_2.append(LayerNorm(channels))
94
-
95
- def forward(self, x, x_mask, g=None):
96
- if g is not None:
97
- x = x + g
98
- for i in range(self.n_layers):
99
- y = self.convs_sep[i](x * x_mask)
100
- y = self.norms_1[i](y)
101
- y = F.gelu(y)
102
- y = self.convs_1x1[i](y)
103
- y = self.norms_2[i](y)
104
- y = F.gelu(y)
105
- y = self.drop(y)
106
- x = x + y
107
- return x * x_mask
108
-
109
-
110
- class WN(torch.nn.Module):
111
- def __init__(self, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=0, p_dropout=0):
112
- super(WN, self).__init__()
113
- assert(kernel_size % 2 == 1)
114
- self.hidden_channels =hidden_channels
115
- self.kernel_size = kernel_size,
116
- self.dilation_rate = dilation_rate
117
- self.n_layers = n_layers
118
- self.gin_channels = gin_channels
119
- self.p_dropout = p_dropout
120
-
121
- self.in_layers = torch.nn.ModuleList()
122
- self.res_skip_layers = torch.nn.ModuleList()
123
- self.drop = nn.Dropout(p_dropout)
124
-
125
- if gin_channels != 0:
126
- cond_layer = torch.nn.Conv1d(gin_channels, 2*hidden_channels*n_layers, 1)
127
- self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name='weight')
128
-
129
- for i in range(n_layers):
130
- dilation = dilation_rate ** i
131
- padding = int((kernel_size * dilation - dilation) / 2)
132
- in_layer = torch.nn.Conv1d(hidden_channels, 2*hidden_channels, kernel_size,
133
- dilation=dilation, padding=padding)
134
- in_layer = torch.nn.utils.weight_norm(in_layer, name='weight')
135
- self.in_layers.append(in_layer)
136
-
137
- # last one is not necessary
138
- if i < n_layers - 1:
139
- res_skip_channels = 2 * hidden_channels
140
- else:
141
- res_skip_channels = hidden_channels
142
-
143
- res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
144
- res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name='weight')
145
- self.res_skip_layers.append(res_skip_layer)
146
-
147
- def forward(self, x, x_mask, g=None, **kwargs):
148
- output = torch.zeros_like(x)
149
- n_channels_tensor = torch.IntTensor([self.hidden_channels])
150
-
151
- if g is not None:
152
- g = self.cond_layer(g)
153
-
154
- for i in range(self.n_layers):
155
- x_in = self.in_layers[i](x)
156
- if g is not None:
157
- cond_offset = i * 2 * self.hidden_channels
158
- g_l = g[:,cond_offset:cond_offset+2*self.hidden_channels,:]
159
- else:
160
- g_l = torch.zeros_like(x_in)
161
-
162
- acts = commons.fused_add_tanh_sigmoid_multiply(
163
- x_in,
164
- g_l,
165
- n_channels_tensor)
166
- acts = self.drop(acts)
167
-
168
- res_skip_acts = self.res_skip_layers[i](acts)
169
- if i < self.n_layers - 1:
170
- res_acts = res_skip_acts[:,:self.hidden_channels,:]
171
- x = (x + res_acts) * x_mask
172
- output = output + res_skip_acts[:,self.hidden_channels:,:]
173
- else:
174
- output = output + res_skip_acts
175
- return output * x_mask
176
-
177
- def remove_weight_norm(self):
178
- if self.gin_channels != 0:
179
- torch.nn.utils.remove_weight_norm(self.cond_layer)
180
- for l in self.in_layers:
181
- torch.nn.utils.remove_weight_norm(l)
182
- for l in self.res_skip_layers:
183
- torch.nn.utils.remove_weight_norm(l)
184
-
185
-
186
- class ResBlock1(torch.nn.Module):
187
- def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
188
- super(ResBlock1, self).__init__()
189
- self.convs1 = nn.ModuleList([
190
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
191
- padding=get_padding(kernel_size, dilation[0]))),
192
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
193
- padding=get_padding(kernel_size, dilation[1]))),
194
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
195
- padding=get_padding(kernel_size, dilation[2])))
196
- ])
197
- self.convs1.apply(init_weights)
198
-
199
- self.convs2 = nn.ModuleList([
200
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
201
- padding=get_padding(kernel_size, 1))),
202
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
203
- padding=get_padding(kernel_size, 1))),
204
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
205
- padding=get_padding(kernel_size, 1)))
206
- ])
207
- self.convs2.apply(init_weights)
208
-
209
- def forward(self, x, x_mask=None):
210
- for c1, c2 in zip(self.convs1, self.convs2):
211
- xt = F.leaky_relu(x, LRELU_SLOPE)
212
- if x_mask is not None:
213
- xt = xt * x_mask
214
- xt = c1(xt)
215
- xt = F.leaky_relu(xt, LRELU_SLOPE)
216
- if x_mask is not None:
217
- xt = xt * x_mask
218
- xt = c2(xt)
219
- x = xt + x
220
- if x_mask is not None:
221
- x = x * x_mask
222
- return x
223
-
224
- def remove_weight_norm(self):
225
- for l in self.convs1:
226
- remove_weight_norm(l)
227
- for l in self.convs2:
228
- remove_weight_norm(l)
229
-
230
-
231
- class ResBlock2(torch.nn.Module):
232
- def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
233
- super(ResBlock2, self).__init__()
234
- self.convs = nn.ModuleList([
235
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
236
- padding=get_padding(kernel_size, dilation[0]))),
237
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
238
- padding=get_padding(kernel_size, dilation[1])))
239
- ])
240
- self.convs.apply(init_weights)
241
-
242
- def forward(self, x, x_mask=None):
243
- for c in self.convs:
244
- xt = F.leaky_relu(x, LRELU_SLOPE)
245
- if x_mask is not None:
246
- xt = xt * x_mask
247
- xt = c(xt)
248
- x = xt + x
249
- if x_mask is not None:
250
- x = x * x_mask
251
- return x
252
-
253
- def remove_weight_norm(self):
254
- for l in self.convs:
255
- remove_weight_norm(l)
256
-
257
-
258
- class Log(nn.Module):
259
- def forward(self, x, x_mask, reverse=False, **kwargs):
260
- if not reverse:
261
- y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
262
- logdet = torch.sum(-y, [1, 2])
263
- return y, logdet
264
- else:
265
- x = torch.exp(x) * x_mask
266
- return x
267
-
268
-
269
- class Flip(nn.Module):
270
- def forward(self, x, *args, reverse=False, **kwargs):
271
- x = torch.flip(x, [1])
272
- if not reverse:
273
- logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
274
- return x, logdet
275
- else:
276
- return x
277
-
278
-
279
- class ElementwiseAffine(nn.Module):
280
- def __init__(self, channels):
281
- super().__init__()
282
- self.channels = channels
283
- self.m = nn.Parameter(torch.zeros(channels,1))
284
- self.logs = nn.Parameter(torch.zeros(channels,1))
285
-
286
- def forward(self, x, x_mask, reverse=False, **kwargs):
287
- if not reverse:
288
- y = self.m + torch.exp(self.logs) * x
289
- y = y * x_mask
290
- logdet = torch.sum(self.logs * x_mask, [1,2])
291
- return y, logdet
292
- else:
293
- x = (x - self.m) * torch.exp(-self.logs) * x_mask
294
- return x
295
-
296
-
297
- class ResidualCouplingLayer(nn.Module):
298
- def __init__(self,
299
- channels,
300
- hidden_channels,
301
- kernel_size,
302
- dilation_rate,
303
- n_layers,
304
- p_dropout=0,
305
- gin_channels=0,
306
- mean_only=False):
307
- assert channels % 2 == 0, "channels should be divisible by 2"
308
- super().__init__()
309
- self.channels = channels
310
- self.hidden_channels = hidden_channels
311
- self.kernel_size = kernel_size
312
- self.dilation_rate = dilation_rate
313
- self.n_layers = n_layers
314
- self.half_channels = channels // 2
315
- self.mean_only = mean_only
316
-
317
- self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
318
- self.enc = WN(hidden_channels, kernel_size, dilation_rate, n_layers, p_dropout=p_dropout, gin_channels=gin_channels)
319
- self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
320
- self.post.weight.data.zero_()
321
- self.post.bias.data.zero_()
322
-
323
- def forward(self, x, x_mask, g=None, reverse=False):
324
- x0, x1 = torch.split(x, [self.half_channels]*2, 1)
325
- h = self.pre(x0) * x_mask
326
- h = self.enc(h, x_mask, g=g)
327
- stats = self.post(h) * x_mask
328
- if not self.mean_only:
329
- m, logs = torch.split(stats, [self.half_channels]*2, 1)
330
- else:
331
- m = stats
332
- logs = torch.zeros_like(m)
333
-
334
- if not reverse:
335
- x1 = m + x1 * torch.exp(logs) * x_mask
336
- x = torch.cat([x0, x1], 1)
337
- logdet = torch.sum(logs, [1,2])
338
- return x, logdet
339
- else:
340
- x1 = (x1 - m) * torch.exp(-logs) * x_mask
341
- x = torch.cat([x0, x1], 1)
342
- return x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/onnx_export.py DELETED
@@ -1,53 +0,0 @@
1
- import torch
2
- from onnxexport.model_onnx import SynthesizerTrn
3
- import utils
4
-
5
- def main(NetExport):
6
- path = "SoVits4.0"
7
- if NetExport:
8
- device = torch.device("cpu")
9
- hps = utils.get_hparams_from_file(f"checkpoints/{path}/config.json")
10
- SVCVITS = SynthesizerTrn(
11
- hps.data.filter_length // 2 + 1,
12
- hps.train.segment_size // hps.data.hop_length,
13
- **hps.model)
14
- _ = utils.load_checkpoint(f"checkpoints/{path}/model.pth", SVCVITS, None)
15
- _ = SVCVITS.eval().to(device)
16
- for i in SVCVITS.parameters():
17
- i.requires_grad = False
18
-
19
- test_hidden_unit = torch.rand(1, 10, 256)
20
- test_pitch = torch.rand(1, 10)
21
- test_mel2ph = torch.LongTensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).unsqueeze(0)
22
- test_uv = torch.ones(1, 10, dtype=torch.float32)
23
- test_noise = torch.randn(1, 192, 10)
24
- test_sid = torch.LongTensor([0])
25
- input_names = ["c", "f0", "mel2ph", "uv", "noise", "sid"]
26
- output_names = ["audio", ]
27
-
28
- torch.onnx.export(SVCVITS,
29
- (
30
- test_hidden_unit.to(device),
31
- test_pitch.to(device),
32
- test_mel2ph.to(device),
33
- test_uv.to(device),
34
- test_noise.to(device),
35
- test_sid.to(device)
36
- ),
37
- f"checkpoints/{path}/model.onnx",
38
- dynamic_axes={
39
- "c": [0, 1],
40
- "f0": [1],
41
- "mel2ph": [1],
42
- "uv": [1],
43
- "noise": [2],
44
- },
45
- do_constant_folding=False,
46
- opset_version=16,
47
- verbose=False,
48
- input_names=input_names,
49
- output_names=output_names)
50
-
51
-
52
- if __name__ == '__main__':
53
- main(True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/onnxexport/model_onnx.py DELETED
@@ -1,335 +0,0 @@
1
- import torch
2
- from torch import nn
3
- from torch.nn import functional as F
4
-
5
- import modules.attentions as attentions
6
- import modules.commons as commons
7
- import modules.modules as modules
8
-
9
- from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
10
- from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
11
-
12
- import utils
13
- from modules.commons import init_weights, get_padding
14
- from vdecoder.hifigan.models import Generator
15
- from utils import f0_to_coarse
16
-
17
-
18
- class ResidualCouplingBlock(nn.Module):
19
- def __init__(self,
20
- channels,
21
- hidden_channels,
22
- kernel_size,
23
- dilation_rate,
24
- n_layers,
25
- n_flows=4,
26
- gin_channels=0):
27
- super().__init__()
28
- self.channels = channels
29
- self.hidden_channels = hidden_channels
30
- self.kernel_size = kernel_size
31
- self.dilation_rate = dilation_rate
32
- self.n_layers = n_layers
33
- self.n_flows = n_flows
34
- self.gin_channels = gin_channels
35
-
36
- self.flows = nn.ModuleList()
37
- for i in range(n_flows):
38
- self.flows.append(
39
- modules.ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers,
40
- gin_channels=gin_channels, mean_only=True))
41
- self.flows.append(modules.Flip())
42
-
43
- def forward(self, x, x_mask, g=None, reverse=False):
44
- if not reverse:
45
- for flow in self.flows:
46
- x, _ = flow(x, x_mask, g=g, reverse=reverse)
47
- else:
48
- for flow in reversed(self.flows):
49
- x = flow(x, x_mask, g=g, reverse=reverse)
50
- return x
51
-
52
-
53
- class Encoder(nn.Module):
54
- def __init__(self,
55
- in_channels,
56
- out_channels,
57
- hidden_channels,
58
- kernel_size,
59
- dilation_rate,
60
- n_layers,
61
- gin_channels=0):
62
- super().__init__()
63
- self.in_channels = in_channels
64
- self.out_channels = out_channels
65
- self.hidden_channels = hidden_channels
66
- self.kernel_size = kernel_size
67
- self.dilation_rate = dilation_rate
68
- self.n_layers = n_layers
69
- self.gin_channels = gin_channels
70
-
71
- self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
72
- self.enc = modules.WN(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels)
73
- self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
74
-
75
- def forward(self, x, x_lengths, g=None):
76
- # print(x.shape,x_lengths.shape)
77
- x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
78
- x = self.pre(x) * x_mask
79
- x = self.enc(x, x_mask, g=g)
80
- stats = self.proj(x) * x_mask
81
- m, logs = torch.split(stats, self.out_channels, dim=1)
82
- z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
83
- return z, m, logs, x_mask
84
-
85
-
86
- class TextEncoder(nn.Module):
87
- def __init__(self,
88
- out_channels,
89
- hidden_channels,
90
- kernel_size,
91
- n_layers,
92
- gin_channels=0,
93
- filter_channels=None,
94
- n_heads=None,
95
- p_dropout=None):
96
- super().__init__()
97
- self.out_channels = out_channels
98
- self.hidden_channels = hidden_channels
99
- self.kernel_size = kernel_size
100
- self.n_layers = n_layers
101
- self.gin_channels = gin_channels
102
- self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
103
- self.f0_emb = nn.Embedding(256, hidden_channels)
104
-
105
- self.enc_ = attentions.Encoder(
106
- hidden_channels,
107
- filter_channels,
108
- n_heads,
109
- n_layers,
110
- kernel_size,
111
- p_dropout)
112
-
113
- def forward(self, x, x_mask, f0=None, z=None):
114
- x = x + self.f0_emb(f0).transpose(1, 2)
115
- x = self.enc_(x * x_mask, x_mask)
116
- stats = self.proj(x) * x_mask
117
- m, logs = torch.split(stats, self.out_channels, dim=1)
118
- z = (m + z * torch.exp(logs)) * x_mask
119
- return z, m, logs, x_mask
120
-
121
-
122
- class DiscriminatorP(torch.nn.Module):
123
- def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
124
- super(DiscriminatorP, self).__init__()
125
- self.period = period
126
- self.use_spectral_norm = use_spectral_norm
127
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
128
- self.convs = nn.ModuleList([
129
- norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
130
- norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
131
- norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
132
- norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
133
- norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(get_padding(kernel_size, 1), 0))),
134
- ])
135
- self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
136
-
137
- def forward(self, x):
138
- fmap = []
139
-
140
- # 1d to 2d
141
- b, c, t = x.shape
142
- if t % self.period != 0: # pad first
143
- n_pad = self.period - (t % self.period)
144
- x = F.pad(x, (0, n_pad), "reflect")
145
- t = t + n_pad
146
- x = x.view(b, c, t // self.period, self.period)
147
-
148
- for l in self.convs:
149
- x = l(x)
150
- x = F.leaky_relu(x, modules.LRELU_SLOPE)
151
- fmap.append(x)
152
- x = self.conv_post(x)
153
- fmap.append(x)
154
- x = torch.flatten(x, 1, -1)
155
-
156
- return x, fmap
157
-
158
-
159
- class DiscriminatorS(torch.nn.Module):
160
- def __init__(self, use_spectral_norm=False):
161
- super(DiscriminatorS, self).__init__()
162
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
163
- self.convs = nn.ModuleList([
164
- norm_f(Conv1d(1, 16, 15, 1, padding=7)),
165
- norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
166
- norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
167
- norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
168
- norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
169
- norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
170
- ])
171
- self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
172
-
173
- def forward(self, x):
174
- fmap = []
175
-
176
- for l in self.convs:
177
- x = l(x)
178
- x = F.leaky_relu(x, modules.LRELU_SLOPE)
179
- fmap.append(x)
180
- x = self.conv_post(x)
181
- fmap.append(x)
182
- x = torch.flatten(x, 1, -1)
183
-
184
- return x, fmap
185
-
186
-
187
- class F0Decoder(nn.Module):
188
- def __init__(self,
189
- out_channels,
190
- hidden_channels,
191
- filter_channels,
192
- n_heads,
193
- n_layers,
194
- kernel_size,
195
- p_dropout,
196
- spk_channels=0):
197
- super().__init__()
198
- self.out_channels = out_channels
199
- self.hidden_channels = hidden_channels
200
- self.filter_channels = filter_channels
201
- self.n_heads = n_heads
202
- self.n_layers = n_layers
203
- self.kernel_size = kernel_size
204
- self.p_dropout = p_dropout
205
- self.spk_channels = spk_channels
206
-
207
- self.prenet = nn.Conv1d(hidden_channels, hidden_channels, 3, padding=1)
208
- self.decoder = attentions.FFT(
209
- hidden_channels,
210
- filter_channels,
211
- n_heads,
212
- n_layers,
213
- kernel_size,
214
- p_dropout)
215
- self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
216
- self.f0_prenet = nn.Conv1d(1, hidden_channels, 3, padding=1)
217
- self.cond = nn.Conv1d(spk_channels, hidden_channels, 1)
218
-
219
- def forward(self, x, norm_f0, x_mask, spk_emb=None):
220
- x = torch.detach(x)
221
- if spk_emb is not None:
222
- x = x + self.cond(spk_emb)
223
- x += self.f0_prenet(norm_f0)
224
- x = self.prenet(x) * x_mask
225
- x = self.decoder(x * x_mask, x_mask)
226
- x = self.proj(x) * x_mask
227
- return x
228
-
229
-
230
- class SynthesizerTrn(nn.Module):
231
- """
232
- Synthesizer for Training
233
- """
234
-
235
- def __init__(self,
236
- spec_channels,
237
- segment_size,
238
- inter_channels,
239
- hidden_channels,
240
- filter_channels,
241
- n_heads,
242
- n_layers,
243
- kernel_size,
244
- p_dropout,
245
- resblock,
246
- resblock_kernel_sizes,
247
- resblock_dilation_sizes,
248
- upsample_rates,
249
- upsample_initial_channel,
250
- upsample_kernel_sizes,
251
- gin_channels,
252
- ssl_dim,
253
- n_speakers,
254
- sampling_rate=44100,
255
- **kwargs):
256
- super().__init__()
257
- self.spec_channels = spec_channels
258
- self.inter_channels = inter_channels
259
- self.hidden_channels = hidden_channels
260
- self.filter_channels = filter_channels
261
- self.n_heads = n_heads
262
- self.n_layers = n_layers
263
- self.kernel_size = kernel_size
264
- self.p_dropout = p_dropout
265
- self.resblock = resblock
266
- self.resblock_kernel_sizes = resblock_kernel_sizes
267
- self.resblock_dilation_sizes = resblock_dilation_sizes
268
- self.upsample_rates = upsample_rates
269
- self.upsample_initial_channel = upsample_initial_channel
270
- self.upsample_kernel_sizes = upsample_kernel_sizes
271
- self.segment_size = segment_size
272
- self.gin_channels = gin_channels
273
- self.ssl_dim = ssl_dim
274
- self.emb_g = nn.Embedding(n_speakers, gin_channels)
275
-
276
- self.pre = nn.Conv1d(ssl_dim, hidden_channels, kernel_size=5, padding=2)
277
-
278
- self.enc_p = TextEncoder(
279
- inter_channels,
280
- hidden_channels,
281
- filter_channels=filter_channels,
282
- n_heads=n_heads,
283
- n_layers=n_layers,
284
- kernel_size=kernel_size,
285
- p_dropout=p_dropout
286
- )
287
- hps = {
288
- "sampling_rate": sampling_rate,
289
- "inter_channels": inter_channels,
290
- "resblock": resblock,
291
- "resblock_kernel_sizes": resblock_kernel_sizes,
292
- "resblock_dilation_sizes": resblock_dilation_sizes,
293
- "upsample_rates": upsample_rates,
294
- "upsample_initial_channel": upsample_initial_channel,
295
- "upsample_kernel_sizes": upsample_kernel_sizes,
296
- "gin_channels": gin_channels,
297
- }
298
- self.dec = Generator(h=hps)
299
- self.enc_q = Encoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16, gin_channels=gin_channels)
300
- self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels)
301
- self.f0_decoder = F0Decoder(
302
- 1,
303
- hidden_channels,
304
- filter_channels,
305
- n_heads,
306
- n_layers,
307
- kernel_size,
308
- p_dropout,
309
- spk_channels=gin_channels
310
- )
311
- self.emb_uv = nn.Embedding(2, hidden_channels)
312
- self.predict_f0 = False
313
-
314
- def forward(self, c, f0, mel2ph, uv, noise=None, g=None):
315
-
316
- decoder_inp = F.pad(c, [0, 0, 1, 0])
317
- mel2ph_ = mel2ph.unsqueeze(2).repeat([1, 1, c.shape[-1]])
318
- c = torch.gather(decoder_inp, 1, mel2ph_).transpose(1, 2) # [B, T, H]
319
-
320
- c_lengths = (torch.ones(c.size(0)) * c.size(-1)).to(c.device)
321
- g = g.unsqueeze(0)
322
- g = self.emb_g(g).transpose(1, 2)
323
- x_mask = torch.unsqueeze(commons.sequence_mask(c_lengths, c.size(2)), 1).to(c.dtype)
324
- x = self.pre(c) * x_mask + self.emb_uv(uv.long()).transpose(1, 2)
325
-
326
- if self.predict_f0:
327
- lf0 = 2595. * torch.log10(1. + f0.unsqueeze(1) / 700.) / 500
328
- norm_lf0 = utils.normalize_f0(lf0, x_mask, uv, random_scale=False)
329
- pred_lf0 = self.f0_decoder(x, norm_lf0, x_mask, spk_emb=g)
330
- f0 = (700 * (torch.pow(10, pred_lf0 * 500 / 2595) - 1)).squeeze(1)
331
-
332
- z_p, m_p, logs_p, c_mask = self.enc_p(x, x_mask, f0=f0_to_coarse(f0), z=noise)
333
- z = self.flow(z_p, c_mask, g=g, reverse=True)
334
- o = self.dec(z * c_mask, g=g, f0=f0)
335
- return o
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/preprocess_flist_config.py DELETED
@@ -1,83 +0,0 @@
1
- import os
2
- import argparse
3
- import re
4
-
5
- from tqdm import tqdm
6
- from random import shuffle
7
- import json
8
- import wave
9
-
10
- config_template = json.load(open("configs_template/config_template.json"))
11
-
12
- pattern = re.compile(r'^[\.a-zA-Z0-9_\/]+$')
13
-
14
- def get_wav_duration(file_path):
15
- with wave.open(file_path, 'rb') as wav_file:
16
- # 获取音频帧数
17
- n_frames = wav_file.getnframes()
18
- # 获取采样率
19
- framerate = wav_file.getframerate()
20
- # 计算时长(秒)
21
- duration = n_frames / float(framerate)
22
- return duration
23
-
24
- if __name__ == "__main__":
25
- parser = argparse.ArgumentParser()
26
- parser.add_argument("--train_list", type=str, default="./filelists/train.txt", help="path to train list")
27
- parser.add_argument("--val_list", type=str, default="./filelists/val.txt", help="path to val list")
28
- parser.add_argument("--test_list", type=str, default="./filelists/test.txt", help="path to test list")
29
- parser.add_argument("--source_dir", type=str, default="./dataset/44k", help="path to source dir")
30
- args = parser.parse_args()
31
-
32
- train = []
33
- val = []
34
- test = []
35
- idx = 0
36
- spk_dict = {}
37
- spk_id = 0
38
- for speaker in tqdm(os.listdir(args.source_dir)):
39
- spk_dict[speaker] = spk_id
40
- spk_id += 1
41
- wavs = ["/".join([args.source_dir, speaker, i]) for i in os.listdir(os.path.join(args.source_dir, speaker))]
42
- new_wavs = []
43
- for file in wavs:
44
- if not file.endswith("wav"):
45
- continue
46
- if not pattern.match(file):
47
- print(f"warning:文件名{file}中包含非字母数字下划线,可能会导致错误。(也可能不会)")
48
- if get_wav_duration(file) < 0.3:
49
- print("skip too short audio:", file)
50
- continue
51
- new_wavs.append(file)
52
- wavs = new_wavs
53
- shuffle(wavs)
54
- train += wavs[2:-2]
55
- val += wavs[:2]
56
- test += wavs[-2:]
57
-
58
- shuffle(train)
59
- shuffle(val)
60
- shuffle(test)
61
-
62
- print("Writing", args.train_list)
63
- with open(args.train_list, "w") as f:
64
- for fname in tqdm(train):
65
- wavpath = fname
66
- f.write(wavpath + "\n")
67
-
68
- print("Writing", args.val_list)
69
- with open(args.val_list, "w") as f:
70
- for fname in tqdm(val):
71
- wavpath = fname
72
- f.write(wavpath + "\n")
73
-
74
- print("Writing", args.test_list)
75
- with open(args.test_list, "w") as f:
76
- for fname in tqdm(test):
77
- wavpath = fname
78
- f.write(wavpath + "\n")
79
-
80
- config_template["spk"] = spk_dict
81
- print("Writing configs/config.json")
82
- with open("configs/config.json", "w") as f:
83
- json.dump(config_template, f, indent=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/preprocess_hubert_f0.py DELETED
@@ -1,62 +0,0 @@
1
- import math
2
- import multiprocessing
3
- import os
4
- import argparse
5
- from random import shuffle
6
-
7
- import torch
8
- from glob import glob
9
- from tqdm import tqdm
10
-
11
- import utils
12
- import logging
13
- logging.getLogger('numba').setLevel(logging.WARNING)
14
- import librosa
15
- import numpy as np
16
-
17
- hps = utils.get_hparams_from_file("configs/config.json")
18
- sampling_rate = hps.data.sampling_rate
19
- hop_length = hps.data.hop_length
20
-
21
-
22
- def process_one(filename, hmodel):
23
- # print(filename)
24
- wav, sr = librosa.load(filename, sr=sampling_rate)
25
- soft_path = filename + ".soft.pt"
26
- if not os.path.exists(soft_path):
27
- devive = torch.device("cuda" if torch.cuda.is_available() else "cpu")
28
- wav16k = librosa.resample(wav, orig_sr=sampling_rate, target_sr=16000)
29
- wav16k = torch.from_numpy(wav16k).to(devive)
30
- c = utils.get_hubert_content(hmodel, wav_16k_tensor=wav16k)
31
- torch.save(c.cpu(), soft_path)
32
- f0_path = filename + ".f0.npy"
33
- if not os.path.exists(f0_path):
34
- f0 = utils.compute_f0_dio(wav, sampling_rate=sampling_rate, hop_length=hop_length)
35
- np.save(f0_path, f0)
36
-
37
-
38
- def process_batch(filenames):
39
- print("Loading hubert for content...")
40
- device = "cuda" if torch.cuda.is_available() else "cpu"
41
- hmodel = utils.get_hubert_model().to(device)
42
- print("Loaded hubert.")
43
- for filename in tqdm(filenames):
44
- process_one(filename, hmodel)
45
-
46
-
47
- if __name__ == "__main__":
48
- parser = argparse.ArgumentParser()
49
- parser.add_argument("--in_dir", type=str, default="dataset/44k", help="path to input dir")
50
-
51
- args = parser.parse_args()
52
- filenames = glob(f'{args.in_dir}/*/*.wav', recursive=True) # [:10]
53
- shuffle(filenames)
54
- multiprocessing.set_start_method('spawn',force=True)
55
-
56
- num_processes = 1
57
- chunk_size = int(math.ceil(len(filenames) / num_processes))
58
- chunks = [filenames[i:i + chunk_size] for i in range(0, len(filenames), chunk_size)]
59
- print([len(c) for c in chunks])
60
- processes = [multiprocessing.Process(target=process_batch, args=(chunk,)) for chunk in chunks]
61
- for p in processes:
62
- p.start()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/requirements_win.txt DELETED
@@ -1,21 +0,0 @@
1
- librosa==0.9.2
2
- fairseq==0.12.2
3
- Flask==2.1.2
4
- Flask_Cors==3.0.10
5
- gradio==3.4.1
6
- numpy==1.20.0
7
- playsound==1.3.0
8
- PyAudio==0.2.12
9
- pydub==0.25.1
10
- pyworld==0.3.0
11
- requests==2.28.1
12
- scipy==1.7.3
13
- sounddevice==0.4.5
14
- SoundFile==0.10.3.post1
15
- starlette==0.19.1
16
- tqdm==4.63.0
17
- scikit-maad
18
- praat-parselmouth
19
- onnx
20
- onnxsim
21
- onnxoptimizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/resample.py DELETED
@@ -1,48 +0,0 @@
1
- import os
2
- import argparse
3
- import librosa
4
- import numpy as np
5
- from multiprocessing import Pool, cpu_count
6
- from scipy.io import wavfile
7
- from tqdm import tqdm
8
-
9
-
10
- def process(item):
11
- spkdir, wav_name, args = item
12
- # speaker 's5', 'p280', 'p315' are excluded,
13
- speaker = spkdir.replace("\\", "/").split("/")[-1]
14
- wav_path = os.path.join(args.in_dir, speaker, wav_name)
15
- if os.path.exists(wav_path) and '.wav' in wav_path:
16
- os.makedirs(os.path.join(args.out_dir2, speaker), exist_ok=True)
17
- wav, sr = librosa.load(wav_path, sr=None)
18
- wav, _ = librosa.effects.trim(wav, top_db=20)
19
- peak = np.abs(wav).max()
20
- if peak > 1.0:
21
- wav = 0.98 * wav / peak
22
- wav2 = librosa.resample(wav, orig_sr=sr, target_sr=args.sr2)
23
- wav2 /= max(wav2.max(), -wav2.min())
24
- save_name = wav_name
25
- save_path2 = os.path.join(args.out_dir2, speaker, save_name)
26
- wavfile.write(
27
- save_path2,
28
- args.sr2,
29
- (wav2 * np.iinfo(np.int16).max).astype(np.int16)
30
- )
31
-
32
-
33
-
34
- if __name__ == "__main__":
35
- parser = argparse.ArgumentParser()
36
- parser.add_argument("--sr2", type=int, default=44100, help="sampling rate")
37
- parser.add_argument("--in_dir", type=str, default="./dataset_raw", help="path to source dir")
38
- parser.add_argument("--out_dir2", type=str, default="./dataset/44k", help="path to target dir")
39
- args = parser.parse_args()
40
- processs = cpu_count()-2 if cpu_count() >4 else 1
41
- pool = Pool(processes=processs)
42
-
43
- for speaker in os.listdir(args.in_dir):
44
- spk_dir = os.path.join(args.in_dir, speaker)
45
- if os.path.isdir(spk_dir):
46
- print(spk_dir)
47
- for _ in tqdm(pool.imap_unordered(process, [(spk_dir, i, args) for i in os.listdir(spk_dir) if i.endswith("wav")])):
48
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/spec_gen.py DELETED
@@ -1,22 +0,0 @@
1
- from data_utils import TextAudioSpeakerLoader
2
- import json
3
- from tqdm import tqdm
4
-
5
- from utils import HParams
6
-
7
- config_path = 'configs/config.json'
8
- with open(config_path, "r") as f:
9
- data = f.read()
10
- config = json.loads(data)
11
- hps = HParams(**config)
12
-
13
- train_dataset = TextAudioSpeakerLoader("filelists/train.txt", hps)
14
- test_dataset = TextAudioSpeakerLoader("filelists/test.txt", hps)
15
- eval_dataset = TextAudioSpeakerLoader("filelists/val.txt", hps)
16
-
17
- for _ in tqdm(train_dataset):
18
- pass
19
- for _ in tqdm(eval_dataset):
20
- pass
21
- for _ in tqdm(test_dataset):
22
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/train.py DELETED
@@ -1,310 +0,0 @@
1
- import logging
2
- import multiprocessing
3
- import time
4
-
5
- logging.getLogger('matplotlib').setLevel(logging.WARNING)
6
- import os
7
- import json
8
- import argparse
9
- import itertools
10
- import math
11
- import torch
12
- from torch import nn, optim
13
- from torch.nn import functional as F
14
- from torch.utils.data import DataLoader
15
- from torch.utils.tensorboard import SummaryWriter
16
- import torch.multiprocessing as mp
17
- import torch.distributed as dist
18
- from torch.nn.parallel import DistributedDataParallel as DDP
19
- from torch.cuda.amp import autocast, GradScaler
20
-
21
- import modules.commons as commons
22
- import utils
23
- from data_utils import TextAudioSpeakerLoader, TextAudioCollate
24
- from models import (
25
- SynthesizerTrn,
26
- MultiPeriodDiscriminator,
27
- )
28
- from modules.losses import (
29
- kl_loss,
30
- generator_loss, discriminator_loss, feature_loss
31
- )
32
-
33
- from modules.mel_processing import mel_spectrogram_torch, spec_to_mel_torch
34
-
35
- torch.backends.cudnn.benchmark = True
36
- global_step = 0
37
- start_time = time.time()
38
-
39
- # os.environ['TORCH_DISTRIBUTED_DEBUG'] = 'INFO'
40
-
41
-
42
- def main():
43
- """Assume Single Node Multi GPUs Training Only"""
44
- assert torch.cuda.is_available(), "CPU training is not allowed."
45
- hps = utils.get_hparams()
46
-
47
- n_gpus = torch.cuda.device_count()
48
- os.environ['MASTER_ADDR'] = 'localhost'
49
- os.environ['MASTER_PORT'] = hps.train.port
50
-
51
- mp.spawn(run, nprocs=n_gpus, args=(n_gpus, hps,))
52
-
53
-
54
- def run(rank, n_gpus, hps):
55
- global global_step
56
- if rank == 0:
57
- logger = utils.get_logger(hps.model_dir)
58
- logger.info(hps)
59
- utils.check_git_hash(hps.model_dir)
60
- writer = SummaryWriter(log_dir=hps.model_dir)
61
- writer_eval = SummaryWriter(log_dir=os.path.join(hps.model_dir, "eval"))
62
-
63
- # for pytorch on win, backend use gloo
64
- dist.init_process_group(backend= 'gloo' if os.name == 'nt' else 'nccl', init_method='env://', world_size=n_gpus, rank=rank)
65
- torch.manual_seed(hps.train.seed)
66
- torch.cuda.set_device(rank)
67
- collate_fn = TextAudioCollate()
68
- train_dataset = TextAudioSpeakerLoader(hps.data.training_files, hps)
69
- num_workers = 5 if multiprocessing.cpu_count() > 4 else multiprocessing.cpu_count()
70
- train_loader = DataLoader(train_dataset, num_workers=num_workers, shuffle=False, pin_memory=True,
71
- batch_size=hps.train.batch_size, collate_fn=collate_fn)
72
- if rank == 0:
73
- eval_dataset = TextAudioSpeakerLoader(hps.data.validation_files, hps)
74
- eval_loader = DataLoader(eval_dataset, num_workers=1, shuffle=False,
75
- batch_size=1, pin_memory=False,
76
- drop_last=False, collate_fn=collate_fn)
77
-
78
- net_g = SynthesizerTrn(
79
- hps.data.filter_length // 2 + 1,
80
- hps.train.segment_size // hps.data.hop_length,
81
- **hps.model).cuda(rank)
82
- net_d = MultiPeriodDiscriminator(hps.model.use_spectral_norm).cuda(rank)
83
- optim_g = torch.optim.AdamW(
84
- net_g.parameters(),
85
- hps.train.learning_rate,
86
- betas=hps.train.betas,
87
- eps=hps.train.eps)
88
- optim_d = torch.optim.AdamW(
89
- net_d.parameters(),
90
- hps.train.learning_rate,
91
- betas=hps.train.betas,
92
- eps=hps.train.eps)
93
- net_g = DDP(net_g, device_ids=[rank]) # , find_unused_parameters=True)
94
- net_d = DDP(net_d, device_ids=[rank])
95
-
96
- skip_optimizer = False
97
- try:
98
- _, _, _, epoch_str = utils.load_checkpoint(utils.latest_checkpoint_path(hps.model_dir, "G_*.pth"), net_g,
99
- optim_g, skip_optimizer)
100
- _, _, _, epoch_str = utils.load_checkpoint(utils.latest_checkpoint_path(hps.model_dir, "D_*.pth"), net_d,
101
- optim_d, skip_optimizer)
102
- epoch_str = max(epoch_str, 1)
103
- global_step = (epoch_str - 1) * len(train_loader)
104
- except:
105
- print("load old checkpoint failed...")
106
- epoch_str = 1
107
- global_step = 0
108
- if skip_optimizer:
109
- epoch_str = 1
110
- global_step = 0
111
-
112
- scheduler_g = torch.optim.lr_scheduler.ExponentialLR(optim_g, gamma=hps.train.lr_decay, last_epoch=epoch_str - 2)
113
- scheduler_d = torch.optim.lr_scheduler.ExponentialLR(optim_d, gamma=hps.train.lr_decay, last_epoch=epoch_str - 2)
114
-
115
- scaler = GradScaler(enabled=hps.train.fp16_run)
116
-
117
- for epoch in range(epoch_str, hps.train.epochs + 1):
118
- if rank == 0:
119
- train_and_evaluate(rank, epoch, hps, [net_g, net_d], [optim_g, optim_d], [scheduler_g, scheduler_d], scaler,
120
- [train_loader, eval_loader], logger, [writer, writer_eval])
121
- else:
122
- train_and_evaluate(rank, epoch, hps, [net_g, net_d], [optim_g, optim_d], [scheduler_g, scheduler_d], scaler,
123
- [train_loader, None], None, None)
124
- scheduler_g.step()
125
- scheduler_d.step()
126
-
127
-
128
- def train_and_evaluate(rank, epoch, hps, nets, optims, schedulers, scaler, loaders, logger, writers):
129
- net_g, net_d = nets
130
- optim_g, optim_d = optims
131
- scheduler_g, scheduler_d = schedulers
132
- train_loader, eval_loader = loaders
133
- if writers is not None:
134
- writer, writer_eval = writers
135
-
136
- # train_loader.batch_sampler.set_epoch(epoch)
137
- global global_step
138
-
139
- net_g.train()
140
- net_d.train()
141
- for batch_idx, items in enumerate(train_loader):
142
- c, f0, spec, y, spk, lengths, uv = items
143
- g = spk.cuda(rank, non_blocking=True)
144
- spec, y = spec.cuda(rank, non_blocking=True), y.cuda(rank, non_blocking=True)
145
- c = c.cuda(rank, non_blocking=True)
146
- f0 = f0.cuda(rank, non_blocking=True)
147
- uv = uv.cuda(rank, non_blocking=True)
148
- lengths = lengths.cuda(rank, non_blocking=True)
149
- mel = spec_to_mel_torch(
150
- spec,
151
- hps.data.filter_length,
152
- hps.data.n_mel_channels,
153
- hps.data.sampling_rate,
154
- hps.data.mel_fmin,
155
- hps.data.mel_fmax)
156
-
157
- with autocast(enabled=hps.train.fp16_run):
158
- y_hat, ids_slice, z_mask, \
159
- (z, z_p, m_p, logs_p, m_q, logs_q), pred_lf0, norm_lf0, lf0 = net_g(c, f0, uv, spec, g=g, c_lengths=lengths,
160
- spec_lengths=lengths)
161
-
162
- y_mel = commons.slice_segments(mel, ids_slice, hps.train.segment_size // hps.data.hop_length)
163
- y_hat_mel = mel_spectrogram_torch(
164
- y_hat.squeeze(1),
165
- hps.data.filter_length,
166
- hps.data.n_mel_channels,
167
- hps.data.sampling_rate,
168
- hps.data.hop_length,
169
- hps.data.win_length,
170
- hps.data.mel_fmin,
171
- hps.data.mel_fmax
172
- )
173
- y = commons.slice_segments(y, ids_slice * hps.data.hop_length, hps.train.segment_size) # slice
174
-
175
- # Discriminator
176
- y_d_hat_r, y_d_hat_g, _, _ = net_d(y, y_hat.detach())
177
-
178
- with autocast(enabled=False):
179
- loss_disc, losses_disc_r, losses_disc_g = discriminator_loss(y_d_hat_r, y_d_hat_g)
180
- loss_disc_all = loss_disc
181
-
182
- optim_d.zero_grad()
183
- scaler.scale(loss_disc_all).backward()
184
- scaler.unscale_(optim_d)
185
- grad_norm_d = commons.clip_grad_value_(net_d.parameters(), None)
186
- scaler.step(optim_d)
187
-
188
- with autocast(enabled=hps.train.fp16_run):
189
- # Generator
190
- y_d_hat_r, y_d_hat_g, fmap_r, fmap_g = net_d(y, y_hat)
191
- with autocast(enabled=False):
192
- loss_mel = F.l1_loss(y_mel, y_hat_mel) * hps.train.c_mel
193
- loss_kl = kl_loss(z_p, logs_q, m_p, logs_p, z_mask) * hps.train.c_kl
194
- loss_fm = feature_loss(fmap_r, fmap_g)
195
- loss_gen, losses_gen = generator_loss(y_d_hat_g)
196
- loss_lf0 = F.mse_loss(pred_lf0, lf0)
197
- loss_gen_all = loss_gen + loss_fm + loss_mel + loss_kl + loss_lf0
198
- optim_g.zero_grad()
199
- scaler.scale(loss_gen_all).backward()
200
- scaler.unscale_(optim_g)
201
- grad_norm_g = commons.clip_grad_value_(net_g.parameters(), None)
202
- scaler.step(optim_g)
203
- scaler.update()
204
-
205
- if rank == 0:
206
- if global_step % hps.train.log_interval == 0:
207
- lr = optim_g.param_groups[0]['lr']
208
- losses = [loss_disc, loss_gen, loss_fm, loss_mel, loss_kl]
209
- logger.info('Train Epoch: {} [{:.0f}%]'.format(
210
- epoch,
211
- 100. * batch_idx / len(train_loader)))
212
- logger.info(f"Losses: {[x.item() for x in losses]}, step: {global_step}, lr: {lr}")
213
-
214
- scalar_dict = {"loss/g/total": loss_gen_all, "loss/d/total": loss_disc_all, "learning_rate": lr,
215
- "grad_norm_d": grad_norm_d, "grad_norm_g": grad_norm_g}
216
- scalar_dict.update({"loss/g/fm": loss_fm, "loss/g/mel": loss_mel, "loss/g/kl": loss_kl,
217
- "loss/g/lf0": loss_lf0})
218
-
219
- # scalar_dict.update({"loss/g/{}".format(i): v for i, v in enumerate(losses_gen)})
220
- # scalar_dict.update({"loss/d_r/{}".format(i): v for i, v in enumerate(losses_disc_r)})
221
- # scalar_dict.update({"loss/d_g/{}".format(i): v for i, v in enumerate(losses_disc_g)})
222
- image_dict = {
223
- "slice/mel_org": utils.plot_spectrogram_to_numpy(y_mel[0].data.cpu().numpy()),
224
- "slice/mel_gen": utils.plot_spectrogram_to_numpy(y_hat_mel[0].data.cpu().numpy()),
225
- "all/mel": utils.plot_spectrogram_to_numpy(mel[0].data.cpu().numpy()),
226
- "all/lf0": utils.plot_data_to_numpy(lf0[0, 0, :].cpu().numpy(),
227
- pred_lf0[0, 0, :].detach().cpu().numpy()),
228
- "all/norm_lf0": utils.plot_data_to_numpy(lf0[0, 0, :].cpu().numpy(),
229
- norm_lf0[0, 0, :].detach().cpu().numpy())
230
- }
231
-
232
- utils.summarize(
233
- writer=writer,
234
- global_step=global_step,
235
- images=image_dict,
236
- scalars=scalar_dict
237
- )
238
-
239
- if global_step % hps.train.eval_interval == 0:
240
- evaluate(hps, net_g, eval_loader, writer_eval)
241
- utils.save_checkpoint(net_g, optim_g, hps.train.learning_rate, epoch,
242
- os.path.join(hps.model_dir, "G_{}.pth".format(global_step)))
243
- utils.save_checkpoint(net_d, optim_d, hps.train.learning_rate, epoch,
244
- os.path.join(hps.model_dir, "D_{}.pth".format(global_step)))
245
- keep_ckpts = getattr(hps.train, 'keep_ckpts', 0)
246
- if keep_ckpts > 0:
247
- utils.clean_checkpoints(path_to_models=hps.model_dir, n_ckpts_to_keep=keep_ckpts, sort_by_time=True)
248
-
249
- global_step += 1
250
-
251
- if rank == 0:
252
- global start_time
253
- now = time.time()
254
- durtaion = format(now - start_time, '.2f')
255
- logger.info(f'====> Epoch: {epoch}, cost {durtaion} s')
256
- start_time = now
257
-
258
-
259
- def evaluate(hps, generator, eval_loader, writer_eval):
260
- generator.eval()
261
- image_dict = {}
262
- audio_dict = {}
263
- with torch.no_grad():
264
- for batch_idx, items in enumerate(eval_loader):
265
- c, f0, spec, y, spk, _, uv = items
266
- g = spk[:1].cuda(0)
267
- spec, y = spec[:1].cuda(0), y[:1].cuda(0)
268
- c = c[:1].cuda(0)
269
- f0 = f0[:1].cuda(0)
270
- uv= uv[:1].cuda(0)
271
- mel = spec_to_mel_torch(
272
- spec,
273
- hps.data.filter_length,
274
- hps.data.n_mel_channels,
275
- hps.data.sampling_rate,
276
- hps.data.mel_fmin,
277
- hps.data.mel_fmax)
278
- y_hat = generator.module.infer(c, f0, uv, g=g)
279
-
280
- y_hat_mel = mel_spectrogram_torch(
281
- y_hat.squeeze(1).float(),
282
- hps.data.filter_length,
283
- hps.data.n_mel_channels,
284
- hps.data.sampling_rate,
285
- hps.data.hop_length,
286
- hps.data.win_length,
287
- hps.data.mel_fmin,
288
- hps.data.mel_fmax
289
- )
290
-
291
- audio_dict.update({
292
- f"gen/audio_{batch_idx}": y_hat[0],
293
- f"gt/audio_{batch_idx}": y[0]
294
- })
295
- image_dict.update({
296
- f"gen/mel": utils.plot_spectrogram_to_numpy(y_hat_mel[0].cpu().numpy()),
297
- "gt/mel": utils.plot_spectrogram_to_numpy(mel[0].cpu().numpy())
298
- })
299
- utils.summarize(
300
- writer=writer_eval,
301
- global_step=global_step,
302
- images=image_dict,
303
- audios=audio_dict,
304
- audio_sampling_rate=hps.data.sampling_rate
305
- )
306
- generator.train()
307
-
308
-
309
- if __name__ == "__main__":
310
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/utils.py DELETED
@@ -1,502 +0,0 @@
1
- import os
2
- import glob
3
- import re
4
- import sys
5
- import argparse
6
- import logging
7
- import json
8
- import subprocess
9
- import random
10
-
11
- import librosa
12
- import numpy as np
13
- from scipy.io.wavfile import read
14
- import torch
15
- from torch.nn import functional as F
16
- from modules.commons import sequence_mask
17
- from hubert import hubert_model
18
- MATPLOTLIB_FLAG = False
19
-
20
- logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
21
- logger = logging
22
-
23
- f0_bin = 256
24
- f0_max = 1100.0
25
- f0_min = 50.0
26
- f0_mel_min = 1127 * np.log(1 + f0_min / 700)
27
- f0_mel_max = 1127 * np.log(1 + f0_max / 700)
28
-
29
-
30
- # def normalize_f0(f0, random_scale=True):
31
- # f0_norm = f0.clone() # create a copy of the input Tensor
32
- # batch_size, _, frame_length = f0_norm.shape
33
- # for i in range(batch_size):
34
- # means = torch.mean(f0_norm[i, 0, :])
35
- # if random_scale:
36
- # factor = random.uniform(0.8, 1.2)
37
- # else:
38
- # factor = 1
39
- # f0_norm[i, 0, :] = (f0_norm[i, 0, :] - means) * factor
40
- # return f0_norm
41
- # def normalize_f0(f0, random_scale=True):
42
- # means = torch.mean(f0[:, 0, :], dim=1, keepdim=True)
43
- # if random_scale:
44
- # factor = torch.Tensor(f0.shape[0],1).uniform_(0.8, 1.2).to(f0.device)
45
- # else:
46
- # factor = torch.ones(f0.shape[0], 1, 1).to(f0.device)
47
- # f0_norm = (f0 - means.unsqueeze(-1)) * factor.unsqueeze(-1)
48
- # return f0_norm
49
- def normalize_f0(f0, x_mask, uv, random_scale=True):
50
- # calculate means based on x_mask
51
- uv_sum = torch.sum(uv, dim=1, keepdim=True)
52
- uv_sum[uv_sum == 0] = 9999
53
- means = torch.sum(f0[:, 0, :] * uv, dim=1, keepdim=True) / uv_sum
54
-
55
- if random_scale:
56
- factor = torch.Tensor(f0.shape[0], 1).uniform_(0.8, 1.2).to(f0.device)
57
- else:
58
- factor = torch.ones(f0.shape[0], 1).to(f0.device)
59
- # normalize f0 based on means and factor
60
- f0_norm = (f0 - means.unsqueeze(-1)) * factor.unsqueeze(-1)
61
- if torch.isnan(f0_norm).any():
62
- exit(0)
63
- return f0_norm * x_mask
64
-
65
-
66
- def plot_data_to_numpy(x, y):
67
- global MATPLOTLIB_FLAG
68
- if not MATPLOTLIB_FLAG:
69
- import matplotlib
70
- matplotlib.use("Agg")
71
- MATPLOTLIB_FLAG = True
72
- mpl_logger = logging.getLogger('matplotlib')
73
- mpl_logger.setLevel(logging.WARNING)
74
- import matplotlib.pylab as plt
75
- import numpy as np
76
-
77
- fig, ax = plt.subplots(figsize=(10, 2))
78
- plt.plot(x)
79
- plt.plot(y)
80
- plt.tight_layout()
81
-
82
- fig.canvas.draw()
83
- data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
84
- data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
85
- plt.close()
86
- return data
87
-
88
-
89
-
90
- def interpolate_f0(f0):
91
- '''
92
- 对F0进行插值处理
93
- '''
94
-
95
- data = np.reshape(f0, (f0.size, 1))
96
-
97
- vuv_vector = np.zeros((data.size, 1), dtype=np.float32)
98
- vuv_vector[data > 0.0] = 1.0
99
- vuv_vector[data <= 0.0] = 0.0
100
-
101
- ip_data = data
102
-
103
- frame_number = data.size
104
- last_value = 0.0
105
- for i in range(frame_number):
106
- if data[i] <= 0.0:
107
- j = i + 1
108
- for j in range(i + 1, frame_number):
109
- if data[j] > 0.0:
110
- break
111
- if j < frame_number - 1:
112
- if last_value > 0.0:
113
- step = (data[j] - data[i - 1]) / float(j - i)
114
- for k in range(i, j):
115
- ip_data[k] = data[i - 1] + step * (k - i + 1)
116
- else:
117
- for k in range(i, j):
118
- ip_data[k] = data[j]
119
- else:
120
- for k in range(i, frame_number):
121
- ip_data[k] = last_value
122
- else:
123
- ip_data[i] = data[i]
124
- last_value = data[i]
125
-
126
- return ip_data[:,0], vuv_vector[:,0]
127
-
128
-
129
- def compute_f0_parselmouth(wav_numpy, p_len=None, sampling_rate=44100, hop_length=512):
130
- import parselmouth
131
- x = wav_numpy
132
- if p_len is None:
133
- p_len = x.shape[0]//hop_length
134
- else:
135
- assert abs(p_len-x.shape[0]//hop_length) < 4, "pad length error"
136
- time_step = hop_length / sampling_rate * 1000
137
- f0_min = 50
138
- f0_max = 1100
139
- f0 = parselmouth.Sound(x, sampling_rate).to_pitch_ac(
140
- time_step=time_step / 1000, voicing_threshold=0.6,
141
- pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency']
142
-
143
- pad_size=(p_len - len(f0) + 1) // 2
144
- if(pad_size>0 or p_len - len(f0) - pad_size>0):
145
- f0 = np.pad(f0,[[pad_size,p_len - len(f0) - pad_size]], mode='constant')
146
- return f0
147
-
148
- def resize_f0(x, target_len):
149
- source = np.array(x)
150
- source[source<0.001] = np.nan
151
- target = np.interp(np.arange(0, len(source)*target_len, len(source))/ target_len, np.arange(0, len(source)), source)
152
- res = np.nan_to_num(target)
153
- return res
154
-
155
- def compute_f0_dio(wav_numpy, p_len=None, sampling_rate=44100, hop_length=512):
156
- import pyworld
157
- if p_len is None:
158
- p_len = wav_numpy.shape[0]//hop_length
159
- f0, t = pyworld.dio(
160
- wav_numpy.astype(np.double),
161
- fs=sampling_rate,
162
- f0_ceil=800,
163
- frame_period=1000 * hop_length / sampling_rate,
164
- )
165
- f0 = pyworld.stonemask(wav_numpy.astype(np.double), f0, t, sampling_rate)
166
- for index, pitch in enumerate(f0):
167
- f0[index] = round(pitch, 1)
168
- return resize_f0(f0, p_len)
169
-
170
- def f0_to_coarse(f0):
171
- is_torch = isinstance(f0, torch.Tensor)
172
- f0_mel = 1127 * (1 + f0 / 700).log() if is_torch else 1127 * np.log(1 + f0 / 700)
173
- f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * (f0_bin - 2) / (f0_mel_max - f0_mel_min) + 1
174
-
175
- f0_mel[f0_mel <= 1] = 1
176
- f0_mel[f0_mel > f0_bin - 1] = f0_bin - 1
177
- f0_coarse = (f0_mel + 0.5).long() if is_torch else np.rint(f0_mel).astype(np.int)
178
- assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (f0_coarse.max(), f0_coarse.min())
179
- return f0_coarse
180
-
181
-
182
- def get_hubert_model():
183
- vec_path = "hubert/checkpoint_best_legacy_500.pt"
184
- print("load model(s) from {}".format(vec_path))
185
- from fairseq import checkpoint_utils
186
- models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
187
- [vec_path],
188
- suffix="",
189
- )
190
- model = models[0]
191
- model.eval()
192
- return model
193
-
194
- def get_hubert_content(hmodel, wav_16k_tensor):
195
- feats = wav_16k_tensor
196
- if feats.dim() == 2: # double channels
197
- feats = feats.mean(-1)
198
- assert feats.dim() == 1, feats.dim()
199
- feats = feats.view(1, -1)
200
- padding_mask = torch.BoolTensor(feats.shape).fill_(False)
201
- inputs = {
202
- "source": feats.to(wav_16k_tensor.device),
203
- "padding_mask": padding_mask.to(wav_16k_tensor.device),
204
- "output_layer": 9, # layer 9
205
- }
206
- with torch.no_grad():
207
- logits = hmodel.extract_features(**inputs)
208
- feats = hmodel.final_proj(logits[0])
209
- return feats.transpose(1, 2)
210
-
211
-
212
- def get_content(cmodel, y):
213
- with torch.no_grad():
214
- c = cmodel.extract_features(y.squeeze(1))[0]
215
- c = c.transpose(1, 2)
216
- return c
217
-
218
-
219
-
220
- def load_checkpoint(checkpoint_path, model, optimizer=None, skip_optimizer=False):
221
- assert os.path.isfile(checkpoint_path)
222
- checkpoint_dict = torch.load(checkpoint_path, map_location='cpu')
223
- iteration = checkpoint_dict['iteration']
224
- learning_rate = checkpoint_dict['learning_rate']
225
- if optimizer is not None and not skip_optimizer and checkpoint_dict['optimizer'] is not None:
226
- optimizer.load_state_dict(checkpoint_dict['optimizer'])
227
- saved_state_dict = checkpoint_dict['model']
228
- if hasattr(model, 'module'):
229
- state_dict = model.module.state_dict()
230
- else:
231
- state_dict = model.state_dict()
232
- new_state_dict = {}
233
- for k, v in state_dict.items():
234
- try:
235
- # assert "dec" in k or "disc" in k
236
- # print("load", k)
237
- new_state_dict[k] = saved_state_dict[k]
238
- assert saved_state_dict[k].shape == v.shape, (saved_state_dict[k].shape, v.shape)
239
- except:
240
- print("error, %s is not in the checkpoint" % k)
241
- logger.info("%s is not in the checkpoint" % k)
242
- new_state_dict[k] = v
243
- if hasattr(model, 'module'):
244
- model.module.load_state_dict(new_state_dict)
245
- else:
246
- model.load_state_dict(new_state_dict)
247
- print("load ")
248
- logger.info("Loaded checkpoint '{}' (iteration {})".format(
249
- checkpoint_path, iteration))
250
- return model, optimizer, learning_rate, iteration
251
-
252
-
253
- def save_checkpoint(model, optimizer, learning_rate, iteration, checkpoint_path):
254
- logger.info("Saving model and optimizer state at iteration {} to {}".format(
255
- iteration, checkpoint_path))
256
- if hasattr(model, 'module'):
257
- state_dict = model.module.state_dict()
258
- else:
259
- state_dict = model.state_dict()
260
- torch.save({'model': state_dict,
261
- 'iteration': iteration,
262
- 'optimizer': optimizer.state_dict(),
263
- 'learning_rate': learning_rate}, checkpoint_path)
264
-
265
- def clean_checkpoints(path_to_models='logs/44k/', n_ckpts_to_keep=2, sort_by_time=True):
266
- """Freeing up space by deleting saved ckpts
267
-
268
- Arguments:
269
- path_to_models -- Path to the model directory
270
- n_ckpts_to_keep -- Number of ckpts to keep, excluding G_0.pth and D_0.pth
271
- sort_by_time -- True -> chronologically delete ckpts
272
- False -> lexicographically delete ckpts
273
- """
274
- ckpts_files = [f for f in os.listdir(path_to_models) if os.path.isfile(os.path.join(path_to_models, f))]
275
- name_key = (lambda _f: int(re.compile('._(\d+)\.pth').match(_f).group(1)))
276
- time_key = (lambda _f: os.path.getmtime(os.path.join(path_to_models, _f)))
277
- sort_key = time_key if sort_by_time else name_key
278
- x_sorted = lambda _x: sorted([f for f in ckpts_files if f.startswith(_x) and not f.endswith('_0.pth')], key=sort_key)
279
- to_del = [os.path.join(path_to_models, fn) for fn in
280
- (x_sorted('G')[:-n_ckpts_to_keep] + x_sorted('D')[:-n_ckpts_to_keep])]
281
- del_info = lambda fn: logger.info(f".. Free up space by deleting ckpt {fn}")
282
- del_routine = lambda x: [os.remove(x), del_info(x)]
283
- rs = [del_routine(fn) for fn in to_del]
284
-
285
- def summarize(writer, global_step, scalars={}, histograms={}, images={}, audios={}, audio_sampling_rate=22050):
286
- for k, v in scalars.items():
287
- writer.add_scalar(k, v, global_step)
288
- for k, v in histograms.items():
289
- writer.add_histogram(k, v, global_step)
290
- for k, v in images.items():
291
- writer.add_image(k, v, global_step, dataformats='HWC')
292
- for k, v in audios.items():
293
- writer.add_audio(k, v, global_step, audio_sampling_rate)
294
-
295
-
296
- def latest_checkpoint_path(dir_path, regex="G_*.pth"):
297
- f_list = glob.glob(os.path.join(dir_path, regex))
298
- f_list.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
299
- x = f_list[-1]
300
- print(x)
301
- return x
302
-
303
-
304
- def plot_spectrogram_to_numpy(spectrogram):
305
- global MATPLOTLIB_FLAG
306
- if not MATPLOTLIB_FLAG:
307
- import matplotlib
308
- matplotlib.use("Agg")
309
- MATPLOTLIB_FLAG = True
310
- mpl_logger = logging.getLogger('matplotlib')
311
- mpl_logger.setLevel(logging.WARNING)
312
- import matplotlib.pylab as plt
313
- import numpy as np
314
-
315
- fig, ax = plt.subplots(figsize=(10,2))
316
- im = ax.imshow(spectrogram, aspect="auto", origin="lower",
317
- interpolation='none')
318
- plt.colorbar(im, ax=ax)
319
- plt.xlabel("Frames")
320
- plt.ylabel("Channels")
321
- plt.tight_layout()
322
-
323
- fig.canvas.draw()
324
- data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
325
- data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
326
- plt.close()
327
- return data
328
-
329
-
330
- def plot_alignment_to_numpy(alignment, info=None):
331
- global MATPLOTLIB_FLAG
332
- if not MATPLOTLIB_FLAG:
333
- import matplotlib
334
- matplotlib.use("Agg")
335
- MATPLOTLIB_FLAG = True
336
- mpl_logger = logging.getLogger('matplotlib')
337
- mpl_logger.setLevel(logging.WARNING)
338
- import matplotlib.pylab as plt
339
- import numpy as np
340
-
341
- fig, ax = plt.subplots(figsize=(6, 4))
342
- im = ax.imshow(alignment.transpose(), aspect='auto', origin='lower',
343
- interpolation='none')
344
- fig.colorbar(im, ax=ax)
345
- xlabel = 'Decoder timestep'
346
- if info is not None:
347
- xlabel += '\n\n' + info
348
- plt.xlabel(xlabel)
349
- plt.ylabel('Encoder timestep')
350
- plt.tight_layout()
351
-
352
- fig.canvas.draw()
353
- data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
354
- data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
355
- plt.close()
356
- return data
357
-
358
-
359
- def load_wav_to_torch(full_path):
360
- sampling_rate, data = read(full_path)
361
- return torch.FloatTensor(data.astype(np.float32)), sampling_rate
362
-
363
-
364
- def load_filepaths_and_text(filename, split="|"):
365
- with open(filename, encoding='utf-8') as f:
366
- filepaths_and_text = [line.strip().split(split) for line in f]
367
- return filepaths_and_text
368
-
369
-
370
- def get_hparams(init=True):
371
- parser = argparse.ArgumentParser()
372
- parser.add_argument('-c', '--config', type=str, default="./configs/base.json",
373
- help='JSON file for configuration')
374
- parser.add_argument('-m', '--model', type=str, required=True,
375
- help='Model name')
376
-
377
- args = parser.parse_args()
378
- model_dir = os.path.join("./logs", args.model)
379
-
380
- if not os.path.exists(model_dir):
381
- os.makedirs(model_dir)
382
-
383
- config_path = args.config
384
- config_save_path = os.path.join(model_dir, "config.json")
385
- if init:
386
- with open(config_path, "r") as f:
387
- data = f.read()
388
- with open(config_save_path, "w") as f:
389
- f.write(data)
390
- else:
391
- with open(config_save_path, "r") as f:
392
- data = f.read()
393
- config = json.loads(data)
394
-
395
- hparams = HParams(**config)
396
- hparams.model_dir = model_dir
397
- return hparams
398
-
399
-
400
- def get_hparams_from_dir(model_dir):
401
- config_save_path = os.path.join(model_dir, "config.json")
402
- with open(config_save_path, "r") as f:
403
- data = f.read()
404
- config = json.loads(data)
405
-
406
- hparams =HParams(**config)
407
- hparams.model_dir = model_dir
408
- return hparams
409
-
410
-
411
- def get_hparams_from_file(config_path):
412
- with open(config_path, "r") as f:
413
- data = f.read()
414
- config = json.loads(data)
415
-
416
- hparams =HParams(**config)
417
- return hparams
418
-
419
-
420
- def check_git_hash(model_dir):
421
- source_dir = os.path.dirname(os.path.realpath(__file__))
422
- if not os.path.exists(os.path.join(source_dir, ".git")):
423
- logger.warn("{} is not a git repository, therefore hash value comparison will be ignored.".format(
424
- source_dir
425
- ))
426
- return
427
-
428
- cur_hash = subprocess.getoutput("git rev-parse HEAD")
429
-
430
- path = os.path.join(model_dir, "githash")
431
- if os.path.exists(path):
432
- saved_hash = open(path).read()
433
- if saved_hash != cur_hash:
434
- logger.warn("git hash values are different. {}(saved) != {}(current)".format(
435
- saved_hash[:8], cur_hash[:8]))
436
- else:
437
- open(path, "w").write(cur_hash)
438
-
439
-
440
- def get_logger(model_dir, filename="train.log"):
441
- global logger
442
- logger = logging.getLogger(os.path.basename(model_dir))
443
- logger.setLevel(logging.DEBUG)
444
-
445
- formatter = logging.Formatter("%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s")
446
- if not os.path.exists(model_dir):
447
- os.makedirs(model_dir)
448
- h = logging.FileHandler(os.path.join(model_dir, filename))
449
- h.setLevel(logging.DEBUG)
450
- h.setFormatter(formatter)
451
- logger.addHandler(h)
452
- return logger
453
-
454
-
455
- def repeat_expand_2d(content, target_len):
456
- # content : [h, t]
457
-
458
- src_len = content.shape[-1]
459
- target = torch.zeros([content.shape[0], target_len], dtype=torch.float).to(content.device)
460
- temp = torch.arange(src_len+1) * target_len / src_len
461
- current_pos = 0
462
- for i in range(target_len):
463
- if i < temp[current_pos+1]:
464
- target[:, i] = content[:, current_pos]
465
- else:
466
- current_pos += 1
467
- target[:, i] = content[:, current_pos]
468
-
469
- return target
470
-
471
-
472
- class HParams():
473
- def __init__(self, **kwargs):
474
- for k, v in kwargs.items():
475
- if type(v) == dict:
476
- v = HParams(**v)
477
- self[k] = v
478
-
479
- def keys(self):
480
- return self.__dict__.keys()
481
-
482
- def items(self):
483
- return self.__dict__.items()
484
-
485
- def values(self):
486
- return self.__dict__.values()
487
-
488
- def __len__(self):
489
- return len(self.__dict__)
490
-
491
- def __getitem__(self, key):
492
- return getattr(self, key)
493
-
494
- def __setitem__(self, key, value):
495
- return setattr(self, key, value)
496
-
497
- def __contains__(self, key):
498
- return key in self.__dict__
499
-
500
- def __repr__(self):
501
- return self.__dict__.__repr__()
502
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/vdecoder/__init__.py DELETED
File without changes
Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/env.py DELETED
@@ -1,15 +0,0 @@
1
- import os
2
- import shutil
3
-
4
-
5
- class AttrDict(dict):
6
- def __init__(self, *args, **kwargs):
7
- super(AttrDict, self).__init__(*args, **kwargs)
8
- self.__dict__ = self
9
-
10
-
11
- def build_env(config, config_name, path):
12
- t_path = os.path.join(path, config_name)
13
- if config != t_path:
14
- os.makedirs(path, exist_ok=True)
15
- shutil.copyfile(config, os.path.join(path, config_name))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/models.py DELETED
@@ -1,503 +0,0 @@
1
- import os
2
- import json
3
- from .env import AttrDict
4
- import numpy as np
5
- import torch
6
- import torch.nn.functional as F
7
- import torch.nn as nn
8
- from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
9
- from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
10
- from .utils import init_weights, get_padding
11
-
12
- LRELU_SLOPE = 0.1
13
-
14
-
15
- def load_model(model_path, device='cuda'):
16
- config_file = os.path.join(os.path.split(model_path)[0], 'config.json')
17
- with open(config_file) as f:
18
- data = f.read()
19
-
20
- global h
21
- json_config = json.loads(data)
22
- h = AttrDict(json_config)
23
-
24
- generator = Generator(h).to(device)
25
-
26
- cp_dict = torch.load(model_path)
27
- generator.load_state_dict(cp_dict['generator'])
28
- generator.eval()
29
- generator.remove_weight_norm()
30
- del cp_dict
31
- return generator, h
32
-
33
-
34
- class ResBlock1(torch.nn.Module):
35
- def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)):
36
- super(ResBlock1, self).__init__()
37
- self.h = h
38
- self.convs1 = nn.ModuleList([
39
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
40
- padding=get_padding(kernel_size, dilation[0]))),
41
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
42
- padding=get_padding(kernel_size, dilation[1]))),
43
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
44
- padding=get_padding(kernel_size, dilation[2])))
45
- ])
46
- self.convs1.apply(init_weights)
47
-
48
- self.convs2 = nn.ModuleList([
49
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
50
- padding=get_padding(kernel_size, 1))),
51
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
52
- padding=get_padding(kernel_size, 1))),
53
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
54
- padding=get_padding(kernel_size, 1)))
55
- ])
56
- self.convs2.apply(init_weights)
57
-
58
- def forward(self, x):
59
- for c1, c2 in zip(self.convs1, self.convs2):
60
- xt = F.leaky_relu(x, LRELU_SLOPE)
61
- xt = c1(xt)
62
- xt = F.leaky_relu(xt, LRELU_SLOPE)
63
- xt = c2(xt)
64
- x = xt + x
65
- return x
66
-
67
- def remove_weight_norm(self):
68
- for l in self.convs1:
69
- remove_weight_norm(l)
70
- for l in self.convs2:
71
- remove_weight_norm(l)
72
-
73
-
74
- class ResBlock2(torch.nn.Module):
75
- def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)):
76
- super(ResBlock2, self).__init__()
77
- self.h = h
78
- self.convs = nn.ModuleList([
79
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
80
- padding=get_padding(kernel_size, dilation[0]))),
81
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
82
- padding=get_padding(kernel_size, dilation[1])))
83
- ])
84
- self.convs.apply(init_weights)
85
-
86
- def forward(self, x):
87
- for c in self.convs:
88
- xt = F.leaky_relu(x, LRELU_SLOPE)
89
- xt = c(xt)
90
- x = xt + x
91
- return x
92
-
93
- def remove_weight_norm(self):
94
- for l in self.convs:
95
- remove_weight_norm(l)
96
-
97
-
98
- def padDiff(x):
99
- return F.pad(F.pad(x, (0,0,-1,1), 'constant', 0) - x, (0,0,0,-1), 'constant', 0)
100
-
101
- class SineGen(torch.nn.Module):
102
- """ Definition of sine generator
103
- SineGen(samp_rate, harmonic_num = 0,
104
- sine_amp = 0.1, noise_std = 0.003,
105
- voiced_threshold = 0,
106
- flag_for_pulse=False)
107
- samp_rate: sampling rate in Hz
108
- harmonic_num: number of harmonic overtones (default 0)
109
- sine_amp: amplitude of sine-wavefrom (default 0.1)
110
- noise_std: std of Gaussian noise (default 0.003)
111
- voiced_thoreshold: F0 threshold for U/V classification (default 0)
112
- flag_for_pulse: this SinGen is used inside PulseGen (default False)
113
- Note: when flag_for_pulse is True, the first time step of a voiced
114
- segment is always sin(np.pi) or cos(0)
115
- """
116
-
117
- def __init__(self, samp_rate, harmonic_num=0,
118
- sine_amp=0.1, noise_std=0.003,
119
- voiced_threshold=0,
120
- flag_for_pulse=False):
121
- super(SineGen, self).__init__()
122
- self.sine_amp = sine_amp
123
- self.noise_std = noise_std
124
- self.harmonic_num = harmonic_num
125
- self.dim = self.harmonic_num + 1
126
- self.sampling_rate = samp_rate
127
- self.voiced_threshold = voiced_threshold
128
- self.flag_for_pulse = flag_for_pulse
129
-
130
- def _f02uv(self, f0):
131
- # generate uv signal
132
- uv = (f0 > self.voiced_threshold).type(torch.float32)
133
- return uv
134
-
135
- def _f02sine(self, f0_values):
136
- """ f0_values: (batchsize, length, dim)
137
- where dim indicates fundamental tone and overtones
138
- """
139
- # convert to F0 in rad. The interger part n can be ignored
140
- # because 2 * np.pi * n doesn't affect phase
141
- rad_values = (f0_values / self.sampling_rate) % 1
142
-
143
- # initial phase noise (no noise for fundamental component)
144
- rand_ini = torch.rand(f0_values.shape[0], f0_values.shape[2], \
145
- device=f0_values.device)
146
- rand_ini[:, 0] = 0
147
- rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
148
-
149
- # instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
150
- if not self.flag_for_pulse:
151
- # for normal case
152
-
153
- # To prevent torch.cumsum numerical overflow,
154
- # it is necessary to add -1 whenever \sum_k=1^n rad_value_k > 1.
155
- # Buffer tmp_over_one_idx indicates the time step to add -1.
156
- # This will not change F0 of sine because (x-1) * 2*pi = x * 2*pi
157
- tmp_over_one = torch.cumsum(rad_values, 1) % 1
158
- tmp_over_one_idx = (padDiff(tmp_over_one)) < 0
159
- cumsum_shift = torch.zeros_like(rad_values)
160
- cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
161
-
162
- sines = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1)
163
- * 2 * np.pi)
164
- else:
165
- # If necessary, make sure that the first time step of every
166
- # voiced segments is sin(pi) or cos(0)
167
- # This is used for pulse-train generation
168
-
169
- # identify the last time step in unvoiced segments
170
- uv = self._f02uv(f0_values)
171
- uv_1 = torch.roll(uv, shifts=-1, dims=1)
172
- uv_1[:, -1, :] = 1
173
- u_loc = (uv < 1) * (uv_1 > 0)
174
-
175
- # get the instantanouse phase
176
- tmp_cumsum = torch.cumsum(rad_values, dim=1)
177
- # different batch needs to be processed differently
178
- for idx in range(f0_values.shape[0]):
179
- temp_sum = tmp_cumsum[idx, u_loc[idx, :, 0], :]
180
- temp_sum[1:, :] = temp_sum[1:, :] - temp_sum[0:-1, :]
181
- # stores the accumulation of i.phase within
182
- # each voiced segments
183
- tmp_cumsum[idx, :, :] = 0
184
- tmp_cumsum[idx, u_loc[idx, :, 0], :] = temp_sum
185
-
186
- # rad_values - tmp_cumsum: remove the accumulation of i.phase
187
- # within the previous voiced segment.
188
- i_phase = torch.cumsum(rad_values - tmp_cumsum, dim=1)
189
-
190
- # get the sines
191
- sines = torch.cos(i_phase * 2 * np.pi)
192
- return sines
193
-
194
- def forward(self, f0):
195
- """ sine_tensor, uv = forward(f0)
196
- input F0: tensor(batchsize=1, length, dim=1)
197
- f0 for unvoiced steps should be 0
198
- output sine_tensor: tensor(batchsize=1, length, dim)
199
- output uv: tensor(batchsize=1, length, 1)
200
- """
201
- with torch.no_grad():
202
- f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim,
203
- device=f0.device)
204
- # fundamental component
205
- fn = torch.multiply(f0, torch.FloatTensor([[range(1, self.harmonic_num + 2)]]).to(f0.device))
206
-
207
- # generate sine waveforms
208
- sine_waves = self._f02sine(fn) * self.sine_amp
209
-
210
- # generate uv signal
211
- # uv = torch.ones(f0.shape)
212
- # uv = uv * (f0 > self.voiced_threshold)
213
- uv = self._f02uv(f0)
214
-
215
- # noise: for unvoiced should be similar to sine_amp
216
- # std = self.sine_amp/3 -> max value ~ self.sine_amp
217
- # . for voiced regions is self.noise_std
218
- noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
219
- noise = noise_amp * torch.randn_like(sine_waves)
220
-
221
- # first: set the unvoiced part to 0 by uv
222
- # then: additive noise
223
- sine_waves = sine_waves * uv + noise
224
- return sine_waves, uv, noise
225
-
226
-
227
- class SourceModuleHnNSF(torch.nn.Module):
228
- """ SourceModule for hn-nsf
229
- SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
230
- add_noise_std=0.003, voiced_threshod=0)
231
- sampling_rate: sampling_rate in Hz
232
- harmonic_num: number of harmonic above F0 (default: 0)
233
- sine_amp: amplitude of sine source signal (default: 0.1)
234
- add_noise_std: std of additive Gaussian noise (default: 0.003)
235
- note that amplitude of noise in unvoiced is decided
236
- by sine_amp
237
- voiced_threshold: threhold to set U/V given F0 (default: 0)
238
- Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
239
- F0_sampled (batchsize, length, 1)
240
- Sine_source (batchsize, length, 1)
241
- noise_source (batchsize, length 1)
242
- uv (batchsize, length, 1)
243
- """
244
-
245
- def __init__(self, sampling_rate, harmonic_num=0, sine_amp=0.1,
246
- add_noise_std=0.003, voiced_threshod=0):
247
- super(SourceModuleHnNSF, self).__init__()
248
-
249
- self.sine_amp = sine_amp
250
- self.noise_std = add_noise_std
251
-
252
- # to produce sine waveforms
253
- self.l_sin_gen = SineGen(sampling_rate, harmonic_num,
254
- sine_amp, add_noise_std, voiced_threshod)
255
-
256
- # to merge source harmonics into a single excitation
257
- self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
258
- self.l_tanh = torch.nn.Tanh()
259
-
260
- def forward(self, x):
261
- """
262
- Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
263
- F0_sampled (batchsize, length, 1)
264
- Sine_source (batchsize, length, 1)
265
- noise_source (batchsize, length 1)
266
- """
267
- # source for harmonic branch
268
- sine_wavs, uv, _ = self.l_sin_gen(x)
269
- sine_merge = self.l_tanh(self.l_linear(sine_wavs))
270
-
271
- # source for noise branch, in the same shape as uv
272
- noise = torch.randn_like(uv) * self.sine_amp / 3
273
- return sine_merge, noise, uv
274
-
275
-
276
- class Generator(torch.nn.Module):
277
- def __init__(self, h):
278
- super(Generator, self).__init__()
279
- self.h = h
280
-
281
- self.num_kernels = len(h["resblock_kernel_sizes"])
282
- self.num_upsamples = len(h["upsample_rates"])
283
- self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(h["upsample_rates"]))
284
- self.m_source = SourceModuleHnNSF(
285
- sampling_rate=h["sampling_rate"],
286
- harmonic_num=8)
287
- self.noise_convs = nn.ModuleList()
288
- self.conv_pre = weight_norm(Conv1d(h["inter_channels"], h["upsample_initial_channel"], 7, 1, padding=3))
289
- resblock = ResBlock1 if h["resblock"] == '1' else ResBlock2
290
- self.ups = nn.ModuleList()
291
- for i, (u, k) in enumerate(zip(h["upsample_rates"], h["upsample_kernel_sizes"])):
292
- c_cur = h["upsample_initial_channel"] // (2 ** (i + 1))
293
- self.ups.append(weight_norm(
294
- ConvTranspose1d(h["upsample_initial_channel"] // (2 ** i), h["upsample_initial_channel"] // (2 ** (i + 1)),
295
- k, u, padding=(k - u) // 2)))
296
- if i + 1 < len(h["upsample_rates"]): #
297
- stride_f0 = np.prod(h["upsample_rates"][i + 1:])
298
- self.noise_convs.append(Conv1d(
299
- 1, c_cur, kernel_size=stride_f0 * 2, stride=stride_f0, padding=stride_f0 // 2))
300
- else:
301
- self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
302
- self.resblocks = nn.ModuleList()
303
- for i in range(len(self.ups)):
304
- ch = h["upsample_initial_channel"] // (2 ** (i + 1))
305
- for j, (k, d) in enumerate(zip(h["resblock_kernel_sizes"], h["resblock_dilation_sizes"])):
306
- self.resblocks.append(resblock(h, ch, k, d))
307
-
308
- self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
309
- self.ups.apply(init_weights)
310
- self.conv_post.apply(init_weights)
311
- self.cond = nn.Conv1d(h['gin_channels'], h['upsample_initial_channel'], 1)
312
-
313
- def forward(self, x, f0, g=None):
314
- # print(1,x.shape,f0.shape,f0[:, None].shape)
315
- f0 = self.f0_upsamp(f0[:, None]).transpose(1, 2) # bs,n,t
316
- # print(2,f0.shape)
317
- har_source, noi_source, uv = self.m_source(f0)
318
- har_source = har_source.transpose(1, 2)
319
- x = self.conv_pre(x)
320
- x = x + self.cond(g)
321
- # print(124,x.shape,har_source.shape)
322
- for i in range(self.num_upsamples):
323
- x = F.leaky_relu(x, LRELU_SLOPE)
324
- # print(3,x.shape)
325
- x = self.ups[i](x)
326
- x_source = self.noise_convs[i](har_source)
327
- # print(4,x_source.shape,har_source.shape,x.shape)
328
- x = x + x_source
329
- xs = None
330
- for j in range(self.num_kernels):
331
- if xs is None:
332
- xs = self.resblocks[i * self.num_kernels + j](x)
333
- else:
334
- xs += self.resblocks[i * self.num_kernels + j](x)
335
- x = xs / self.num_kernels
336
- x = F.leaky_relu(x)
337
- x = self.conv_post(x)
338
- x = torch.tanh(x)
339
-
340
- return x
341
-
342
- def remove_weight_norm(self):
343
- print('Removing weight norm...')
344
- for l in self.ups:
345
- remove_weight_norm(l)
346
- for l in self.resblocks:
347
- l.remove_weight_norm()
348
- remove_weight_norm(self.conv_pre)
349
- remove_weight_norm(self.conv_post)
350
-
351
-
352
- class DiscriminatorP(torch.nn.Module):
353
- def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
354
- super(DiscriminatorP, self).__init__()
355
- self.period = period
356
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
357
- self.convs = nn.ModuleList([
358
- norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
359
- norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
360
- norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
361
- norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
362
- norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),
363
- ])
364
- self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
365
-
366
- def forward(self, x):
367
- fmap = []
368
-
369
- # 1d to 2d
370
- b, c, t = x.shape
371
- if t % self.period != 0: # pad first
372
- n_pad = self.period - (t % self.period)
373
- x = F.pad(x, (0, n_pad), "reflect")
374
- t = t + n_pad
375
- x = x.view(b, c, t // self.period, self.period)
376
-
377
- for l in self.convs:
378
- x = l(x)
379
- x = F.leaky_relu(x, LRELU_SLOPE)
380
- fmap.append(x)
381
- x = self.conv_post(x)
382
- fmap.append(x)
383
- x = torch.flatten(x, 1, -1)
384
-
385
- return x, fmap
386
-
387
-
388
- class MultiPeriodDiscriminator(torch.nn.Module):
389
- def __init__(self, periods=None):
390
- super(MultiPeriodDiscriminator, self).__init__()
391
- self.periods = periods if periods is not None else [2, 3, 5, 7, 11]
392
- self.discriminators = nn.ModuleList()
393
- for period in self.periods:
394
- self.discriminators.append(DiscriminatorP(period))
395
-
396
- def forward(self, y, y_hat):
397
- y_d_rs = []
398
- y_d_gs = []
399
- fmap_rs = []
400
- fmap_gs = []
401
- for i, d in enumerate(self.discriminators):
402
- y_d_r, fmap_r = d(y)
403
- y_d_g, fmap_g = d(y_hat)
404
- y_d_rs.append(y_d_r)
405
- fmap_rs.append(fmap_r)
406
- y_d_gs.append(y_d_g)
407
- fmap_gs.append(fmap_g)
408
-
409
- return y_d_rs, y_d_gs, fmap_rs, fmap_gs
410
-
411
-
412
- class DiscriminatorS(torch.nn.Module):
413
- def __init__(self, use_spectral_norm=False):
414
- super(DiscriminatorS, self).__init__()
415
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
416
- self.convs = nn.ModuleList([
417
- norm_f(Conv1d(1, 128, 15, 1, padding=7)),
418
- norm_f(Conv1d(128, 128, 41, 2, groups=4, padding=20)),
419
- norm_f(Conv1d(128, 256, 41, 2, groups=16, padding=20)),
420
- norm_f(Conv1d(256, 512, 41, 4, groups=16, padding=20)),
421
- norm_f(Conv1d(512, 1024, 41, 4, groups=16, padding=20)),
422
- norm_f(Conv1d(1024, 1024, 41, 1, groups=16, padding=20)),
423
- norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
424
- ])
425
- self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
426
-
427
- def forward(self, x):
428
- fmap = []
429
- for l in self.convs:
430
- x = l(x)
431
- x = F.leaky_relu(x, LRELU_SLOPE)
432
- fmap.append(x)
433
- x = self.conv_post(x)
434
- fmap.append(x)
435
- x = torch.flatten(x, 1, -1)
436
-
437
- return x, fmap
438
-
439
-
440
- class MultiScaleDiscriminator(torch.nn.Module):
441
- def __init__(self):
442
- super(MultiScaleDiscriminator, self).__init__()
443
- self.discriminators = nn.ModuleList([
444
- DiscriminatorS(use_spectral_norm=True),
445
- DiscriminatorS(),
446
- DiscriminatorS(),
447
- ])
448
- self.meanpools = nn.ModuleList([
449
- AvgPool1d(4, 2, padding=2),
450
- AvgPool1d(4, 2, padding=2)
451
- ])
452
-
453
- def forward(self, y, y_hat):
454
- y_d_rs = []
455
- y_d_gs = []
456
- fmap_rs = []
457
- fmap_gs = []
458
- for i, d in enumerate(self.discriminators):
459
- if i != 0:
460
- y = self.meanpools[i - 1](y)
461
- y_hat = self.meanpools[i - 1](y_hat)
462
- y_d_r, fmap_r = d(y)
463
- y_d_g, fmap_g = d(y_hat)
464
- y_d_rs.append(y_d_r)
465
- fmap_rs.append(fmap_r)
466
- y_d_gs.append(y_d_g)
467
- fmap_gs.append(fmap_g)
468
-
469
- return y_d_rs, y_d_gs, fmap_rs, fmap_gs
470
-
471
-
472
- def feature_loss(fmap_r, fmap_g):
473
- loss = 0
474
- for dr, dg in zip(fmap_r, fmap_g):
475
- for rl, gl in zip(dr, dg):
476
- loss += torch.mean(torch.abs(rl - gl))
477
-
478
- return loss * 2
479
-
480
-
481
- def discriminator_loss(disc_real_outputs, disc_generated_outputs):
482
- loss = 0
483
- r_losses = []
484
- g_losses = []
485
- for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
486
- r_loss = torch.mean((1 - dr) ** 2)
487
- g_loss = torch.mean(dg ** 2)
488
- loss += (r_loss + g_loss)
489
- r_losses.append(r_loss.item())
490
- g_losses.append(g_loss.item())
491
-
492
- return loss, r_losses, g_losses
493
-
494
-
495
- def generator_loss(disc_outputs):
496
- loss = 0
497
- gen_losses = []
498
- for dg in disc_outputs:
499
- l = torch.mean((1 - dg) ** 2)
500
- gen_losses.append(l)
501
- loss += l
502
-
503
- return loss, gen_losses
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/nvSTFT.py DELETED
@@ -1,111 +0,0 @@
1
- import math
2
- import os
3
- os.environ["LRU_CACHE_CAPACITY"] = "3"
4
- import random
5
- import torch
6
- import torch.utils.data
7
- import numpy as np
8
- import librosa
9
- from librosa.util import normalize
10
- from librosa.filters import mel as librosa_mel_fn
11
- from scipy.io.wavfile import read
12
- import soundfile as sf
13
-
14
- def load_wav_to_torch(full_path, target_sr=None, return_empty_on_exception=False):
15
- sampling_rate = None
16
- try:
17
- data, sampling_rate = sf.read(full_path, always_2d=True)# than soundfile.
18
- except Exception as ex:
19
- print(f"'{full_path}' failed to load.\nException:")
20
- print(ex)
21
- if return_empty_on_exception:
22
- return [], sampling_rate or target_sr or 32000
23
- else:
24
- raise Exception(ex)
25
-
26
- if len(data.shape) > 1:
27
- data = data[:, 0]
28
- assert len(data) > 2# check duration of audio file is > 2 samples (because otherwise the slice operation was on the wrong dimension)
29
-
30
- if np.issubdtype(data.dtype, np.integer): # if audio data is type int
31
- max_mag = -np.iinfo(data.dtype).min # maximum magnitude = min possible value of intXX
32
- else: # if audio data is type fp32
33
- max_mag = max(np.amax(data), -np.amin(data))
34
- max_mag = (2**31)+1 if max_mag > (2**15) else ((2**15)+1 if max_mag > 1.01 else 1.0) # data should be either 16-bit INT, 32-bit INT or [-1 to 1] float32
35
-
36
- data = torch.FloatTensor(data.astype(np.float32))/max_mag
37
-
38
- if (torch.isinf(data) | torch.isnan(data)).any() and return_empty_on_exception:# resample will crash with inf/NaN inputs. return_empty_on_exception will return empty arr instead of except
39
- return [], sampling_rate or target_sr or 32000
40
- if target_sr is not None and sampling_rate != target_sr:
41
- data = torch.from_numpy(librosa.core.resample(data.numpy(), orig_sr=sampling_rate, target_sr=target_sr))
42
- sampling_rate = target_sr
43
-
44
- return data, sampling_rate
45
-
46
- def dynamic_range_compression(x, C=1, clip_val=1e-5):
47
- return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)
48
-
49
- def dynamic_range_decompression(x, C=1):
50
- return np.exp(x) / C
51
-
52
- def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
53
- return torch.log(torch.clamp(x, min=clip_val) * C)
54
-
55
- def dynamic_range_decompression_torch(x, C=1):
56
- return torch.exp(x) / C
57
-
58
- class STFT():
59
- def __init__(self, sr=22050, n_mels=80, n_fft=1024, win_size=1024, hop_length=256, fmin=20, fmax=11025, clip_val=1e-5):
60
- self.target_sr = sr
61
-
62
- self.n_mels = n_mels
63
- self.n_fft = n_fft
64
- self.win_size = win_size
65
- self.hop_length = hop_length
66
- self.fmin = fmin
67
- self.fmax = fmax
68
- self.clip_val = clip_val
69
- self.mel_basis = {}
70
- self.hann_window = {}
71
-
72
- def get_mel(self, y, center=False):
73
- sampling_rate = self.target_sr
74
- n_mels = self.n_mels
75
- n_fft = self.n_fft
76
- win_size = self.win_size
77
- hop_length = self.hop_length
78
- fmin = self.fmin
79
- fmax = self.fmax
80
- clip_val = self.clip_val
81
-
82
- if torch.min(y) < -1.:
83
- print('min value is ', torch.min(y))
84
- if torch.max(y) > 1.:
85
- print('max value is ', torch.max(y))
86
-
87
- if fmax not in self.mel_basis:
88
- mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax)
89
- self.mel_basis[str(fmax)+'_'+str(y.device)] = torch.from_numpy(mel).float().to(y.device)
90
- self.hann_window[str(y.device)] = torch.hann_window(self.win_size).to(y.device)
91
-
92
- y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_length)/2), int((n_fft-hop_length)/2)), mode='reflect')
93
- y = y.squeeze(1)
94
-
95
- spec = torch.stft(y, n_fft, hop_length=hop_length, win_length=win_size, window=self.hann_window[str(y.device)],
96
- center=center, pad_mode='reflect', normalized=False, onesided=True)
97
- # print(111,spec)
98
- spec = torch.sqrt(spec.pow(2).sum(-1)+(1e-9))
99
- # print(222,spec)
100
- spec = torch.matmul(self.mel_basis[str(fmax)+'_'+str(y.device)], spec)
101
- # print(333,spec)
102
- spec = dynamic_range_compression_torch(spec, clip_val=clip_val)
103
- # print(444,spec)
104
- return spec
105
-
106
- def __call__(self, audiopath):
107
- audio, sr = load_wav_to_torch(audiopath, target_sr=self.target_sr)
108
- spect = self.get_mel(audio.unsqueeze(0)).squeeze(0)
109
- return spect
110
-
111
- stft = STFT()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/vdecoder/hifigan/utils.py DELETED
@@ -1,68 +0,0 @@
1
- import glob
2
- import os
3
- import matplotlib
4
- import torch
5
- from torch.nn.utils import weight_norm
6
- # matplotlib.use("Agg")
7
- import matplotlib.pylab as plt
8
-
9
-
10
- def plot_spectrogram(spectrogram):
11
- fig, ax = plt.subplots(figsize=(10, 2))
12
- im = ax.imshow(spectrogram, aspect="auto", origin="lower",
13
- interpolation='none')
14
- plt.colorbar(im, ax=ax)
15
-
16
- fig.canvas.draw()
17
- plt.close()
18
-
19
- return fig
20
-
21
-
22
- def init_weights(m, mean=0.0, std=0.01):
23
- classname = m.__class__.__name__
24
- if classname.find("Conv") != -1:
25
- m.weight.data.normal_(mean, std)
26
-
27
-
28
- def apply_weight_norm(m):
29
- classname = m.__class__.__name__
30
- if classname.find("Conv") != -1:
31
- weight_norm(m)
32
-
33
-
34
- def get_padding(kernel_size, dilation=1):
35
- return int((kernel_size*dilation - dilation)/2)
36
-
37
-
38
- def load_checkpoint(filepath, device):
39
- assert os.path.isfile(filepath)
40
- print("Loading '{}'".format(filepath))
41
- checkpoint_dict = torch.load(filepath, map_location=device)
42
- print("Complete.")
43
- return checkpoint_dict
44
-
45
-
46
- def save_checkpoint(filepath, obj):
47
- print("Saving checkpoint to {}".format(filepath))
48
- torch.save(obj, filepath)
49
- print("Complete.")
50
-
51
-
52
- def del_old_checkpoints(cp_dir, prefix, n_models=2):
53
- pattern = os.path.join(cp_dir, prefix + '????????')
54
- cp_list = glob.glob(pattern) # get checkpoint paths
55
- cp_list = sorted(cp_list)# sort by iter
56
- if len(cp_list) > n_models: # if more than n_models models are found
57
- for cp in cp_list[:-n_models]:# delete the oldest models other than lastest n_models
58
- open(cp, 'w').close()# empty file contents
59
- os.unlink(cp)# delete file (move to trash when using Colab)
60
-
61
-
62
- def scan_checkpoint(cp_dir, prefix):
63
- pattern = os.path.join(cp_dir, prefix + '????????')
64
- cp_list = glob.glob(pattern)
65
- if len(cp_list) == 0:
66
- return None
67
- return sorted(cp_list)[-1]
68
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/Arknights/wav_upload.py DELETED
@@ -1,23 +0,0 @@
1
- from google.colab import files
2
- import shutil
3
- import os
4
- import argparse
5
- if __name__ == "__main__":
6
- parser = argparse.ArgumentParser()
7
- parser.add_argument("--type", type=str, required=True, help="type of file to upload")
8
- args = parser.parse_args()
9
- file_type = args.type
10
-
11
- basepath = os.getcwd()
12
- uploaded = files.upload() # 上传文件
13
- assert(file_type in ['zip', 'audio'])
14
- if file_type == "zip":
15
- upload_path = "./upload/"
16
- for filename in uploaded.keys():
17
- #将上传的文件移动到指定的位置上
18
- shutil.move(os.path.join(basepath, filename), os.path.join(upload_path, "userzip.zip"))
19
- elif file_type == "audio":
20
- upload_path = "./raw/"
21
- for filename in uploaded.keys():
22
- #将上传的文件移动到指定的位置上
23
- shutil.move(os.path.join(basepath, filename), os.path.join(upload_path, filename))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/.pre-commit-config.yaml DELETED
@@ -1,25 +0,0 @@
1
- repos:
2
- - repo: https://github.com/pre-commit/pre-commit-hooks
3
- rev: v4.5.0
4
- hooks:
5
- - id: check-yaml
6
- - id: end-of-file-fixer
7
- - id: trailing-whitespace
8
-
9
- - repo: https://github.com/astral-sh/ruff-pre-commit
10
- rev: v0.1.7
11
- hooks:
12
- - id: ruff
13
- args: [ --fix ]
14
-
15
- - repo: https://github.com/psf/black
16
- rev: 23.11.0
17
- hooks:
18
- - id: black
19
-
20
- - repo: https://github.com/codespell-project/codespell
21
- rev: v2.2.6
22
- hooks:
23
- - id: codespell
24
- files: ^.*\.(py|md|rst|yml)$
25
- args: [-L=fro]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/LICENSE DELETED
@@ -1,661 +0,0 @@
1
- GNU AFFERO GENERAL PUBLIC LICENSE
2
- Version 3, 19 November 2007
3
-
4
- Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
- Everyone is permitted to copy and distribute verbatim copies
6
- of this license document, but changing it is not allowed.
7
-
8
- Preamble
9
-
10
- The GNU Affero General Public License is a free, copyleft license for
11
- software and other kinds of works, specifically designed to ensure
12
- cooperation with the community in the case of network server software.
13
-
14
- The licenses for most software and other practical works are designed
15
- to take away your freedom to share and change the works. By contrast,
16
- our General Public Licenses are intended to guarantee your freedom to
17
- share and change all versions of a program--to make sure it remains free
18
- software for all its users.
19
-
20
- When we speak of free software, we are referring to freedom, not
21
- price. Our General Public Licenses are designed to make sure that you
22
- have the freedom to distribute copies of free software (and charge for
23
- them if you wish), that you receive source code or can get it if you
24
- want it, that you can change the software or use pieces of it in new
25
- free programs, and that you know you can do these things.
26
-
27
- Developers that use our General Public Licenses protect your rights
28
- with two steps: (1) assert copyright on the software, and (2) offer
29
- you this License which gives you legal permission to copy, distribute
30
- and/or modify the software.
31
-
32
- A secondary benefit of defending all users' freedom is that
33
- improvements made in alternate versions of the program, if they
34
- receive widespread use, become available for other developers to
35
- incorporate. Many developers of free software are heartened and
36
- encouraged by the resulting cooperation. However, in the case of
37
- software used on network servers, this result may fail to come about.
38
- The GNU General Public License permits making a modified version and
39
- letting the public access it on a server without ever releasing its
40
- source code to the public.
41
-
42
- The GNU Affero General Public License is designed specifically to
43
- ensure that, in such cases, the modified source code becomes available
44
- to the community. It requires the operator of a network server to
45
- provide the source code of the modified version running there to the
46
- users of that server. Therefore, public use of a modified version, on
47
- a publicly accessible server, gives the public access to the source
48
- code of the modified version.
49
-
50
- An older license, called the Affero General Public License and
51
- published by Affero, was designed to accomplish similar goals. This is
52
- a different license, not a version of the Affero GPL, but Affero has
53
- released a new version of the Affero GPL which permits relicensing under
54
- this license.
55
-
56
- The precise terms and conditions for copying, distribution and
57
- modification follow.
58
-
59
- TERMS AND CONDITIONS
60
-
61
- 0. Definitions.
62
-
63
- "This License" refers to version 3 of the GNU Affero General Public License.
64
-
65
- "Copyright" also means copyright-like laws that apply to other kinds of
66
- works, such as semiconductor masks.
67
-
68
- "The Program" refers to any copyrightable work licensed under this
69
- License. Each licensee is addressed as "you". "Licensees" and
70
- "recipients" may be individuals or organizations.
71
-
72
- To "modify" a work means to copy from or adapt all or part of the work
73
- in a fashion requiring copyright permission, other than the making of an
74
- exact copy. The resulting work is called a "modified version" of the
75
- earlier work or a work "based on" the earlier work.
76
-
77
- A "covered work" means either the unmodified Program or a work based
78
- on the Program.
79
-
80
- To "propagate" a work means to do anything with it that, without
81
- permission, would make you directly or secondarily liable for
82
- infringement under applicable copyright law, except executing it on a
83
- computer or modifying a private copy. Propagation includes copying,
84
- distribution (with or without modification), making available to the
85
- public, and in some countries other activities as well.
86
-
87
- To "convey" a work means any kind of propagation that enables other
88
- parties to make or receive copies. Mere interaction with a user through
89
- a computer network, with no transfer of a copy, is not conveying.
90
-
91
- An interactive user interface displays "Appropriate Legal Notices"
92
- to the extent that it includes a convenient and prominently visible
93
- feature that (1) displays an appropriate copyright notice, and (2)
94
- tells the user that there is no warranty for the work (except to the
95
- extent that warranties are provided), that licensees may convey the
96
- work under this License, and how to view a copy of this License. If
97
- the interface presents a list of user commands or options, such as a
98
- menu, a prominent item in the list meets this criterion.
99
-
100
- 1. Source Code.
101
-
102
- The "source code" for a work means the preferred form of the work
103
- for making modifications to it. "Object code" means any non-source
104
- form of a work.
105
-
106
- A "Standard Interface" means an interface that either is an official
107
- standard defined by a recognized standards body, or, in the case of
108
- interfaces specified for a particular programming language, one that
109
- is widely used among developers working in that language.
110
-
111
- The "System Libraries" of an executable work include anything, other
112
- than the work as a whole, that (a) is included in the normal form of
113
- packaging a Major Component, but which is not part of that Major
114
- Component, and (b) serves only to enable use of the work with that
115
- Major Component, or to implement a Standard Interface for which an
116
- implementation is available to the public in source code form. A
117
- "Major Component", in this context, means a major essential component
118
- (kernel, window system, and so on) of the specific operating system
119
- (if any) on which the executable work runs, or a compiler used to
120
- produce the work, or an object code interpreter used to run it.
121
-
122
- The "Corresponding Source" for a work in object code form means all
123
- the source code needed to generate, install, and (for an executable
124
- work) run the object code and to modify the work, including scripts to
125
- control those activities. However, it does not include the work's
126
- System Libraries, or general-purpose tools or generally available free
127
- programs which are used unmodified in performing those activities but
128
- which are not part of the work. For example, Corresponding Source
129
- includes interface definition files associated with source files for
130
- the work, and the source code for shared libraries and dynamically
131
- linked subprograms that the work is specifically designed to require,
132
- such as by intimate data communication or control flow between those
133
- subprograms and other parts of the work.
134
-
135
- The Corresponding Source need not include anything that users
136
- can regenerate automatically from other parts of the Corresponding
137
- Source.
138
-
139
- The Corresponding Source for a work in source code form is that
140
- same work.
141
-
142
- 2. Basic Permissions.
143
-
144
- All rights granted under this License are granted for the term of
145
- copyright on the Program, and are irrevocable provided the stated
146
- conditions are met. This License explicitly affirms your unlimited
147
- permission to run the unmodified Program. The output from running a
148
- covered work is covered by this License only if the output, given its
149
- content, constitutes a covered work. This License acknowledges your
150
- rights of fair use or other equivalent, as provided by copyright law.
151
-
152
- You may make, run and propagate covered works that you do not
153
- convey, without conditions so long as your license otherwise remains
154
- in force. You may convey covered works to others for the sole purpose
155
- of having them make modifications exclusively for you, or provide you
156
- with facilities for running those works, provided that you comply with
157
- the terms of this License in conveying all material for which you do
158
- not control copyright. Those thus making or running the covered works
159
- for you must do so exclusively on your behalf, under your direction
160
- and control, on terms that prohibit them from making any copies of
161
- your copyrighted material outside their relationship with you.
162
-
163
- Conveying under any other circumstances is permitted solely under
164
- the conditions stated below. Sublicensing is not allowed; section 10
165
- makes it unnecessary.
166
-
167
- 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168
-
169
- No covered work shall be deemed part of an effective technological
170
- measure under any applicable law fulfilling obligations under article
171
- 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172
- similar laws prohibiting or restricting circumvention of such
173
- measures.
174
-
175
- When you convey a covered work, you waive any legal power to forbid
176
- circumvention of technological measures to the extent such circumvention
177
- is effected by exercising rights under this License with respect to
178
- the covered work, and you disclaim any intention to limit operation or
179
- modification of the work as a means of enforcing, against the work's
180
- users, your or third parties' legal rights to forbid circumvention of
181
- technological measures.
182
-
183
- 4. Conveying Verbatim Copies.
184
-
185
- You may convey verbatim copies of the Program's source code as you
186
- receive it, in any medium, provided that you conspicuously and
187
- appropriately publish on each copy an appropriate copyright notice;
188
- keep intact all notices stating that this License and any
189
- non-permissive terms added in accord with section 7 apply to the code;
190
- keep intact all notices of the absence of any warranty; and give all
191
- recipients a copy of this License along with the Program.
192
-
193
- You may charge any price or no price for each copy that you convey,
194
- and you may offer support or warranty protection for a fee.
195
-
196
- 5. Conveying Modified Source Versions.
197
-
198
- You may convey a work based on the Program, or the modifications to
199
- produce it from the Program, in the form of source code under the
200
- terms of section 4, provided that you also meet all of these conditions:
201
-
202
- a) The work must carry prominent notices stating that you modified
203
- it, and giving a relevant date.
204
-
205
- b) The work must carry prominent notices stating that it is
206
- released under this License and any conditions added under section
207
- 7. This requirement modifies the requirement in section 4 to
208
- "keep intact all notices".
209
-
210
- c) You must license the entire work, as a whole, under this
211
- License to anyone who comes into possession of a copy. This
212
- License will therefore apply, along with any applicable section 7
213
- additional terms, to the whole of the work, and all its parts,
214
- regardless of how they are packaged. This License gives no
215
- permission to license the work in any other way, but it does not
216
- invalidate such permission if you have separately received it.
217
-
218
- d) If the work has interactive user interfaces, each must display
219
- Appropriate Legal Notices; however, if the Program has interactive
220
- interfaces that do not display Appropriate Legal Notices, your
221
- work need not make them do so.
222
-
223
- A compilation of a covered work with other separate and independent
224
- works, which are not by their nature extensions of the covered work,
225
- and which are not combined with it such as to form a larger program,
226
- in or on a volume of a storage or distribution medium, is called an
227
- "aggregate" if the compilation and its resulting copyright are not
228
- used to limit the access or legal rights of the compilation's users
229
- beyond what the individual works permit. Inclusion of a covered work
230
- in an aggregate does not cause this License to apply to the other
231
- parts of the aggregate.
232
-
233
- 6. Conveying Non-Source Forms.
234
-
235
- You may convey a covered work in object code form under the terms
236
- of sections 4 and 5, provided that you also convey the
237
- machine-readable Corresponding Source under the terms of this License,
238
- in one of these ways:
239
-
240
- a) Convey the object code in, or embodied in, a physical product
241
- (including a physical distribution medium), accompanied by the
242
- Corresponding Source fixed on a durable physical medium
243
- customarily used for software interchange.
244
-
245
- b) Convey the object code in, or embodied in, a physical product
246
- (including a physical distribution medium), accompanied by a
247
- written offer, valid for at least three years and valid for as
248
- long as you offer spare parts or customer support for that product
249
- model, to give anyone who possesses the object code either (1) a
250
- copy of the Corresponding Source for all the software in the
251
- product that is covered by this License, on a durable physical
252
- medium customarily used for software interchange, for a price no
253
- more than your reasonable cost of physically performing this
254
- conveying of source, or (2) access to copy the
255
- Corresponding Source from a network server at no charge.
256
-
257
- c) Convey individual copies of the object code with a copy of the
258
- written offer to provide the Corresponding Source. This
259
- alternative is allowed only occasionally and noncommercially, and
260
- only if you received the object code with such an offer, in accord
261
- with subsection 6b.
262
-
263
- d) Convey the object code by offering access from a designated
264
- place (gratis or for a charge), and offer equivalent access to the
265
- Corresponding Source in the same way through the same place at no
266
- further charge. You need not require recipients to copy the
267
- Corresponding Source along with the object code. If the place to
268
- copy the object code is a network server, the Corresponding Source
269
- may be on a different server (operated by you or a third party)
270
- that supports equivalent copying facilities, provided you maintain
271
- clear directions next to the object code saying where to find the
272
- Corresponding Source. Regardless of what server hosts the
273
- Corresponding Source, you remain obligated to ensure that it is
274
- available for as long as needed to satisfy these requirements.
275
-
276
- e) Convey the object code using peer-to-peer transmission, provided
277
- you inform other peers where the object code and Corresponding
278
- Source of the work are being offered to the general public at no
279
- charge under subsection 6d.
280
-
281
- A separable portion of the object code, whose source code is excluded
282
- from the Corresponding Source as a System Library, need not be
283
- included in conveying the object code work.
284
-
285
- A "User Product" is either (1) a "consumer product", which means any
286
- tangible personal property which is normally used for personal, family,
287
- or household purposes, or (2) anything designed or sold for incorporation
288
- into a dwelling. In determining whether a product is a consumer product,
289
- doubtful cases shall be resolved in favor of coverage. For a particular
290
- product received by a particular user, "normally used" refers to a
291
- typical or common use of that class of product, regardless of the status
292
- of the particular user or of the way in which the particular user
293
- actually uses, or expects or is expected to use, the product. A product
294
- is a consumer product regardless of whether the product has substantial
295
- commercial, industrial or non-consumer uses, unless such uses represent
296
- the only significant mode of use of the product.
297
-
298
- "Installation Information" for a User Product means any methods,
299
- procedures, authorization keys, or other information required to install
300
- and execute modified versions of a covered work in that User Product from
301
- a modified version of its Corresponding Source. The information must
302
- suffice to ensure that the continued functioning of the modified object
303
- code is in no case prevented or interfered with solely because
304
- modification has been made.
305
-
306
- If you convey an object code work under this section in, or with, or
307
- specifically for use in, a User Product, and the conveying occurs as
308
- part of a transaction in which the right of possession and use of the
309
- User Product is transferred to the recipient in perpetuity or for a
310
- fixed term (regardless of how the transaction is characterized), the
311
- Corresponding Source conveyed under this section must be accompanied
312
- by the Installation Information. But this requirement does not apply
313
- if neither you nor any third party retains the ability to install
314
- modified object code on the User Product (for example, the work has
315
- been installed in ROM).
316
-
317
- The requirement to provide Installation Information does not include a
318
- requirement to continue to provide support service, warranty, or updates
319
- for a work that has been modified or installed by the recipient, or for
320
- the User Product in which it has been modified or installed. Access to a
321
- network may be denied when the modification itself materially and
322
- adversely affects the operation of the network or violates the rules and
323
- protocols for communication across the network.
324
-
325
- Corresponding Source conveyed, and Installation Information provided,
326
- in accord with this section must be in a format that is publicly
327
- documented (and with an implementation available to the public in
328
- source code form), and must require no special password or key for
329
- unpacking, reading or copying.
330
-
331
- 7. Additional Terms.
332
-
333
- "Additional permissions" are terms that supplement the terms of this
334
- License by making exceptions from one or more of its conditions.
335
- Additional permissions that are applicable to the entire Program shall
336
- be treated as though they were included in this License, to the extent
337
- that they are valid under applicable law. If additional permissions
338
- apply only to part of the Program, that part may be used separately
339
- under those permissions, but the entire Program remains governed by
340
- this License without regard to the additional permissions.
341
-
342
- When you convey a copy of a covered work, you may at your option
343
- remove any additional permissions from that copy, or from any part of
344
- it. (Additional permissions may be written to require their own
345
- removal in certain cases when you modify the work.) You may place
346
- additional permissions on material, added by you to a covered work,
347
- for which you have or can give appropriate copyright permission.
348
-
349
- Notwithstanding any other provision of this License, for material you
350
- add to a covered work, you may (if authorized by the copyright holders of
351
- that material) supplement the terms of this License with terms:
352
-
353
- a) Disclaiming warranty or limiting liability differently from the
354
- terms of sections 15 and 16 of this License; or
355
-
356
- b) Requiring preservation of specified reasonable legal notices or
357
- author attributions in that material or in the Appropriate Legal
358
- Notices displayed by works containing it; or
359
-
360
- c) Prohibiting misrepresentation of the origin of that material, or
361
- requiring that modified versions of such material be marked in
362
- reasonable ways as different from the original version; or
363
-
364
- d) Limiting the use for publicity purposes of names of licensors or
365
- authors of the material; or
366
-
367
- e) Declining to grant rights under trademark law for use of some
368
- trade names, trademarks, or service marks; or
369
-
370
- f) Requiring indemnification of licensors and authors of that
371
- material by anyone who conveys the material (or modified versions of
372
- it) with contractual assumptions of liability to the recipient, for
373
- any liability that these contractual assumptions directly impose on
374
- those licensors and authors.
375
-
376
- All other non-permissive additional terms are considered "further
377
- restrictions" within the meaning of section 10. If the Program as you
378
- received it, or any part of it, contains a notice stating that it is
379
- governed by this License along with a term that is a further
380
- restriction, you may remove that term. If a license document contains
381
- a further restriction but permits relicensing or conveying under this
382
- License, you may add to a covered work material governed by the terms
383
- of that license document, provided that the further restriction does
384
- not survive such relicensing or conveying.
385
-
386
- If you add terms to a covered work in accord with this section, you
387
- must place, in the relevant source files, a statement of the
388
- additional terms that apply to those files, or a notice indicating
389
- where to find the applicable terms.
390
-
391
- Additional terms, permissive or non-permissive, may be stated in the
392
- form of a separately written license, or stated as exceptions;
393
- the above requirements apply either way.
394
-
395
- 8. Termination.
396
-
397
- You may not propagate or modify a covered work except as expressly
398
- provided under this License. Any attempt otherwise to propagate or
399
- modify it is void, and will automatically terminate your rights under
400
- this License (including any patent licenses granted under the third
401
- paragraph of section 11).
402
-
403
- However, if you cease all violation of this License, then your
404
- license from a particular copyright holder is reinstated (a)
405
- provisionally, unless and until the copyright holder explicitly and
406
- finally terminates your license, and (b) permanently, if the copyright
407
- holder fails to notify you of the violation by some reasonable means
408
- prior to 60 days after the cessation.
409
-
410
- Moreover, your license from a particular copyright holder is
411
- reinstated permanently if the copyright holder notifies you of the
412
- violation by some reasonable means, this is the first time you have
413
- received notice of violation of this License (for any work) from that
414
- copyright holder, and you cure the violation prior to 30 days after
415
- your receipt of the notice.
416
-
417
- Termination of your rights under this section does not terminate the
418
- licenses of parties who have received copies or rights from you under
419
- this License. If your rights have been terminated and not permanently
420
- reinstated, you do not qualify to receive new licenses for the same
421
- material under section 10.
422
-
423
- 9. Acceptance Not Required for Having Copies.
424
-
425
- You are not required to accept this License in order to receive or
426
- run a copy of the Program. Ancillary propagation of a covered work
427
- occurring solely as a consequence of using peer-to-peer transmission
428
- to receive a copy likewise does not require acceptance. However,
429
- nothing other than this License grants you permission to propagate or
430
- modify any covered work. These actions infringe copyright if you do
431
- not accept this License. Therefore, by modifying or propagating a
432
- covered work, you indicate your acceptance of this License to do so.
433
-
434
- 10. Automatic Licensing of Downstream Recipients.
435
-
436
- Each time you convey a covered work, the recipient automatically
437
- receives a license from the original licensors, to run, modify and
438
- propagate that work, subject to this License. You are not responsible
439
- for enforcing compliance by third parties with this License.
440
-
441
- An "entity transaction" is a transaction transferring control of an
442
- organization, or substantially all assets of one, or subdividing an
443
- organization, or merging organizations. If propagation of a covered
444
- work results from an entity transaction, each party to that
445
- transaction who receives a copy of the work also receives whatever
446
- licenses to the work the party's predecessor in interest had or could
447
- give under the previous paragraph, plus a right to possession of the
448
- Corresponding Source of the work from the predecessor in interest, if
449
- the predecessor has it or can get it with reasonable efforts.
450
-
451
- You may not impose any further restrictions on the exercise of the
452
- rights granted or affirmed under this License. For example, you may
453
- not impose a license fee, royalty, or other charge for exercise of
454
- rights granted under this License, and you may not initiate litigation
455
- (including a cross-claim or counterclaim in a lawsuit) alleging that
456
- any patent claim is infringed by making, using, selling, offering for
457
- sale, or importing the Program or any portion of it.
458
-
459
- 11. Patents.
460
-
461
- A "contributor" is a copyright holder who authorizes use under this
462
- License of the Program or a work on which the Program is based. The
463
- work thus licensed is called the contributor's "contributor version".
464
-
465
- A contributor's "essential patent claims" are all patent claims
466
- owned or controlled by the contributor, whether already acquired or
467
- hereafter acquired, that would be infringed by some manner, permitted
468
- by this License, of making, using, or selling its contributor version,
469
- but do not include claims that would be infringed only as a
470
- consequence of further modification of the contributor version. For
471
- purposes of this definition, "control" includes the right to grant
472
- patent sublicenses in a manner consistent with the requirements of
473
- this License.
474
-
475
- Each contributor grants you a non-exclusive, worldwide, royalty-free
476
- patent license under the contributor's essential patent claims, to
477
- make, use, sell, offer for sale, import and otherwise run, modify and
478
- propagate the contents of its contributor version.
479
-
480
- In the following three paragraphs, a "patent license" is any express
481
- agreement or commitment, however denominated, not to enforce a patent
482
- (such as an express permission to practice a patent or covenant not to
483
- sue for patent infringement). To "grant" such a patent license to a
484
- party means to make such an agreement or commitment not to enforce a
485
- patent against the party.
486
-
487
- If you convey a covered work, knowingly relying on a patent license,
488
- and the Corresponding Source of the work is not available for anyone
489
- to copy, free of charge and under the terms of this License, through a
490
- publicly available network server or other readily accessible means,
491
- then you must either (1) cause the Corresponding Source to be so
492
- available, or (2) arrange to deprive yourself of the benefit of the
493
- patent license for this particular work, or (3) arrange, in a manner
494
- consistent with the requirements of this License, to extend the patent
495
- license to downstream recipients. "Knowingly relying" means you have
496
- actual knowledge that, but for the patent license, your conveying the
497
- covered work in a country, or your recipient's use of the covered work
498
- in a country, would infringe one or more identifiable patents in that
499
- country that you have reason to believe are valid.
500
-
501
- If, pursuant to or in connection with a single transaction or
502
- arrangement, you convey, or propagate by procuring conveyance of, a
503
- covered work, and grant a patent license to some of the parties
504
- receiving the covered work authorizing them to use, propagate, modify
505
- or convey a specific copy of the covered work, then the patent license
506
- you grant is automatically extended to all recipients of the covered
507
- work and works based on it.
508
-
509
- A patent license is "discriminatory" if it does not include within
510
- the scope of its coverage, prohibits the exercise of, or is
511
- conditioned on the non-exercise of one or more of the rights that are
512
- specifically granted under this License. You may not convey a covered
513
- work if you are a party to an arrangement with a third party that is
514
- in the business of distributing software, under which you make payment
515
- to the third party based on the extent of your activity of conveying
516
- the work, and under which the third party grants, to any of the
517
- parties who would receive the covered work from you, a discriminatory
518
- patent license (a) in connection with copies of the covered work
519
- conveyed by you (or copies made from those copies), or (b) primarily
520
- for and in connection with specific products or compilations that
521
- contain the covered work, unless you entered into that arrangement,
522
- or that patent license was granted, prior to 28 March 2007.
523
-
524
- Nothing in this License shall be construed as excluding or limiting
525
- any implied license or other defenses to infringement that may
526
- otherwise be available to you under applicable patent law.
527
-
528
- 12. No Surrender of Others' Freedom.
529
-
530
- If conditions are imposed on you (whether by court order, agreement or
531
- otherwise) that contradict the conditions of this License, they do not
532
- excuse you from the conditions of this License. If you cannot convey a
533
- covered work so as to satisfy simultaneously your obligations under this
534
- License and any other pertinent obligations, then as a consequence you may
535
- not convey it at all. For example, if you agree to terms that obligate you
536
- to collect a royalty for further conveying from those to whom you convey
537
- the Program, the only way you could satisfy both those terms and this
538
- License would be to refrain entirely from conveying the Program.
539
-
540
- 13. Remote Network Interaction; Use with the GNU General Public License.
541
-
542
- Notwithstanding any other provision of this License, if you modify the
543
- Program, your modified version must prominently offer all users
544
- interacting with it remotely through a computer network (if your version
545
- supports such interaction) an opportunity to receive the Corresponding
546
- Source of your version by providing access to the Corresponding Source
547
- from a network server at no charge, through some standard or customary
548
- means of facilitating copying of software. This Corresponding Source
549
- shall include the Corresponding Source for any work covered by version 3
550
- of the GNU General Public License that is incorporated pursuant to the
551
- following paragraph.
552
-
553
- Notwithstanding any other provision of this License, you have
554
- permission to link or combine any covered work with a work licensed
555
- under version 3 of the GNU General Public License into a single
556
- combined work, and to convey the resulting work. The terms of this
557
- License will continue to apply to the part which is the covered work,
558
- but the work with which it is combined will remain governed by version
559
- 3 of the GNU General Public License.
560
-
561
- 14. Revised Versions of this License.
562
-
563
- The Free Software Foundation may publish revised and/or new versions of
564
- the GNU Affero General Public License from time to time. Such new versions
565
- will be similar in spirit to the present version, but may differ in detail to
566
- address new problems or concerns.
567
-
568
- Each version is given a distinguishing version number. If the
569
- Program specifies that a certain numbered version of the GNU Affero General
570
- Public License "or any later version" applies to it, you have the
571
- option of following the terms and conditions either of that numbered
572
- version or of any later version published by the Free Software
573
- Foundation. If the Program does not specify a version number of the
574
- GNU Affero General Public License, you may choose any version ever published
575
- by the Free Software Foundation.
576
-
577
- If the Program specifies that a proxy can decide which future
578
- versions of the GNU Affero General Public License can be used, that proxy's
579
- public statement of acceptance of a version permanently authorizes you
580
- to choose that version for the Program.
581
-
582
- Later license versions may give you additional or different
583
- permissions. However, no additional obligations are imposed on any
584
- author or copyright holder as a result of your choosing to follow a
585
- later version.
586
-
587
- 15. Disclaimer of Warranty.
588
-
589
- THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590
- APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591
- HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592
- OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593
- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594
- PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595
- IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596
- ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597
-
598
- 16. Limitation of Liability.
599
-
600
- IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601
- WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602
- THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603
- GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604
- USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605
- DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606
- PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607
- EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608
- SUCH DAMAGES.
609
-
610
- 17. Interpretation of Sections 15 and 16.
611
-
612
- If the disclaimer of warranty and limitation of liability provided
613
- above cannot be given local legal effect according to their terms,
614
- reviewing courts shall apply local law that most closely approximates
615
- an absolute waiver of all civil liability in connection with the
616
- Program, unless a warranty or assumption of liability accompanies a
617
- copy of the Program in return for a fee.
618
-
619
- END OF TERMS AND CONDITIONS
620
-
621
- How to Apply These Terms to Your New Programs
622
-
623
- If you develop a new program, and you want it to be of the greatest
624
- possible use to the public, the best way to achieve this is to make it
625
- free software which everyone can redistribute and change under these terms.
626
-
627
- To do so, attach the following notices to the program. It is safest
628
- to attach them to the start of each source file to most effectively
629
- state the exclusion of warranty; and each file should have at least
630
- the "copyright" line and a pointer to where the full notice is found.
631
-
632
- <one line to give the program's name and a brief idea of what it does.>
633
- Copyright (C) <year> <name of author>
634
-
635
- This program is free software: you can redistribute it and/or modify
636
- it under the terms of the GNU Affero General Public License as published
637
- by the Free Software Foundation, either version 3 of the License, or
638
- (at your option) any later version.
639
-
640
- This program is distributed in the hope that it will be useful,
641
- but WITHOUT ANY WARRANTY; without even the implied warranty of
642
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643
- GNU Affero General Public License for more details.
644
-
645
- You should have received a copy of the GNU Affero General Public License
646
- along with this program. If not, see <https://www.gnu.org/licenses/>.
647
-
648
- Also add information on how to contact you by electronic and paper mail.
649
-
650
- If your software can interact with users remotely through a computer
651
- network, you should also make sure that it provides a way for users to
652
- get its source. For example, if your program is a web application, its
653
- interface could display a "Source" link that leads users to an archive
654
- of the code. There are many ways you could offer source, and different
655
- solutions will be better for different programs; see section 13 for the
656
- specific requirements.
657
-
658
- You should also get your employer (if you work as a programmer) or school,
659
- if any, to sign a "copyright disclaimer" for the program, if necessary.
660
- For more information on this, and how to apply and follow the GNU AGPL, see
661
- <https://www.gnu.org/licenses/>.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/attentions.py DELETED
@@ -1,464 +0,0 @@
1
- import math
2
- import torch
3
- from torch import nn
4
- from torch.nn import functional as F
5
-
6
- import commons
7
- import logging
8
-
9
- logger = logging.getLogger(__name__)
10
-
11
-
12
- class LayerNorm(nn.Module):
13
- def __init__(self, channels, eps=1e-5):
14
- super().__init__()
15
- self.channels = channels
16
- self.eps = eps
17
-
18
- self.gamma = nn.Parameter(torch.ones(channels))
19
- self.beta = nn.Parameter(torch.zeros(channels))
20
-
21
- def forward(self, x):
22
- x = x.transpose(1, -1)
23
- x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
24
- return x.transpose(1, -1)
25
-
26
-
27
- @torch.jit.script
28
- def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
29
- n_channels_int = n_channels[0]
30
- in_act = input_a + input_b
31
- t_act = torch.tanh(in_act[:, :n_channels_int, :])
32
- s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
33
- acts = t_act * s_act
34
- return acts
35
-
36
-
37
- class Encoder(nn.Module):
38
- def __init__(
39
- self,
40
- hidden_channels,
41
- filter_channels,
42
- n_heads,
43
- n_layers,
44
- kernel_size=1,
45
- p_dropout=0.0,
46
- window_size=4,
47
- isflow=True,
48
- **kwargs
49
- ):
50
- super().__init__()
51
- self.hidden_channels = hidden_channels
52
- self.filter_channels = filter_channels
53
- self.n_heads = n_heads
54
- self.n_layers = n_layers
55
- self.kernel_size = kernel_size
56
- self.p_dropout = p_dropout
57
- self.window_size = window_size
58
- # if isflow:
59
- # cond_layer = torch.nn.Conv1d(256, 2*hidden_channels*n_layers, 1)
60
- # self.cond_pre = torch.nn.Conv1d(hidden_channels, 2*hidden_channels, 1)
61
- # self.cond_layer = weight_norm(cond_layer, name='weight')
62
- # self.gin_channels = 256
63
- self.cond_layer_idx = self.n_layers
64
- if "gin_channels" in kwargs:
65
- self.gin_channels = kwargs["gin_channels"]
66
- if self.gin_channels != 0:
67
- self.spk_emb_linear = nn.Linear(self.gin_channels, self.hidden_channels)
68
- # vits2 says 3rd block, so idx is 2 by default
69
- self.cond_layer_idx = (
70
- kwargs["cond_layer_idx"] if "cond_layer_idx" in kwargs else 2
71
- )
72
- logging.debug(self.gin_channels, self.cond_layer_idx)
73
- assert (
74
- self.cond_layer_idx < self.n_layers
75
- ), "cond_layer_idx should be less than n_layers"
76
- self.drop = nn.Dropout(p_dropout)
77
- self.attn_layers = nn.ModuleList()
78
- self.norm_layers_1 = nn.ModuleList()
79
- self.ffn_layers = nn.ModuleList()
80
- self.norm_layers_2 = nn.ModuleList()
81
- for i in range(self.n_layers):
82
- self.attn_layers.append(
83
- MultiHeadAttention(
84
- hidden_channels,
85
- hidden_channels,
86
- n_heads,
87
- p_dropout=p_dropout,
88
- window_size=window_size,
89
- )
90
- )
91
- self.norm_layers_1.append(LayerNorm(hidden_channels))
92
- self.ffn_layers.append(
93
- FFN(
94
- hidden_channels,
95
- hidden_channels,
96
- filter_channels,
97
- kernel_size,
98
- p_dropout=p_dropout,
99
- )
100
- )
101
- self.norm_layers_2.append(LayerNorm(hidden_channels))
102
-
103
- def forward(self, x, x_mask, g=None):
104
- attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
105
- x = x * x_mask
106
- for i in range(self.n_layers):
107
- if i == self.cond_layer_idx and g is not None:
108
- g = self.spk_emb_linear(g.transpose(1, 2))
109
- g = g.transpose(1, 2)
110
- x = x + g
111
- x = x * x_mask
112
- y = self.attn_layers[i](x, x, attn_mask)
113
- y = self.drop(y)
114
- x = self.norm_layers_1[i](x + y)
115
-
116
- y = self.ffn_layers[i](x, x_mask)
117
- y = self.drop(y)
118
- x = self.norm_layers_2[i](x + y)
119
- x = x * x_mask
120
- return x
121
-
122
-
123
- class Decoder(nn.Module):
124
- def __init__(
125
- self,
126
- hidden_channels,
127
- filter_channels,
128
- n_heads,
129
- n_layers,
130
- kernel_size=1,
131
- p_dropout=0.0,
132
- proximal_bias=False,
133
- proximal_init=True,
134
- **kwargs
135
- ):
136
- super().__init__()
137
- self.hidden_channels = hidden_channels
138
- self.filter_channels = filter_channels
139
- self.n_heads = n_heads
140
- self.n_layers = n_layers
141
- self.kernel_size = kernel_size
142
- self.p_dropout = p_dropout
143
- self.proximal_bias = proximal_bias
144
- self.proximal_init = proximal_init
145
-
146
- self.drop = nn.Dropout(p_dropout)
147
- self.self_attn_layers = nn.ModuleList()
148
- self.norm_layers_0 = nn.ModuleList()
149
- self.encdec_attn_layers = nn.ModuleList()
150
- self.norm_layers_1 = nn.ModuleList()
151
- self.ffn_layers = nn.ModuleList()
152
- self.norm_layers_2 = nn.ModuleList()
153
- for i in range(self.n_layers):
154
- self.self_attn_layers.append(
155
- MultiHeadAttention(
156
- hidden_channels,
157
- hidden_channels,
158
- n_heads,
159
- p_dropout=p_dropout,
160
- proximal_bias=proximal_bias,
161
- proximal_init=proximal_init,
162
- )
163
- )
164
- self.norm_layers_0.append(LayerNorm(hidden_channels))
165
- self.encdec_attn_layers.append(
166
- MultiHeadAttention(
167
- hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
168
- )
169
- )
170
- self.norm_layers_1.append(LayerNorm(hidden_channels))
171
- self.ffn_layers.append(
172
- FFN(
173
- hidden_channels,
174
- hidden_channels,
175
- filter_channels,
176
- kernel_size,
177
- p_dropout=p_dropout,
178
- causal=True,
179
- )
180
- )
181
- self.norm_layers_2.append(LayerNorm(hidden_channels))
182
-
183
- def forward(self, x, x_mask, h, h_mask):
184
- """
185
- x: decoder input
186
- h: encoder output
187
- """
188
- self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
189
- device=x.device, dtype=x.dtype
190
- )
191
- encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
192
- x = x * x_mask
193
- for i in range(self.n_layers):
194
- y = self.self_attn_layers[i](x, x, self_attn_mask)
195
- y = self.drop(y)
196
- x = self.norm_layers_0[i](x + y)
197
-
198
- y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
199
- y = self.drop(y)
200
- x = self.norm_layers_1[i](x + y)
201
-
202
- y = self.ffn_layers[i](x, x_mask)
203
- y = self.drop(y)
204
- x = self.norm_layers_2[i](x + y)
205
- x = x * x_mask
206
- return x
207
-
208
-
209
- class MultiHeadAttention(nn.Module):
210
- def __init__(
211
- self,
212
- channels,
213
- out_channels,
214
- n_heads,
215
- p_dropout=0.0,
216
- window_size=None,
217
- heads_share=True,
218
- block_length=None,
219
- proximal_bias=False,
220
- proximal_init=False,
221
- ):
222
- super().__init__()
223
- assert channels % n_heads == 0
224
-
225
- self.channels = channels
226
- self.out_channels = out_channels
227
- self.n_heads = n_heads
228
- self.p_dropout = p_dropout
229
- self.window_size = window_size
230
- self.heads_share = heads_share
231
- self.block_length = block_length
232
- self.proximal_bias = proximal_bias
233
- self.proximal_init = proximal_init
234
- self.attn = None
235
-
236
- self.k_channels = channels // n_heads
237
- self.conv_q = nn.Conv1d(channels, channels, 1)
238
- self.conv_k = nn.Conv1d(channels, channels, 1)
239
- self.conv_v = nn.Conv1d(channels, channels, 1)
240
- self.conv_o = nn.Conv1d(channels, out_channels, 1)
241
- self.drop = nn.Dropout(p_dropout)
242
-
243
- if window_size is not None:
244
- n_heads_rel = 1 if heads_share else n_heads
245
- rel_stddev = self.k_channels**-0.5
246
- self.emb_rel_k = nn.Parameter(
247
- torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
248
- * rel_stddev
249
- )
250
- self.emb_rel_v = nn.Parameter(
251
- torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
252
- * rel_stddev
253
- )
254
-
255
- nn.init.xavier_uniform_(self.conv_q.weight)
256
- nn.init.xavier_uniform_(self.conv_k.weight)
257
- nn.init.xavier_uniform_(self.conv_v.weight)
258
- if proximal_init:
259
- with torch.no_grad():
260
- self.conv_k.weight.copy_(self.conv_q.weight)
261
- self.conv_k.bias.copy_(self.conv_q.bias)
262
-
263
- def forward(self, x, c, attn_mask=None):
264
- q = self.conv_q(x)
265
- k = self.conv_k(c)
266
- v = self.conv_v(c)
267
-
268
- x, self.attn = self.attention(q, k, v, mask=attn_mask)
269
-
270
- x = self.conv_o(x)
271
- return x
272
-
273
- def attention(self, query, key, value, mask=None):
274
- # reshape [b, d, t] -> [b, n_h, t, d_k]
275
- b, d, t_s, t_t = (*key.size(), query.size(2))
276
- query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
277
- key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
278
- value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
279
-
280
- scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
281
- if self.window_size is not None:
282
- assert (
283
- t_s == t_t
284
- ), "Relative attention is only available for self-attention."
285
- key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
286
- rel_logits = self._matmul_with_relative_keys(
287
- query / math.sqrt(self.k_channels), key_relative_embeddings
288
- )
289
- scores_local = self._relative_position_to_absolute_position(rel_logits)
290
- scores = scores + scores_local
291
- if self.proximal_bias:
292
- assert t_s == t_t, "Proximal bias is only available for self-attention."
293
- scores = scores + self._attention_bias_proximal(t_s).to(
294
- device=scores.device, dtype=scores.dtype
295
- )
296
- if mask is not None:
297
- scores = scores.masked_fill(mask == 0, -1e4)
298
- if self.block_length is not None:
299
- assert (
300
- t_s == t_t
301
- ), "Local attention is only available for self-attention."
302
- block_mask = (
303
- torch.ones_like(scores)
304
- .triu(-self.block_length)
305
- .tril(self.block_length)
306
- )
307
- scores = scores.masked_fill(block_mask == 0, -1e4)
308
- p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
309
- p_attn = self.drop(p_attn)
310
- output = torch.matmul(p_attn, value)
311
- if self.window_size is not None:
312
- relative_weights = self._absolute_position_to_relative_position(p_attn)
313
- value_relative_embeddings = self._get_relative_embeddings(
314
- self.emb_rel_v, t_s
315
- )
316
- output = output + self._matmul_with_relative_values(
317
- relative_weights, value_relative_embeddings
318
- )
319
- output = (
320
- output.transpose(2, 3).contiguous().view(b, d, t_t)
321
- ) # [b, n_h, t_t, d_k] -> [b, d, t_t]
322
- return output, p_attn
323
-
324
- def _matmul_with_relative_values(self, x, y):
325
- """
326
- x: [b, h, l, m]
327
- y: [h or 1, m, d]
328
- ret: [b, h, l, d]
329
- """
330
- ret = torch.matmul(x, y.unsqueeze(0))
331
- return ret
332
-
333
- def _matmul_with_relative_keys(self, x, y):
334
- """
335
- x: [b, h, l, d]
336
- y: [h or 1, m, d]
337
- ret: [b, h, l, m]
338
- """
339
- ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
340
- return ret
341
-
342
- def _get_relative_embeddings(self, relative_embeddings, length):
343
- 2 * self.window_size + 1
344
- # Pad first before slice to avoid using cond ops.
345
- pad_length = max(length - (self.window_size + 1), 0)
346
- slice_start_position = max((self.window_size + 1) - length, 0)
347
- slice_end_position = slice_start_position + 2 * length - 1
348
- if pad_length > 0:
349
- padded_relative_embeddings = F.pad(
350
- relative_embeddings,
351
- commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
352
- )
353
- else:
354
- padded_relative_embeddings = relative_embeddings
355
- used_relative_embeddings = padded_relative_embeddings[
356
- :, slice_start_position:slice_end_position
357
- ]
358
- return used_relative_embeddings
359
-
360
- def _relative_position_to_absolute_position(self, x):
361
- """
362
- x: [b, h, l, 2*l-1]
363
- ret: [b, h, l, l]
364
- """
365
- batch, heads, length, _ = x.size()
366
- # Concat columns of pad to shift from relative to absolute indexing.
367
- x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
368
-
369
- # Concat extra elements so to add up to shape (len+1, 2*len-1).
370
- x_flat = x.view([batch, heads, length * 2 * length])
371
- x_flat = F.pad(
372
- x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
373
- )
374
-
375
- # Reshape and slice out the padded elements.
376
- x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
377
- :, :, :length, length - 1 :
378
- ]
379
- return x_final
380
-
381
- def _absolute_position_to_relative_position(self, x):
382
- """
383
- x: [b, h, l, l]
384
- ret: [b, h, l, 2*l-1]
385
- """
386
- batch, heads, length, _ = x.size()
387
- # pad along column
388
- x = F.pad(
389
- x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
390
- )
391
- x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
392
- # add 0's in the beginning that will skew the elements after reshape
393
- x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
394
- x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
395
- return x_final
396
-
397
- def _attention_bias_proximal(self, length):
398
- """Bias for self-attention to encourage attention to close positions.
399
- Args:
400
- length: an integer scalar.
401
- Returns:
402
- a Tensor with shape [1, 1, length, length]
403
- """
404
- r = torch.arange(length, dtype=torch.float32)
405
- diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
406
- return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
407
-
408
-
409
- class FFN(nn.Module):
410
- def __init__(
411
- self,
412
- in_channels,
413
- out_channels,
414
- filter_channels,
415
- kernel_size,
416
- p_dropout=0.0,
417
- activation=None,
418
- causal=False,
419
- ):
420
- super().__init__()
421
- self.in_channels = in_channels
422
- self.out_channels = out_channels
423
- self.filter_channels = filter_channels
424
- self.kernel_size = kernel_size
425
- self.p_dropout = p_dropout
426
- self.activation = activation
427
- self.causal = causal
428
-
429
- if causal:
430
- self.padding = self._causal_padding
431
- else:
432
- self.padding = self._same_padding
433
-
434
- self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
435
- self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
436
- self.drop = nn.Dropout(p_dropout)
437
-
438
- def forward(self, x, x_mask):
439
- x = self.conv_1(self.padding(x * x_mask))
440
- if self.activation == "gelu":
441
- x = x * torch.sigmoid(1.702 * x)
442
- else:
443
- x = torch.relu(x)
444
- x = self.drop(x)
445
- x = self.conv_2(self.padding(x * x_mask))
446
- return x * x_mask
447
-
448
- def _causal_padding(self, x):
449
- if self.kernel_size == 1:
450
- return x
451
- pad_l = self.kernel_size - 1
452
- pad_r = 0
453
- padding = [[0, 0], [0, 0], [pad_l, pad_r]]
454
- x = F.pad(x, commons.convert_pad_shape(padding))
455
- return x
456
-
457
- def _same_padding(self, x):
458
- if self.kernel_size == 1:
459
- return x
460
- pad_l = (self.kernel_size - 1) // 2
461
- pad_r = self.kernel_size // 2
462
- padding = [[0, 0], [0, 0], [pad_l, pad_r]]
463
- x = F.pad(x, commons.convert_pad_shape(padding))
464
- return x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/attentions_onnx.py DELETED
@@ -1,378 +0,0 @@
1
- import math
2
- import torch
3
- from torch import nn
4
- from torch.nn import functional as F
5
-
6
- import commons
7
- import logging
8
-
9
- logger = logging.getLogger(__name__)
10
-
11
-
12
- class LayerNorm(nn.Module):
13
- def __init__(self, channels, eps=1e-5):
14
- super().__init__()
15
- self.channels = channels
16
- self.eps = eps
17
-
18
- self.gamma = nn.Parameter(torch.ones(channels))
19
- self.beta = nn.Parameter(torch.zeros(channels))
20
-
21
- def forward(self, x):
22
- x = x.transpose(1, -1)
23
- x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
24
- return x.transpose(1, -1)
25
-
26
-
27
- @torch.jit.script
28
- def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
29
- n_channels_int = n_channels[0]
30
- in_act = input_a + input_b
31
- t_act = torch.tanh(in_act[:, :n_channels_int, :])
32
- s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
33
- acts = t_act * s_act
34
- return acts
35
-
36
-
37
- class Encoder(nn.Module):
38
- def __init__(
39
- self,
40
- hidden_channels,
41
- filter_channels,
42
- n_heads,
43
- n_layers,
44
- kernel_size=1,
45
- p_dropout=0.0,
46
- window_size=4,
47
- isflow=True,
48
- **kwargs
49
- ):
50
- super().__init__()
51
- self.hidden_channels = hidden_channels
52
- self.filter_channels = filter_channels
53
- self.n_heads = n_heads
54
- self.n_layers = n_layers
55
- self.kernel_size = kernel_size
56
- self.p_dropout = p_dropout
57
- self.window_size = window_size
58
- # if isflow:
59
- # cond_layer = torch.nn.Conv1d(256, 2*hidden_channels*n_layers, 1)
60
- # self.cond_pre = torch.nn.Conv1d(hidden_channels, 2*hidden_channels, 1)
61
- # self.cond_layer = weight_norm(cond_layer, name='weight')
62
- # self.gin_channels = 256
63
- self.cond_layer_idx = self.n_layers
64
- if "gin_channels" in kwargs:
65
- self.gin_channels = kwargs["gin_channels"]
66
- if self.gin_channels != 0:
67
- self.spk_emb_linear = nn.Linear(self.gin_channels, self.hidden_channels)
68
- # vits2 says 3rd block, so idx is 2 by default
69
- self.cond_layer_idx = (
70
- kwargs["cond_layer_idx"] if "cond_layer_idx" in kwargs else 2
71
- )
72
- logging.debug(self.gin_channels, self.cond_layer_idx)
73
- assert (
74
- self.cond_layer_idx < self.n_layers
75
- ), "cond_layer_idx should be less than n_layers"
76
- self.drop = nn.Dropout(p_dropout)
77
- self.attn_layers = nn.ModuleList()
78
- self.norm_layers_1 = nn.ModuleList()
79
- self.ffn_layers = nn.ModuleList()
80
- self.norm_layers_2 = nn.ModuleList()
81
- for i in range(self.n_layers):
82
- self.attn_layers.append(
83
- MultiHeadAttention(
84
- hidden_channels,
85
- hidden_channels,
86
- n_heads,
87
- p_dropout=p_dropout,
88
- window_size=window_size,
89
- )
90
- )
91
- self.norm_layers_1.append(LayerNorm(hidden_channels))
92
- self.ffn_layers.append(
93
- FFN(
94
- hidden_channels,
95
- hidden_channels,
96
- filter_channels,
97
- kernel_size,
98
- p_dropout=p_dropout,
99
- )
100
- )
101
- self.norm_layers_2.append(LayerNorm(hidden_channels))
102
-
103
- def forward(self, x, x_mask, g=None):
104
- attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
105
- x = x * x_mask
106
- for i in range(self.n_layers):
107
- if i == self.cond_layer_idx and g is not None:
108
- g = self.spk_emb_linear(g.transpose(1, 2))
109
- g = g.transpose(1, 2)
110
- x = x + g
111
- x = x * x_mask
112
- y = self.attn_layers[i](x, x, attn_mask)
113
- y = self.drop(y)
114
- x = self.norm_layers_1[i](x + y)
115
-
116
- y = self.ffn_layers[i](x, x_mask)
117
- y = self.drop(y)
118
- x = self.norm_layers_2[i](x + y)
119
- x = x * x_mask
120
- return x
121
-
122
-
123
- class MultiHeadAttention(nn.Module):
124
- def __init__(
125
- self,
126
- channels,
127
- out_channels,
128
- n_heads,
129
- p_dropout=0.0,
130
- window_size=None,
131
- heads_share=True,
132
- block_length=None,
133
- proximal_bias=False,
134
- proximal_init=False,
135
- ):
136
- super().__init__()
137
- assert channels % n_heads == 0
138
-
139
- self.channels = channels
140
- self.out_channels = out_channels
141
- self.n_heads = n_heads
142
- self.p_dropout = p_dropout
143
- self.window_size = window_size
144
- self.heads_share = heads_share
145
- self.block_length = block_length
146
- self.proximal_bias = proximal_bias
147
- self.proximal_init = proximal_init
148
- self.attn = None
149
-
150
- self.k_channels = channels // n_heads
151
- self.conv_q = nn.Conv1d(channels, channels, 1)
152
- self.conv_k = nn.Conv1d(channels, channels, 1)
153
- self.conv_v = nn.Conv1d(channels, channels, 1)
154
- self.conv_o = nn.Conv1d(channels, out_channels, 1)
155
- self.drop = nn.Dropout(p_dropout)
156
-
157
- if window_size is not None:
158
- n_heads_rel = 1 if heads_share else n_heads
159
- rel_stddev = self.k_channels**-0.5
160
- self.emb_rel_k = nn.Parameter(
161
- torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
162
- * rel_stddev
163
- )
164
- self.emb_rel_v = nn.Parameter(
165
- torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
166
- * rel_stddev
167
- )
168
-
169
- nn.init.xavier_uniform_(self.conv_q.weight)
170
- nn.init.xavier_uniform_(self.conv_k.weight)
171
- nn.init.xavier_uniform_(self.conv_v.weight)
172
- if proximal_init:
173
- with torch.no_grad():
174
- self.conv_k.weight.copy_(self.conv_q.weight)
175
- self.conv_k.bias.copy_(self.conv_q.bias)
176
-
177
- def forward(self, x, c, attn_mask=None):
178
- q = self.conv_q(x)
179
- k = self.conv_k(c)
180
- v = self.conv_v(c)
181
-
182
- x, self.attn = self.attention(q, k, v, mask=attn_mask)
183
-
184
- x = self.conv_o(x)
185
- return x
186
-
187
- def attention(self, query, key, value, mask=None):
188
- # reshape [b, d, t] -> [b, n_h, t, d_k]
189
- b, d, t_s, t_t = (*key.size(), query.size(2))
190
- query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
191
- key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
192
- value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
193
-
194
- scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
195
- if self.window_size is not None:
196
- assert (
197
- t_s == t_t
198
- ), "Relative attention is only available for self-attention."
199
- key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
200
- rel_logits = self._matmul_with_relative_keys(
201
- query / math.sqrt(self.k_channels), key_relative_embeddings
202
- )
203
- scores_local = self._relative_position_to_absolute_position(rel_logits)
204
- scores = scores + scores_local
205
- if self.proximal_bias:
206
- assert t_s == t_t, "Proximal bias is only available for self-attention."
207
- scores = scores + self._attention_bias_proximal(t_s).to(
208
- device=scores.device, dtype=scores.dtype
209
- )
210
- if mask is not None:
211
- scores = scores.masked_fill(mask == 0, -1e4)
212
- if self.block_length is not None:
213
- assert (
214
- t_s == t_t
215
- ), "Local attention is only available for self-attention."
216
- block_mask = (
217
- torch.ones_like(scores)
218
- .triu(-self.block_length)
219
- .tril(self.block_length)
220
- )
221
- scores = scores.masked_fill(block_mask == 0, -1e4)
222
- p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
223
- p_attn = self.drop(p_attn)
224
- output = torch.matmul(p_attn, value)
225
- if self.window_size is not None:
226
- relative_weights = self._absolute_position_to_relative_position(p_attn)
227
- value_relative_embeddings = self._get_relative_embeddings(
228
- self.emb_rel_v, t_s
229
- )
230
- output = output + self._matmul_with_relative_values(
231
- relative_weights, value_relative_embeddings
232
- )
233
- output = (
234
- output.transpose(2, 3).contiguous().view(b, d, t_t)
235
- ) # [b, n_h, t_t, d_k] -> [b, d, t_t]
236
- return output, p_attn
237
-
238
- def _matmul_with_relative_values(self, x, y):
239
- """
240
- x: [b, h, l, m]
241
- y: [h or 1, m, d]
242
- ret: [b, h, l, d]
243
- """
244
- ret = torch.matmul(x, y.unsqueeze(0))
245
- return ret
246
-
247
- def _matmul_with_relative_keys(self, x, y):
248
- """
249
- x: [b, h, l, d]
250
- y: [h or 1, m, d]
251
- ret: [b, h, l, m]
252
- """
253
- ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
254
- return ret
255
-
256
- def _get_relative_embeddings(self, relative_embeddings, length):
257
- max_relative_position = 2 * self.window_size + 1
258
- # Pad first before slice to avoid using cond ops.
259
- pad_length = max(length - (self.window_size + 1), 0)
260
- slice_start_position = max((self.window_size + 1) - length, 0)
261
- slice_end_position = slice_start_position + 2 * length - 1
262
- if pad_length > 0:
263
- padded_relative_embeddings = F.pad(
264
- relative_embeddings,
265
- commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
266
- )
267
- else:
268
- padded_relative_embeddings = relative_embeddings
269
- used_relative_embeddings = padded_relative_embeddings[
270
- :, slice_start_position:slice_end_position
271
- ]
272
- return used_relative_embeddings
273
-
274
- def _relative_position_to_absolute_position(self, x):
275
- """
276
- x: [b, h, l, 2*l-1]
277
- ret: [b, h, l, l]
278
- """
279
- batch, heads, length, _ = x.size()
280
- # Concat columns of pad to shift from relative to absolute indexing.
281
- x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
282
-
283
- # Concat extra elements so to add up to shape (len+1, 2*len-1).
284
- x_flat = x.view([batch, heads, length * 2 * length])
285
- x_flat = F.pad(
286
- x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
287
- )
288
-
289
- # Reshape and slice out the padded elements.
290
- x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
291
- :, :, :length, length - 1 :
292
- ]
293
- return x_final
294
-
295
- def _absolute_position_to_relative_position(self, x):
296
- """
297
- x: [b, h, l, l]
298
- ret: [b, h, l, 2*l-1]
299
- """
300
- batch, heads, length, _ = x.size()
301
- # padd along column
302
- x = F.pad(
303
- x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
304
- )
305
- x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
306
- # add 0's in the beginning that will skew the elements after reshape
307
- x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
308
- x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
309
- return x_final
310
-
311
- def _attention_bias_proximal(self, length):
312
- """Bias for self-attention to encourage attention to close positions.
313
- Args:
314
- length: an integer scalar.
315
- Returns:
316
- a Tensor with shape [1, 1, length, length]
317
- """
318
- r = torch.arange(length, dtype=torch.float32)
319
- diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
320
- return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
321
-
322
-
323
- class FFN(nn.Module):
324
- def __init__(
325
- self,
326
- in_channels,
327
- out_channels,
328
- filter_channels,
329
- kernel_size,
330
- p_dropout=0.0,
331
- activation=None,
332
- causal=False,
333
- ):
334
- super().__init__()
335
- self.in_channels = in_channels
336
- self.out_channels = out_channels
337
- self.filter_channels = filter_channels
338
- self.kernel_size = kernel_size
339
- self.p_dropout = p_dropout
340
- self.activation = activation
341
- self.causal = causal
342
-
343
- if causal:
344
- self.padding = self._causal_padding
345
- else:
346
- self.padding = self._same_padding
347
-
348
- self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
349
- self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
350
- self.drop = nn.Dropout(p_dropout)
351
-
352
- def forward(self, x, x_mask):
353
- x = self.conv_1(self.padding(x * x_mask))
354
- if self.activation == "gelu":
355
- x = x * torch.sigmoid(1.702 * x)
356
- else:
357
- x = torch.relu(x)
358
- x = self.drop(x)
359
- x = self.conv_2(self.padding(x * x_mask))
360
- return x * x_mask
361
-
362
- def _causal_padding(self, x):
363
- if self.kernel_size == 1:
364
- return x
365
- pad_l = self.kernel_size - 1
366
- pad_r = 0
367
- padding = [[0, 0], [0, 0], [pad_l, pad_r]]
368
- x = F.pad(x, commons.convert_pad_shape(padding))
369
- return x
370
-
371
- def _same_padding(self, x):
372
- if self.kernel_size == 1:
373
- return x
374
- pad_l = (self.kernel_size - 1) // 2
375
- pad_r = self.kernel_size // 2
376
- padding = [[0, 0], [0, 0], [pad_l, pad_r]]
377
- x = F.pad(x, commons.convert_pad_shape(padding))
378
- return x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/bert_gen.py DELETED
@@ -1,81 +0,0 @@
1
- import torch
2
- from multiprocessing import Pool
3
- import commons
4
- import utils
5
- from tqdm import tqdm
6
- from text import check_bert_models, cleaned_text_to_sequence, get_bert
7
- import argparse
8
- import torch.multiprocessing as mp
9
- from config import config
10
-
11
-
12
- def process_line(x):
13
- line, add_blank = x
14
- device = config.bert_gen_config.device
15
- if config.bert_gen_config.use_multi_device:
16
- rank = mp.current_process()._identity
17
- rank = rank[0] if len(rank) > 0 else 0
18
- if torch.cuda.is_available():
19
- gpu_id = rank % torch.cuda.device_count()
20
- device = torch.device(f"cuda:{gpu_id}")
21
- else:
22
- device = torch.device("cpu")
23
- wav_path, _, language_str, text, phones, tone, word2ph = line.strip().split("|")
24
- phone = phones.split(" ")
25
- tone = [int(i) for i in tone.split(" ")]
26
- word2ph = [int(i) for i in word2ph.split(" ")]
27
- word2ph = [i for i in word2ph]
28
- phone, tone, language = cleaned_text_to_sequence(phone, tone, language_str)
29
-
30
- if add_blank:
31
- phone = commons.intersperse(phone, 0)
32
- tone = commons.intersperse(tone, 0)
33
- language = commons.intersperse(language, 0)
34
- for i in range(len(word2ph)):
35
- word2ph[i] = word2ph[i] * 2
36
- word2ph[0] += 1
37
-
38
- bert_path = wav_path.replace(".WAV", ".wav").replace(".wav", ".bert.pt")
39
-
40
- try:
41
- bert = torch.load(bert_path)
42
- assert bert.shape[-1] == len(phone)
43
- except Exception:
44
- bert = get_bert(text, word2ph, language_str, device)
45
- assert bert.shape[-1] == len(phone)
46
- torch.save(bert, bert_path)
47
-
48
-
49
- preprocess_text_config = config.preprocess_text_config
50
-
51
- if __name__ == "__main__":
52
- parser = argparse.ArgumentParser()
53
- parser.add_argument(
54
- "-c", "--config", type=str, default=config.bert_gen_config.config_path
55
- )
56
- parser.add_argument(
57
- "--num_processes", type=int, default=config.bert_gen_config.num_processes
58
- )
59
- args, _ = parser.parse_known_args()
60
- config_path = args.config
61
- hps = utils.get_hparams_from_file(config_path)
62
- check_bert_models()
63
- lines = []
64
- with open(hps.data.training_files, encoding="utf-8") as f:
65
- lines.extend(f.readlines())
66
-
67
- with open(hps.data.validation_files, encoding="utf-8") as f:
68
- lines.extend(f.readlines())
69
- add_blank = [hps.data.add_blank] * len(lines)
70
-
71
- if len(lines) != 0:
72
- num_processes = args.num_processes
73
- with Pool(processes=num_processes) as pool:
74
- for _ in tqdm(
75
- pool.imap_unordered(process_line, zip(lines, add_blank)),
76
- total=len(lines),
77
- ):
78
- # 这里是缩进的代码块,表示循环体
79
- pass # 使用pass语句作为占位符
80
-
81
- print(f"bert生成完毕!, 共有{len(lines)}个bert.pt生成!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/clap_gen.py DELETED
@@ -1,64 +0,0 @@
1
- import argparse
2
- from multiprocessing import Pool, cpu_count
3
-
4
- import torch
5
- import torch.multiprocessing as mp
6
- from tqdm import tqdm
7
-
8
- import utils
9
- from config import config
10
- from clap_wrapper import get_clap_audio_feature
11
- import librosa
12
- import os
13
-
14
- os.environ["OMP_NUM_THREADS"] = "1"
15
- os.environ["MKL_NUM_THREADS"] = "1"
16
-
17
-
18
- def process_line(line):
19
- device = config.emo_gen_config.device
20
- if config.emo_gen_config.use_multi_device:
21
- rank = mp.current_process()._identity
22
- rank = rank[0] if len(rank) > 0 else 0
23
- if torch.cuda.is_available():
24
- gpu_id = rank % torch.cuda.device_count()
25
- device = torch.device(f"cuda:{gpu_id}")
26
- else:
27
- device = torch.device("cpu")
28
- wav_path, _, language_str, text, phones, tone, word2ph = line.strip().split("|")
29
-
30
- clap_path = wav_path.replace(".WAV", ".wav").replace(".wav", ".emo.pt")
31
- if os.path.isfile(clap_path):
32
- return
33
-
34
- audio = librosa.load(wav_path, 48000)[0]
35
- # audio = librosa.resample(audio, 44100, 48000)
36
-
37
- clap = get_clap_audio_feature(audio, device)
38
- torch.save(clap, clap_path)
39
-
40
-
41
- if __name__ == "__main__":
42
- parser = argparse.ArgumentParser()
43
- parser.add_argument(
44
- "-c", "--config", type=str, default=config.emo_gen_config.config_path
45
- )
46
- parser.add_argument(
47
- "--num_processes", type=int, default=config.emo_gen_config.num_processes
48
- )
49
- args, _ = parser.parse_known_args()
50
- config_path = args.config
51
- hps = utils.get_hparams_from_file(config_path)
52
- lines = []
53
- with open(hps.data.training_files, encoding="utf-8") as f:
54
- lines.extend(f.readlines())
55
-
56
- with open(hps.data.validation_files, encoding="utf-8") as f:
57
- lines.extend(f.readlines())
58
- if len(lines) != 0:
59
- num_processes = min(args.num_processes, cpu_count())
60
- with Pool(processes=num_processes) as pool:
61
- for _ in tqdm(pool.imap_unordered(process_line, lines), total=len(lines)):
62
- pass
63
-
64
- print(f"clap生成完毕!, 共有{len(lines)}个emo.pt生成!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/clap_wrapper.py DELETED
@@ -1,49 +0,0 @@
1
- import sys
2
-
3
- import torch
4
- from transformers import ClapModel, ClapProcessor
5
-
6
- from config import config
7
-
8
- models = dict()
9
- processor = ClapProcessor.from_pretrained("./emotional/clap-htsat-fused")
10
-
11
-
12
- def get_clap_audio_feature(audio_data, device=config.bert_gen_config.device):
13
- if (
14
- sys.platform == "darwin"
15
- and torch.backends.mps.is_available()
16
- and device == "cpu"
17
- ):
18
- device = "mps"
19
- if not device:
20
- device = "cuda"
21
- if device not in models.keys():
22
- models[device] = ClapModel.from_pretrained("./emotional/clap-htsat-fused").to(
23
- device
24
- )
25
- with torch.no_grad():
26
- inputs = processor(
27
- audios=audio_data, return_tensors="pt", sampling_rate=48000
28
- ).to(device)
29
- emb = models[device].get_audio_features(**inputs)
30
- return emb.T
31
-
32
-
33
- def get_clap_text_feature(text, device=config.bert_gen_config.device):
34
- if (
35
- sys.platform == "darwin"
36
- and torch.backends.mps.is_available()
37
- and device == "cpu"
38
- ):
39
- device = "mps"
40
- if not device:
41
- device = "cuda"
42
- if device not in models.keys():
43
- models[device] = ClapModel.from_pretrained("./emotional/clap-htsat-fused").to(
44
- device
45
- )
46
- with torch.no_grad():
47
- inputs = processor(text=text, return_tensors="pt").to(device)
48
- emb = models[device].get_text_features(**inputs)
49
- return emb.T
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/commons.py DELETED
@@ -1,158 +0,0 @@
1
- import math
2
- import torch
3
- from torch.nn import functional as F
4
-
5
-
6
- def init_weights(m, mean=0.0, std=0.01):
7
- classname = m.__class__.__name__
8
- if classname.find("Conv") != -1:
9
- m.weight.data.normal_(mean, std)
10
-
11
-
12
- def get_padding(kernel_size, dilation=1):
13
- return int((kernel_size * dilation - dilation) / 2)
14
-
15
-
16
- def convert_pad_shape(pad_shape):
17
- layer = pad_shape[::-1]
18
- pad_shape = [item for sublist in layer for item in sublist]
19
- return pad_shape
20
-
21
-
22
- def intersperse(lst, item):
23
- result = [item] * (len(lst) * 2 + 1)
24
- result[1::2] = lst
25
- return result
26
-
27
-
28
- def kl_divergence(m_p, logs_p, m_q, logs_q):
29
- """KL(P||Q)"""
30
- kl = (logs_q - logs_p) - 0.5
31
- kl += (
32
- 0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
33
- )
34
- return kl
35
-
36
-
37
- def rand_gumbel(shape):
38
- """Sample from the Gumbel distribution, protect from overflows."""
39
- uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
40
- return -torch.log(-torch.log(uniform_samples))
41
-
42
-
43
- def rand_gumbel_like(x):
44
- g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
45
- return g
46
-
47
-
48
- def slice_segments(x, ids_str, segment_size=4):
49
- gather_indices = ids_str.view(x.size(0), 1, 1).repeat(
50
- 1, x.size(1), 1
51
- ) + torch.arange(segment_size, device=x.device)
52
- return torch.gather(x, 2, gather_indices)
53
-
54
-
55
- def rand_slice_segments(x, x_lengths=None, segment_size=4):
56
- b, d, t = x.size()
57
- if x_lengths is None:
58
- x_lengths = t
59
- ids_str_max = torch.clamp(x_lengths - segment_size + 1, min=0)
60
- ids_str = (torch.rand([b], device=x.device) * ids_str_max).to(dtype=torch.long)
61
- ret = slice_segments(x, ids_str, segment_size)
62
- return ret, ids_str
63
-
64
-
65
- def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
66
- position = torch.arange(length, dtype=torch.float)
67
- num_timescales = channels // 2
68
- log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
69
- num_timescales - 1
70
- )
71
- inv_timescales = min_timescale * torch.exp(
72
- torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
73
- )
74
- scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
75
- signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
76
- signal = F.pad(signal, [0, 0, 0, channels % 2])
77
- signal = signal.view(1, channels, length)
78
- return signal
79
-
80
-
81
- def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
82
- b, channels, length = x.size()
83
- signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
84
- return x + signal.to(dtype=x.dtype, device=x.device)
85
-
86
-
87
- def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
88
- b, channels, length = x.size()
89
- signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
90
- return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
91
-
92
-
93
- def subsequent_mask(length):
94
- mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
95
- return mask
96
-
97
-
98
- @torch.jit.script
99
- def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
100
- n_channels_int = n_channels[0]
101
- in_act = input_a + input_b
102
- t_act = torch.tanh(in_act[:, :n_channels_int, :])
103
- s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
104
- acts = t_act * s_act
105
- return acts
106
-
107
-
108
- def convert_pad_shape(pad_shape):
109
- layer = pad_shape[::-1]
110
- pad_shape = [item for sublist in layer for item in sublist]
111
- return pad_shape
112
-
113
-
114
- def shift_1d(x):
115
- x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
116
- return x
117
-
118
-
119
- def sequence_mask(length, max_length=None):
120
- if max_length is None:
121
- max_length = length.max()
122
- x = torch.arange(max_length, dtype=length.dtype, device=length.device)
123
- return x.unsqueeze(0) < length.unsqueeze(1)
124
-
125
-
126
- def generate_path(duration, mask):
127
- """
128
- duration: [b, 1, t_x]
129
- mask: [b, 1, t_y, t_x]
130
- """
131
-
132
- b, _, t_y, t_x = mask.shape
133
- cum_duration = torch.cumsum(duration, -1)
134
-
135
- cum_duration_flat = cum_duration.view(b * t_x)
136
- path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
137
- path = path.view(b, t_x, t_y)
138
- path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
139
- path = path.unsqueeze(1).transpose(2, 3) * mask
140
- return path
141
-
142
-
143
- def clip_grad_value_(parameters, clip_value, norm_type=2):
144
- if isinstance(parameters, torch.Tensor):
145
- parameters = [parameters]
146
- parameters = list(filter(lambda p: p.grad is not None, parameters))
147
- norm_type = float(norm_type)
148
- if clip_value is not None:
149
- clip_value = float(clip_value)
150
-
151
- total_norm = 0
152
- for p in parameters:
153
- param_norm = p.grad.data.norm(norm_type)
154
- total_norm += param_norm.item() ** norm_type
155
- if clip_value is not None:
156
- p.grad.data.clamp_(min=-clip_value, max=clip_value)
157
- total_norm = total_norm ** (1.0 / norm_type)
158
- return total_norm
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blue-Archive/Library-VITS/BanG-Dream/compress_model.py DELETED
@@ -1,89 +0,0 @@
1
- from collections import OrderedDict
2
- from text.symbols import symbols
3
- import torch
4
-
5
- from tools.log import logger
6
- import utils
7
- from models import SynthesizerTrn
8
- import os
9
-
10
-
11
- def copyStateDict(state_dict):
12
- if list(state_dict.keys())[0].startswith("module"):
13
- start_idx = 1
14
- else:
15
- start_idx = 0
16
- new_state_dict = OrderedDict()
17
- for k, v in state_dict.items():
18
- name = ",".join(k.split(".")[start_idx:])
19
- new_state_dict[name] = v
20
- return new_state_dict
21
-
22
-
23
- def removeOptimizer(config: str, input_model: str, ishalf: bool, output_model: str):
24
- hps = utils.get_hparams_from_file(config)
25
-
26
- net_g = SynthesizerTrn(
27
- len(symbols),
28
- hps.data.filter_length // 2 + 1,
29
- hps.train.segment_size // hps.data.hop_length,
30
- n_speakers=hps.data.n_speakers,
31
- **hps.model,
32
- )
33
-
34
- optim_g = torch.optim.AdamW(
35
- net_g.parameters(),
36
- hps.train.learning_rate,
37
- betas=hps.train.betas,
38
- eps=hps.train.eps,
39
- )
40
-
41
- state_dict_g = torch.load(input_model, map_location="cpu")
42
- new_dict_g = copyStateDict(state_dict_g)
43
- keys = []
44
- for k, v in new_dict_g["model"].items():
45
- if "enc_q" in k:
46
- continue # noqa: E701
47
- keys.append(k)
48
-
49
- new_dict_g = (
50
- {k: new_dict_g["model"][k].half() for k in keys}
51
- if ishalf
52
- else {k: new_dict_g["model"][k] for k in keys}
53
- )
54
-
55
- torch.save(
56
- {
57
- "model": new_dict_g,
58
- "iteration": 0,
59
- "optimizer": optim_g.state_dict(),
60
- "learning_rate": 0.0001,
61
- },
62
- output_model,
63
- )
64
-
65
-
66
- if __name__ == "__main__":
67
- import argparse
68
-
69
- parser = argparse.ArgumentParser()
70
- parser.add_argument("-c", "--config", type=str, default="configs/config.json")
71
- parser.add_argument("-i", "--input", type=str)
72
- parser.add_argument("-o", "--output", type=str, default=None)
73
- parser.add_argument(
74
- "-hf", "--half", action="store_true", default=False, help="Save as FP16"
75
- )
76
-
77
- args = parser.parse_args()
78
-
79
- output = args.output
80
-
81
- if output is None:
82
- import os.path
83
-
84
- filename, ext = os.path.splitext(args.input)
85
- half = "_half" if args.half else ""
86
- output = filename + "_release" + half + ext
87
-
88
- removeOptimizer(args.config, args.input, args.half, output)
89
- logger.info(f"压缩模型成功, 输出模型: {os.path.abspath(output)}")