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
979c48be
Unverified
Commit
979c48be
authored
Jul 01, 2022
by
Suraj Patil
Committed by
GitHub
Jul 01, 2022
Browse files
Merge pull request #61 from huggingface/conversion-scripts
add conversion script for LatentDiffusionUncondPipeline
parents
c691bb2f
099d3eab
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
0 deletions
+56
-0
scripts/conversion_ldm_uncond.py
scripts/conversion_ldm_uncond.py
+56
-0
No files found.
scripts/conversion_ldm_uncond.py
0 → 100644
View file @
979c48be
import
argparse
import
OmegaConf
import
torch
from
diffusers
import
UNetLDMModel
,
VQModel
,
LatentDiffusionUncondPipeline
,
DDIMScheduler
def
convert_ldm_original
(
checkpoint_path
,
config_path
,
output_path
):
config
=
OmegaConf
.
load
(
config_path
)
state_dict
=
torch
.
load
(
checkpoint_path
,
map_location
=
"cpu"
)[
"model"
]
keys
=
list
(
state_dict
.
keys
())
# extract state_dict for VQVAE
first_stage_dict
=
{}
first_stage_key
=
"first_stage_model."
for
key
in
keys
:
if
key
.
startswith
(
first_stage_key
):
first_stage_dict
[
key
.
replace
(
first_stage_key
,
""
)]
=
state_dict
[
key
]
# extract state_dict for UNetLDM
unet_state_dict
=
{}
unet_key
=
"model.diffusion_model."
for
key
in
keys
:
if
key
.
startswith
(
unet_key
):
unet_state_dict
[
key
.
replace
(
unet_key
,
""
)]
=
state_dict
[
key
]
vqvae_init_args
=
config
.
model
.
params
.
first_stage_config
.
params
unet_init_args
=
config
.
model
.
params
.
unet_config
.
params
vqvae
=
VQModel
(
**
vqvae_init_args
).
eval
()
vqvae
.
load_state_dict
(
first_stage_dict
)
unet
=
UNetLDMModel
(
**
unet_init_args
).
eval
()
unet
.
load_state_dict
(
unet_state_dict
)
noise_scheduler
=
DDIMScheduler
(
timesteps
=
config
.
model
.
params
.
timesteps
,
beta_schedule
=
"scaled_linear"
,
beta_start
=
config
.
model
.
params
.
linear_start
,
beta_end
=
config
.
model
.
params
.
linear_end
,
clip_sample
=
False
,
)
pipeline
=
LatentDiffusionUncondPipeline
(
vqvae
,
unet
,
noise_scheduler
)
pipeline
.
save_pretrained
(
output_path
)
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--checkpoint_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"--config_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"--output_path"
,
type
=
str
,
required
=
True
)
args
=
parser
.
parse_args
()
convert_ldm_original
(
args
.
checkpoint_path
,
args
.
config_path
,
args
.
output_path
)
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