LoginSignup
4
5

More than 1 year has passed since last update.

DiffusersでNSFW警告からの黒画像を無効にする方法 [Potential NSFW content was detected in one or more images. A black image will be returned instead. Try again with a different prompt and/or seed.]

Last updated at Posted at 2023-02-23

はじめに

diffusersを使って画像を生成しようとするとモデルによってはNSFW検出されてしまい黒く塗りつぶされてしまう。
今回はそれの回避方法

探してみると、モジュールのPythonを書き換える方法が見当たるが、pipが使えなくなってしまうので無し。

公式的な方法もなく、いろいろ探していると、まともなやつが出てきたので備忘録

コード

以下のコードでパイプライン生成後にsafety_checker()を上書きする

pipe = StableDiffusionPipeline.from_pretrained(MODEL_PATH).to("cuda")
if pipe.safety_checker is not None:
    pipe.safety_checker = lambda images, **kwargs: (images, False)

そうすることでセーフティチェッカがダミー関数に置き換わって、そのまま出してくれる。

参考

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5