Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
stable-diffusion
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Qin, Jiuming
stable-diffusion
Commits
d0c714ae
Commit
d0c714ae
authored
2 years ago
by
Patrick von Platen
Browse files
Options
Downloads
Patches
Plain Diff
[Safety Checker] Add Safety Checker Module
parent
7b8c883b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/txt2img.py
+24
-1
24 additions, 1 deletion
scripts/txt2img.py
with
24 additions
and
1 deletion
scripts/txt2img.py
+
24
−
1
View file @
d0c714ae
...
...
@@ -16,12 +16,29 @@ from ldm.util import instantiate_from_config
from
ldm.models.diffusion.ddim
import
DDIMSampler
from
ldm.models.diffusion.plms
import
PLMSSampler
from
diffusers.pipelines.stable_diffusion.safety_checker
import
StableDiffusionSafetyChecker
from
transformers
import
AutoFeatureExtractor
feature_extractor
=
AutoFeatureExtractor
.
from_pretrained
(
"
CompVis/stable-diffusion-v-1-3
"
,
use_auth_token
=
True
)
safety_checker
=
StableDiffusionSafetyChecker
.
from_pretrained
(
"
CompVis/stable-diffusion-v-1-3
"
,
use_auth_token
=
True
)
def
chunk
(
it
,
size
):
it
=
iter
(
it
)
return
iter
(
lambda
:
tuple
(
islice
(
it
,
size
)),
())
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
def
load_model_from_config
(
config
,
ckpt
,
verbose
=
False
):
print
(
f
"
Loading model from
{
ckpt
}
"
)
pl_sd
=
torch
.
load
(
ckpt
,
map_location
=
"
cpu
"
)
...
...
@@ -220,7 +237,9 @@ def main():
if
opt
.
fixed_code
:
start_code
=
torch
.
randn
([
opt
.
n_samples
,
opt
.
C
,
opt
.
H
//
opt
.
f
,
opt
.
W
//
opt
.
f
],
device
=
device
)
print
(
"
start code
"
,
start_code
.
abs
().
sum
())
precision_scope
=
autocast
if
opt
.
precision
==
"
autocast
"
else
nullcontext
precision_scope
=
nullcontext
with
torch
.
no_grad
():
with
precision_scope
(
"
cuda
"
):
with
model
.
ema_scope
():
...
...
@@ -269,7 +288,11 @@ def main():
Image
.
fromarray
(
grid
.
astype
(
np
.
uint8
)).
save
(
os
.
path
.
join
(
outpath
,
f
'
grid-
{
grid_count
:
04
}
.png
'
))
grid_count
+=
1
toc
=
time
.
time
()
image
=
x_samples_ddim
.
cpu
().
permute
(
0
,
2
,
3
,
1
).
numpy
()
# run safety checker
safety_checker_input
=
pipe
.
feature_extractor
(
numpy_to_pil
(
image
),
return_tensors
=
"
pt
"
)
image
,
has_nsfw_concept
=
pipe
.
safety_checker
(
images
=
image
,
clip_input
=
safety_checker_input
.
pixel_values
)
print
(
f
"
Your samples are ready and waiting for you here:
\n
{
outpath
}
\n
"
f
"
\n
Enjoy.
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment