一、简介
目前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-censor
https://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_checker
https://huggingface.co/CompVis/stable-diffusion-v1-4/tree/main/safety_checker三、实现效果
当检测到prompt或者图片含有不健康的内容时,输出的图片将会被打上黑码具体效果如下:


但在生成的时候控制台还是会出现模糊的不健康预览,只是到最后图片生成的时候调用了safety_check把整个图片涂黑,这个需要注意
文章来源于互联网:Stable Diffusion健康审查机制
相关推荐: AI绘画Stable Diffusion,手把手教你反推提示词
“ AI绘画,学会生成卡通人物的工具后,通过一些高级提示词,就能生成质量不错的图片,掌握个人快速变现能力……” 经常在小红书或其他公众号上看到别人家的图片内容很高级,等自己去写提示词又提笔忘字。 不是因为你英文不好,主要是没有具体概念。有没有工具可以一键获取别…
5bei.cn大模型教程网










