当前位置:   article > 正文

Stable Diffusion健康审查机制_stablediffusionsafetychecker

stablediffusionsafetychecker

一、简介

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

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

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

二、步骤

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

  1. # safety_model_id = "CompVis/stable-diffusion-safety-checker"
  2. # safety_feature_extractor = AutoFeatureExtractor.from_pretrained(safety_model_id)
  3. # safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id)
  4. # def check_safety(x_image):
  5. # safety_checker_input = safety_feature_extractor(numpy_to_pil(x_image), return_tensors="pt")
  6. # x_checked_image, has_nsfw_concept = safety_checker(images=x_image, clip_input=safety_checker_input.pixel_values)
  7. # assert x_checked_image.shape[0] == len(has_nsfw_concept)
  8. # for i in range(len(has_nsfw_concept)):
  9. # if has_nsfw_concept[i]:
  10. # x_checked_image[i] = load_replacement(x_checked_image[i])
  11. # 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把整个图片涂黑,这个需要注意

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/838332
推荐阅读
相关标签
  

闽ICP备14008679号