当地用AIGC天生图像与视频

[复制链接]
发表于 2026-2-23 03:46:28 | 显示全部楼层 |阅读模式
近来AI界最火的话题,当属Sora了。遗憾的是,Sora如今还没开源或提供模子下载,以是没法在当地跑起来。但是,业界有一些开源的图像与视频天生模子。固然结果上还没那么惊艳,但还是值得我们体验与学习下的。
Stable Diffusion(SD)是比力盛行的开源方案,可用于文生图、图生图及图像修复。Stability AI近来发布了Stable Diffusion 3,接纳的是与Sora类似的Diffusion Transformer(DiT)技能。别的,Stable Video Diffusion(SVD)将图像升级到视频,可用于文生视频和图生视频。
下面先容下怎样在当地呆板上运行SD和SVD。起首假定有一台带GPU的呆板(本人用的RTX 4070),并装好Python和CUDA根本环境。
Stable Diffusion

最简朴的方式是用Python脚本运行。我们可以用diffusers库来运行。该库集成了各种diffusion pipeline。注意脚本大概实验从hugging-face官方下载模子。如果下载失败,可以设置下面的环境变量:
  1. export HF_ENDPOINT=https://hf-mirror.com
复制代码
按官方文档(https://hf-mirror.com/runwayml/stable-diffusion-v1-5)运行Stable diffusion 1.5:
  1. from diffusers import StableDiffusionPipeline
  2. import torch
  3. model_id = "runwayml/stable-diffusion-v1-5"
  4. pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
  5. pipe = pipe.to("cuda")
  6. prompt = "a photo of an astronaut riding a horse on mars"
  7. image = pipe(prompt).images[0]  
  8.    
  9. image.save("astronaut_rides_horse.png")
复制代码
运行上面脚本,结果:

运行Stable Diffusion 2.1也是类似的。运行官方例子:
  1. import torch
  2. from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
  3. model_id = "stabilityai/stable-diffusion-2-1"
  4. # Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
  5. pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
  6. pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
  7. pipe = pipe.to("cuda")
  8. prompt = "a photo of an astronaut riding a horse on mars"
  9. image = pipe(prompt).images[0]
  10.    
  11. image.save("astronaut_rides_horse.png")
复制代码
结果:

以上是文生图。图生图,图像修补的使用可拜见:


  • https://hf-mirror.com/docs/diffusers/en/using-diffusers/img2img
  • https://hf-mirror.com/docs/diffusers/en/using-diffusers/inpaint
对结果不太满意可以调治参数。
Stable Diffusion XL(SDXL)是一个更为强盛的天生模子。用法可拜见:https://hf-mirror.com/docs/diffusers/en/using-diffusers/sdxl。比如文生图的例子:
  1. from diffusers import AutoPipelineForText2Image
  2. import torch
  3. pipeline_text2image = AutoPipelineForText2Image.from_pretrained(
  4.     "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
  5. ).to("cuda")
  6. prompt = "a photo of an astronaut riding a horse on mars"
  7. image = pipeline_text2image(prompt=prompt).images[0]
  8. image.save("astronaut_rides_horse.png")
复制代码
结果:

如果想用TensorRT加速的话可拜见:https://github.com/NVIDIA/TensorRT/tree/release/8.6/demo/Diffusion。在此不再累述。
Stable Video Diffusion

Stable Video Diffusion(SVD)可用于天生视频。使用方法可拜见:https://hf-mirror.com/docs/diffusers/en/using-diffusers/text-img2vid。如官方中的例子:
  1. import torch
  2. from diffusers import StableVideoDiffusionPipeline
  3. from diffusers.utils import load_image, export_to_video
  4. pipeline = StableVideoDiffusionPipeline.from_pretrained(
  5.     "stabilityai/stable-video-diffusion-img2vid", torch_dtype=torch.float16, variant="fp16"
  6. )
  7. pipeline.enable_model_cpu_offload()
  8. image = load_image("https://hf-mirror.com/datasets/huggingface/documentation-images/resolve/main/diffusers/svd/rocket.png")
  9. image = image.resize((1024, 576))
  10. generator = torch.manual_seed(42)
  11. frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]
  12. export_to_video(frames, "generated.mp4", fps=7)
复制代码
由于stable-video-diffusion-img2vid-xt在我的4070卡上貌似会OOM,因此换成stable-video-diffusion-img2vid。
结果:

   

Stable Diffusion web UI

前面都是用的Python脚本。要调模子的各种参数须要改调用参数,不太易用和直观。接下来看看怎么基于Diffusion模子构建App。
stable-diffusion-webui是用Gradio库实现的Stable Diffusion的web接口。在Linux环境可以按照以下文档搭环境:
https://github.com/AUTOMATIC1111/stable-diffusion-webui?tab=readme-ov-file#automatic-installation-on-linux
如果在实验webui.sh的过程碰到下面标题:
  1. stderr: ERROR: Could not find a version that satisfies the requirement tb-nightly (from versions: none)
  2. ERROR: No matching distribution found for tb-nightly
复制代码
可以换成阿里的pip源:
  1. pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
复制代码
别的脚本中会实验从hugging-face官网下载,无法下载的话可以将地点更换成:
  1. diff --git a/modules/sd_models.py b/modules/sd_models.py
  2. index 9355f1e1..bf5dbba5 100644
  3. --- a/modules/sd_models.py
  4. +++ b/modules/sd_models.py
  5. @@ -150,7 +150,7 @@ def list_models():
  6.      if shared.cmd_opts.no_download_sd_model or cmd_ckpt != shared.sd_model_file or os.path.exists(cmd_ckpt):
  7.          model_url = None
  8.      else:
  9. -        model_url = "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors"
  10. +        model_url = "https://hf-mirror.com/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors"
  11.      model_list = modelloader.load_models(model_path=model_path, model_url=model_url, command_path=shared.cmd_opts.ckpt_dir, ext_filter=[".ckpt", ".safetensors"], download_name="v1-5-pruned-emaonly.safetensors", ext_blacklist=[".vae.ckpt", ".vae.safetensors"])
复制代码
脚本实验完,顺遂的话就可以看到UI界面了。恣意输入点啥点Generate按钮就可以出图了。

比起脚本,这里参数的调治就直观得多,使用上傻瓜得多。
ComfyUI

ComfyUI是图形化、模块化的Diffusion模子工作流构建工具。别的它还支持插件扩展。可以按照https://github.com/comfyanonymous/ComfyUI?tab=readme-ov-file#nvidia搭建环境,末了运行:
  1. python main.py
复制代码
运行乐成后,打开http://127.0.0.1:8188,就可以看到UI界面:

接下来准备模子:
  1. cd models/checkpoints
  2. wget https://hf-mirror.com/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt
复制代码
然后在UI中选择该模子后点Queue Prompt按钮,默认的例子就可以跑通了。整个过程图形化,很直观。

根本环境搭好后,接下来就可以试试官方的别的例子:https://comfyanonymous.github.io/ComfyUI_examples。比如用于视频天生的SVD(先容可拜见https://blog.comfyui.ca/comfyui/update/2023/11/24/Update.html)。根听阐明:https://comfyanonymous.github.io/ComfyUI_examples/video,先下载所需模子:
  1. cd models/checkpoints
  2. wget https://hf-mirror.com/stabilityai/stable-video-diffusion-img2vid/resolve/main/svd.safetensors
  3. wget https://hf-mirror.com/stabilityai/stable-video-diffusion-img2vid-xt/resolve/main/svd_xt.safetensors
  4. https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors?download=true
复制代码
然后运行。这是图生视频的结果:

   

这是文生图再生视频的结果:

   


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表