FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ControlNet/gradio_normal2image.py at main · Coderx7/ControlNet · GitHub
Coderx7
/
ControlNet
Public
forked from
lllyasviel/ControlNet
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ControlNet
/
gradio_normal2image.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (64 loc) · 4.15 KB
Breadcrumbs
ControlNet
/
gradio_normal2image.py
Copy path
File metadata and controls
80 lines (64 loc) · 4.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
cv2
import
einops
import
gradio
as
gr
import
numpy
as
np
import
torch
from
cldm
.
hack
import
disable_verbosity
disable_verbosity
()
from
pytorch_lightning
import
seed_everything
from
annotator
.
util
import
resize_image
,
HWC3
from
annotator
.
midas
import
apply_midas
from
cldm
.
model
import
create_model
,
load_state_dict
from
ldm
.
models
.
diffusion
.
ddim
import
DDIMSampler
model
=
create_model
(
'./models/cldm_v15.yaml'
).
cuda
()
model
.
load_state_dict
(
load_state_dict
(
'./models/control_sd15_normal.pth'
,
location
=
'cuda'
))
ddim_sampler
=
DDIMSampler
(
model
)
def
process
(
input_image
,
prompt
,
a_prompt
,
n_prompt
,
num_samples
,
image_resolution
,
detect_resolution
,
ddim_steps
,
scale
,
seed
,
eta
,
bg_threshold
):
with
torch
.
no_grad
():
input_image
=
HWC3
(
input_image
)
_
,
detected_map
=
apply_midas
(
resize_image
(
input_image
,
detect_resolution
),
bg_th
=
bg_threshold
)
detected_map
=
HWC3
(
detected_map
)
img
=
resize_image
(
input_image
,
image_resolution
)
H
,
W
,
C
=
img
.
shape
detected_map
=
cv2
.
resize
(
detected_map
, (
W
,
H
),
interpolation
=
cv2
.
INTER_LINEAR
)
control
=
torch
.
from_numpy
(
detected_map
[:, :, ::
-
1
].
copy
()).
float
().
cuda
()
/
255.0
control
=
torch
.
stack
([
control
for
_
in
range
(
num_samples
)],
dim
=
0
)
control
=
einops
.
rearrange
(
control
,
'b h w c -> b c h w'
).
clone
()
seed_everything
(
seed
)
cond
=
{
"c_concat"
: [
control
],
"c_crossattn"
: [
model
.
get_learned_conditioning
([
prompt
+
', '
+
a_prompt
]
*
num_samples
)]}
un_cond
=
{
"c_concat"
: [
control
],
"c_crossattn"
: [
model
.
get_learned_conditioning
([
n_prompt
]
*
num_samples
)]}
shape
=
(
4
,
H
//
8
,
W
//
8
)
samples
,
intermediates
=
ddim_sampler
.
sample
(
ddim_steps
,
num_samples
,
shape
,
cond
,
verbose
=
False
,
eta
=
eta
,
unconditional_guidance_scale
=
scale
,
unconditional_conditioning
=
un_cond
)
x_samples
=
model
.
decode_first_stage
(
samples
)
x_samples
=
(
einops
.
rearrange
(
x_samples
,
'b c h w -> b h w c'
)
*
127.5
+
127.5
).
cpu
().
numpy
().
clip
(
0
,
255
).
astype
(
np
.
uint8
)
results
=
[
x_samples
[
i
]
for
i
in
range
(
num_samples
)]
return
[
detected_map
]
+
results
block
=
gr
.
Blocks
().
queue
()
with
block
:
with
gr
.
Row
():
gr
.
Markdown
(
"## Control Stable Diffusion with Normal Maps"
)
with
gr
.
Row
():
with
gr
.
Column
():
input_image
=
gr
.
Image
(
source
=
'upload'
,
type
=
"numpy"
)
prompt
=
gr
.
Textbox
(
label
=
"Prompt"
)
run_button
=
gr
.
Button
(
label
=
"Run"
)
with
gr
.
Accordion
(
"Advanced options"
,
open
=
False
):
num_samples
=
gr
.
Slider
(
label
=
"Images"
,
minimum
=
1
,
maximum
=
12
,
value
=
1
,
step
=
1
)
image_resolution
=
gr
.
Slider
(
label
=
"Image Resolution"
,
minimum
=
256
,
maximum
=
768
,
value
=
512
,
step
=
256
)
detect_resolution
=
gr
.
Slider
(
label
=
"Normal Resolution"
,
minimum
=
128
,
maximum
=
1024
,
value
=
384
,
step
=
1
)
bg_threshold
=
gr
.
Slider
(
label
=
"Normal background threshold"
,
minimum
=
0.0
,
maximum
=
1.0
,
value
=
0.4
,
step
=
0.01
)
ddim_steps
=
gr
.
Slider
(
label
=
"Steps"
,
minimum
=
1
,
maximum
=
100
,
value
=
20
,
step
=
1
)
scale
=
gr
.
Slider
(
label
=
"Guidance Scale"
,
minimum
=
0.1
,
maximum
=
30.0
,
value
=
9.0
,
step
=
0.1
)
seed
=
gr
.
Slider
(
label
=
"Seed"
,
minimum
=
0
,
maximum
=
2147483647
,
step
=
1
,
randomize
=
True
)
eta
=
gr
.
Number
(
label
=
"eta (DDIM)"
,
value
=
0.0
)
a_prompt
=
gr
.
Textbox
(
label
=
"Added Prompt"
,
value
=
'best quality, extremely detailed'
)
n_prompt
=
gr
.
Textbox
(
label
=
"Negative Prompt"
,
value
=
'longbody, lowres, bad anatomy, bad hands, missing fingers, pubic hair,extra digit, fewer digits, cropped, worst quality, low quality'
)
with
gr
.
Column
():
result_gallery
=
gr
.
Gallery
(
label
=
'Output'
,
show_label
=
False
,
elem_id
=
"gallery"
).
style
(
grid
=
2
,
height
=
'auto'
)
ips
=
[
input_image
,
prompt
,
a_prompt
,
n_prompt
,
num_samples
,
image_resolution
,
detect_resolution
,
ddim_steps
,
scale
,
seed
,
eta
,
bg_threshold
]
run_button
.
click
(
fn
=
process
,
inputs
=
ips
,
outputs
=
[
result_gallery
])
block
.
launch
(
server_name
=
'0.0.0.0'
)
Back
|
FazBrowse Home
|
New Git URL