4、stable diffusion

github

安装anaconda环境

python 复制代码
conda env create -f environment.yaml
conda activate ldm

安装依赖

python 复制代码
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安装包

python 复制代码
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,但是国内无法下载,这时候我们会科学上网,同时安装下述依赖,这样就可以下载了

python 复制代码
os.environ["http_proxy"] = "http://ip:7890"
os.environ["https_proxy"] = "https://ip:7890"

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

文生图

python 复制代码
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,否则报错

python 复制代码
RuntimeError: expected scalar type BFloat16 but found Float


图生图

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

python 复制代码
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文件最后一行

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


api

depth2image

python 复制代码
pip install -U git+https://github.com/huggingface/transformers.git
pip install diffusers transformers accelerate scipy safetensors
python 复制代码
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

python 复制代码
  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()
相关推荐
AI极客菌2 天前
[ComfyUI]Flux:繁荣生态魔盒已开启,6款LORA已来,更有MJ6&写实&动漫&风景&艺术&迪士尼全套
ai作画·stable diffusion·aigc·midjourney·人工智能作画·comfyui·风景
许野平2 天前
Stable Diffusion:照片转视频插件
stable diffusion·animatediff·temporal·照片转视频
python零基础入门小白3 天前
如何通过AI绘画技术轻松变现?教你三种方法赚取高额利润!
ai作画·stable diffusion·aigc
AI设计酷卡5 天前
什么是 Stable Diffusion? | StableDiffusion 基础知识
人工智能·stable diffusion·aigc
AI极客菌7 天前
Controlnet作者新作IC-light V2:基于FLUX训练,支持处理风格化图像,细节远高于SD1.5。
人工智能·计算机视觉·ai作画·stable diffusion·aigc·flux·人工智能作画
AI绘画小338 天前
【comfyui教程】comfyui古风一键线稿上色,效果还挺惊艳!
人工智能·ai作画·stable diffusion·aigc·comfyui
AI绘画月月8 天前
【comfyui教程】ComfyUI有趣工作流推荐:快速换脸,创意随手掌握!
人工智能·ai作画·stable diffusion·aigc·comfyui
AI绘画咪酱8 天前
【AI绘画】AI绘图教程|stable diffusion(SD)图生图涂鸦超详细攻略,教你快速上手
人工智能·ai作画·stable diffusion·aigc·midjourney
HuggingAI9 天前
stable diffusion 大模型
人工智能·ai·stable diffusion·ai绘画
HuggingAI9 天前
stable diffusion图生图
人工智能·ai·stable diffusion·ai绘画