forked from huggingface/diffusers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] AnimateDiff SparseCtrl (huggingface#8897)
* initial sparse control model draft * remove unnecessary implementation * copy animatediff pipeline * remove deprecated callbacks * update * update pipeline implementation progress * make style * make fix-copies * update progress * add partially working pipeline * remove debug prints * add model docs * dummy objects * improve motion lora conversion script * fix bugs * update docstrings * remove unnecessary model params; docs * address review comment * add copied from to zero_module * copy animatediff test * add fast tests * update docs * update * update pipeline docs * fix expected slice values * fix license * remove get_down_block usage * remove temporal_double_self_attention from get_down_block * update * update docs with org and documentation images * make from_unet work in sparsecontrolnetmodel * add latest freeinit test from huggingface#8969 * make fix-copies * LoraLoaderMixin -> StableDiffsuionLoraLoaderMixin
- Loading branch information
Showing
16 changed files
with
2,664 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!-- Copyright 2024 The HuggingFace Team. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. --> | ||
|
||
# SparseControlNetModel | ||
|
||
SparseControlNetModel is an implementation of ControlNet for [AnimateDiff](https://arxiv.org/abs/2307.04725). | ||
|
||
ControlNet was introduced in [Adding Conditional Control to Text-to-Image Diffusion Models](https://huggingface.co/papers/2302.05543) by Lvmin Zhang, Anyi Rao, and Maneesh Agrawala. | ||
|
||
The SparseCtrl version of ControlNet was introduced in [SparseCtrl: Adding Sparse Controls to Text-to-Video Diffusion Models](https://arxiv.org/abs/2311.16933) for achieving controlled generation in text-to-video diffusion models by Yuwei Guo, Ceyuan Yang, Anyi Rao, Maneesh Agrawala, Dahua Lin, and Bo Dai. | ||
|
||
The abstract from the paper is: | ||
|
||
*The development of text-to-video (T2V), i.e., generating videos with a given text prompt, has been significantly advanced in recent years. However, relying solely on text prompts often results in ambiguous frame composition due to spatial uncertainty. The research community thus leverages the dense structure signals, e.g., per-frame depth/edge sequences, to enhance controllability, whose collection accordingly increases the burden of inference. In this work, we present SparseCtrl to enable flexible structure control with temporally sparse signals, requiring only one or a few inputs, as shown in Figure 1. It incorporates an additional condition encoder to process these sparse signals while leaving the pre-trained T2V model untouched. The proposed approach is compatible with various modalities, including sketches, depth maps, and RGB images, providing more practical control for video generation and promoting applications such as storyboarding, depth rendering, keyframe animation, and interpolation. Extensive experiments demonstrate the generalization of SparseCtrl on both original and personalized T2V generators. Codes and models will be publicly available at [this https URL](https://guoyww.github.io/projects/SparseCtrl).* | ||
|
||
## Example for loading SparseControlNetModel | ||
|
||
```python | ||
import torch | ||
from diffusers import SparseControlNetModel | ||
|
||
# fp32 variant in float16 | ||
# 1. Scribble checkpoint | ||
controlnet = SparseControlNetModel.from_pretrained("guoyww/animatediff-sparsectrl-scribble", torch_dtype=torch.float16) | ||
|
||
# 2. RGB checkpoint | ||
controlnet = SparseControlNetModel.from_pretrained("guoyww/animatediff-sparsectrl-rgb", torch_dtype=torch.float16) | ||
|
||
# For loading fp16 variant, pass `variant="fp16"` as an additional parameter | ||
``` | ||
|
||
## SparseControlNetModel | ||
|
||
[[autodoc]] SparseControlNetModel | ||
|
||
## SparseControlNetOutput | ||
|
||
[[autodoc]] models.controlnet_sparsectrl.SparseControlNetOutput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import argparse | ||
from typing import Dict | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
from diffusers import SparseControlNetModel | ||
|
||
|
||
KEYS_RENAME_MAPPING = { | ||
".attention_blocks.0": ".attn1", | ||
".attention_blocks.1": ".attn2", | ||
".attn1.pos_encoder": ".pos_embed", | ||
".ff_norm": ".norm3", | ||
".norms.0": ".norm1", | ||
".norms.1": ".norm2", | ||
".temporal_transformer": "", | ||
} | ||
|
||
|
||
def convert(original_state_dict: Dict[str, nn.Module]) -> Dict[str, nn.Module]: | ||
converted_state_dict = {} | ||
|
||
for key in list(original_state_dict.keys()): | ||
renamed_key = key | ||
for new_name, old_name in KEYS_RENAME_MAPPING.items(): | ||
renamed_key = renamed_key.replace(new_name, old_name) | ||
converted_state_dict[renamed_key] = original_state_dict.pop(key) | ||
|
||
return converted_state_dict | ||
|
||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ckpt_path", type=str, required=True, help="Path to checkpoint") | ||
parser.add_argument("--output_path", type=str, required=True, help="Path to output directory") | ||
parser.add_argument( | ||
"--max_motion_seq_length", | ||
type=int, | ||
default=32, | ||
help="Max motion sequence length supported by the motion adapter", | ||
) | ||
parser.add_argument( | ||
"--conditioning_channels", type=int, default=4, help="Number of channels in conditioning input to controlnet" | ||
) | ||
parser.add_argument( | ||
"--use_simplified_condition_embedding", | ||
action="store_true", | ||
default=False, | ||
help="Whether or not to use simplified condition embedding. When `conditioning_channels==4` i.e. latent inputs, set this to `True`. When `conditioning_channels==3` i.e. image inputs, set this to `False`", | ||
) | ||
parser.add_argument( | ||
"--save_fp16", | ||
action="store_true", | ||
default=False, | ||
help="Whether or not to save model in fp16 precision along with fp32", | ||
) | ||
parser.add_argument( | ||
"--push_to_hub", action="store_true", default=False, help="Whether or not to push saved model to the HF hub" | ||
) | ||
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: dict = state_dict["state_dict"] | ||
|
||
controlnet = SparseControlNetModel( | ||
conditioning_channels=args.conditioning_channels, | ||
motion_max_seq_length=args.max_motion_seq_length, | ||
use_simplified_condition_embedding=args.use_simplified_condition_embedding, | ||
) | ||
|
||
state_dict = convert(state_dict) | ||
controlnet.load_state_dict(state_dict, strict=True) | ||
|
||
controlnet.save_pretrained(args.output_path, push_to_hub=args.push_to_hub) | ||
if args.save_fp16: | ||
controlnet = controlnet.to(dtype=torch.float16) | ||
controlnet.save_pretrained(args.output_path, variant="fp16", push_to_hub=args.push_to_hub) |
Oops, something went wrong.