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
54f74d44
Commit
54f74d44
authored
Aug 30, 2022
by
AUTOMATIC
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added first version of inpainting
fixed flag option
parent
587db9c4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
10 deletions
+72
-10
webui.py
webui.py
+72
-10
No files found.
webui.py
View file @
54f74d44
...
@@ -9,7 +9,7 @@ import torch.nn as nn
...
@@ -9,7 +9,7 @@ import torch.nn as nn
import
numpy
as
np
import
numpy
as
np
import
gradio
as
gr
import
gradio
as
gr
from
omegaconf
import
OmegaConf
from
omegaconf
import
OmegaConf
from
PIL
import
Image
,
ImageFont
,
ImageDraw
,
PngImagePlugin
from
PIL
import
Image
,
ImageFont
,
ImageDraw
,
PngImagePlugin
,
ImageFilter
,
ImageOps
from
torch
import
autocast
from
torch
import
autocast
import
mimetypes
import
mimetypes
import
random
import
random
...
@@ -158,6 +158,7 @@ class Options:
...
@@ -158,6 +158,7 @@ class Options:
"samples_save"
:
OptionInfo
(
True
,
"Save indiviual samples"
),
"samples_save"
:
OptionInfo
(
True
,
"Save indiviual samples"
),
"samples_format"
:
OptionInfo
(
'png'
,
'File format for indiviual samples'
),
"samples_format"
:
OptionInfo
(
'png'
,
'File format for indiviual samples'
),
"grid_save"
:
OptionInfo
(
True
,
"Save image grids"
),
"grid_save"
:
OptionInfo
(
True
,
"Save image grids"
),
"return_grid"
:
OptionInfo
(
True
,
"Show grid in results for web"
),
"grid_format"
:
OptionInfo
(
'png'
,
'File format for grids'
),
"grid_format"
:
OptionInfo
(
'png'
,
'File format for grids'
),
"grid_extended_filename"
:
OptionInfo
(
False
,
"Add extended info (seed, prompt) to filename when saving grid"
),
"grid_extended_filename"
:
OptionInfo
(
False
,
"Add extended info (seed, prompt) to filename when saving grid"
),
"grid_only_if_multiple"
:
OptionInfo
(
True
,
"Do not save grids consisting of one picture"
),
"grid_only_if_multiple"
:
OptionInfo
(
True
,
"Do not save grids consisting of one picture"
),
...
@@ -957,6 +958,8 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
...
@@ -957,6 +958,8 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
unwanted_grid_because_of_img_count
=
len
(
output_images
)
<
2
and
opts
.
grid_only_if_multiple
unwanted_grid_because_of_img_count
=
len
(
output_images
)
<
2
and
opts
.
grid_only_if_multiple
if
(
p
.
prompt_matrix
or
opts
.
grid_save
)
and
not
p
.
do_not_save_grid
and
not
unwanted_grid_because_of_img_count
:
if
(
p
.
prompt_matrix
or
opts
.
grid_save
)
and
not
p
.
do_not_save_grid
and
not
unwanted_grid_because_of_img_count
:
return_grid
=
opts
.
return_grid
if
p
.
prompt_matrix
:
if
p
.
prompt_matrix
:
grid
=
image_grid
(
output_images
,
p
.
batch_size
,
rows
=
1
<<
((
len
(
prompt_matrix_parts
)
-
1
)
//
2
))
grid
=
image_grid
(
output_images
,
p
.
batch_size
,
rows
=
1
<<
((
len
(
prompt_matrix_parts
)
-
1
)
//
2
))
...
@@ -967,10 +970,13 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
...
@@ -967,10 +970,13 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
print
(
"Error creating prompt_matrix text:"
,
file
=
sys
.
stderr
)
print
(
"Error creating prompt_matrix text:"
,
file
=
sys
.
stderr
)
print
(
traceback
.
format_exc
(),
file
=
sys
.
stderr
)
print
(
traceback
.
format_exc
(),
file
=
sys
.
stderr
)
output_images
.
insert
(
0
,
grid
)
return_grid
=
True
else
:
else
:
grid
=
image_grid
(
output_images
,
p
.
batch_size
)
grid
=
image_grid
(
output_images
,
p
.
batch_size
)
if
return_grid
:
output_images
.
insert
(
0
,
grid
)
save_image
(
grid
,
p
.
outpath
,
f
"grid-{grid_count:04}"
,
seed
,
prompt
,
opts
.
grid_format
,
info
=
infotext
(),
short_filename
=
not
opts
.
grid_extended_filename
)
save_image
(
grid
,
p
.
outpath
,
f
"grid-{grid_count:04}"
,
seed
,
prompt
,
opts
.
grid_format
,
info
=
infotext
(),
short_filename
=
not
opts
.
grid_extended_filename
)
grid_count
+=
1
grid_count
+=
1
...
@@ -1042,7 +1048,7 @@ class Flagging(gr.FlaggingCallback):
...
@@ -1042,7 +1048,7 @@ class Flagging(gr.FlaggingCallback):
os
.
makedirs
(
"log/images"
,
exist_ok
=
True
)
os
.
makedirs
(
"log/images"
,
exist_ok
=
True
)
# those must match the "txt2img" function
# those must match the "txt2img" function
prompt
,
ddim_steps
,
sampler_name
,
use_gfpgan
,
prompt_matrix
,
ddim_eta
,
n_iter
,
n_samples
,
cfg_scale
,
request_
seed
,
height
,
width
,
code
,
images
,
seed
,
comment
=
flag_data
prompt
,
steps
,
sampler_index
,
use_gfpgan
,
prompt_matrix
,
n_iter
,
batch_size
,
cfg_scale
,
seed
,
height
,
width
,
code
,
images
,
seed
,
comment
=
flag_data
filenames
=
[]
filenames
=
[]
...
@@ -1067,7 +1073,7 @@ class Flagging(gr.FlaggingCallback):
...
@@ -1067,7 +1073,7 @@ class Flagging(gr.FlaggingCallback):
filenames
.
append
(
filename
)
filenames
.
append
(
filename
)
writer
.
writerow
([
prompt
,
seed
,
width
,
height
,
cfg_scale
,
ddim_
steps
,
filenames
[
0
]])
writer
.
writerow
([
prompt
,
seed
,
width
,
height
,
cfg_scale
,
steps
,
filenames
[
0
]])
print
(
"Logged:"
,
filenames
[
0
])
print
(
"Logged:"
,
filenames
[
0
])
...
@@ -1097,27 +1103,64 @@ txt2img_interface = gr.Interface(
...
@@ -1097,27 +1103,64 @@ txt2img_interface = gr.Interface(
flagging_callback
=
Flagging
()
flagging_callback
=
Flagging
()
)
)
def
fill
(
image
,
mask
):
image_mod
=
Image
.
new
(
'RGBA'
,
(
image
.
width
,
image
.
height
))
image_masked
=
Image
.
new
(
'RGBa'
,
(
image
.
width
,
image
.
height
))
image_masked
.
paste
(
image
.
convert
(
"RGBA"
)
.
convert
(
"RGBa"
),
mask
=
ImageOps
.
invert
(
mask
.
convert
(
'L'
)))
image_masked
=
image_masked
.
convert
(
'RGBa'
)
for
radius
,
repeats
in
[(
64
,
1
),
(
16
,
2
),
(
4
,
4
),
(
2
,
2
),
(
0
,
1
)]:
blurred
=
image_masked
.
filter
(
ImageFilter
.
GaussianBlur
(
radius
))
.
convert
(
'RGBA'
)
for
_
in
range
(
repeats
):
image_mod
.
alpha_composite
(
blurred
)
return
image_mod
.
convert
(
"RGB"
)
class
StableDiffusionProcessingImg2Img
(
StableDiffusionProcessing
):
class
StableDiffusionProcessingImg2Img
(
StableDiffusionProcessing
):
sampler
=
None
sampler
=
None
def
__init__
(
self
,
init_images
=
None
,
resize_mode
=
0
,
denoising_strength
=
0.75
,
**
kwargs
):
def
__init__
(
self
,
init_images
=
None
,
resize_mode
=
0
,
denoising_strength
=
0.75
,
mask
=
None
,
mask_blur
=
4
,
**
kwargs
):
super
()
.
__init__
(
**
kwargs
)
super
()
.
__init__
(
**
kwargs
)
self
.
init_images
=
init_images
self
.
init_images
=
init_images
self
.
resize_mode
:
int
=
resize_mode
self
.
resize_mode
:
int
=
resize_mode
self
.
denoising_strength
:
float
=
denoising_strength
self
.
denoising_strength
:
float
=
denoising_strength
self
.
init_latent
=
None
self
.
init_latent
=
None
self
.
original_mask
=
mask
self
.
mask_blur
=
mask_blur
self
.
mask
=
None
self
.
nmask
=
None
def
init
(
self
):
def
init
(
self
):
self
.
sampler
=
samplers_for_img2img
[
self
.
sampler_index
]
.
constructor
()
self
.
sampler
=
samplers_for_img2img
[
self
.
sampler_index
]
.
constructor
()
if
self
.
original_mask
is
not
None
:
if
self
.
mask_blur
>
0
:
self
.
original_mask
=
self
.
original_mask
.
filter
(
ImageFilter
.
GaussianBlur
(
self
.
mask_blur
))
.
convert
(
'L'
)
latmask
=
self
.
original_mask
.
convert
(
'RGB'
)
.
resize
((
64
,
64
))
latmask
=
np
.
moveaxis
(
np
.
array
(
latmask
,
dtype
=
np
.
float
),
2
,
0
)
/
255
latmask
=
latmask
[
0
]
latmask
=
np
.
tile
(
latmask
[
None
],
(
4
,
1
,
1
))
self
.
mask
=
torch
.
asarray
(
1.0
-
latmask
)
.
to
(
device
)
.
type
(
sd_model
.
dtype
)
self
.
nmask
=
torch
.
asarray
(
latmask
)
.
to
(
device
)
.
type
(
sd_model
.
dtype
)
imgs
=
[]
imgs
=
[]
for
img
in
self
.
init_images
:
for
img
in
self
.
init_images
:
image
=
img
.
convert
(
"RGB"
)
image
=
img
.
convert
(
"RGB"
)
image
=
resize_image
(
self
.
resize_mode
,
image
,
self
.
width
,
self
.
height
)
image
=
resize_image
(
self
.
resize_mode
,
image
,
self
.
width
,
self
.
height
)
if
self
.
original_mask
is
not
None
image
=
fill
(
image
,
self
.
original_mask
)
image
=
np
.
array
(
image
)
.
astype
(
np
.
float32
)
/
255.0
image
=
np
.
array
(
image
)
.
astype
(
np
.
float32
)
/
255.0
image
=
np
.
moveaxis
(
image
,
2
,
0
)
image
=
np
.
moveaxis
(
image
,
2
,
0
)
imgs
.
append
(
image
)
imgs
.
append
(
image
)
if
len
(
imgs
)
==
1
:
if
len
(
imgs
)
==
1
:
...
@@ -1139,16 +1182,33 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
...
@@ -1139,16 +1182,33 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
sigmas
=
self
.
sampler
.
model_wrap
.
get_sigmas
(
self
.
steps
)
sigmas
=
self
.
sampler
.
model_wrap
.
get_sigmas
(
self
.
steps
)
noise
=
x
*
sigmas
[
self
.
steps
-
t_enc
-
1
]
noise
=
x
*
sigmas
[
self
.
steps
-
t_enc
-
1
]
xi
=
self
.
init_latent
+
noise
xi
=
self
.
init_latent
+
noise
sigma_sched
=
sigmas
[
self
.
steps
-
t_enc
-
1
:]
sigma_sched
=
sigmas
[
self
.
steps
-
t_enc
-
1
:]
samples_ddim
=
self
.
sampler
.
func
(
self
.
sampler
.
model_wrap_cfg
,
xi
,
sigma_sched
,
extra_args
=
{
'cond'
:
conditioning
,
'uncond'
:
unconditional_conditioning
,
'cond_scale'
:
self
.
cfg_scale
},
disable
=
False
)
#if self.mask is not None:
# xi = xi * self.mask + noise * self.nmask
def
mask_cb
(
v
):
v
[
"denoised"
][:]
=
v
[
"denoised"
][:]
*
self
.
nmask
+
self
.
init_latent
*
self
.
mask
samples_ddim
=
self
.
sampler
.
func
(
self
.
sampler
.
model_wrap_cfg
,
xi
,
sigma_sched
,
extra_args
=
{
'cond'
:
conditioning
,
'uncond'
:
unconditional_conditioning
,
'cond_scale'
:
self
.
cfg_scale
},
disable
=
False
,
callback
=
mask_cb
if
self
.
mask
is
not
None
else
None
)
if
self
.
mask
is
not
None
:
samples_ddim
=
samples_ddim
*
self
.
nmask
+
self
.
init_latent
*
self
.
mask
return
samples_ddim
return
samples_ddim
def
img2img
(
prompt
:
str
,
init_img
,
ddim_steps
:
int
,
sampler_index
:
int
,
use_GFPGAN
:
bool
,
prompt_matrix
,
loopback
:
bool
,
sd_upscale
:
bool
,
n_iter
:
int
,
batch_size
:
int
,
cfg_scale
:
float
,
denoising_strength
:
float
,
seed
:
int
,
height
:
int
,
width
:
int
,
resize_mode
:
int
):
def
img2img
(
prompt
:
str
,
init_img
,
init_img_with_mask
,
ddim_steps
:
int
,
sampler_index
:
int
,
use_GFPGAN
:
bool
,
prompt_matrix
,
loopback
:
bool
,
sd_upscale
:
bool
,
n_iter
:
int
,
batch_size
:
int
,
cfg_scale
:
float
,
denoising_strength
:
float
,
seed
:
int
,
height
:
int
,
width
:
int
,
resize_mode
:
int
):
outpath
=
opts
.
outdir
or
"outputs/img2img-samples"
outpath
=
opts
.
outdir
or
"outputs/img2img-samples"
if
init_img_with_mask
is
not
None
:
image
=
init_img_with_mask
[
'image'
]
mask
=
init_img_with_mask
[
'mask'
]
else
:
image
=
init_img
mask
=
None
assert
0.
<=
denoising_strength
<=
1.
,
'can only work with strength in [0.0, 1.0]'
assert
0.
<=
denoising_strength
<=
1.
,
'can only work with strength in [0.0, 1.0]'
p
=
StableDiffusionProcessingImg2Img
(
p
=
StableDiffusionProcessingImg2Img
(
...
@@ -1164,7 +1224,8 @@ def img2img(prompt: str, init_img, ddim_steps: int, sampler_index: int, use_GFPG
...
@@ -1164,7 +1224,8 @@ def img2img(prompt: str, init_img, ddim_steps: int, sampler_index: int, use_GFPG
height
=
height
,
height
=
height
,
prompt_matrix
=
prompt_matrix
,
prompt_matrix
=
prompt_matrix
,
use_GFPGAN
=
use_GFPGAN
,
use_GFPGAN
=
use_GFPGAN
,
init_images
=
[
init_img
],
init_images
=
[
image
],
mask
=
mask
,
resize_mode
=
resize_mode
,
resize_mode
=
resize_mode
,
denoising_strength
=
denoising_strength
,
denoising_strength
=
denoising_strength
,
extra_generation_params
=
{
"Denoising Strength"
:
denoising_strength
}
extra_generation_params
=
{
"Denoising Strength"
:
denoising_strength
}
...
@@ -1262,7 +1323,8 @@ img2img_interface = gr.Interface(
...
@@ -1262,7 +1323,8 @@ img2img_interface = gr.Interface(
wrap_gradio_call
(
img2img
),
wrap_gradio_call
(
img2img
),
inputs
=
[
inputs
=
[
gr
.
Textbox
(
placeholder
=
"A fantasy landscape, trending on artstation."
,
lines
=
1
),
gr
.
Textbox
(
placeholder
=
"A fantasy landscape, trending on artstation."
,
lines
=
1
),
gr
.
Image
(
value
=
sample_img2img
,
source
=
"upload"
,
interactive
=
True
,
type
=
"pil"
),
gr
.
Image
(
label
=
"Image for img2img"
,
source
=
"upload"
,
interactive
=
True
,
type
=
"pil"
),
gr
.
Image
(
label
=
"Image for inpainting with mask"
,
source
=
"upload"
,
interactive
=
True
,
type
=
"pil"
,
tool
=
"sketch"
),
gr
.
Slider
(
minimum
=
1
,
maximum
=
150
,
step
=
1
,
label
=
"Sampling Steps"
,
value
=
20
),
gr
.
Slider
(
minimum
=
1
,
maximum
=
150
,
step
=
1
,
label
=
"Sampling Steps"
,
value
=
20
),
gr
.
Radio
(
label
=
'Sampling method'
,
choices
=
[
x
.
name
for
x
in
samplers_for_img2img
],
value
=
samplers_for_img2img
[
0
]
.
name
,
type
=
"index"
),
gr
.
Radio
(
label
=
'Sampling method'
,
choices
=
[
x
.
name
for
x
in
samplers_for_img2img
],
value
=
samplers_for_img2img
[
0
]
.
name
,
type
=
"index"
),
gr
.
Checkbox
(
label
=
'Fix faces using GFPGAN'
,
value
=
False
,
visible
=
have_gfpgan
),
gr
.
Checkbox
(
label
=
'Fix faces using GFPGAN'
,
value
=
False
,
visible
=
have_gfpgan
),
...
...
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