How to enable safety checker in stable-diffusion-2-1 pipeline?
Safety checker is not specified in stabilityai/stable-diffusion-2-1 pipeline:huggingface.comodel_index.json · stabilityai/stable-diffusion-2-1 at mainWe’re on a journey to advance and democratize artificial intelligence through open source and open science.How can I enable safety checker? What Python packages need to be installed? Thanks.
2023-02-07T20:42:19Z
hi@samulik,Since the safety checker is not on the model config, you can do it manuallyfrom transformers import CLIPFeatureExtractor
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker

repo_id = "stabilityai/stable-diffusion-2"
pipe = DiffusionPipeline.from_pretrained(repo_id,
                                         safety_checker=StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker"),
                                         feature_extractor=CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32"))
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")

prompt = "naked austronaut in the beach"
results = pipe(prompt, guidance_scale=9, num_inference_steps=25, num_images_per_prompt=1)

if not results.nsfw_content_detected[0]:
  print("Not NSFW")
  results.images[0].save("astronaut.png")
else:
  print("NSFW Detected")
2023-02-11T20:55:41Z
Hello, I tried this and everything seems to run fine, only nsfw_content_detected=None is being returned for very NSFW images. I pretty much replicated your code. Not sure why it isn’t returning correctly.
2023-02-11T22:16:57Z
hi@caldwecg, if you’re running the code above, you should expected an array of Booleans for thensfw_content_detected=[True, False, False ...]depending the number of generated images per prompt. So if you’re gettingnsfw_content_detected=Nonesomething might be missing
2023-02-11T22:59:10Z
@radamesthanks for the response. I’m using a dreambooth trained model, would that affect the results? I copied your code verbatim and when printing the result object it gave the following:StableDiffusionPipelineOutput(images=[<PIL.Image.Image image mode=RGB size=512x512 at 0x7F2CDC646440>], nsfw_content_detected=None)
2023-02-11T23:26:21Z
just tested the above code with dreamboothed model `repo_id = “nitrosocke/mo-di-diffusion”  and the safety filter works. Could you please share a bit more?
2023-02-12T14:59:01Z
@radamesI’m using Shivam Srirao’s implementation of DreamBooth (Google Colab) and it looks like it uses stable-diffusion-v1-5. Would the safety checker be different for this version? Thanks again for the help.
2023-02-13T16:56:59Z
@caldwecgI don’t believe the safety checker is trained on a specific stable diffusion version
2023-02-13T23:00:58Z
hi@caldwecgin your notebook, just replace the inference cell with thisimport torch
from torch import autocast
from diffusers import StableDiffusionPipeline, DDIMScheduler
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
from IPython.display import display

model_path = WEIGHTS_DIR             # If you want to use previously trained model saved in gdrive, replace this with the full path of model in gdrive

pipe = StableDiffusionPipeline.from_pretrained(model_path,
                                               safety_checker=StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker", torch_dtype=torch.float16),
                                               torch_dtype=torch.float16).to("cuda")
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe.enable_xformers_memory_efficient_attention()
g_cuda = None
2023-02-15T03:43:26Z
hi@radamesI ran it your recent solution and then again on the nitrosocke/mo-di-diffusion with your original solution and still ran into nsfw_content_detected=None. Not sure what else could be different between ours… maybe something now working with the feature_extractor?I also looked at StableDiffusionPipeline and there’s a “requires_safety_checker: bool = False” field, do I need to specify this?
2023-02-15T22:41:49Z
could you please share a reproducible code? Spaces or on Colab thanks
2023-02-20T15:30:57Z
Hi@radamesit ended up being an issue with the version of diffusers I had installed (0.9.0). Upgrading to 0.13.1 worked. Thanks for the help.
2023-12-16T06:35:01Z
hi@radamesdoes this safety feature take additional time, i have a startup, we generate a lots of images, im thinking of trying the safety feature, will it comsume more time. i cannot afford to experiement. its expensive. can you atleast make a guess what extra time it may take? tnks