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
OpenDAS
diffusers
Commits
fb02316d
Unverified
Commit
fb02316d
authored
Dec 26, 2023
by
Dhruv Nair
Committed by
GitHub
Dec 26, 2023
Browse files
Add AnimateDiff conversion scripts (#6340)
* add scripts * update
parent
98a2b3d2
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
0 deletions
+102
-0
scripts/convert_animatediff_motion_lora_to_diffusers.py
scripts/convert_animatediff_motion_lora_to_diffusers.py
+51
-0
scripts/convert_animatediff_motion_module_to_diffusers.py
scripts/convert_animatediff_motion_module_to_diffusers.py
+51
-0
No files found.
scripts/convert_animatediff_motion_lora_to_diffusers.py
0 → 100644
View file @
fb02316d
import
argparse
import
torch
from
safetensors.torch
import
save_file
def
convert_motion_module
(
original_state_dict
):
converted_state_dict
=
{}
for
k
,
v
in
original_state_dict
.
items
():
if
"pos_encoder"
in
k
:
continue
else
:
converted_state_dict
[
k
.
replace
(
".norms.0"
,
".norm1"
)
.
replace
(
".norms.1"
,
".norm2"
)
.
replace
(
".ff_norm"
,
".norm3"
)
.
replace
(
".attention_blocks.0"
,
".attn1"
)
.
replace
(
".attention_blocks.1"
,
".attn2"
)
.
replace
(
".temporal_transformer"
,
""
)
]
=
v
return
converted_state_dict
def
get_args
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--ckpt_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"--output_path"
,
type
=
str
,
required
=
True
)
return
parser
.
parse_args
()
if
__name__
==
"__main__"
:
args
=
get_args
()
state_dict
=
torch
.
load
(
args
.
ckpt_path
,
map_location
=
"cpu"
)
if
"state_dict"
in
state_dict
.
keys
():
state_dict
=
state_dict
[
"state_dict"
]
conv_state_dict
=
convert_motion_module
(
state_dict
)
# convert to new format
output_dict
=
{}
for
module_name
,
params
in
conv_state_dict
.
items
():
if
type
(
params
)
is
not
torch
.
Tensor
:
continue
output_dict
.
update
({
f
"unet.
{
module_name
}
"
:
params
})
save_file
(
output_dict
,
f
"
{
args
.
output_path
}
/diffusion_pytorch_model.safetensors"
)
scripts/convert_animatediff_motion_module_to_diffusers.py
0 → 100644
View file @
fb02316d
import
argparse
import
torch
from
diffusers
import
MotionAdapter
def
convert_motion_module
(
original_state_dict
):
converted_state_dict
=
{}
for
k
,
v
in
original_state_dict
.
items
():
if
"pos_encoder"
in
k
:
continue
else
:
converted_state_dict
[
k
.
replace
(
".norms.0"
,
".norm1"
)
.
replace
(
".norms.1"
,
".norm2"
)
.
replace
(
".ff_norm"
,
".norm3"
)
.
replace
(
".attention_blocks.0"
,
".attn1"
)
.
replace
(
".attention_blocks.1"
,
".attn2"
)
.
replace
(
".temporal_transformer"
,
""
)
]
=
v
return
converted_state_dict
def
get_args
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--ckpt_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"--output_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"--use_motion_mid_block"
,
action
=
"store_true"
)
parser
.
add_argument
(
"--motion_max_seq_length"
,
type
=
int
,
default
=
32
)
return
parser
.
parse_args
()
if
__name__
==
"__main__"
:
args
=
get_args
()
state_dict
=
torch
.
load
(
args
.
ckpt_path
,
map_location
=
"cpu"
)
if
"state_dict"
in
state_dict
.
keys
():
state_dict
=
state_dict
[
"state_dict"
]
conv_state_dict
=
convert_motion_module
(
state_dict
)
adapter
=
MotionAdapter
(
use_motion_mid_block
=
args
.
use_motion_mid_block
,
motion_max_seq_length
=
args
.
motion_max_seq_length
)
# skip loading position embeddings
adapter
.
load_state_dict
(
conv_state_dict
,
strict
=
False
)
adapter
.
save_pretrained
(
args
.
output_path
)
adapter
.
save_pretrained
(
args
.
output_path
,
variant
=
"fp16"
,
torch_dtype
=
torch
.
float16
)
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