适合画图修图的AI模型开源程序

 火... [复制链接]  307查看
 楼主| 易西 发表于 2025-6-26 22:06:09 | 显示全部楼层
& @' a! H" {' N) N3 v( m

& A" M9 i" r1 p1 }$ F/ o: @9 XStable Diffusion安装GitHub  W5 b& y& H8 G/ M3 `7 l8 x/ |
在安装 Stable Diffusion 模型时,通常有两种主要方式:直接从官方 GitHub 仓库克隆代码,或通过 Hugging Face 等平台安装预训练模型。下面将分别介绍这两种方法。8 H) c/ k7 n+ |; g- e" N) ]9 w

- t: Z$ v7 t3 R) c. K方法一:从 GitHub 克隆代码
7 h4 f! o1 Q+ R2 F& d克隆 Stable Diffusion 仓库
2 n2 A1 D' N$ U( o
  `! R# W" J: g* o( S+ D首先,你需要从 GitHub 克隆 Stable Diffusion 的官方仓库。截至目前(2023年),官方仓库地址为:
# A7 P$ B) c% _, k& N5 t) G+ n2 ?: _& @4 u8 p. F8 l
https://github.com/CompVis/stable-diffusion
- `2 B6 l3 j: g3 j* N' l
( k# P, R+ @8 B  n+ Z) Q在终端中,使用以下命令克隆仓库:
# p; G1 i5 @* C
2 v; c7 x/ e4 Z4 Tgit clone https://github.com/CompVis/stable-diffusion.git
! c# S+ C% @% gcd stable-diffusion' ~% M; ]6 c- ^1 {8 o- p

6 ^1 Q6 N& W0 J) C: t; n( |0 A: X设置环境
3 y" {% s8 O5 z0 k  ^. a3 F' o8 {  d& H9 J; x
安装必要的依赖。Stable Diffusion 需要 Python 和一些特定的库。你可以使用 requirements.txt 文件中的依赖来安装它们。首先,确保你的 Python 版本符合要求(通常是 Python 3.8 或更高)。然后,使用以下命令安装依赖:; U7 J0 K5 w6 I! t  ~1 b* D
5 ]8 s, K- j- \( h6 F
pip install -r requirements.txt
' s* g% g* ^7 x! c
3 \; |( }- C3 \; x运行 Stable Diffusion! v8 @. T+ r+ f
6 O! z! g! }- G  e
根据你的具体需求,你可以运行不同的脚本。例如,如果你只是想测试模型,可以运行:
2 [3 i/ a! s: |/ g' `2 i1 }7 j& X' |* z0 S$ j7 T1 Z% X5 |) Z
python scripts/txt2img.py --prompt "a photo of an astronaut riding a horse"6 {) u! q$ v- A' e8 _0 ?
( O* B$ ?! W( {3 W2 T1 _4 u0 F
方法二:通过 Hugging Face 安装预训练模型
( h, g2 m' P) Z* w如果你只是想使用预训练的模型而不想从源代码开始,你可以使用 Hugging Face 的 diffusers 库来加载和使用 Stable Diffusion 模型。
' h2 t7 y  y. A. v: E1 ~. I! }8 U% I
安装 diffusers( k, l# d; \# J

" F: W5 F1 C- ~6 A! e( l首先,安装 diffusers 库:
$ l; I' O8 D' Z1 `2 b- J( a- A
/ k  `% X/ ^: D5 \pip install diffusers transformers scipy/ U- K" ]( S7 \. K

1 Q7 [1 v# {/ e' ?# K( E9 h  N加载和使用模型/ X6 N" m, g% _3 N/ k( n
/ W1 _: ]9 g( w, Z
使用以下代码加载和使用 Stable Diffusion 模型:: }$ S1 t3 w( o) `! t

  c' H4 X2 t0 t% W' g" Kfrom diffusers import StableDiffusionPipeline' j# ^9 Y6 g4 l; d, p
import torch* z3 O( x4 R& B! {" N  u
, J$ {" ~2 D8 W  `; \
device = "cuda" if torch.cuda.is_available() else "cpu"
1 a/ U. `4 V( a4 n) v* ~% B8 Smodel_id = "runwayml/stable-diffusion-v1-5"  # 使用官方预训练模型ID
( O  r5 t! _; I# hpipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True)
" s! }4 l  H. o5 Tpipe = pipe.to(device)4 O1 p! U$ d( k* z0 j" Z
- [( G. f, d, H) F' a
image = pipe("a photo of an astronaut riding a horse", guidance_scale=7.5)
/ I8 N! ^7 T2 H5 {1 @5 R0 G, himage[0].save("astronaut_riding_horse.png")
9 Y0 a+ \) W* r4 U% b' `# _7 i& P0 ?. s) O+ e* y# w) ]+ O
确保替换 use_auth_token=True 中的 True 为你的 Hugging Face 访问令牌,如果你有的话。如果没有,可以省略或设置为 None。你可以在 Hugging Face 的网站上获取访问令牌。
3 [( f. f+ D$ Y9 b2 s/ m, H* ]6 |& ~2 K3 t$ C+ j5 ]% k
通过上述任何一种方法,你都可以开始使用 Stable Diffusion 生成图像了。选择哪种方法取决于你的具体需求,比如是否需要自定义模型或只是快速开始使用预训练模型。- I, K" \/ z2 @0 F& J" B8 L

# R% \; ?- h  Y, M0 W  M: F& _提示:AI自动生成,仅供参考% s3 A( ^: h/ @8 `1 }

: I$ `% {9 F4 v0 U参考
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

邮箱|首页|小黑屋|吾侪 ( 蜀ICP备2020029307号-4 )

GMT+8, 2026-3-5 10:54 , Processed in 0.052301 second(s), 18 queries , APCu On.

Powered by Discuz! X3.5

Copyright © , 吾侪网

快速回复 返回顶部 返回列表