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
d8acd34f
Commit
d8acd34f
authored
Oct 20, 2022
by
AngelBottomless
Committed by
GitHub
Oct 20, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generalized some functions and option for ignoring first layer
parent
f8733ad0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
8 deletions
+15
-8
modules/hypernetworks/hypernetwork.py
modules/hypernetworks/hypernetwork.py
+15
-8
No files found.
modules/hypernetworks/hypernetwork.py
View file @
d8acd34f
...
@@ -21,21 +21,27 @@ from modules.textual_inversion.learn_schedule import LearnRateScheduler
...
@@ -21,21 +21,27 @@ from modules.textual_inversion.learn_schedule import LearnRateScheduler
class
HypernetworkModule
(
torch
.
nn
.
Module
):
class
HypernetworkModule
(
torch
.
nn
.
Module
):
multiplier
=
1.0
multiplier
=
1.0
activation_dict
=
{
"relu"
:
torch
.
nn
.
ReLU
,
"leakyrelu"
:
torch
.
nn
.
LeakyReLU
,
"elu"
:
torch
.
nn
.
ELU
,
"swish"
:
torch
.
nn
.
Hardswish
}
def
__init__
(
self
,
dim
,
state_dict
=
None
,
layer_structure
=
None
,
add_layer_norm
=
False
,
activation_func
=
None
):
def
__init__
(
self
,
dim
,
state_dict
=
None
,
layer_structure
=
None
,
add_layer_norm
=
False
,
activation_func
=
None
):
super
()
.
__init__
()
super
()
.
__init__
()
assert
layer_structure
is
not
None
,
"layer_structure must not be None"
assert
layer_structure
is
not
None
,
"layer_structure must not be None"
assert
layer_structure
[
0
]
==
1
,
"Multiplier Sequence should start with size 1!"
assert
layer_structure
[
0
]
==
1
,
"Multiplier Sequence should start with size 1!"
assert
layer_structure
[
-
1
]
==
1
,
"Multiplier Sequence should end with size 1!"
assert
layer_structure
[
-
1
]
==
1
,
"Multiplier Sequence should end with size 1!"
linears
=
[]
linears
=
[]
for
i
in
range
(
len
(
layer_structure
)
-
1
):
for
i
in
range
(
len
(
layer_structure
)
-
1
):
linears
.
append
(
torch
.
nn
.
Linear
(
int
(
dim
*
layer_structure
[
i
]),
int
(
dim
*
layer_structure
[
i
+
1
])))
linears
.
append
(
torch
.
nn
.
Linear
(
int
(
dim
*
layer_structure
[
i
]),
int
(
dim
*
layer_structure
[
i
+
1
])))
if
activation_func
==
"relu"
:
# if skip_first_layer because first parameters potentially contain negative values
linears
.
append
(
torch
.
nn
.
ReLU
())
if
i
<
1
:
continue
if
activation_func
==
"leakyrelu"
:
if
activation_func
in
HypernetworkModule
.
activation_dict
:
linears
.
append
(
torch
.
nn
.
LeakyReLU
())
linears
.
append
(
HypernetworkModule
.
activation_dict
[
activation_func
]())
else
:
print
(
"Invalid key {} encountered as activation function!"
.
format
(
activation_func
))
# if use_dropout:
linears
.
append
(
torch
.
nn
.
Dropout
(
p
=
0.3
))
if
add_layer_norm
:
if
add_layer_norm
:
linears
.
append
(
torch
.
nn
.
LayerNorm
(
int
(
dim
*
layer_structure
[
i
+
1
])))
linears
.
append
(
torch
.
nn
.
LayerNorm
(
int
(
dim
*
layer_structure
[
i
+
1
])))
...
@@ -46,7 +52,7 @@ class HypernetworkModule(torch.nn.Module):
...
@@ -46,7 +52,7 @@ class HypernetworkModule(torch.nn.Module):
self
.
load_state_dict
(
state_dict
)
self
.
load_state_dict
(
state_dict
)
else
:
else
:
for
layer
in
self
.
linear
:
for
layer
in
self
.
linear
:
if
not
"ReLU"
in
layer
.
__str__
(
):
if
isinstance
(
layer
,
torch
.
nn
.
Linear
):
layer
.
weight
.
data
.
normal_
(
mean
=
0.0
,
std
=
0.01
)
layer
.
weight
.
data
.
normal_
(
mean
=
0.0
,
std
=
0.01
)
layer
.
bias
.
data
.
zero_
()
layer
.
bias
.
data
.
zero_
()
...
@@ -298,7 +304,8 @@ def train_hypernetwork(hypernetwork_name, learn_rate, batch_size, data_root, log
...
@@ -298,7 +304,8 @@ def train_hypernetwork(hypernetwork_name, learn_rate, batch_size, data_root, log
return
hypernetwork
,
filename
return
hypernetwork
,
filename
scheduler
=
LearnRateScheduler
(
learn_rate
,
steps
,
ititial_step
)
scheduler
=
LearnRateScheduler
(
learn_rate
,
steps
,
ititial_step
)
optimizer
=
torch
.
optim
.
AdamW
(
weights
,
lr
=
scheduler
.
learn_rate
)
# if optimizer == "Adam": or else Adam / AdamW / etc...
optimizer
=
torch
.
optim
.
Adam
(
weights
,
lr
=
scheduler
.
learn_rate
)
pbar
=
tqdm
.
tqdm
(
enumerate
(
ds
),
total
=
steps
-
ititial_step
)
pbar
=
tqdm
.
tqdm
(
enumerate
(
ds
),
total
=
steps
-
ititial_step
)
for
i
,
entries
in
pbar
:
for
i
,
entries
in
pbar
:
...
...
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