Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
b5a8b99d
Commit
b5a8b99d
authored
Sep 13, 2022
by
AUTOMATIC
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
put safety checker into a separate file because it's already crowded in processing
parent
b03bc4e7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
34 deletions
+44
-34
modules/processing.py
modules/processing.py
+2
-34
modules/safety.py
modules/safety.py
+42
-0
No files found.
modules/processing.py
View file @
b5a8b99d
...
...
@@ -19,20 +19,11 @@ import modules.face_restoration
import
modules.images
as
images
import
modules.styles
from
diffusers.pipelines.stable_diffusion.safety_checker
import
StableDiffusionSafetyChecker
from
transformers
import
AutoFeatureExtractor
# load safety model
safety_model_id
=
"CompVis/stable-diffusion-safety-checker"
safety_feature_extractor
=
None
safety_checker
=
None
# some of those options should not be changed at all because they would break the model, so I removed them from options.
opt_C
=
4
opt_f
=
8
class
StableDiffusionProcessing
:
def
__init__
(
self
,
sd_model
=
None
,
outpath_samples
=
None
,
outpath_grids
=
None
,
prompt
=
""
,
prompt_style
=
"None"
,
seed
=-
1
,
subseed
=-
1
,
subseed_strength
=
0
,
seed_resize_from_h
=-
1
,
seed_resize_from_w
=-
1
,
sampler_index
=
0
,
batch_size
=
1
,
n_iter
=
1
,
steps
=
50
,
cfg_scale
=
7.0
,
width
=
512
,
height
=
512
,
restore_faces
=
False
,
tiling
=
False
,
do_not_save_samples
=
False
,
do_not_save_grid
=
False
,
extra_generation_params
=
None
,
overlay_images
=
None
,
negative_prompt
=
None
):
self
.
sd_model
=
sd_model
...
...
@@ -154,28 +145,6 @@ def fix_seed(p):
p
.
subseed
=
int
(
random
.
randrange
(
4294967294
))
if
p
.
subseed
is
None
or
p
.
subseed
==
-
1
else
p
.
subseed
def
numpy_to_pil
(
images
):
"""
Convert a numpy image or a batch of images to a PIL image.
"""
if
images
.
ndim
==
3
:
images
=
images
[
None
,
...
]
images
=
(
images
*
255
)
.
round
()
.
astype
(
"uint8"
)
pil_images
=
[
Image
.
fromarray
(
image
)
for
image
in
images
]
return
pil_images
# check and replace nsfw content
def
check_safety
(
x_image
):
global
safety_feature_extractor
,
safety_checker
if
safety_feature_extractor
is
None
:
safety_feature_extractor
=
AutoFeatureExtractor
.
from_pretrained
(
safety_model_id
)
safety_checker
=
StableDiffusionSafetyChecker
.
from_pretrained
(
safety_model_id
)
safety_checker_input
=
safety_feature_extractor
(
numpy_to_pil
(
x_image
),
return_tensors
=
"pt"
)
x_checked_image
,
has_nsfw_concept
=
safety_checker
(
images
=
x_image
,
clip_input
=
safety_checker_input
.
pixel_values
)
return
x_checked_image
,
has_nsfw_concept
def
process_images
(
p
:
StableDiffusionProcessing
)
->
Processed
:
"""this is the main loop that both txt2img and img2img use; it calls func_init once inside all the scopes and func_sample once per batch"""
...
...
@@ -279,9 +248,8 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
x_samples_ddim
=
torch
.
clamp
((
x_samples_ddim
+
1.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
if
opts
.
filter_nsfw
:
x_samples_ddim_numpy
=
x_samples_ddim
.
cpu
()
.
permute
(
0
,
2
,
3
,
1
)
.
numpy
()
x_checked_image
,
has_nsfw_concept
=
check_safety
(
x_samples_ddim_numpy
)
x_samples_ddim
=
torch
.
from_numpy
(
x_checked_image
)
.
permute
(
0
,
3
,
1
,
2
)
import
modules.safety
as
safety
x_samples_ddim
=
modules
.
safety
.
censor_batch
(
x_samples_ddim
)
for
i
,
x_sample
in
enumerate
(
x_samples_ddim
):
x_sample
=
255.
*
np
.
moveaxis
(
x_sample
.
cpu
()
.
numpy
(),
0
,
2
)
...
...
modules/safety.py
0 → 100644
View file @
b5a8b99d
import
torch
from
diffusers.pipelines.stable_diffusion.safety_checker
import
StableDiffusionSafetyChecker
from
transformers
import
AutoFeatureExtractor
from
PIL
import
Image
import
modules.shared
as
shared
safety_model_id
=
"CompVis/stable-diffusion-safety-checker"
safety_feature_extractor
=
None
safety_checker
=
None
def
numpy_to_pil
(
images
):
"""
Convert a numpy image or a batch of images to a PIL image.
"""
if
images
.
ndim
==
3
:
images
=
images
[
None
,
...
]
images
=
(
images
*
255
)
.
round
()
.
astype
(
"uint8"
)
pil_images
=
[
Image
.
fromarray
(
image
)
for
image
in
images
]
return
pil_images
# check and replace nsfw content
def
check_safety
(
x_image
):
global
safety_feature_extractor
,
safety_checker
if
safety_feature_extractor
is
None
:
safety_feature_extractor
=
AutoFeatureExtractor
.
from_pretrained
(
safety_model_id
)
safety_checker
=
StableDiffusionSafetyChecker
.
from_pretrained
(
safety_model_id
)
safety_checker_input
=
safety_feature_extractor
(
numpy_to_pil
(
x_image
),
return_tensors
=
"pt"
)
x_checked_image
,
has_nsfw_concept
=
safety_checker
(
images
=
x_image
,
clip_input
=
safety_checker_input
.
pixel_values
)
return
x_checked_image
,
has_nsfw_concept
def
censor_batch
(
x
):
x_samples_ddim_numpy
=
x
.
cpu
()
.
permute
(
0
,
2
,
3
,
1
)
.
numpy
()
x_checked_image
,
has_nsfw_concept
=
check_safety
(
x_samples_ddim_numpy
)
x
=
torch
.
from_numpy
(
x_checked_image
)
.
permute
(
0
,
3
,
1
,
2
)
return
x
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment