SDXL Safety Checker

I’m trying to specify a safety checker in SDXL with:

pipe = DiffusionPipeline.from_pretrained(model, torch_dtype = torch.float16, variant="fp16", safety_checker=??)

I was able to get it to work on 1.5 with the below:

pipe = StableDiffusionPipeline.from_pretrained(model_path, scheduler=scheduler, safety_checker=StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker"), feature_extractor=CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch32"))

Is there a safety checker for SDXL out yet? Thanks for any help!

1 Like

@sayakpaul could you plese respond on this ,

I was also need the safety checker for sdxl

hi @Sandeep5252525 ,

Yes, the SDXL pipeline on diffusers does not include the SafetyChecker implemented in previous versions. Here is how you can use the same code with your SDXL pipeline:

from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
from transformers import CLIPFeatureExtractor
import numpy as np
import torch
from PIL import Image 
from typing import Optional, Tuple, Union

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch_device = device
torch_dtype = torch.float16

safety_checker = StableDiffusionSafetyChecker.from_pretrained(
    "CompVis/stable-diffusion-safety-checker"
).to(device)
feature_extractor = CLIPFeatureExtractor.from_pretrained(
    "openai/clip-vit-base-patch32"
)

def check_nsfw_images(
    images: list[Image.Image],
    output_type: str | None = "pil"
) -> tuple[list[Image.Image], list[bool]]:
    safety_checker_input = feature_extractor(images, return_tensors="pt").to(device)
    images_np = [np.array(img) for img in images]

    _, has_nsfw_concepts = safety_checker(
        images=images_np,
        clip_input=safety_checker_input.pixel_values.to(torch_device),
    )
    if output_type == "pil":
      return images, has_nsfw_concepts
    return images_np, has_nsfw_concepts