Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to change the strength of safety_checker #9068

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

suzukimain
Copy link
Contributor

What does this PR do?

Fixes #9003

"""
About safety_Level.
`int` or `float` or one of the following
'WEAK',
'MEDIUM',
'NOMAL',
'STRONG',
'MAX'.
"""

#To see the filter strength.
pipe.filter_level() # 0.0 (default)

#--------------
#If you want to change the intensity.
pipe.safety_checker_level("STRONG")
pipe.filter_level() # 1.0

#--------------
# If numbers are used
pipe.safety_checker_level(3.0)
pipe.filter_level() # 3.0

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@suzukimain
Copy link
Contributor Author

suzukimain commented Aug 3, 2024

However, I could not solve the following two problems by myself.

1. regarding the warning message when safety_checker is weakened, the message when safety_checker=None is passed is used as is.
(I was going to put in a modified message, but I was worried, so I decided it was better not to change it and copied it as is.)
Resolved in #9404.


  1. for the number to replace when a string such as 'NOMAL' is entered, I have set the following, but I am not sure if this is the best value.
"WEAK": -1.0
"MEDIUM": -0.5
"NOMAL": 0.0
"STRONG": 0.5
"MAX": 1.0

Please let me know if there are other problems.
thank you

@a-r-r-o-w a-r-r-o-w requested a review from yiyixuxu August 3, 2024 15:55
@suzukimain
Copy link
Contributor Author

Hello, @yiyixuxu
Is there any problem?
I hope to not have caused any inconvenience. thank you for your cooperation.

@yiyixuxu
Copy link
Collaborator

Hi @suzukimain
what would be a use case to adjust the strength of safety_checker?

@suzukimain
Copy link
Contributor Author

Hi, @yiyixuxu

It is intended to be used in classes such as StableDiffusionPipelineSafe and StableDiffusionImg2ImgPipeline, which inherit from DiffusionPipeline and take safety_checker as an argument.
Also, the module name may need to be changed to something more descriptive,
although I couldn't come up with a good idea myself.

pip install git+https://github.com/suzukimain/diffusers@safety_checker

from diffusers import StableDiffusionPipeline
import torch

pipe = StableDiffusionPipeline.from_pretrained(
    "stable-diffusion-v1-5/stable-diffusion-v1-5",
    torch_dtype=torch.float16
    ).to("cuda")

#To see the filter strength.
print(f"Default filter strength: {pipe.filter_level()}") # 0.0

pipe.safety_checker_level("STRONG")

print(f"Filter strength: {pipe.filter_level()}") # 0.5

img=pipe("An image of a squirrel in Picasso style").images[0]
img

@yiyixuxu
Copy link
Collaborator

thanks I'm trying to understand whether it would be a common/meaningful use case that people need this feature -
could you explain a little bit?

@suzukimain
Copy link
Contributor Author

thanks I'm trying to understand whether it would be a common/meaningful use case that people need this feature - could you explain a little bit?

Currently, the safety checker only allows for enabling or disabling its functionality.
This means that when the filtering strength feels too weak, there's no way to increase it, and conversely, when the filtering strength feels too strong, the only option is to disable it.
This can be inconvenient, so it would be beneficial to provide users with the ability to adjust the filtering strength flexibly.
Additionally, it might be helpful to refer to #5623

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to select the strength of the safety_checker
2 participants