こすたろーんエンジニアの試行錯誤部屋

作成物の備忘録を書いていきますー

【Stable Diffusion】google colaboratory上でDiffusers環境を構築する

以前にgoogle colab上でdiffusersを動かして、画像生成を行いました
technoxs-stacker.hatenablog.com

この記事はdiffusersモジュールをgoogle colabで使用するための備忘録です

目次

スポンサーリンク

この記事でわかること

google colab上でdiffusers環境を構築する方法

1.実行環境

Google Colab
Diffusers transformers==4.26.0
model : basil_mix

2.インストール

!pip install diffusers transformers==4.26.0 ftfy accelerate
!pip install safetensors

3.画像生成コード

model_name = "nuigurumi/basil_mix"
save_name = 'basil_mix.png'
save_dir = path/to/save/directory
os.makedirs(save_dir, exist_ok=True)
save_path = os.path.join(save_dir, save_name)
seed = 3000000
device = "cpu"

pipe = StableDiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float32)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")
if pipe.safety_checker is not None:
    pipe.safety_checker = lambda images, **kwargs: (images, False)


positive="masterpiece, ultra high res, 1 girl, (photo realistic:1.2),small_breasts,cleavage,smile,detailed beautiful skin, face focus, (black eyes), cute, young, looking at viewers, (scoop neck tee:2.0), ,collarbone, ((black hair)), windy, soft light, hand between legs, from front"
negative="painting,sketches,(worst quality:2),(low quality:2),(normal quality:2),lowers,normal quality,((monochrome)),((grayscale)),skin spots,acnes,skin blemishes,age spot,(outdoor:1.6),nsfw,ugly face,fat,(missing fingers),extra fingers,extra arms,extra legs,open chest,chest pad"
generator = torch.Generator(device).manual_seed(seed)

image = pipe(positive, negative_prompt=negative, generator=generator).images[0]
image.save(save_path)

4.生成結果

positive

masterpiece, ultra high res, 1 girl, (photo realistic:1.2),small_breasts,cleavage,smile,detailed beautiful skin, face focus, (black eyes), cute, young, looking at viewers, (scoop neck tee:2.0), ,collarbone, ((black hair)), windy, soft light, hand between legs, from front

negative

painting,sketches,(worst quality:2),(low quality:2),(normal quality:2),lowers,normal quality,((monochrome)),((grayscale)),skin spots,acnes,skin blemishes,age spot,(outdoor:1.6),nsfw,ugly face,fat,(missing fingers),extra fingers,extra arms,extra legs,open chest,chest pad

seed = 60000

seed = 90000

seed = 9000000

スポンサーリンク

参考

github.com
huggingface.co