14
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

M1 MacでCoreMLを使ってStable Diffusionを動かす

Last updated at Posted at 2023-03-01

はじめに

AppleがGitHubで公開している「Core ML Stable Diffusion」を使ってみたという記事です。M1 MacでもPyTorchのバグを引かずに画像生成ができるおすすめの方法です。

記事の流れ

  1. 環境構築
  2. 任意のモデルをCoreMLモデルへ変換
  3. 画像生成(t2i)

環境構築

以下が使用可能な環境想定して進みます。

  • 最新バージョンのXcode
  • pyenv
  • git

git clone
リポジトリをクローンします。

git clone https://github.com/apple/ml-stable-diffusion.git
cd ml-stable-diffusion

インストール
pyenvで環境を作ってからインストールします。python3.8.16を使用してますが3.8系統ならなんでも良いです。

pyenv install 3.8.16
pyenv local 3.8.16
python3 --version #一応確認します。
python3 -m venv coreml_env
source ./coreml_env/bin/activate
python3 -m pip install -U pip
python3 -m pip install -e .

huggin faceの設定など
以下を実行するとhugginfaceのアクセストークンを要求されるので入力してログインします。

huggingface-cli login

CoreMLモデルへの変換

基本的には以下の通りです。自分の使いたいモデルに合わせて変えてください。

python -m python_coreml_stable_diffusion.torch2coreml --convert-unet --convert-text-encoder --convert-vae-decoder --convert-safety-checker --model-version <HugginfaceModelCard> -o <output-mlpackages-directory>

例(Anything-V4)

mkdir -p ./model/anything-v4
python -m python_coreml_stable_diffusion.torch2coreml --convert-unet --convert-text-encoder --convert-vae-decoder --convert-safety-checker --model-version andite/anything-v4.0 -o ./model/anything-v4

画像生成(t2i)

基本的には以下の通りです。モデル変換の際に使用したものに合わせて変えてください。

python -m python_coreml_stable_diffusion.pipeline --prompt "プロンプト" -i <モデル変換の際に指定したディレクトリ> --model-version <モデル変化の際に指定したモデル> -o <画像の出力先> --compute-unit ALL --seed <シード値>

例(Anything-V4)。*先ほどのモデル変換の例の続き

mkdir ./image
python -m python_coreml_stable_diffusion.pipeline --prompt "masterpiece, best quality, 1girl, white hair, medium hair, cat ears, closed eyes, looking at viewer, :3, cute, scarf, jacket, outdoors, streets" -i ./model/anything-v4 --model-version andite/anything-v4.0  -o ./image --compute-unit ALL --seed 93

生成されたもの
randomSeed_93_computeUnit_ALL_modelVersion_andite_anything-v4.0.png

14
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?