; @6 C+ a& b# w
' J+ u5 C0 X8 l# |5 `Stable Diffusion安装GitHub. n3 G+ C& }) p) [0 W, u) n
在安装 Stable Diffusion 模型时,通常有两种主要方式:直接从官方 GitHub 仓库克隆代码,或通过 Hugging Face 等平台安装预训练模型。下面将分别介绍这两种方法。% V& i+ C( Z7 C! J; o2 x
' @4 ] n, ^; f方法一:从 GitHub 克隆代码
- \4 Z: U9 B- P& ?7 J8 x0 o+ L克隆 Stable Diffusion 仓库
$ L$ q7 P1 P* ` l2 y% X$ U) H; P$ r% i, Y$ O5 O; U
首先,你需要从 GitHub 克隆 Stable Diffusion 的官方仓库。截至目前(2023年),官方仓库地址为:
3 p, ?# J1 A1 b& O' P! O: i5 t
8 e0 f, y; N, m- k. a/ }0 U9 Ihttps://github.com/CompVis/stable-diffusion
6 j& [: ~# D: R6 }8 I- F, C6 y- a6 m) c. r# |
在终端中,使用以下命令克隆仓库:& c' A/ L( r3 B I9 r3 C: L
5 ^' w+ b8 T ?$ z+ l/ Q) [git clone https://github.com/CompVis/stable-diffusion.git7 H' E1 q- K2 m6 a; \5 F( T* J
cd stable-diffusion g# Z2 I) t r( {3 q& m: v( h4 e
6 w6 K1 z* {& w+ W
设置环境6 D* Q" E6 j8 F9 x r" K7 Q% C
2 m Z' S" p v
安装必要的依赖。Stable Diffusion 需要 Python 和一些特定的库。你可以使用 requirements.txt 文件中的依赖来安装它们。首先,确保你的 Python 版本符合要求(通常是 Python 3.8 或更高)。然后,使用以下命令安装依赖:
% H. }0 R2 _6 a; ~% |" f n8 F. Z$ ]: j" g9 X
pip install -r requirements.txt1 E! T; W. X$ z9 A& Y ~
& k1 ?! \! V# H
运行 Stable Diffusion
- ]0 N1 Y u* }. H! G1 ]
/ w' Q0 h5 V8 z+ J) e根据你的具体需求,你可以运行不同的脚本。例如,如果你只是想测试模型,可以运行:
4 P, K, D* y! [3 P4 ?7 U3 \
5 G# r8 ]- \2 o% U( _python scripts/txt2img.py --prompt "a photo of an astronaut riding a horse"* Q% S$ I% ]* H: p; `8 u5 Q& Z' H
* e3 `/ g n( ]方法二:通过 Hugging Face 安装预训练模型
5 [3 j/ g( m% j8 P% @+ j9 r0 {如果你只是想使用预训练的模型而不想从源代码开始,你可以使用 Hugging Face 的 diffusers 库来加载和使用 Stable Diffusion 模型。
; a# W; U. |7 a# R( q V+ R& o$ A0 ~; b
安装 diffusers/ x w% \+ j" j( m5 Q; ~8 o
9 h/ r0 t/ n) K4 H* ]
首先,安装 diffusers 库:; t. x# g) \3 M% k
9 E' s2 u* T6 d5 o. L- Zpip install diffusers transformers scipy
$ J% m6 W% i1 ?$ R) h9 v9 A0 D! F2 G" W4 J' h% A( I( O' [0 j# M
加载和使用模型
! k3 M: X( W5 ~5 ]6 |; ~: t$ q2 V: ~
使用以下代码加载和使用 Stable Diffusion 模型:
& Z4 h/ R/ b4 H7 \: ~# L& {: B- U+ N! S3 v0 _9 C+ [ {
from diffusers import StableDiffusionPipeline
$ D5 Q9 N& v5 T1 \ x" L( Kimport torch
# s5 {3 j z: Q2 m) ~* ^0 f
! |% t0 C/ L2 R3 k5 }device = "cuda" if torch.cuda.is_available() else "cpu"
) R! y, V* F2 d8 jmodel_id = "runwayml/stable-diffusion-v1-5" # 使用官方预训练模型ID- T- R+ L/ C! K# s( `
pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True)
% O# x) j; D" n( ?, z) C. xpipe = pipe.to(device)4 _( d. W) _! d7 h7 p! H
, B! E; X% w$ y: {5 x+ h$ cimage = pipe("a photo of an astronaut riding a horse", guidance_scale=7.5)" y6 e* d8 S/ A$ \( C" W
image[0].save("astronaut_riding_horse.png")
) d: s4 R( G6 ~
$ Z9 O& X; F! t0 C0 ^% P* V确保替换 use_auth_token=True 中的 True 为你的 Hugging Face 访问令牌,如果你有的话。如果没有,可以省略或设置为 None。你可以在 Hugging Face 的网站上获取访问令牌。2 `$ Q1 m( k( M P" [$ n
1 G% f2 w9 t" q/ `7 J$ Y( ]
通过上述任何一种方法,你都可以开始使用 Stable Diffusion 生成图像了。选择哪种方法取决于你的具体需求,比如是否需要自定义模型或只是快速开始使用预训练模型。
. d- `" F/ P& j# C" J0 D( u. V# v( M4 i3 C1 K- X7 P
提示:AI自动生成,仅供参考
; ?0 `* v) W& t) C: y5 e( S4 y4 \# `+ {" P0 K3 A: g& K7 A
参考 |