Featured image of post 使用Stable Diffusion生成AI插画:Google Colab步骤与注意事项

使用Stable Diffusion生成AI插画:Google Colab步骤与注意事项

介绍使用Stable Diffusion和Google Colab生成插画的步骤,并说明GPU可用性与模型许可等注意事项。

什么是 Stable Diffusion

Stable Diffusion是一类根据文本等条件生成图像的潜在扩散模型。相关研究包括CompVis团队的工作,作者中也有慕尼黑大学的研究人员。

准备工作

  • Google账号

只需要这个。

生成步骤

本文将2023年的示例更新为当前Diffusers API。Colab免费GPU并非始终可用;使用限制、GPU类型和运行时间会变化。如果没有分配GPU,请稍后重试或使用其他可用的运行环境。

加载模型权重前,请阅读Hugging Face上的模型卡及CreativeML OpenRAIL-M许可证,并遵守其中的禁止用途、分发条款和使用地法律。该许可证不保证生成内容合法或不涉及第三方权利。

本例中的EasyNegative是单独发布的Textual Inversion嵌入文件。下面的代码会先加载它,再在提示词中使用该标记。

  1. 打开 https://colab.research.google.com
  2. 选择左上角文件中的新建笔记本
  3. 选择编辑中的笔记本设置
  4. 将硬件加速器更改为GPU img_2.png
  5. 粘贴并运行以下代码
1
!pip install -U diffusers transformers accelerate
  1. 粘贴并运行以下代码
1
2
3
import torch
from diffusers import DiffusionPipeline
from huggingface_hub import hf_hub_download
  1. 粘贴并运行以下代码
1
2
3
4
pipe = DiffusionPipeline.from_pretrained("gsdf/Counterfeit-V2.5", dtype=torch.float16)
easynegative_path = hf_hub_download(repo_id="gsdf/EasyNegative", repo_type="dataset", filename="EasyNegative.safetensors")
pipe.load_textual_inversion(easynegative_path, token="EasyNegative")
pipe.to("cuda")
  1. 粘贴并运行以下代码
1
2
3
4
5
prompt = "((masterpiece,best quality)),1girl, solo, animal ears, rabbit, barefoot, knees up, dress, sitting, rabbit ears, short sleeves, looking at viewer, grass, short hair, smile, white hair, puffy sleeves, outdoors, puffy short sleeves, bangs, on ground, full body, animal, white dress, sunlight, brown eyes, dappled sunlight, day, depth of field"
n_prompt = "EasyNegative, extra fingers,fewer fingers"

image = pipe(prompt, negative_prompt = n_prompt).images[0]
image

这里使用的Prompt参考了https://huggingface.co/gsdf/Counterfeit-V2.5的Prompt。

生成结果(部分)

img_1.png

img_3.png

img_4.png

参考

comments powered by Disqus