coqui-ai / TTS

🐸💬 - a deep learning toolkit for Text-to-Speech, battle-tested in research and production
http://coqui.ai
Mozilla Public License 2.0
35.13k stars 4.28k forks source link

[Bug] Doesn't respect the cuda flag #2947

Closed maxime-fleury closed 1 year ago

maxime-fleury commented 1 year ago

Describe the bug

Here is the bug:

PS E:\AI> tts --model_name "tts_models/multilingual/multi-dataset/xtts_v1" --text "Ceci est un teste de voix." --language_idx "fr"  --use_cuda False
 > tts_models/multilingual/multi-dataset/xtts_v1 is already downloaded.
 > Using model: xtts
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Scripts\tts.exe\__main__.py", line 7, in <module>
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\bin\synthesize.py", line 401, in main
    synthesizer = Synthesizer(
                  ^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\synthesizer.py", line 109, in __init__
    self._load_tts_from_dir(model_dir, use_cuda)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\synthesizer.py", line 164, in _load_tts_from_dir
    self.tts_model.load_checkpoint(config, checkpoint_dir=model_dir, eval=True)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\tts\models\xtts.py", line 645, in load_checkpoint
    self.load_state_dict(load_fsspec(model_path)["model"], strict=strict)
                         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\io.py", line 86, in load_fsspec
    return torch.load(f, map_location=map_location, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 809, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1172, in _load
    result = unpickler.load()
             ^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1142, in persistent_load
    typed_storage = load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1116, in load_tensor
    wrap_storage=restore_location(storage, location),
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 217, in default_restore_location
    result = fn(storage, location)
             ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 182, in _cuda_deserialize
    device = validate_cuda_device(location)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 166, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
PS E:\AI>

Also doesn't work with a file:

from TTS.api import TTS
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1", gpu=False)

tts.tts_to_file(text="Ceci est un test de voix en utilisant python.",
                file_path="E:\AI\output.wav",
                language="fr")
 > tts_models/multilingual/multi-dataset/xtts_v1 is already downloaded.
 > Using model: xtts
Traceback (most recent call last):
  File "e:\AI\test.py", line 2, in <module>
    tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1", gpu=False)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\api.py", line 81, in __init__
    self.load_tts_model_by_name(model_name, gpu)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\api.py", line 185, in load_tts_model_by_name
    self.synthesizer = Synthesizer(
                       ^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\synthesizer.py", line 109, in __init__
    self._load_tts_from_dir(model_dir, use_cuda)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\synthesizer.py", line 164, in _load_tts_from_dir
    self.tts_model.load_checkpoint(config, checkpoint_dir=model_dir, eval=True)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\tts\models\xtts.py", line 645, in load_checkpoint
    self.load_state_dict(load_fsspec(model_path)["model"], strict=strict)
                         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\io.py", line 86, in load_fsspec
    return torch.load(f, map_location=map_location, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 809, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1172, in _load
    result = unpickler.load()
             ^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1142, in persistent_load
    typed_storage = load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1116, in load_tensor
    wrap_storage=restore_location(storage, location),
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 217, in default_restore_location
    result = fn(storage, location)
             ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 182, in _cuda_deserialize
    device = validate_cuda_device(location)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 166, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
PS C:\Users\Max> ```

### To Reproduce

tts --model_name "tts_models/multilingual/multi-dataset/xtts_v1" --text "Ceci est un teste de voix." --language_idx "fr"  --use_cuda False

### Expected behavior

Generate audio without gpu

### Logs

```shell
PS E:\AI> tts --model_name "tts_models/multilingual/multi-dataset/xtts_v1" --text "Ceci est un teste de voix." --language_idx "fr"  --use_cuda False
 > tts_models/multilingual/multi-dataset/xtts_v1 is already downloaded.
 > Using model: xtts
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Scripts\tts.exe\__main__.py", line 7, in <module>
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\bin\synthesize.py", line 401, in main
    synthesizer = Synthesizer(
                  ^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\synthesizer.py", line 109, in __init__
    self._load_tts_from_dir(model_dir, use_cuda)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\synthesizer.py", line 164, in _load_tts_from_dir
    self.tts_model.load_checkpoint(config, checkpoint_dir=model_dir, eval=True)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\tts\models\xtts.py", line 645, in load_checkpoint
    self.load_state_dict(load_fsspec(model_path)["model"], strict=strict)
                         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\TTS\utils\io.py", line 86, in load_fsspec
    return torch.load(f, map_location=map_location, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 809, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1172, in _load
    result = unpickler.load()
             ^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1142, in persistent_load
    typed_storage = load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 1116, in load_tensor
    wrap_storage=restore_location(storage, location),
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 217, in default_restore_location
    result = fn(storage, location)
             ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 182, in _cuda_deserialize
    device = validate_cuda_device(location)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Max\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch-2.0.1-py3.11-win-amd64.egg\torch\serialization.py", line 166, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
PS E:\AI>

Environment

- Coqui TTS is LTS version
- OS windows
- GPU (amd 6700xt, meaning no GPU)
- Ryzen 7 7800x
- pip is installed

Additional context

No response

ibrahimrefai2010 commented 1 year ago

at "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\TTS\utils\io.py" at line 83 and line 86 change torch.load(f, map_location=map_location, kwargs) to torch.load(f, map_location=torch.device("cpu"), kwargs)

alessandroperilli commented 1 year ago

The problem appears even if you use the new --device mps option described in #2875, per #2855.

loupzeur commented 1 year ago

to fix it, just modify this line : https://github.com/coqui-ai/TTS/blob/f829bf50f8cd261a2ef23356cb83f0ac6c3ff6a8/TTS/tts/models/xtts.py#L645

add the device to the map_location from the class :

self.load_state_dict(load_fsspec(model_path, ** map_location=self.device ** )["model"], strict=strict)

loupzeur commented 1 year ago

i'm not sure if the PR was necessary, ignore it if it doesn't fit the requirements, I didn't really paid attention to the code guidelines ...

Olivier-true commented 1 year ago

[EN] I had an equivalent problem a few hours ago, tested on hugging face with the basic cpu (without gpu). The problem seems to come from the default model used: tts_models/multilingual/multi-dataset/xtts_v1. If you want to use a multi-language model, take the second one on the list: tts_models/multilingual/multi-dataset/your_tts. What's more, you'll probably come across another key_error error: for a French text, this template takes fr-fr as the language argument, not fr. Personally, I find the rendering of model 2 less realistic. But perhaps using a French tts_models/fr/mai/tacotron2-DDC or the one that follows on the list, you'll get a better result, I haven't tested it. If you're looking for cleaner rendering, try RVC (RVC-Project/Retrieval-based-Voice-Conversion-WebUI). You can train your voice model with your own samples, coupled with TTS, you can get better results, I'll let you find out, there are tutorials on it. Good evening from France.

[FR] J'ai eu un problème équivalent il y a quelque heure, tester sur hugging face avec le cpu de base (sans gpu). Le poblème semble venir du modèle utilisé par défaut : tts_models/multilingual/multi-dataset/xtts_v1. Si tu veux utiliser un modèle multi-langue prend le deuxième de la liste, soit : tts_models/multilingual/multi-dataset/your_tts. De plus, tu rencontreras probablement une autre erreur key_error, pour un texte français, ce modèle prend fr-fr comme argument de langue et non fr. Personnellement, je trouve le rendu du modèle 2 moins réaliste. Mais peut-être qu'en utilisant un français tts_models/fr/mai/tacotron2-DDC ou celui qui suit sur la liste, tu obtiendras un meilleur résultat, je ne l'ais pas tester. Si tu cherche un rendu plus propre, essaye de voir du coté de RVC (RVC-Project/Retrieval-based-Voice-Conversion-WebUI). Tu peux entrainer ton modèle de voix avec tes propres échantillions, couplé à TTS, tu peux obtenir des meilleurs résultats, je te laisses te renseigner, des tutos existes dessus. Bonsoir de Paris.

grabani commented 1 year ago

at "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\TTS\utils\io.py" at line 83 and line 86 change torch.load(f, map_location=map_location, kwargs) to torch.load(f, map_location=torch.device("cpu"), kwargs)

Didn't work. Retuned the error:

File "C:\Users\Home\Environments\project\lib\site-packages\TTS\tts\models\xtts.py", line 645, in load_checkpoint
    self.load_state_dict(load_fsspec(model_path)["model"], strict=strict)
TypeError: 'NoneType' object is not subscriptable
grabani commented 1 year ago

i'm not sure if the PR was necessary, ignore it if it doesn't fit the requirements, I didn't really paid attention to the code guidelines ...

Didn't work. Retuned error: File "C:\Users\xxx\Environments\project\lib\site-packages\TTS\tts\models\xtts.py", line 646 self.load_state_dict(load_fsspec(model_path, ** map_location=self.device ** )["model"], strict=strict) ^ SyntaxError: invalid syntax

grabani commented 1 year ago

``When executing the following from the command line:

tts --model_name tts_models/multilingual/multi-dataset/xtts_v1 --text "This is a test" --speaker_wav C:\Users\xxx\OneDrive\Projects\scripts\coqui_TTS\data\source.wav --language_idx en --use_cuda false

I get the same error as others; namely:

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

I then created the following script:

import torch
from TTS.tts.configs.xtts_config import XttsConfig
from TTS.tts.models.xtts import Xtts

config = XttsConfig()
config.load_json(r"C:\Users\xxx\AppData\Local\tts\tts_models--multilingual--multi-dataset--xtts_v1\config.json")
model = Xtts.init_from_config(config)

# Load the model checkpoint using torch.load
checkpoint_path = r"C:\Users\xxx\AppData\Local\tts\tts_models--multilingual--multi-dataset--xtts_v1"
if torch.cuda.is_available():
    checkpoint = torch.load(checkpoint_path)
    model.load_state_dict(checkpoint['model'])
    model.cuda()
else:
    checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
    model.load_state_dict(checkpoint['model'])

model.eval()

outputs = model.synthesize(
    "It took me quite a long time to develop a voice and now that I have it I am not going to be silent.",
    config,
    speaker_wav=r"C:\Users\xxx\OneDrive\Projects\Projects\QuranBeheld\scripts\coqui_TTS\data\sh_nuh_ayah_kursi_audacity.wav",
    gpt_cond_len=3,
    language="en",
)

But I get the strange error:

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\xxx\\AppData\\Local\\tts\\tts_models--multilingual--multi-dataset--xtts_v1'

The certainly doesn't seem to be permission issue. So can't explain the above. Any thought?

JulienRioux commented 1 year ago

I'm encountering the same issue when using the example provided in the Readme.md (Running a multi-speaker and multi-lingual model). I've tried a few ways to fix it but none of which I've tried works thus far.

import torch
from TTS.api import TTS

# Get device
device = "cuda" if torch.cuda.is_available() else "cpu"

# List available 🐸TTS models and choose the first one
model_name = TTS().list_models()[0]
# Init TTS
tts = TTS(model_name).to(device)

# Run TTS
# ❗ Since this model is multi-speaker and multi-lingual, we must set the target speaker and the language
# Text to speech with a numpy output
wav = tts.tts("This is a test! This is also a test!!", speaker=tts.speakers[0], language=tts.languages[0])
# Text to speech to a file
tts.tts_to_file(text="Hello world!", speaker=tts.speakers[0], language=tts.languages[0], file_path="output.wav")
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
GJakobi commented 1 year ago

@JulienRioux same here, did you find any fix?

loupzeur commented 1 year ago

the fix (#2951 ) is on the dev environment, we'll have to wait the next version release for a publicly available fix. if you want to use the dev version, you can use : pip install git+https://github.com/coqui-ai/TTS.git@dev

grabani commented 1 year ago

the fix (#2951 ) is on the dev environment, we'll have to wait the next version release for a publicly available fix. if you want to use the dev version, you can use : pip install git+https://github.com/coqui-ai/TTS.git@dev

@loupzeur - That did not work for me :(

I am running on windows 11. i5 12th Gen. Python 3.10.11.

I upgraded to TTS Dev version:

My coqui TTS installed packages are:

Running the following command:

tts --model_name tts_models/multilingual/multi-dataset/xtts_v1 --text "This is a test" --speaker_wav C:\Users\xxx\OneDrive\Projects\scripts\coqui_TTS\data\source.wav --language_idx en --use_cuda false

I get the same error:

File "C:\Users\Home\Environments\projectenv\lib\site-packages\torch\serialization.py", line 166, in validate_cuda_device raise RuntimeError('Attempting to deserialize object on a CUDA ' RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

grabani commented 1 year ago

Looking at the resolution of #2951 I see that line 645 of the file xtts.py has been modified:

image

Although I used the command:

I thought that the above pip install command would upgrade my installation of TTS, it seems that it didn't. I manually modified the xtts.py as follows:

image

However, when running the following from the windows command line:

tts --model_name tts_models/multilingual/multi-dataset/xtts_v1 --text "This is a test" --speaker_wav C:\Users\xxx\OneDrive\Projects\sctts --model_name tts_models/multilingual/multi-dataset/xtts_v1 --text "This is a test" --speaker_wav C:\Users\xxx\OneDrive\Projects\scripts\coqui_TTS\data\source.wav --language_idx en --use_cuda false

I now get the error:

File "C:\Users\Home\Environments\quranbeheld3.10.11\lib\site-packages\torch\cuda\__init__.py", line 239, in _lazy_init raise AssertionError("Torch not compiled with CUDA enabled") AssertionError: Torch not compiled with CUDA enabled

PS - I asked ChatGPT:

image

I then asked:

image

But checking my pip list I have the required torch packages:

image

@loupzeur - thoughts and help please :)

loupzeur commented 1 year ago

if you do it by code, does it work ? my fix only fixed the library, if the executable doesn't transform the argument 'gpu=true' to 'device=cpu' it won't work I guess

grabani commented 1 year ago

Using the following code (where I included my own CPU related workaround(:

import torch
from TTS.tts.configs.xtts_config import XttsConfig
from TTS.tts.models.xtts import Xtts

config = XttsConfig()
config.load_json(r"C:\Users\xxx\AppData\Local\tts\tts_models--multilingual--multi-dataset--xtts_v1\config.json")
model = Xtts.init_from_config(config)

checkpoint_path = r"C:\Users\xxx\AppData\Local\tts\tts_models--multilingual--multi-dataset--xtts_v1"
if torch.cuda.is_available():
    checkpoint = torch.load(checkpoint_path)
    model.load_state_dict(checkpoint['model'])
    model.cuda()
else:
    checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
    model.load_state_dict(checkpoint['model'])

model.eval()

outputs = model.synthesize(
    "It took me quite a long time to develop a voice and now that I have it I am not going to be silent.",
    config,
    speaker_wav=r"C:\Users\xxx\OneDrive\Projects\Projects\scripts\coqui_TTS\data\source.wav",
    gpt_cond_len=3,
    language="en",
)

However, I get the error: PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Home\\AppData\\Local\\tts\\tts_models--multilingual--multi-dataset--xtts_v1'

I have tried everything to resolve this error; including:

1) Ran the python script from the command line as admin - same error. 2) Moved the \tts_models--multilingual--multi-dataset--xtts_v1 directory and all files to my Home directory. 3) Manually downloaded the xtts files from: https://coqui.gateway.scarf.sh/hf-coqui/XTTS-v1 4) Checked the permissions of the files and directory:

Successfully processed 4 files; Failed processing 0 files

loupzeur commented 1 year ago

And with this simpler code, does it work ? this will take care automatically of the model, and configuration.

from TTS.api import TTS
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1").to('cpu')
tts.tts_to_file(text='my text to speech test',file_path="output.wav",speaker_wav="source.wav",language='en')
grabani commented 1 year ago

I get the error:

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

grabani commented 1 year ago

With the help of chatGPT, I tried the code:

import torch
from TTS.api import TTS

# Load the TTS model with map_location to ensure it's on the CPU
model_path = r"C:\Users\xxx\AppData\Local\tts\tts_models--multilingual--multi-dataset--xtts_v1"
device = torch.device('cpu')
tts = torch.load(model_path, map_location=device)
tts.eval()  # Set the model to evaluation mode

# Generate speech to a file
tts.tts_to_file(
    text='my text to speech test',
    file_path="output.wav",
    speaker_wav="source.wav",
    language='en'
)

I now get the standard error:

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\xxx\\AppData\\Local\\tts\\tts_models--multilingual--multi-dataset--xtts_v1'

loupzeur commented 1 year ago

are you sure that you have the correct library with the actual fix ? can you do it with a venv or conda environment ? to be sure that you don't have any other version that may take over the dev one.

I don't use windows, so I can't help with the permission problem, can you try to copy the folder's content to the same place your code is ?

grabani commented 1 year ago

Hey @loupzeur

I tried running your code in my virtualenv:

from TTS.api import TTS
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1").to('cpu')
tts.tts_to_file(text='my text to speech test',file_path="output.wav",speaker_wav="./source.wav",language='en')

It gave me the error:

File "C:\Users\xxx\Environments\xxx\lib\site-packages\torchaudio\backend\soundfile_backend.py", line 233, in load waveform = torch.from_numpy(waveform) RuntimeError: Numpy is not available

As I am running Python==3.10.11 in the virtualenv the requirements.txt file states I should use numpy==1.22.0. However, I upgraded to numpy==1.24.3. This SOLVED the problem!!!

PS - I am still getting permission denied if I try to access the xtts model locally on my PC :☹️That makes no sense whatsoever! I have copied the xtts model directory to the same directory as my script, but I still get the error.☹️

What does you line of code from TTS.api import TTS do? Does it not use my local xtts model files?

Thanks for all your help. It is much appreciated. Issue boards can be the grave-yard for many people's issue. You are proof that all heros don't wear capes 🦸‍♂️

loupzeur commented 1 year ago

Hello, @grabani, no problem, thanks. Its my first contribution here, and I was pretty sure my fix works so I really wanted to understand the problem.

Concerning the permission problem, did you open your folder that causes permissions' problems with the windows navigator ? I'm pretty sure you never allowed your user to get access to it, hence the permissions problem. (you can also check if the permissions' parameters with the navigator gives read access to your user, may be the linux terminal's user from windows doesn't have your user's permission).

Lenos500 commented 1 year ago

Hello, @grabani, no problem, thanks. Its my first contribution here, and I was pretty sure my fix works so I really wanted to understand the problem.

Concerning the permission problem, did you open your folder that causes permissions' problems with the windows navigator ? I'm pretty sure you never allowed your user to get access to it, hence the permissions problem. (you can also check if the permissions' parameters with the navigator gives read access to your user, may be the linux terminal's user from windows doesn't have your user's permission).

i tried your script and it still shows the CUDA error , i am running this in kali linux with python 3.10.3 and TTS 17.04 which is the latest version

Lenos500 commented 1 year ago

at "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\TTS\utils\io.py" at line 83 and line 86 change torch.load(f, map_location=map_location, kwargs) to torch.load(f, map_location=torch.device("cpu"), kwargs)

Didn't work. Retuned the error:

File "C:\Users\Home\Environments\project\lib\site-packages\TTS\tts\models\xtts.py", line 645, in load_checkpoint
    self.load_state_dict(load_fsspec(model_path)["model"], strict=strict)
TypeError: 'NoneType' object is not subscriptable

Did you find any fix?

Lenos500 commented 1 year ago

I'm encountering the same issue when using the example provided in the Readme.md (Running a multi-speaker and multi-lingual model). I've tried a few ways to fix it but none of which I've tried works thus far.

import torch
from TTS.api import TTS

# Get device
device = "cuda" if torch.cuda.is_available() else "cpu"

# List available 🐸TTS models and choose the first one
model_name = TTS().list_models()[0]
# Init TTS
tts = TTS(model_name).to(device)

# Run TTS
# ❗ Since this model is multi-speaker and multi-lingual, we must set the target speaker and the language
# Text to speech with a numpy output
wav = tts.tts("This is a test! This is also a test!!", speaker=tts.speakers[0], language=tts.languages[0])
# Text to speech to a file
tts.tts_to_file(text="Hello world!", speaker=tts.speakers[0], language=tts.languages[0], file_path="output.wav")
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

Did you find any fix?

erogol commented 1 year ago

Duplicate #2980

usernamedd commented 3 months ago

if you do it by code, does it work ? my fix only fixed the library, if the executable doesn't transform the argument 'gpu=true' to 'device=cpu' it won't work I guess

it works for me , ths