Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
ComfyUI
Commits
a74e176a
Commit
a74e176a
authored
May 03, 2023
by
comfyanonymous
Browse files
Merge branch 'tiled-progress' of
https://github.com/pythongosssss/ComfyUI
parents
908dc1d5
5eeecf3f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
8 deletions
+28
-8
comfy/sd.py
comfy/sd.py
+13
-6
comfy/utils.py
comfy/utils.py
+9
-1
comfy_extras/nodes_upscale_model.py
comfy_extras/nodes_upscale_model.py
+6
-1
No files found.
comfy/sd.py
View file @
a74e176a
...
@@ -514,11 +514,14 @@ class VAE:
...
@@ -514,11 +514,14 @@ class VAE:
self
.
device
=
device
self
.
device
=
device
def
decode_tiled_
(
self
,
samples
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
16
):
def
decode_tiled_
(
self
,
samples
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
16
):
steps
=
samples
.
shape
[
0
]
*
utils
.
get_tiled_scale_steps
(
samples
.
shape
[
3
],
samples
.
shape
[
2
],
tile_x
,
tile_y
,
overlap
)
pbar
=
utils
.
ProgressBar
(
steps
)
decode_fn
=
lambda
a
:
(
self
.
first_stage_model
.
decode
(
1.
/
self
.
scale_factor
*
a
.
to
(
self
.
device
))
+
1.0
)
decode_fn
=
lambda
a
:
(
self
.
first_stage_model
.
decode
(
1.
/
self
.
scale_factor
*
a
.
to
(
self
.
device
))
+
1.0
)
output
=
torch
.
clamp
((
output
=
torch
.
clamp
((
(
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
8
)
+
(
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
8
,
pbar
=
pbar
)
+
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
8
)
+
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
8
,
pbar
=
pbar
)
+
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
8
))
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
8
,
pbar
=
pbar
))
/
3.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
/
3.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
return
output
return
output
...
@@ -562,9 +565,13 @@ class VAE:
...
@@ -562,9 +565,13 @@ class VAE:
model_management
.
unload_model
()
model_management
.
unload_model
()
self
.
first_stage_model
=
self
.
first_stage_model
.
to
(
self
.
device
)
self
.
first_stage_model
=
self
.
first_stage_model
.
to
(
self
.
device
)
pixel_samples
=
pixel_samples
.
movedim
(
-
1
,
1
).
to
(
self
.
device
)
pixel_samples
=
pixel_samples
.
movedim
(
-
1
,
1
).
to
(
self
.
device
)
samples
=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
)
steps
=
utils
.
get_tiled_scale_steps
(
pixel_samples
.
shape
[
3
],
pixel_samples
.
shape
[
2
],
tile_x
,
tile_y
,
overlap
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
)
pbar
=
utils
.
ProgressBar
(
steps
)
samples
=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
/=
3.0
samples
/=
3.0
self
.
first_stage_model
=
self
.
first_stage_model
.
cpu
()
self
.
first_stage_model
=
self
.
first_stage_model
.
cpu
()
samples
=
samples
.
cpu
()
samples
=
samples
.
cpu
()
...
...
comfy/utils.py
View file @
a74e176a
...
@@ -62,8 +62,14 @@ def common_upscale(samples, width, height, upscale_method, crop):
...
@@ -62,8 +62,14 @@ def common_upscale(samples, width, height, upscale_method, crop):
s
=
samples
s
=
samples
return
torch
.
nn
.
functional
.
interpolate
(
s
,
size
=
(
height
,
width
),
mode
=
upscale_method
)
return
torch
.
nn
.
functional
.
interpolate
(
s
,
size
=
(
height
,
width
),
mode
=
upscale_method
)
def
get_tiled_scale_steps
(
width
,
height
,
tile_x
,
tile_y
,
overlap
):
it_1
=
-
(
height
//
-
(
tile_y
*
2
-
overlap
))
*
-
(
width
//
-
(
tile_x
//
2
-
overlap
))
it_2
=
-
(
height
//
-
(
tile_y
//
2
-
overlap
))
*
-
(
width
//
-
(
tile_x
*
2
-
overlap
))
it_3
=
-
(
height
//
-
(
tile_y
-
overlap
))
*
-
(
width
//
-
(
tile_x
-
overlap
))
return
it_1
+
it_2
+
it_3
@
torch
.
inference_mode
()
@
torch
.
inference_mode
()
def
tiled_scale
(
samples
,
function
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
8
,
upscale_amount
=
4
,
out_channels
=
3
):
def
tiled_scale
(
samples
,
function
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
8
,
upscale_amount
=
4
,
out_channels
=
3
,
pbar
=
None
):
output
=
torch
.
empty
((
samples
.
shape
[
0
],
out_channels
,
round
(
samples
.
shape
[
2
]
*
upscale_amount
),
round
(
samples
.
shape
[
3
]
*
upscale_amount
)),
device
=
"cpu"
)
output
=
torch
.
empty
((
samples
.
shape
[
0
],
out_channels
,
round
(
samples
.
shape
[
2
]
*
upscale_amount
),
round
(
samples
.
shape
[
3
]
*
upscale_amount
)),
device
=
"cpu"
)
for
b
in
range
(
samples
.
shape
[
0
]):
for
b
in
range
(
samples
.
shape
[
0
]):
s
=
samples
[
b
:
b
+
1
]
s
=
samples
[
b
:
b
+
1
]
...
@@ -83,6 +89,8 @@ def tiled_scale(samples, function, tile_x=64, tile_y=64, overlap = 8, upscale_am
...
@@ -83,6 +89,8 @@ def tiled_scale(samples, function, tile_x=64, tile_y=64, overlap = 8, upscale_am
mask
[:,:,:,
mask
.
shape
[
3
]
-
1
-
t
:
mask
.
shape
[
3
]
-
t
]
*=
((
1.0
/
feather
)
*
(
t
+
1
))
mask
[:,:,:,
mask
.
shape
[
3
]
-
1
-
t
:
mask
.
shape
[
3
]
-
t
]
*=
((
1.0
/
feather
)
*
(
t
+
1
))
out
[:,:,
round
(
y
*
upscale_amount
):
round
((
y
+
tile_y
)
*
upscale_amount
),
round
(
x
*
upscale_amount
):
round
((
x
+
tile_x
)
*
upscale_amount
)]
+=
ps
*
mask
out
[:,:,
round
(
y
*
upscale_amount
):
round
((
y
+
tile_y
)
*
upscale_amount
),
round
(
x
*
upscale_amount
):
round
((
x
+
tile_x
)
*
upscale_amount
)]
+=
ps
*
mask
out_div
[:,:,
round
(
y
*
upscale_amount
):
round
((
y
+
tile_y
)
*
upscale_amount
),
round
(
x
*
upscale_amount
):
round
((
x
+
tile_x
)
*
upscale_amount
)]
+=
mask
out_div
[:,:,
round
(
y
*
upscale_amount
):
round
((
y
+
tile_y
)
*
upscale_amount
),
round
(
x
*
upscale_amount
):
round
((
x
+
tile_x
)
*
upscale_amount
)]
+=
mask
if
pbar
is
not
None
:
pbar
.
update
(
1
)
output
[
b
:
b
+
1
]
=
out
/
out_div
output
[
b
:
b
+
1
]
=
out
/
out_div
return
output
return
output
...
...
comfy_extras/nodes_upscale_model.py
View file @
a74e176a
...
@@ -37,7 +37,12 @@ class ImageUpscaleWithModel:
...
@@ -37,7 +37,12 @@ class ImageUpscaleWithModel:
device
=
model_management
.
get_torch_device
()
device
=
model_management
.
get_torch_device
()
upscale_model
.
to
(
device
)
upscale_model
.
to
(
device
)
in_img
=
image
.
movedim
(
-
1
,
-
3
).
to
(
device
)
in_img
=
image
.
movedim
(
-
1
,
-
3
).
to
(
device
)
s
=
comfy
.
utils
.
tiled_scale
(
in_img
,
lambda
a
:
upscale_model
(
a
),
tile_x
=
128
+
64
,
tile_y
=
128
+
64
,
overlap
=
8
,
upscale_amount
=
upscale_model
.
scale
)
tile
=
128
+
64
overlap
=
8
steps
=
-
(
in_img
.
shape
[
2
]
//
-
(
tile
-
overlap
))
*
-
(
in_img
.
shape
[
3
]
//
-
(
tile
-
overlap
))
pbar
=
comfy
.
utils
.
ProgressBar
(
steps
)
s
=
comfy
.
utils
.
tiled_scale
(
in_img
,
lambda
a
:
upscale_model
(
a
),
tile_x
=
tile
,
tile_y
=
tile
,
overlap
=
overlap
,
upscale_amount
=
upscale_model
.
scale
,
pbar
=
pbar
)
upscale_model
.
cpu
()
upscale_model
.
cpu
()
s
=
torch
.
clamp
(
s
.
movedim
(
-
3
,
-
1
),
min
=
0
,
max
=
1.0
)
s
=
torch
.
clamp
(
s
.
movedim
(
-
3
,
-
1
),
min
=
0
,
max
=
1.0
)
return
(
s
,)
return
(
s
,)
...
...
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