Stable Diffusion健康审查机制

一、简介

目前sd官方已经对健康审查代码删除了,为了在某些场合下炼丹过程中不生成奇奇怪怪的东西,就需要重新启用健康审查。

我们软件版本是使用了B站秋叶的整合包,目前3台机器测试均通过。

ps:测试os均为Windows,linux版未经测试

二、步骤

 历史代码本来存在safety_checker机制,但在最新源码已无法查询到,相关历史源码可从各大论坛获取,下图贴上部分实现代码

# safety_model_id = "CompVis/stable-diffusion-safety-checker"
# safety_feature_extractor = AutoFeatureExtractor.from_pretrained(safety_model_id)
# safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id)

# def check_safety(x_image):
#     safety_checker_input = safety_feature_extractor(numpy_to_pil(x_image), return_tensors="pt")
#     x_checked_image, has_nsfw_concept = safety_checker(images=x_image, clip_input=safety_checker_input.pixel_values)
#     assert x_checked_image.shape[0] == len(has_nsfw_concept)
#     for i in range(len(has_nsfw_concept)):
#         if has_nsfw_concept[i]:
#             x_checked_image[i] = load_replacement(x_checked_image[i])
#     return x_checked_image, has_nsfw_concept

健康审核功能复用是作为一个拓展插件加入到sd中,需要用到两个文件:1、审核插件;2、审核模型

其中审核插件可以在github获取,保存到sd的extensions路径下https://github.com/AUTOMATIC1111/stable-diffusion-webui-nsfw-censorhttps://github.com/AUTOMATIC1111/stable-diffusion-webui-nsfw-censor

审核模型可以在huggingface官网获取,新建目录并保存。目录路径为:sd根目录/CompVis/stable-diffusion-safety-checkerhttps://huggingface.co/CompVis/stable-diffusion-v1-4/tree/main/safety_checkerhttps://huggingface.co/CompVis/stable-diffusion-v1-4/tree/main/safety_checker三、实现效果

当检测到prompt或者图片含有不健康的内容时,输出的图片将会被打上黑码具体效果如下:

 

 但在生成的时候控制台还是会出现模糊的不健康预览,只是到最后图片生成的时候调用了safety_check把整个图片涂黑,这个需要注意

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值