0
2

Stable Diffusionで画像を生成する

Last updated at Posted at 2023-03-13

Stable Diffusionで画像を生成、その生成された画像を利用してSUZURIでTシャツを作成してみた

内容

1:Hugging Faceのトークンを発行
2:googleColaboratoryでStable Diffusionの設定
3:Stable Diffusionで画像を生成
4:生成された画像を使ってTシャツを作成する方法

1:Hugging Faceのトークンを発行

Hugging Faceのアカウントを作成してトークンの発行を行う。こちら記事を参考に登録
アクセストークンの発行が出来たら保存しておく

2:GoogleColaboratoryでStable Diffusionの設定をする

・GoogleColaboratoryでノートブックを新規作成したら、編集から「ノートブックの設定」を開く
スクリーンショット 2023-02-12 16.22.53.png
・ハードウェアアクセラレータの欄で「GPU」を選択して「保存」で設定完了
スクリーンショット 2023-02-12 16.24.35.png

3:Stable Diffusionで画像を生成

以下のコードで画像を作成する

pip install diffusers==0.8.0 transformers scipy ftfy
# diffusers==0.2.4だとエラー出たのでdiffusers==0.8.0に修正しエラー解消
YOUR_TOKEN="HuggingFaceで発行したトークン入力"

# パイプラインの準備
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
# パイプラインの第1引数に指定しているのがStable Diffusionモデル本体。第2の引数にはその上で設定しているトークンを指定する
pipe.to("cuda")
# パイプラインをGPUに移す

# 画像作成の実行
prompt = "Cat in the style of Gogh"
# promptに作成したい画像のキーワードを入れる
image = pipe(prompt)["images"][0]
image.save(f"Cat_gogh.png")

これで画像生成が完了。気に入った画像が生成できたら画像を保存する
いろいろ画像つくってみた

ゴッホ風の猫
スクリーンショット 2023-02-12 17.48.57.png
ゴッホ風の猫
スクリーンショット 2023-02-26 15.57.10.png

カットバック決めてる猫
AI_cat_cyber3_カットバック.png

コロー風の猫
コロー.png

ゴッホ風の猫
cat_Gogh2.png

0
2
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
0
2