【Stable Diffusion】Hentai Diffusion 22の利用方法

【Stable Diffusion】Hentai Diffusion 22の利用方法 機械学習

「Hentai Diffusion 22は以前のバージョンとどう違うの?」
「Hentai DiffusionではどうやってVAEを用いるの?」

このような場合には、この記事の内容が参考になります。
この記事では、Hentai Diffusion 22について解説しています。

本記事の内容

  • Hentai Diffusion 22とは?
  • Hentai Diffusion 22の利用方法
  • Hentai Diffusion 22の動作検証

それでは、上記に沿って解説していきます。

Hentai Diffusion 22とは?

Hentai Diffusion 22が、2023年1月31日に公開されました。
2023年1月21日には、Hentai Diffusion 20が公開されています。

その10日後に、バージョンアップが実施されていることになります。
そして、なぜか21は飛ばされています。

バージョンアップの特徴は以下になるようです。

  • プロンプトの認識精度UP
  • VAEの提供
  • 時間帯(日没、日の出、朝、夕暮れなど)への応答性UP

最も大きなモノは、プロンプトの認識精度UPになります。
この改善により、むやみに「()」を使って強調する必要がなくなったということです。

例えば、次のようにドレスの色を指定する場合に「()」の利用が不要になっています。

以前のバージョンであれば、「(((blue dress)))」「(((red dress)))」「(((yellow dress)))」とする必要があったということです。
要するに、プロンプトの小さな変化でも対応するようになったと言えます。

VAEに関しては、逆に今までなかったのですね。
VAEを利用する方法については、後述しています。

時間帯の応答性UPについては、上の画像でも確認できますね。
より反応するようになったということでしょう。

以上、Hentai Diffusion 22について説明しました。
次は、Hentai Diffusion 22の利用方法を説明します。

Hentai Diffusion 22の利用方法

モデルのダウンロードは、公式ページから可能です。
https://www.cognitionai.org/hdhowtogetstarted

「Download」ボタンをクリックすると、ファイルのダウンロードが始まります。

上記ファイルは、Hugging Faceのページでも公開されています。
https://huggingface.co/Deltaadams/HD-22/tree/main

「HD-22-Compressed.zip」をダウンロードしたら、解凍します。

利用方法については、それぞれの環境で異なります。

  • Stable Diffusion web UI(AUTOMATIC1111版)
  • Diffusers

それぞれの場合について、以下で説明します。

Stable Diffusion web UI(AUTOMATIC1111版)

OS環境を汚さないインストール方法を次の記事で説明しています。
OS依存のPythonを利用しない方法のため、安心してインストールできます。

この方法なら、アップデートでぶっ壊れようが問題ありません。
再度、新たにインストールすれば済みます。

Hentai Diffusion 22の利用方法は、指定のディレクトリにファイルを設置するだけです。
具体的には、models/Stable-diffusion以下に二つのファイルを保存します。

YAMLファイルは不要です。
あとは、web UIを起動すれば選択できるようになっています。

Diffusers

Diffusersの場合は、少し工夫が必要となります。
まずは、Stable Diffusionを動かせることが前提となります。

上記記事に従えば、自ずとDiffusersをインストールしています。

ここからは、工夫が必要となります。
ダウンロードしたファイルをDiffusersから読み込むためには、以下の作業が必要となります。

  • モデルの変換
  • VAEの変換

モデルの変換

モデルの変換方法は、次の記事で説明しています。

今回であれば、次のようなコマンドを用いました。
パスなどは、各自の環境に合わせてください。

python convert_diffusers20_original_sd.py ..\..\ckpt\HD-22.ckpt ..\..\model\HD-22   --v1 --reference_model runwayml/stable-diffusion-v1-5

上記コマンドを実行すると、次のような形に変換できます。

VAEの変換

ptファイルのままだと、DiffusersではVAEとして利用できません。
そのため、DiffusersでVAEとして利用できるようにptファイルを変換します。

その方法は、次の記事で解説しています。

上記記事を参考にして、次のコマンドで変換しています。
パスについては、各自の環境に合わせてください。

python convert_pt_to_bin.py  --checkpoint_path ..\..\ckpt\HD-22.vae.pt --dump_path ..\..\model\HD-22_vae

上記コマンドを実行すると、以下ように変換できます。

ここまで準備できたら、あとは次のコードを実行するだけです。

from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
import torch
from diffusers.models import AutoencoderKL

MODEL_ID = "./model/HD-22"
DEVICE = "cuda"
SEED = 2109289960

vae = AutoencoderKL.from_pretrained("./model/HD-22_vae")
pipe = StableDiffusionPipeline.from_pretrained(MODEL_ID, custom_pipeline="lpw_stable_diffusion", vae=vae)
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to(DEVICE)

prompt = "best quality, {{{nsfw}}}, {{{masterpiece}}}, 1girl, (tohsaka_rin:1.000), (thighhighs:1.000), (green_eyes:0.949), short hair, wide hips, yellow dress, portrait, (cropped torso), full head, straight on, sunset, vibrant"
negative_prompt = "(((deformed))), blurry, bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), fused fingers, messy drawing, broken legs censor, censored, censor_bar, multiple breasts, (mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), black-white, bad anatomy, liquid body, liquidtongue, disfigured, malformed, mutated, anatomical nonsense, text font ui, error, malformed hands, long neck, blurred, lowers, low res, bad anatomy, bad proportions, bad shadow, uncoordinated body, unnatural body, fused breasts, bad breasts, huge breasts, poorly drawn breasts, extra breasts, liquid breasts, heavy breasts, missingbreasts, huge haunch, huge thighs, huge calf, bad hands, fused hand, missing hand, disappearing arms, disappearing thigh, disappearing calf, disappearing legs, fusedears, bad ears, poorly drawn ears, extra ears, liquid ears, heavy ears, missing ears, fused animal ears, bad animal ears, poorly drawn animal ears, extra animal ears, liquidanimal ears, heavy animal ears, missing animal ears, text, ui, error, missing fingers, missing limb, fused fingers, one hand with more than 5 fingers, one hand with less than5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, colorful tongue, blacktongue, cropped, watermark, username, blurry, JPEG artifacts, signature, 3D, 3D game, 3D game scene, 3D character, malformed feet, extra feet, bad feet, poorly drawnfeet, fused feet, missing feet, extra shoes, bad shoes, fused shoes, more than two shoes, poorly drawn shoes, bad gloves, poorly drawn gloves, fused gloves, bad cum, poorly drawn cum, fused cum, bad hairs, poorly drawn hairs, fused hairs, big muscles, ugly, bad face, fused face, poorly drawn face, cloned face, big face, long face, badeyes, fused eyes poorly drawn eyes, extra eyes, malformed limbs, more than 2 nipples, missing nipples, different nipples, fused nipples, bad nipples, poorly drawnnipples, black nipples, colorful nipples, gross proportions. short arm, (((missing arms))), missing thighs, missing calf, missing legs, mutation, duplicate, morbid, mutilated, poorly drawn hands, more than 1 left hand, more than 1 right hand, deformed, (blurry), disfigured, missing legs, extra arms, extra thighs, more than 2 thighs, extra calf,fused calf, extra legs, bad knee, extra knee, more than 2 legs, bad tails, bad mouth, fused mouth, poorly drawn mouth, bad tongue, tongue within mouth, too longtongue, black tongue, big mouth, cracked mouth, bad mouth, dirty face, dirty teeth, dirty pantie, fused pantie, poorly drawn pantie, fused cloth, poorly drawn cloth, badpantie, yellow teeth, thick lips, bad camel toe, colorful camel toe, bad asshole, poorly drawn asshole, fused asshole, missing asshole, bad anus, bad pussy, bad crotch, badcrotch seam, fused anus, fused pussy, fused anus, fused crotch, poorly drawn crotch, fused seam, poorly drawn anus, poorly drawn pussy, poorly drawn crotch, poorlydrawn crotch seam, bad thigh gap, missing thigh gap, fused thigh gap, liquid thigh gap, poorly drawn thigh gap, poorly drawn anus, bad collarbone, fused collarbone, missing collarbone, liquid collarbone, strong girl, obesity, worst quality, low quality, normal quality, liquid tentacles, bad tentacles, poorly drawn tentacles, split tentacles, fused tentacles, missing clit, bad clit, fused clit, colorful clit, black clit, liquid clit, QR code, bar code, censored, safety panties, safety knickers, beard, furry, pony, pubic hair, mosaic, futa, testis, (((deformed))), blurry, bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), fused fingers, messy drawing, broken legs censor, censored, censor_bar, multiple breasts, (mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), black-white, bad anatomy, liquid body, liquidtongue, disfigured, malformed, mutated, anatomical nonsense, text font ui, error, malformed hands, long neck, blurred, lowers, low res, bad anatomy, bad proportions, bad shadow, uncoordinated body, unnatural body, fused breasts, bad breasts, huge breasts, poorly drawn breasts, extra breasts, liquid breasts, heavy breasts, missingbreasts, huge haunch, huge thighs, huge calf, bad hands, fused hand, missing hand, disappearing arms, disappearing thigh, disappearing calf, disappearing legs, fusedears, bad ears, poorly drawn ears, extra ears, liquid ears, heavy ears, missing ears, fused animal ears, bad animal ears, poorly drawn animal ears, extra animal ears, liquidanimal ears, heavy animal ears, missing animal ears, text, ui, error, missing fingers, missing limb, fused fingers, one hand with more than 5 fingers, one hand with less than5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, colorful tongue, blacktongue, cropped, watermark, username, blurry, JPEG artifacts, signature, 3D, 3D game, 3D game scene, 3D character, malformed feet, extra feet, bad feet, poorly drawnfeet, fused feet, missing feet, extra shoes, bad shoes, fused shoes, more than two shoes, poorly drawn shoes, bad gloves, poorly drawn gloves, fused gloves, bad cum, poorly drawn cum, fused cum, bad hairs, poorly drawn hairs, fused hairs, big muscles, ugly, bad face, fused face, poorly drawn face, cloned face, big face, long face, badeyes, fused eyes poorly drawn eyes, extra eyes, malformed limbs, more than 2 nipples, missing nipples, different nipples, fused nipples, bad nipples, poorly drawnnipples, black nipples, colorful nipples, gross proportions. short arm, (((missing arms))), missing thighs, missing calf, missing legs, mutation, duplicate, morbid, mutilated, poorly drawn hands, more than 1 left hand, more than 1 right hand, deformed, (blurry), disfigured, missing legs, extra arms, extra thighs, more than 2 thighs, extra calf,fused calf, extra legs, bad knee, extra knee, more than 2 legs, bad tails, bad mouth, fused mouth, poorly drawn mouth, bad tongue, tongue within mouth, too longtongue, black tongue, big mouth, cracked mouth, bad mouth, dirty face, dirty teeth, dirty pantie, fused pantie, poorly drawn pantie, fused cloth, poorly drawn cloth, badpantie, yellow teeth, thick lips, bad camel toe, colorful camel toe, bad asshole, poorly drawn asshole, fused asshole, missing asshole, bad anus, bad pussy, bad crotch, badcrotch seam, fused anus, fused pussy, fused anus, fused crotch, poorly drawn crotch, fused seam, poorly drawn anus, poorly drawn pussy, poorly drawn crotch, poorlydrawn crotch seam, bad thigh gap, missing thigh gap, fused thigh gap, liquid thigh gap, poorly drawn thigh gap, poorly drawn anus, bad collarbone, fused collarbone, missing collarbone, liquid collarbone, strong girl, obesity, worst quality, low quality, normal quality, liquid tentacles, bad tentacles, poorly drawn tentacles, split tentacles, fused tentacles, missing clit, bad clit, fused clit, colorful clit, black clit, liquid clit, QR code, bar code, censored, safety panties, safety knickers, beard, furry, pony, pubic hair, mosaic, futa, testis"

# seed固定
generator = torch.Generator(device=DEVICE).manual_seed(SEED)

image = pipe(
    prompt,
    num_inference_steps=140,
    guidance_scale=11.5,
    width=512,
    height=640,
    negative_prompt=negative_prompt,
    generator=generator,
    max_embeddings_multiples=2
).images[0]

image.save("test.png")

実行した結果、次のような画像が生成されます。

ステップ数140は、公式ページのプロンプトを参考にしています。
実際は、40程度でも十分でしょう。

コード上では、seedの固定と長いプロンプトに対応する処理が含まれています。
それらの方法については、次の記事で説明しています。

以上、Hentai Diffusion 22の利用方法を説明しました。
次は、Hentai Diffusion 22の動作検証を説明します。

Hentai Diffusion 22の動作検証

Hentai Diffusion 22の動作検証を行います。
具体的には、強調表現を用いずにカラー指定ができるのかを検証します。

その際に基準として利用するのは、以下のプロンプトとします。

best quality, {{{masterpiece}}}, 1girl, perfect body, leaf hair, black turtleneck, autumn background, a short white skirt, standing

基準となるプロンプトでは、turtleneckの色がblackです。

blackredbluegreen
purplewhitegraypink

強調表現「()」「{}」を用いずに、基準となるプロンプトを上記の色で指定し直した結果が以下。

対象が1つであれば、上手く反映されました。
しかし、対象が二つとなると少し反応が落ちてしまうようです。

blue, blackpurple, redgreen, redyellow, blue

turtleneck、skirtを上記のように指定した場合の結果が、以下。

基本的には、上手く反応できています。
しかし、「blue, black」と「yellow, blue」を指定したモノは逆になっています。

まあ、こういうこともあるでしょう。
その意味では、要改善と言えますね。

以上、Hentai Diffusion 22の動作検証を説明しました。

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