so-vits-svc/sovits4_for_colab.ipynb

679 lines
22 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "2q0l56aFQhAM"
},
"source": [
"# Terms of Use\n",
"\n",
"### Please solve the authorization problem of the dataset on your own. You shall be solely responsible for any problems caused by the use of non-authorized datasets for training and all consequences thereof.The repository and its maintainer, svc develop team, have nothing to do with the consequences!\n",
"\n",
"1. This project is established for academic exchange purposes only and is intended for communication and learning purposes. It is not intended for production environments.\n",
"2. Any videos based on sovits that are published on video platforms must clearly indicate in the description that they are used for voice changing and specify the input source of the voice or audio, for example, using videos or audios published by others and separating the vocals as input source for conversion, which must provide clear original video or music links. If your own voice or other synthesized voices from other commercial vocal synthesis software are used as the input source for conversion, you must also explain it in the description.\n",
"3. You shall be solely responsible for any infringement problems caused by the input source. When using other commercial vocal synthesis software as input source, please ensure that you comply with the terms of use of the software. Note that many vocal synthesis engines clearly state in their terms of use that they cannot be used for input source conversion.\n",
"4. Continuing to use this project is deemed as agreeing to the relevant provisions stated in this repository README. This repository README has the obligation to persuade, and is not responsible for any subsequent problems that may arise.\n",
"5. If you distribute this repository's code or publish any results produced by this project publicly (including but not limited to video sharing platforms), please indicate the original author and code source (this repository).\n",
"6. If you use this project for any other plan, please contact and inform the author of this repository in advance. Thank you very much.\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "M_RcDbVPhivj"
},
"source": [
"## **Note:**\n",
"## **Make sure there is no a directory named `sovits4data` in your google drive at the first time you use this notebook.**\n",
"## **It will be created to store some necessary files.** \n",
"## **For sure you can change it to another directory by modifying `sovits_data_dir` variable.**"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "fHaw6hGEa_Nk"
},
"source": [
"# **Initialize environment**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0gQcIZ8RsOkn"
},
"outputs": [],
"source": [
"#@title Connect to colab runtime and check GPU\n",
"\n",
"#@markdown # Connect to colab runtime and check GPU\n",
"\n",
"#@markdown\n",
"\n",
"!nvidia-smi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0YUGpYrXhMck"
},
"outputs": [],
"source": [
"#@title Clone repository and install requirements\n",
"\n",
"#@markdown # Clone repository and install requirements\n",
"\n",
"#@markdown\n",
"\n",
"#@markdown ### After the execution is completed, the runtime will **automatically restart**\n",
"\n",
"#@markdown\n",
"\n",
"!git clone https://github.com/svc-develop-team/so-vits-svc -b 4.1-Stable\n",
"%pip uninstall -y torchdata torchtext\n",
"%pip install --upgrade pip setuptools numpy numba\n",
"%pip install pyworld praat-parselmouth fairseq tensorboardX torchcrepe librosa==0.9.1 pyyaml pynvml pyloudnorm faiss-gpu\n",
"%pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118\n",
"exit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wmUkpUmfn_Hs"
},
"outputs": [],
"source": [
"#@title Mount google drive and select which directories to sync with google drive\n",
"\n",
"#@markdown # Mount google drive and select which directories to sync with google drive\n",
"\n",
"#@markdown\n",
"\n",
"from google.colab import drive\n",
"drive.mount(\"/content/drive\")\n",
"\n",
"#@markdown Directory to store **necessary files**, dont miss the slash at the end👇.\n",
"sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\" #@param {type:\"string\"}\n",
"#@markdown By default it will create a `sovits4data/` folder in your google drive.\n",
"RAW_DIR = sovits_data_dir + \"raw/\"\n",
"RESULTS_DIR = sovits_data_dir + \"results/\"\n",
"FILELISTS_DIR = sovits_data_dir + \"filelists/\"\n",
"CONFIGS_DIR = sovits_data_dir + \"configs/\"\n",
"LOGS_DIR = sovits_data_dir + \"logs/44k/\"\n",
"\n",
"#@markdown\n",
"\n",
"#@markdown ### These folders will be synced with your google drvie\n",
"\n",
"#@markdown ### **Strongly recommend to check all.**\n",
"\n",
"#@markdown Sync **input audios** and **output audios**\n",
"sync_raw_and_results = True #@param {type:\"boolean\"}\n",
"if sync_raw_and_results:\n",
" !mkdir -p {RAW_DIR}\n",
" !mkdir -p {RESULTS_DIR}\n",
" !rm -rf /content/so-vits-svc/raw\n",
" !rm -rf /content/so-vits-svc/results\n",
" !ln -s {RAW_DIR} /content/so-vits-svc/raw\n",
" !ln -s {RESULTS_DIR} /content/so-vits-svc/results\n",
"\n",
"#@markdown Sync **config** and **models**\n",
"sync_configs_and_logs = True #@param {type:\"boolean\"}\n",
"if sync_configs_and_logs:\n",
" !mkdir -p {FILELISTS_DIR}\n",
" !mkdir -p {CONFIGS_DIR}\n",
" !mkdir -p {LOGS_DIR}\n",
" !rm -rf /content/so-vits-svc/filelists\n",
" !rm -rf /content/so-vits-svc/configs\n",
" !rm -rf /content/so-vits-svc/logs/44k\n",
" !ln -s {FILELISTS_DIR} /content/so-vits-svc/filelists\n",
" !ln -s {CONFIGS_DIR} /content/so-vits-svc/configs\n",
" !ln -s {LOGS_DIR} /content/so-vits-svc/logs/44k"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "G_PMPCN6wvgZ"
},
"outputs": [],
"source": [
"#@title Get pretrained model(Optional but strongly recommend).\n",
"\n",
"#@markdown # Get pretrained model(Optional but strongly recommend).\n",
"\n",
"#@markdown\n",
"\n",
"#@markdown - Pre-trained model files: `G_0.pth` `D_0.pth`\n",
"#@markdown - Place them under /sovits4data/logs/44k/ in your google drive manualy\n",
"\n",
"#@markdown Get them from svc-develop-team(TBD) or anywhere else.\n",
"\n",
"#@markdown Although the pretrained model generally does not cause any copyright problems, please pay attention to it. For example, ask the author in advance, or the author has indicated the feasible use in the description clearly.\n",
"\n",
"download_pretrained_model = True #@param {type:\"boolean\"}\n",
"D_0_URL = \"https://huggingface.co/datasets/ms903/sovits4.0-768vec-layer12/resolve/main/sovits_768l12_pre_large_320k/clean_D_320000.pth\" #@param {type:\"string\"}\n",
"G_0_URL = \"https://huggingface.co/datasets/ms903/sovits4.0-768vec-layer12/resolve/main/sovits_768l12_pre_large_320k/clean_G_320000.pth\" #@param {type:\"string\"}\n",
"\n",
"download_pretrained_diffusion_model = True #@param {type:\"boolean\"}\n",
"diff_model_URL = \"https://huggingface.co/datasets/ms903/Diff-SVC-refactor-pre-trained-model/resolve/main/fix_pitch_add_vctk_600k/model_0.pt\" #@param {type:\"string\"}\n",
"\n",
"%cd /content/so-vits-svc\n",
"\n",
"if download_pretrained_model:\n",
" !curl -L {D_0_URL} -o logs/44k/D_0.pth\n",
" !curl -L {G_0_URL} -o logs/44k/G_0.pth\n",
"\n",
"if download_pretrained_diffusion_model:\n",
" !curl -L {diff_model_URL} -o logs/44k/diffusion/model_0.pt"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "k1qadJBFehMo"
},
"source": [
"# **Dataset preprocessing**"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "kBlju6Q3lSM6"
},
"source": [
"Pack and upload your raw dataset(dataset_raw/) to your google drive.\n",
"\n",
"Makesure the file structure in your zip file looks like this:\n",
"\n",
"```\n",
"YourZIPforSingleSpeakers.zip\n",
"└───speaker\n",
" ├───xxx1-xxx1.wav\n",
" ├───...\n",
" └───Lxx-0xx8.wav\n",
"```\n",
"\n",
"```\n",
"YourZIPforMultipleSpeakers.zip\n",
"├───speaker0\n",
"│ ├───xxx1-xxx1.wav\n",
"│ ├───...\n",
"│ └───Lxx-0xx8.wav\n",
"└───speaker1\n",
" ├───xx2-0xxx2.wav\n",
" ├───...\n",
" └───xxx7-xxx007.wav\n",
"```\n",
"\n",
"**Even if there is only one speaker, a folder named `{speaker_name}` is needed.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "U05CXlAipvJR"
},
"outputs": [],
"source": [
"#@title Get raw dataset from google drive\n",
"\n",
"#@markdown # Get raw dataset from google drive\n",
"\n",
"#@markdown\n",
"\n",
"#@markdown Directory where **your zip file** located in, dont miss the slash at the end👇.\n",
"sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\" #@param {type:\"string\"}\n",
"#@markdown Filename of **your zip file**, do NOT be \"dataset.zip\"\n",
"zip_filename = \"YourZIPFilenameofRawDataset.zip\" #@param {type:\"string\"}\n",
"ZIP_PATH = sovits_data_dir + zip_filename\n",
"\n",
"!unzip -od /content/so-vits-svc/dataset_raw {ZIP_PATH}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "_ThKTzYs5CfL"
},
"outputs": [],
"source": [
"#@title Resample to 44100Hz and mono\n",
"\n",
"#@markdown # Resample to 44100Hz and mono\n",
"\n",
"#@markdown\n",
"\n",
"%cd /content/so-vits-svc\n",
"!python resample.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "svITReeL5N8K"
},
"outputs": [],
"source": [
"#@title Divide filelists and generate config.json\n",
"\n",
"#@markdown # Divide filelists and generate config.json\n",
"\n",
"#@markdown\n",
"\n",
"%cd /content/so-vits-svc\n",
"\n",
"speech_encoder = \"vec768l12\" #@param [\"vec768l12\", \"vec256l9\", \"hubertsoft\", \"whisper-ppg\"]\n",
"\n",
"from pretrain.meta import download_dict\n",
"download_dict = download_dict()\n",
"\n",
"url = download_dict[speech_encoder][\"url\"]\n",
"output = download_dict[speech_encoder][\"output\"]\n",
"\n",
"import os\n",
"if not os.path.exists(output):\n",
" !curl -L {url} -o {output}\n",
"\n",
"!python preprocess_flist_config.py --speech_encoder={speech_encoder}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "xHUXMi836DMe"
},
"outputs": [],
"source": [
"#@title Generate hubert and f0\n",
"\n",
"#@markdown # Generate hubert and f0\n",
"\n",
"#@markdown\n",
"%cd /content/so-vits-svc\n",
"\n",
"f0_predictor = \"crepe\" #@param [\"crepe\", \"pm\", \"dio\", \"harvest\"]\n",
"use_diff = True #@param {type:\"boolean\"}\n",
"\n",
"diff_param = \"\"\n",
"if use_diff:\n",
" diff_param = \"--use_diff\"\n",
"\n",
" import os\n",
" if not os.path.exists(\"./pretrain/nsf_hifigan/model\"):\n",
" !curl -L https://github.com/openvpi/vocoders/releases/download/nsf-hifigan-v1/nsf_hifigan_20221211.zip -o nsf_hifigan_20221211.zip\n",
" !unzip nsf_hifigan_20221211.zip\n",
" !rm -rf pretrain/nsf_hifigan\n",
" !mv -v nsf_hifigan pretrain\n",
"\n",
"!python preprocess_hubert_f0.py --f0_predictor={f0_predictor} {diff_param}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Wo4OTmTAUXgj"
},
"outputs": [],
"source": [
"#@title Save the preprocessed dataset to google drive\n",
"\n",
"#@markdown # Save the preprocessed dataset to google drive\n",
"\n",
"#@markdown\n",
"\n",
"#@markdown You can save the dataset and related files to your google drive for the next training\n",
"\n",
"#@markdown **Directory for saving**, dont miss the slash at the end👇.\n",
"sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\" #@param {type:\"string\"}\n",
"\n",
"#@markdown There will be a `dataset.zip` contained `dataset/` in your google drive, which is preprocessed data.\n",
"\n",
"!mkdir -p {sovits_data_dir}\n",
"!zip -r dataset.zip /content/so-vits-svc/dataset\n",
"!cp -vr dataset.zip \"{sovits_data_dir}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "P2G6v_6zblWK"
},
"outputs": [],
"source": [
"#@title Unzip preprocessed dataset from google drive directly if you have preprocessed already.\n",
"\n",
"#@markdown # Unzip preprocessed dataset from google drive directly if you have preprocessed already.\n",
"\n",
"#@markdown\n",
"\n",
"#@markdown Directory where **your preprocessed dataset** located in, dont miss the slash at the end👇.\n",
"sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\" #@param {type:\"string\"}\n",
"CONFIG = sovits_data_dir + \"configs/\"\n",
"FILELISTS = sovits_data_dir + \"filelists/\"\n",
"DATASET = sovits_data_dir + \"dataset.zip\"\n",
"\n",
"!cp -vr {CONFIG} /content/so-vits-svc/\n",
"!cp -vr {FILELISTS} /content/so-vits-svc/\n",
"!unzip {DATASET} -d /"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "ENoH-pShel7w"
},
"source": [
"# **Trainning**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-hEFFTCfZf57"
},
"outputs": [],
"source": [
"#@title Start training\n",
"\n",
"#@markdown # Start training\n",
"\n",
"#@markdown If you want to use pre-trained models, upload them to /sovits4data/logs/44k/ in your google drive manualy.\n",
"\n",
"#@markdown\n",
"\n",
"%cd /content/so-vits-svc\n",
"\n",
"#@markdown Whether to enable tensorboard\n",
"tensorboard_on = True #@param {type:\"boolean\"}\n",
"\n",
"if tensorboard_on:\n",
" %load_ext tensorboard\n",
" %tensorboard --logdir logs/44k\n",
"\n",
"config_path = \"configs/config.json\"\n",
"\n",
"from pretrain.meta import get_speech_encoder\n",
"url, output = get_speech_encoder(config_path)\n",
"\n",
"import os\n",
"if not os.path.exists(output):\n",
" !curl -L {url} -o {output}\n",
"\n",
"!python train.py -c {config_path} -m 44k"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZThaMxmIJgWy"
},
"outputs": [],
"source": [
"#@title Train cluster model (Optional)\n",
"\n",
"#@markdown # Train cluster model (Optional)\n",
"\n",
"#@markdown #### Details see [README.md#cluster-based-timbre-leakage-control](https://github.com/svc-develop-team/so-vits-svc#cluster-based-timbre-leakage-control)\n",
"\n",
"#@markdown\n",
"\n",
"%cd /content/so-vits-svc\n",
"!python cluster/train_cluster.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#@title Train diffusion model (Optional)\n",
"\n",
"#@markdown # Train diffusion model (Optional)\n",
"\n",
"#@markdown #### Details see [README.md#-about-shallow-diffusion](https://github.com/svc-develop-team/so-vits-svc#-about-shallow-diffusion)\n",
"\n",
"#@markdown\n",
"\n",
"%cd /content/so-vits-svc\n",
"\n",
"import os\n",
"if not os.path.exists(\"./pretrain/nsf_hifigan/model\"):\n",
" !curl -L https://github.com/openvpi/vocoders/releases/download/nsf-hifigan-v1/nsf_hifigan_20221211.zip -o nsf_hifigan_20221211.zip\n",
" !unzip nsf_hifigan_20221211.zip\n",
" !rm -rf pretrain/nsf_hifigan\n",
" !mv -v nsf_hifigan pretrain\n",
"\n",
"#@markdown Whether to enable tensorboard\n",
"tensorboard_on = True #@param {type:\"boolean\"}\n",
"\n",
"if tensorboard_on:\n",
" %load_ext tensorboard\n",
" %tensorboard --logdir logs/44k\n",
"\n",
"!python train_diff.py -c configs/diffusion.yaml"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# keep colab alive\n",
"Open the devtools and copy & paste to run the scrips.\n",
"\n",
"\n",
"```JavaScript\n",
"const ping = () => {\n",
" const btn = document.querySelector(\"colab-connect-button\");\n",
" if (btn) {\n",
" btn.click();\n",
" console.log(\"Clicked on connect button\");\n",
" } else {\n",
" console.log(\"connect button not found\");\n",
" }\n",
"\n",
" const nextTime = 5000 + Math.random() * 1000;\n",
"\n",
" setTimeout(ping, nextTime);\n",
"};\n",
"\n",
"ping();\n",
"```"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "oCnbX-OT897k"
},
"source": [
"# **Inference**\n",
"### Upload wav files from this notebook\n",
"### **OR**\n",
"### Upload to `sovits4data/raw/` in your google drive manualy (should be faster)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#title Download nsf_hifigan if you need it\n",
"\n",
"%cd /content/so-vits-svc\n",
"!curl -L https://github.com/openvpi/vocoders/releases/download/nsf-hifigan-v1/nsf_hifigan_20221211.zip -o /content/so-vits-svc/nsf_hifigan_20221211.zip\n",
"!unzip nsf_hifigan_20221211.zip\n",
"!rm -rf pretrain/nsf_hifigan\n",
"!mv -v nsf_hifigan pretrain\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 75
},
"executionInfo": {
"elapsed": 94633,
"status": "ok",
"timestamp": 1678591088790,
"user": {
"displayName": "謬紗特",
"userId": "09445825975794260265"
},
"user_tz": -480
},
"id": "XUsmGkgCMD_Q",
"outputId": "8bbfde13-030a-4ba0-bbdb-7eb6b89c02b4"
},
"outputs": [],
"source": [
"#@title Upload wav files, the filename should not contain any special symbols like `#` `$` `(` `)`\n",
"\n",
"#@markdown # Upload wav files, the filename should not contain any special symbols like `#` `$` `(` `)`\n",
"\n",
"#@markdown\n",
"\n",
"%cd /content/so-vits-svc\n",
"%run wav_upload.py --type audio"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "dYnKuKTIj3z1"
},
"outputs": [],
"source": [
"#@title Start inference (and download)\n",
"\n",
"#@markdown # Start inference (and download)\n",
"\n",
"#@markdown Parameters see [README.MD#Inference](https://github.com/svc-develop-team/so-vits-svc#-inference)\n",
"\n",
"#@markdown\n",
"\n",
"wav_filename = \"YourWAVFile.wav\" #@param {type:\"string\"}\n",
"model_filename = \"G_210000.pth\" #@param {type:\"string\"}\n",
"model_path = \"/content/so-vits-svc/logs/44k/\" + model_filename\n",
"speaker = \"YourSpeaker\" #@param {type:\"string\"}\n",
"trans = \"0\" #@param {type:\"string\"}\n",
"cluster_infer_ratio = \"0\" #@param {type:\"string\"}\n",
"auto_predict_f0 = False #@param {type:\"boolean\"}\n",
"apf = \"\"\n",
"if auto_predict_f0:\n",
" apf = \" -a \"\n",
"\n",
"f0_predictor = \"crepe\" #@param [\"crepe\", \"pm\", \"dio\", \"harvest\"]\n",
"\n",
"enhance = False #@param {type:\"boolean\"}\n",
"ehc = \"\"\n",
"if enhance:\n",
" ehc = \" -eh \"\n",
"#@markdown\n",
"\n",
"#@markdown Generally keep default:\n",
"config_filename = \"config.json\" #@param {type:\"string\"}\n",
"config_path = \"/content/so-vits-svc/configs/\" + config_filename\n",
"\n",
"from pretrain.meta import get_speech_encoder\n",
"url, output = get_speech_encoder(config_path)\n",
"\n",
"import os\n",
"if not os.path.exists(output):\n",
" !curl -L {url} -o {output}\n",
"\n",
"kmeans_filenname = \"kmeans_10000.pt\" #@param {type:\"string\"}\n",
"kmeans_path = \"/content/so-vits-svc/logs/44k/\" + kmeans_filenname\n",
"slice_db = \"-40\" #@param {type:\"string\"}\n",
"wav_format = \"flac\" #@param {type:\"string\"}\n",
"\n",
"key = \"auto\" if auto_predict_f0 else f\"{trans}key\"\n",
"cluster_name = \"\" if cluster_infer_ratio == \"0\" else f\"_{cluster_infer_ratio}\"\n",
"isdiffusion = \"sovits\"\n",
"wav_output = f\"/content/so-vits-svc/results/{wav_filename}_{key}_{speaker}{cluster_name}_{isdiffusion}.{wav_format}\"\n",
"\n",
"%cd /content/so-vits-svc\n",
"!python inference_main.py -n {wav_filename} -m {model_path} -s {speaker} -t {trans} -cr {cluster_infer_ratio} -c {config_path} -cm {kmeans_path} -sd {slice_db} -wf {wav_format} {apf} --f0_predictor={f0_predictor} {ehc}\n",
"\n",
"#@markdown\n",
"\n",
"#@markdown If you dont want to download from here, uncheck this.\n",
"download_after_inference = True #@param {type:\"boolean\"}\n",
"\n",
"if download_after_inference:\n",
" from google.colab import files\n",
" files.download(wav_output)"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"provenance": [
{
"file_id": "19fxpo-ZoL_ShEUeZIZi6Di-YioWrEyhR",
"timestamp": 1678516497580
},
{
"file_id": "1rCUOOVG7-XQlVZuWRAj5IpGrMM8t07pE",
"timestamp": 1673086970071
},
{
"file_id": "1Ul5SmzWiSHBj0MaKA0B682C-RZKOycwF",
"timestamp": 1670483515921
}
]
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 0
}