CPUでstable-audio-open-smallで音声作成

この記事は約7分で読めます。
スポンサーリンク

やりたいこと

下の記事ではStable Audio Open 1.0で音声作成を試しましたが、かなり動作がギリギリでした。

新たに軽量になったstable-audio-open-smallがリリースされたので試してみます。

いろいろ試したのですが、stable-audio-open-smallはDirectMLで動作させることができませんでした。(stable-audio-toolsで使用しているPytorchとtorch-directmlで使用しているPytorchのバージョンがコンフリクトするため)

ライセンスに関して

モデルのライセンスは以下のリンクを参照してください。

非商用であれば、無料です。

Professional Membership Agreement — Stability AI
スポンサーリンク

環境構築

作業用のフォルダを作成します。

venv環境への移動(任意)

必要であればコマンドプロンプトで以下のコマンドを実行して、Venvの環境を作成してアクティブにします。

今回はライブラリの修正を行う可能性があるので、venv環境を推奨します。

python -mvenv venv
venv\scripts\activate.bat

ライブラリのインストール

以下のコマンドを実行して必要なライブラリのインストールを行います。

pip install stable-audio-tools

エラー: AttributeError: module ‘pkgutil’ has no attribute ‘ImpImporter’. Did you mean: ‘zipimporter’?

環境により以下のエラーが出力されました。使用するPyhtonのバージョンを変更することで回避できました。

問題の起きたバージョン:3.10.6

問題の起きなかったバージョン:3.12.8

      AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

スクリプトの作成

以下の内容をrun.pyのファイル名で保存します。

CUDAが実行できる環境で用いるスクリプトと同じです。(自動で切り替わります。)

import torch
import torchaudio
from einops import rearrange
from stable_audio_tools import get_pretrained_model
from stable_audio_tools.inference.generation import generate_diffusion_cond

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

# Download model
model, model_config = get_pretrained_model("stabilityai/stable-audio-open-small")
sample_rate = model_config["sample_rate"]
sample_size = model_config["sample_size"]

model = model.to(device)

# Set up text and timing conditioning
conditioning = [{
    "prompt": "128 BPM tech house drum loop",
    "seconds_total": 11
}]

# Generate stereo audio
output = generate_diffusion_cond(
    model,
    steps=8,
    conditioning=conditioning,
    sample_size=sample_size,
    sampler_type="pingpong",
    device=device
)

# Rearrange audio batch to a single sequence
output = rearrange(output, "b d n -> d (b n)")

# Peak normalize, clip, convert to int16, and save to file
output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
torchaudio.save("output.wav", output, sample_rate)
スポンサーリンク

実行

以下のコマンドを実行してスクリプトを実行します。

実行したフォルダにoutput.wavというファイルが作成されます。

python run.py

実行時間

下のキャプチャのように1itが1秒ほどで終わるため、8itでも10秒ほどでも終わります。

しかし、上の進捗が100%になった後にかなり時間がかかります。(具体的に図ったわけではないのですが3時間では終わりませんでしたが一晩待ったら終わってました。)

ちなみに

以下の処理が遅いです。

sampled = model.pretransform.decode(sampled)

モデルのダウンロードに失敗する場合

下の記事を参考にしてください。

エラー(ValueError: high is out of bounds for int32)対応

以下のエラーが起きて実行に失敗することがあります。(環境依存?)

  File "F:\projects\python\StableAL\venv\lib\site-packages\stable_audio_tools\inference\generation.py", line 138, in generate_diffusion_cond
    seed = seed if seed != -1 else np.random.randint(0, 2**32 - 1)
  File "mtrand.pyx", line 746, in numpy.random.mtrand.RandomState.randint
  File "_bounded_integers.pyx", line 1336, in numpy.random._bounded_integers._rand_int32
ValueError: high is out of bounds for int32

回避するにはvenv\Lib\site-packages\stable_audio_tools\inference¥generation.pyの138行目を以下のように修正します。(stable_audio_toolsのバージョンにより異なる可能性があります。)

修正前:

    seed = seed if seed != -1 else np.random.randint(0, 2**32 - 1, dtype=np.uint32)

修正後:

    seed = seed if seed != -1 else np.random.randint(0, 2**31 - 1, dtype=np.uint32)
スポンサーリンク

結果

stable-audio-open-smallとCPUで音声を作成できましたが、実際に使用するには時間はかなりかかるので使用方法と相談になりそうです。

スポンサーリンク

参考にさせていただいたサイト

stabilityai/stable-audio-open-small · Hugging Face
We’re on a journey to advance and democratize artificial intelligence through open source and open science.

コメント

タイトルとURLをコピーしました