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
fd93c759
Commit
fd93c759
authored
Sep 23, 2023
by
comfyanonymous
Browse files
Implement FreeU: Free Lunch in Diffusion U-Net node.
_for_testing->FreeU
parent
afa2399f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
0 deletions
+57
-0
comfy_extras/nodes_freelunch.py
comfy_extras/nodes_freelunch.py
+56
-0
nodes.py
nodes.py
+1
-0
No files found.
comfy_extras/nodes_freelunch.py
0 → 100644
View file @
fd93c759
#code originally taken from: https://github.com/ChenyangSi/FreeU (under MIT License)
import
torch
def
Fourier_filter
(
x
,
threshold
,
scale
):
# FFT
x_freq
=
torch
.
fft
.
fftn
(
x
.
float
(),
dim
=
(
-
2
,
-
1
))
x_freq
=
torch
.
fft
.
fftshift
(
x_freq
,
dim
=
(
-
2
,
-
1
))
B
,
C
,
H
,
W
=
x_freq
.
shape
mask
=
torch
.
ones
((
B
,
C
,
H
,
W
),
device
=
x
.
device
)
crow
,
ccol
=
H
//
2
,
W
//
2
mask
[...,
crow
-
threshold
:
crow
+
threshold
,
ccol
-
threshold
:
ccol
+
threshold
]
=
scale
x_freq
=
x_freq
*
mask
# IFFT
x_freq
=
torch
.
fft
.
ifftshift
(
x_freq
,
dim
=
(
-
2
,
-
1
))
x_filtered
=
torch
.
fft
.
ifftn
(
x_freq
,
dim
=
(
-
2
,
-
1
)).
real
return
x_filtered
.
to
(
x
.
dtype
)
class
FreeU
:
@
classmethod
def
INPUT_TYPES
(
s
):
return
{
"required"
:
{
"model"
:
(
"MODEL"
,),
"b1"
:
(
"FLOAT"
,
{
"default"
:
1.1
,
"min"
:
0.0
,
"max"
:
10.0
,
"step"
:
0.01
}),
"b2"
:
(
"FLOAT"
,
{
"default"
:
1.2
,
"min"
:
0.0
,
"max"
:
10.0
,
"step"
:
0.01
}),
"s1"
:
(
"FLOAT"
,
{
"default"
:
0.9
,
"min"
:
0.0
,
"max"
:
10.0
,
"step"
:
0.01
}),
"s2"
:
(
"FLOAT"
,
{
"default"
:
0.2
,
"min"
:
0.0
,
"max"
:
10.0
,
"step"
:
0.01
}),
}}
RETURN_TYPES
=
(
"MODEL"
,)
FUNCTION
=
"patch"
CATEGORY
=
"_for_testing"
def
patch
(
self
,
model
,
b1
,
b2
,
s1
,
s2
):
def
output_block_patch
(
h
,
hsp
,
transformer_options
):
if
h
.
shape
[
1
]
==
1280
:
h
[:,:
640
]
=
h
[:,:
640
]
*
b1
hsp
=
Fourier_filter
(
hsp
,
threshold
=
1
,
scale
=
s1
)
if
h
.
shape
[
1
]
==
640
:
h
[:,:
320
]
=
h
[:,:
320
]
*
b2
hsp
=
Fourier_filter
(
hsp
,
threshold
=
1
,
scale
=
s2
)
return
h
,
hsp
m
=
model
.
clone
()
m
.
set_model_output_block_patch
(
output_block_patch
)
return
(
m
,
)
NODE_CLASS_MAPPINGS
=
{
"FreeU"
:
FreeU
,
}
nodes.py
View file @
fd93c759
...
@@ -1782,4 +1782,5 @@ def init_custom_nodes():
...
@@ -1782,4 +1782,5 @@ def init_custom_nodes():
load_custom_node
(
os
.
path
.
join
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
"comfy_extras"
),
"nodes_tomesd.py"
))
load_custom_node
(
os
.
path
.
join
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
"comfy_extras"
),
"nodes_tomesd.py"
))
load_custom_node
(
os
.
path
.
join
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
"comfy_extras"
),
"nodes_clip_sdxl.py"
))
load_custom_node
(
os
.
path
.
join
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
"comfy_extras"
),
"nodes_clip_sdxl.py"
))
load_custom_node
(
os
.
path
.
join
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
"comfy_extras"
),
"nodes_canny.py"
))
load_custom_node
(
os
.
path
.
join
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
"comfy_extras"
),
"nodes_canny.py"
))
load_custom_node
(
os
.
path
.
join
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
"comfy_extras"
),
"nodes_freelunch.py"
))
load_custom_nodes
()
load_custom_nodes
()
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