Skip to content

Instantly share code, notes, and snippets.

@Neos21
Created September 14, 2022 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neos21/fe14b63fe9dad4c61478f9e71ec5a489 to your computer and use it in GitHub Desktop.
Save Neos21/fe14b63fe9dad4c61478f9e71ec5a489 to your computer and use it in GitHub Desktop.
Stable Diffusion

2022-08-23 HなStable Diffusion : https://anond.hatelabo.jp/20220823205005

前提として、Stable Diffusionでエロ画像を出そうとしてもsafety checkerという機能が入っており、センシティブな画像を出そうとすると黒塗りになる。

(Stable DiffusionのSaaSであるDream Studioはぼかしだが、多分別の技術)

https://github.com/huggingface/diffusers/releases/tag/v0.2.3

そこでGoogle Colabでちゃちゃっと環境を作り、なおかつNSFWを回避する。

  1. 下記のリンクでノートを開く

https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/stable_diffusion.ipynb

  1. 下記の箇所を書き換える

vvvvvvvvvvvvvvvvvv

from diffusers import StableDiffusionPipeline

^^^^^^^^^^^^

この一行を書き換えて自前のStable Diffusion Pipelineをクラス定義する。

https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py

をこぴってきてL157行目~159行目を消して貼り付ける。

https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L157-L159

  1. Google Colabの上から順番に実行する

これだけだ。だが、自分の性癖に刺さるStable Diffusionの作成は難しい。つーかマジ安定しない。waifuを探したければ、多分Stable Diffusionは合わない。hentai御用達ワードもなかなかヒットしなかったのでムズイ。

一応、redditを参考にワイが発掘したpromptを置いておく。

"full page antique lithograph of naked girl, sexual position, White background, art print, clean brush stroke, realistic highly detailed, post-processing highly detailed, rendered by octane engine, esty"

naked girlの間に年齢を指定するとガチであかんやつ。人の顔を安定して出すのに"lithograph of" はかなり使える。

"nude painting, big breasts, hot petite, long braided hair, hazel eyes, full round face, short smile, cinematic lightning, medium shot, mid-shot, cinematic wallpaper -C 13"

おっぱいの大きさに定評がある白人の女性がたくさん出てくる。顔の部位を丁寧に指定することで安定性が増すらしい。

追記

・肝心のコード改修がテキトーな説明でごめんなさい。safety checkerのメソッドをオーバーライドするのが多分1番簡単だから、ぶら下がってるコメント見てください。ありがとう!

・prompt(おまじない)は無から生まれたものではなく、当然おまじないと画像を紐付けしたデータが元になっている。汎用画像分類モデルCLIPはopenaiという別の団体が公開してるおまじないと画像のデータセットだけど、これを検索できるようにしてくれた人がいる。

https://rom1504.github.io/clip-retrieval/?back=https%3A%2F%2Fknn5.laion.ai&index=laion5B&useMclip=false

なんでこんなサイトを紹介しているかって?

お気に入りのエロ画像が出てこねーのはお前のpromptが悪いからだ。それを確認できるのがこのサイトだ。

例えば中学生男子なみのムラムラしているおまえはStablediffusionでsexと入れるだろう。だが決して出てこない。

その理由はこのサイトを検索すればわかるだろう。邪魔な画像が多すぎるのだ。

同様に足をぱっかーんと開いたお姉さんを召喚してみよう。

spread her legs

spreadだっていってんだろ。なに足閉じてんだよをクロスしてんだよ。

この辺が上手くいかない理由だ。

フレーバーをいくら増やしてもこの手の問題は解決しづらい。例えば sex human でググると多分直感に反してラブドールの画像ばかりひっかかるだろう。

promptで重要なのは何を学習したか、その見えない文脈を推測することだ。そのためにはGoogle先生なみの文字センスと検索力が必要となるだろう。

ヒントは与えた。後は健闘を祈る


r/UnstableDiffusion has been banned from Reddit とのこと。

貴重な情報源が...


from diffusers import StableDiffusionPipeline

をgithubのファイルの内容そのままに置き換えても

from ...models import AutoencoderKL, UNet2DConditionModel

以下4行でエラー出るんすけど


動いた参考例

10-12行目の以下、「...」を「diffusers.」に

from ...models import AutoencoderKL, UNet2DConditionModel

from ...pipeline_utils import DiffusionPipeline

from ...schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler

13行目の以下、先頭に#でコメントアウト

from .safety_checker import StableDiffusionSafetyChecker

24行目の以下、先頭に#でコメントアウト

safety_checker: StableDiffusionSafetyChecker,

35行目の以下、先頭に#でコメントアウト

safety_checker=safety_checker,

164行目の以下を

return {"sample": image, "nsfw_content_detected": has_nsfw_concept}

以下にする

return {"sample": image}


mainブランチのままのリンクだとそのうち行がずれると思うので今のcommit idのリンクを張った方が良いのでは

https://github.com/huggingface/diffusers/blob/511bd3aaf2772d09ffe6bfaeca2709682184f920/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L157-L159


元増田とは別の書き方を参考までに。

https://note.com/npaka/n/ndd549d2ce556

基本的にこの記事と同じ方法。

from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN) pipe.to("cuda")

def dummy_checker(images, **kwargs): return images, False pipe.safety_checker = dummy_checker

下の2行を追加するだけ。これでも動いた。


これ優秀やね。元増田のやつよりお手軽。

あとせっかくだからサムネイルを並べて表示するコードを紹介しとこう。

import matplotlib.pyplot as plt

num_inference_steps = 10 # Number of denoising steps guidance_scale = 7.5 # Scale for classifier-free guidance batch_size = 1

def show_images(images, figsize=(20,10), columns = 5): plt.figure(figsize=figsize) for i, image in enumerate(images): plt.subplot(len(images) / columns + 1, columns, i + 1) plt.imshow(image)

#入力文字 ここに好きな禁則文字をいれてください prompt = ["hatena anonymous diary"]

画像のサイズ

height = 512 # default height of Stable Diffusion width = 512 # default width of Stable Diffusion

SEED値、ここをかえると 同じ入力文字でも別の画像がでます

seedId = 1

images = [] for i in range(5): generator = torch.manual_seed(seedId + i) print(seedId + i) image = run(prompt,generator,height,width,num_inference_steps,guidance_scale,batch_size) images.append(image)

show_images(images)


一番上にあった

https://zenn.dev/hellohazime/articles/8ae3e6ccd698d3

これの通りにやったんだが

なんでサンプルにある

🍆とgenerator = torch.manual_seed(4)でエロ画像がでるのかさっぱりわからん

実は 🍆と torch.manual_seed(3) で出てきた緑のコレもエロ画像なんか?

なんなの🍆

追記 🍑と generator = torch.manual_seed(4)とかmanual_seed(1) もエロ画像だったが

この考えでいくと🍌もエロくないの納得いかん

それともtorch.manual_seed の数字 変えてくと🍌もエロ画像に変わったりするのか?

そして

🍆+torch.manual_seed(4)

🍑+torch.manual_seed(4)

が同じ構図の性別違いだったんだが、これ seedで構図決まるの? 教えてエロい人

そして👓ではエロ画像出ないの納得いかない!!


pornhubから使えそうなタイトル文をそのままコピペしてpromptにいれて

この一気に生成できるコードで確かめるとめっちゃ捗る「Naughty Japanese School Girls Vol. 18 」とか入れるとめっちゃエロ画像しか出てこない

ただ、これだとどのSEEDがどの画像なのかわからんのでSEEDを画像の上に表示したいんだがやり方がわからん、たすけて


def my_safety_checker(images, clip_input):

return images, False

pipe.safety_checker = my_safety_checker

StableDiffusion v1.4 : https://rentry.org/retardsguide

Step 1: Download the NEW 1.4 model from https://drive.yerf.org/wl/?id=EBfTrmcCCUAGaQBXVIj5lJmEhjoP1tgl Torrent magnet: https://rentry.org/sdiffusionmagnet Step 2: Download the cloned repo by going to https://github.com/basujindal/stable-diffusion and unzip it Step 3: Go into the repo you downloaded and go to stable-diffusion-main/models/ldm. Create a folder called "stable-diffusion-v1". Rename the .ckpt file to "model.ckpt", and copy it into that folder you've made Step 4: Under stable-diffusion-main/src, delete the empty folders named "clip" and "taming-transformers" Step 5: Open environment.yaml in Notepad, and after the line saying "dependencies:", add "- git" The "-" should be lined up with the ones in the following lines. Step 6: Download miniconda HERE: https://docs.conda.io/en/latest/miniconda.html. Download Miniconda 3 Windows Step 7: Install miniconda. Install for all users. Uncheck "Register Miniconda as the system Python 3.9" unless you want to. Step 8: Open Anaconda Prompt (miniconda3). Go to the stable-diffusion-main folder wherever you downloaded using "cd" to jump folders. or just type "cd" and then drag the folder into the Anaconda prompt. Step 9: Run the following command: "conda env create -f environment.yaml". Make sure you are in the stable-diffusion-main folder with stuff in it. (i made that mistake lol) Step 10: Run the following command "conda activate ldm". You'll need to do this every time you start makin' prompts (and Step 8 to get to the right folder!) Step 11: congrats this is the gud part. to generate run "python scripts/txt2img.py --prompt "promot" --H 512 --W 512 --seed 27 --n_iter 2 --ddim_steps 50". (you might be able to get away with 512x640 if you have a 3080) If you don't have a 3080 or better (10GB VRAM required), you will need to run "python optimizedSD/optimized_txt2img.py --prompt "your prompt here" --H 512 --W 512 --seed 27 --n_iter 2 --ddim_steps 50" You may need to lower the resolution with --W for width and --H for height if you don't have enough VRAM. Also it does not generate a random seed, so modify --seed and type random numbers if you want to get something different

tips:

-Got less than 10GB of VRAM, and want a higher resolution? You can do this if you accept half precision; saving over 2GB of vram for barely any perceptible difference Replace your txt2img.py file in Scripts with this one: https://filebin.net/6jxnsnj3w7qsxr6j -prompts fucking matter. I typed something bullshit "an anime gorilla" and got shit, tried the princess prompt and got kino. -Make sure you follow this shit to the letter, it's very techy and easy to fuck up -have fucking fun


  • Torrent Magnet : magnet:?xt=urn:btih:3a4a612d75ed088ea542acac52f9f45987488d1c&dn=sd-v1-4.ckpt&tr=udp%3a%2f%2ftracker.openbittorrent.com%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337
  • FileBin https://filebin.net/6jxnsnj3w7qsxr6j : Not Found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment