在Colab上玩Stable Diffusion?

0 阅读5分钟

前言

Stable Diffusion 虽然免费但是对于新手小白来说,想体验需要事先购买配置好点的机器,万一坚持不了放弃又是一笔损失。真是又菜又爱玩,小溪无意间读到一篇文章说 Colab 上可以玩 Stable Diffusion,抱着好奇心体验一波,这里记录一下使用过程,最后附上几组提示词及效果图。对Colab还不熟悉的小伙伴可以看往期内容:

使用限制

  • 需要谷歌账号
  • 需要科学上网
  • GPU机器有使用时长限制(2-12小时),不用尽量不要挂着,达到每天使用限额就不能用了

Image

GPU免费时长到期后将无法使用

Image

在Colab上使用模版

作者提供的笔记链接:

colab.research.google.com/github/hugg…

打开笔记链接进入 Stable Diffusion 笔记,可以看到笔记是基于Colab笔记搭建的

Image

登录后点击右上角的下拉菜单,选择【切换运行时环境】

Image

Colab会随机分配一个机器,我这里分配的是 T4 机器,不得不说这配置比我电脑配置好。在【硬件加速器】类型中选择GPU类型,点击【保存】

Image

启动完成成功后状态如下:

Image

点击右上角下拉菜单,选择【查看资源】可以查看资源的使用情况,使用时需要时刻注意运行时剩余时长,尽量不要超过显示的时长

Image

使用流程

按照笔记文档一步步执行操作,检查配置、安装依赖、文生图

查看机器配置信息

Image

安装必要的依赖

Image

Image

如果执行上面命令报错,更新 diffusers 版本

Image

Image

GPU上运行管道

看过之前Hugging Face系列内容多小伙伴对这段代码应该不陌生,对Hugging Face系列感兴趣的小伙伴可以翻看往期内容:

Image

修改提示词

随便写一个提示词看看效果

“一个穿着汉服的女孩在月光下弹奏古筝”

"A girl in Hanfu playing the guzheng under the moonlight, beautiful, elegant, detailed, intricate, cinematic lighting"

先试试中文,直接黑屏了,后面又重试了多次,中文也可以,可能提示词理解上没有英文好。

Image

为了更好的体验可以试试将中文提示词翻译为英文

Image

这次成功了,不过这效果有点吓人😢,不过也正常,文生图本来就是抽卡,生图效果受众多因素影响,对效果不满意可以多尝试几次或者切换为其他模型

化繁为简

操作一遍后就知道其实不复杂,在了解过Hugging Face系列后可以知道,该笔记其实就是使用 Hugging Face Diffusers库 + Stable Diffusion模型 搭建的Colab文生图笔记,主要分为三步,下面提供对应命令

Image

查看GPU

!nvidia-smi

安装依赖

!pip install --upgrade diffusers accelerate transformers

文生图

基于Hugging Face的Diffusers库加载模型实现文生图

from diffusers import DiffusionPipeline
import torch
# 创建 DiffusionPipeline 实例
pipeline = DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16)
# 将模型移动到 GPU 上
pipeline.to("cuda")
# 提示词
prompt"An image of a squirrel in Picasso style"
# 使用模型生成图像
pipeline(prompt).images[0]

提示词及效果

最后提供几组提示词,这几组提示词来自秋风大佬

赛博朋克风:Cyberpunk, 8k resolution, castle, the rose sea, dream

 

Image

 

水墨画风格:prompt: a watercolor ink painting of a fallen angel with a broken halo wielding a jagged broken blade standing on top of a skyscraper in the style of anti - art trending on artstation deviantart pinterest detailed realistic hd 8 k high resolution

Image

油画: portrait of bob barker playing twister with scarlett johansson, an oil painting by ross tran and thomas kincade

 

Image

水彩画: a girl with lavender hair and black skirt, fairy tale style background, a beautiful half body illustration, top lighting, perfect shadow, soft painting, reduce saturation, leaning towards watercolor, art by hidari and krenz cushart and wenjun lin and akihiko yoshida

Image

参考

友情提示

见原文:在Colab上玩Stable Diffusion?

本文同步自微信公众号 "程序员小溪" ,这里只是同步,想看及时消息请移步我的公众号,不定时更新我的学习经验。