You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

StableDiffusionPipeline生成空白图,如何正确设置safety_checker?

解决Stable Diffusion生成空白图片及安全检查器配置问题

问题根源

空白图片通常是安全检查器将生成内容判定为违规后自动替换导致的;你设置safety_checker=1是错误操作——该参数仅接受StableDiffusionSafetyChecker实例或None,数字并非合法取值。同时警告提示的requires_safety_checker属性访问方式已过时,需调整配置修改路径。

两种解决方法

方法一:正确加载并启用安全检查器

若需保留安全过滤功能,需手动加载对应安全检查器模型和特征提取器:

from diffusers import StableDiffusionPipeline
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from transformers import AutoFeatureExtractor

# 加载主模型
pipeline = StableDiffusionPipeline.from_single_file("./v2-1_768-ema-pruned.ckpt")

# 加载安全检查器组件
safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker")
feature_extractor = AutoFeatureExtractor.from_pretrained("CompVis/stable-diffusion-safety-checker")

# 绑定到pipeline
pipeline.safety_checker = safety_checker
pipeline.feature_extractor = feature_extractor

# 修正过时属性访问的警告
pipeline.config.requires_safety_checker = True

pipeline.to("cpu")

prompt = "a photo of a knight riding a horse on icy lake"
image = pipeline(prompt).images[0]
image.save("result.png")
print("result.png created")

方法二:完全禁用安全检查器(仅用于非公开场景)

若不需要安全过滤,需同时禁用安全检查器并关闭违规图像替换逻辑,同时修正警告问题:

from diffusers import StableDiffusionPipeline

pipeline = StableDiffusionPipeline.from_single_file("./v2-1_768-ema-pruned.ckpt")

# 禁用安全检查器
pipeline.safety_checker = None
# 修正过时的配置属性
pipeline.config.requires_safety_checker = False

pipeline.to("cpu")

prompt = "a photo of a knight riding a horse on icy lake"
image = pipeline(prompt).images[0]
image.save("result.png")
print("result.png created")

注意事项

  • 禁用安全检查器需遵守Stable Diffusion许可证,仅可用于非公开的个人测试、模型分析等场景,不可用于面向公众的服务。
  • 若加载安全检查器后仍生成空白图,可能是prompt被误判,可尝试调整prompt内容。

内容的提问来源于stack exchange,提问作者Niu Bee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 14:15:19