4、stable diffusion

github

安装anaconda环境

conda env create -f environment.yaml
conda activate ldm

安装依赖

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
pip install transformers==4.19.2 diffusers invisible-watermark
pip install -e .

安装xformers加速
xformers安装包
找到符合自己python,pytorch和cuda版本的xformers安装包
在这里插入图片描述

wget https://anaconda.org/xformers/xformers/0.0.16/download/linux-64/xformers-0.0.16-py39_cu11.3_pyt1.12.1.tar.bz2
conda install xformers-0.0.16-py310_cu11.3_pyt1.12.1.tar.bz2
python -m xformers.info

安装成功为下述图片
在这里插入图片描述

源码

下载预训练模型权重
文生图
SD2.1-v
Image Inpainting with Stable Diffusion
512-inpainting-ema.ckpt

从Hugging Face下载encoding
当我们运行scripts/txt2img.py时,会发现还需要从hugging face下载encoding,但是国内无法下载,这时候我们会科学上网,同时安装下述依赖,这样就可以下载了

os.environ["http_proxy"] = "http://ip:7890"
os.environ["https_proxy"] = "http://ip:7890"

pip install urllib3==1.25.11
pip install requests==2.27.1

在这里插入图片描述

文生图

python scripts/txt2img.py 
--prompt
"a professional photograph of an astronaut riding a horse"
--ckpt
/devdata/chengan/stablediffusion/v2-1_768-ema-pruned.ckpt
--config
configs/stable-diffusion/v2-inference-v.yaml
--H
768
--W
768
--device 
cuda

注意: --device 默认为cpu,要换成 cuda,否则报错

RuntimeError: expected scalar type BFloat16 but found Float

在这里插入图片描述
在这里插入图片描述

图生图

图生图使用到Gradio依赖,但是会缺少一个文件,使用下述命令补全

wget https://cdn-media.huggingface.co/frpc-gradio-0.2/frpc_linux_amd64
mv frpc_linux_amd64 frpc_linux_amd64_v0.2
mv frpc_linux_amd64_v0.2 /home/chengan/anaconda3/envs/llm/lib/python3.9/site-packages/gradio 

chmod +x /home/chengan/anaconda3/envs/llm/lib/python3.9/site-packages/gradio/frpc_linux_amd64_v0.2

当部署项目到服务器,本地浏览器无法打开Gradio链接,修改inpainting.py文件最后一行

block.launch(server_name="0.0.0.0", server_port=7896, share=True)
python scripts/gradio/inpainting.py configs/stable-diffusion/v2-inpainting-inference.yaml /devdata/chengan/stablediffusion/512-inpainting-ema.ckpt

在这里插入图片描述
在这里插入图片描述

api

depth2image

pip install -U git+https://github.com/huggingface/transformers.git
pip install diffusers transformers accelerate scipy safetensors
import torch
import requests
from PIL import Image
from diffusers import StableDiffusionDepth2ImgPipeline

pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(
   "stabilityai/stable-diffusion-2-depth",
   torch_dtype=torch.float16,
).to("cuda")

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
init_image = Image.open(requests.get(url, stream=True).raw)

prompt = "two tigers"
n_propmt = "bad, deformed, ugly, bad anotomy"
image = pipe(prompt=prompt, image=init_image, negative_prompt=n_propmt, strength=0.7).images[0]

inpaint image

  pipe = StableDiffusionInpaintPipeline.from_pretrained(
      "stabilityai/stable-diffusion-2-inpainting",
      torch_dtype=torch.float16,
  )
  pipe.to("cuda")
  prompt = "two tigers"
  # image and mask_image should be PIL images.
  # The mask structure is white for inpainting and black for keeping as is
  url = "http://images.cocodataset.org/val2017/000000039769.jpg"
  image = Image.open(requests.get(url, stream=True).raw)
  mask_image = np.ones(image.size)
  img = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]
  plt.imshow(img)
  plt.show()
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Stable Diffusion(稳定扩散)是一种用于生成高质量文本的语言模型。它是由CSDN开发的C知道 AI团队开发的一种基于GPT的模型。Stable Diffusion模型通过对大量的文本数据进行预训练,学习到了丰富的语言知识和语义理解能力。它可以用于各种自然语言处理任务,如文本生成、对话系统、机器翻译等。 Stable Diffusion模型的核心是一个深度神经网络,它由多个Transformer模块组成。这些Transformer模块可以有效地捕捉文本中的上下文信息,并生成连贯、有逻辑的文本。Stable Diffusion模型还引入了一种称为Diffusion Mechanism(扩散机制)的技术,用于控制生成文本的稳定性和一致性。 与传统的基于规则或统计方法的文本生成模型相比,Stable Diffusion模型具有以下优势: 1. 生成文本质量高:Stable Diffusion模型通过大规模预训练学习到了丰富的语言知识,可以生成更加准确、流畅、自然的文本。 2. 上下文理解能力强:Stable Diffusion模型利用Transformer模块有效地捕捉了文本中的上下文信息,可以更好地理解和表达复杂的语义。 3. 可控性强:Stable Diffusion模型引入了Diffusion Mechanism技术,可以控制生成文本的稳定性和一致性,使得生成结果更加可控和可靠。 总之,Stable Diffusion是一种强大的语言模型,可以用于各种文本生成任务,并且具有高质量、上下文理解能力强、可控性强等优势。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值