How to disable stable-diffusion's safety filter


reading time 4 min

You may have read Run Stable Diffusion on your M1 Mac’s GPU. (The Linux equivalent is here.)

Here are a few code changes you might wish to make:

Remove the safety filter:

You may receive an error such as:

“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.”

You can disable that check as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ diff -cw  venv/lib/python3.10/site-packages/diffusers/pipelines/stable_diffusion/safety_checker.py-OLD venv/lib/python3.10/site-packages/diffusers/pipelines/stable_diffusion/safety_checker.py
*** venv/lib/python3.10/site-packages/diffusers/pipelines/stable_diffusion/safety_checker.py-OLD	Mon Sep  5 10:31:55 2022
--- venv/lib/python3.10/site-packages/diffusers/pipelines/stable_diffusion/safety_checker.py	Sun Sep  4 09:20:09 2022
***************
*** 65,71 ****

              result.append(result_img)

!         has_nsfw_concepts = [len(res["bad_concepts"]) > 0 for res in result]

          for idx, has_nsfw_concept in enumerate(has_nsfw_concepts):
              if has_nsfw_concept:
--- 65,72 ----

              result.append(result_img)

!         #has_nsfw_concepts = [len(res["bad_concepts"]) > 0 for res in result]
!         has_nsfw_concepts = [False for res in result]

          for idx, has_nsfw_concept in enumerate(has_nsfw_concepts):
              if has_nsfw_concept:

There are probably more elegant ways to disable it, but this works.

Remove some verbosity

Some of the output is quite verbose and isn’t needed by the casual user. Here’s two warnings that I’ve removed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
$ diff -cw venv/lib/python3.10/site-packages/diffusers/modeling_utils.py venv/lib/python3.10/site-packages/diffusers/modeling_utils.py
*** venv/lib/python3.10/site-packages/diffusers/modeling_utils.py	Mon Sep  5 10:31:55 2022
--- venv/lib/python3.10/site-packages/diffusers/modeling_utils.py	Mon Sep  5 10:41:35 2022
***************
*** 500,515 ****
              raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")

          if len(unexpected_keys) > 0:
!             logger.warning(
!                 f"Some weights of the model checkpoint at {pretrained_model_name_or_path} were not used when"
!                 f" initializing {model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are"
!                 f" initializing {model.__class__.__name__} from the checkpoint of a model trained on another task"
!                 " or with another architecture (e.g. initializing a BertForSequenceClassification model from a"
!                 " BertForPreTraining model).\n- This IS NOT expected if you are initializing"
!                 f" {model.__class__.__name__} from the checkpoint of a model that you expect to be exactly"
!                 " identical (initializing a BertForSequenceClassification model from a"
!                 " BertForSequenceClassification model)."
!             )
          else:
              logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n")
          if len(missing_keys) > 0:
--- 500,506 ----
              raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")

          if len(unexpected_keys) > 0:
!             pass
          else:
              logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n")
          if len(missing_keys) > 0:
$ diff -cw venv/lib/python3.10/site-packages/transformers/modeling_flax_utils.py venv/lib/python3.10/site-packages/transformers/modeling_flax_utils.py
*** venv/lib/python3.10/site-packages/transformers/modeling_flax_utils.py	Mon Sep  5 10:31:54 2022
--- venv/lib/python3.10/site-packages/transformers/modeling_flax_utils.py	Mon Sep  5 10:42:19 2022
***************
*** 673,686 ****
              del state[unexpected_key]

          if len(unexpected_keys) > 0:
!             logger.warning(
!                 f"Some weights of the model checkpoint at {pretrained_model_name_or_path} were not used when "
!                 f"initializing {model.__class__.__name__}: {unexpected_keys}\n"
!                 f"- This IS expected if you are initializing {model.__class__.__name__} from the checkpoint of a model trained on another task "
!                 f"or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n"
!                 f"- This IS NOT expected if you are initializing {model.__class__.__name__} from the checkpoint of a model that you expect "
!                 f"to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model)."
!             )
          else:
              logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n")

--- 673,679 ----
              del state[unexpected_key]

          if len(unexpected_keys) > 0:
!             pass
          else:
              logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n")

$ diff -cw venv/lib/python3.10/site-packages/transformers/modeling_utils.py venv/lib/python3.10/site-packages/transformers/modeling_utils.py
*** venv/lib/python3.10/site-packages/transformers/modeling_utils.py	Mon Sep  5 10:31:54 2022
--- venv/lib/python3.10/site-packages/transformers/modeling_utils.py	Mon Sep  5 10:41:56 2022
***************
*** 2251,2264 ****
              raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")

          if len(unexpected_keys) > 0:
!             logger.warning(
!                 f"Some weights of the model checkpoint at {pretrained_model_name_or_path} were not used when "
!                 f"initializing {model.__class__.__name__}: {unexpected_keys}\n"
!                 f"- This IS expected if you are initializing {model.__class__.__name__} from the checkpoint of a model trained on another task "
!                 f"or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n"
!                 f"- This IS NOT expected if you are initializing {model.__class__.__name__} from the checkpoint of a model that you expect "
!                 f"to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model)."
!             )
          else:
              logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n")
          if len(missing_keys) > 0:
--- 2251,2257 ----
              raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")

          if len(unexpected_keys) > 0:
!             pass
          else:
              logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n")
          if len(missing_keys) > 0:

Those messages are fairly informative if you are modifying the model or doing other development work. If you are just recreationally generating images, you won’t miss them.




Tom Limoncelli

Tom Limoncelli

Recent Posts


  1. Harris 2024
  2. Postcards for Democracy
  3. Mrs. Creiger Was Calm
  4. Pride Rocks – New Jersey
  5. Configuring iPhone/MacOS to sync contacts

Archive


Categories


Tags


I agree that this website may store my data to personalize my journey in accordance with their Terms & conditions

Powered by Hugo | Theme - YesThatTheme © 2017 - 2024 Tom Limoncelli