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
04b30822
Commit
04b30822
authored
May 31, 2024
by
comfyanonymous
Browse files
Small refactor of preview code.
parent
bf3e334d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
13 deletions
+10
-13
latent_preview.py
latent_preview.py
+10
-13
No files found.
latent_preview.py
View file @
04b30822
...
@@ -11,6 +11,13 @@ import logging
...
@@ -11,6 +11,13 @@ import logging
MAX_PREVIEW_RESOLUTION
=
512
MAX_PREVIEW_RESOLUTION
=
512
def
preview_to_image
(
latent_image
):
latents_ubyte
=
(((
latent_image
+
1.0
)
/
2.0
).
clamp
(
0
,
1
)
# change scale from -1..1 to 0..1
.
mul
(
0xFF
)
# to 0..255
).
to
(
device
=
"cpu"
,
dtype
=
torch
.
uint8
,
non_blocking
=
comfy
.
model_management
.
device_supports_non_blocking
(
latent_image
.
device
))
return
Image
.
fromarray
(
latents_ubyte
.
numpy
())
class
LatentPreviewer
:
class
LatentPreviewer
:
def
decode_latent_to_preview
(
self
,
x0
):
def
decode_latent_to_preview
(
self
,
x0
):
pass
pass
...
@@ -24,12 +31,8 @@ class TAESDPreviewerImpl(LatentPreviewer):
...
@@ -24,12 +31,8 @@ class TAESDPreviewerImpl(LatentPreviewer):
self
.
taesd
=
taesd
self
.
taesd
=
taesd
def
decode_latent_to_preview
(
self
,
x0
):
def
decode_latent_to_preview
(
self
,
x0
):
x_sample
=
self
.
taesd
.
decode
(
x0
[:
1
])[
0
].
detach
()
x_sample
=
self
.
taesd
.
decode
(
x0
[:
1
])[
0
].
movedim
(
0
,
2
)
x_sample
=
255.
*
torch
.
clamp
((
x_sample
+
1.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
return
preview_to_image
(
x_sample
)
x_sample
=
np
.
moveaxis
(
x_sample
.
to
(
device
=
"cpu"
,
dtype
=
torch
.
uint8
,
non_blocking
=
comfy
.
model_management
.
device_supports_non_blocking
(
x_sample
.
device
)).
numpy
(),
0
,
2
)
preview_image
=
Image
.
fromarray
(
x_sample
)
return
preview_image
class
Latent2RGBPreviewer
(
LatentPreviewer
):
class
Latent2RGBPreviewer
(
LatentPreviewer
):
...
@@ -39,13 +42,7 @@ class Latent2RGBPreviewer(LatentPreviewer):
...
@@ -39,13 +42,7 @@ class Latent2RGBPreviewer(LatentPreviewer):
def
decode_latent_to_preview
(
self
,
x0
):
def
decode_latent_to_preview
(
self
,
x0
):
self
.
latent_rgb_factors
=
self
.
latent_rgb_factors
.
to
(
dtype
=
x0
.
dtype
,
device
=
x0
.
device
)
self
.
latent_rgb_factors
=
self
.
latent_rgb_factors
.
to
(
dtype
=
x0
.
dtype
,
device
=
x0
.
device
)
latent_image
=
x0
[
0
].
permute
(
1
,
2
,
0
)
@
self
.
latent_rgb_factors
latent_image
=
x0
[
0
].
permute
(
1
,
2
,
0
)
@
self
.
latent_rgb_factors
return
preview_to_image
(
latent_image
)
latents_ubyte
=
(((
latent_image
+
1
)
/
2
)
.
clamp
(
0
,
1
)
# change scale from -1..1 to 0..1
.
mul
(
0xFF
)
# to 0..255
).
to
(
device
=
"cpu"
,
dtype
=
torch
.
uint8
,
non_blocking
=
comfy
.
model_management
.
device_supports_non_blocking
(
latent_image
.
device
))
return
Image
.
fromarray
(
latents_ubyte
.
numpy
())
def
get_previewer
(
device
,
latent_format
):
def
get_previewer
(
device
,
latent_format
):
...
...
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