-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add PIA Model/Pipeline #6698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PIA Model/Pipeline #6698
Changes from 7 commits
b14881e
1169370
7be981c
5025fc3
845d7f7
1ba867d
8d1794d
1c7b31a
a93a5ba
4c9244d
b3e4161
e4535a5
8961802
7542799
911b682
2aba631
d01a61b
facdd91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,191 @@ | ||||||||||||||||||||||||||||||||||
| <!--Copyright 2023 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. | ||||||||||||||||||||||||||||||||||
| --> | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Image-to-Video Generation with PIA (Personalized Image Animator) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Overview | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Recent advancements in personalized text-to-image (T2I) models have revolutionized content creation, empowering non-experts to generate stunning images with unique styles. While promising, adding realistic motions into these personalized images by text poses significant challenges in preserving distinct styles, high-fidelity details, and achieving motion controllability by text. In this paper, we present PIA, a Personalized Image Animator that excels in aligning with condition images, achieving motion controllability by text, and the compatibility with various personalized T2I models without specific tuning. To achieve these goals, PIA builds upon a base T2I model with well-trained temporal alignment layers, allowing for the seamless transformation of any personalized T2I model into an image animation model. A key component of PIA is the introduction of the condition module, which utilizes the condition frame and inter-frame affinity as input to transfer appearance information guided by the affinity hint for individual frame synthesis in the latent space. This design mitigates the challenges of appearance-related image alignment within and allows for a stronger focus on aligning with motion-related guidance. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Available Pipelines | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| | Pipeline | Tasks | Demo | ||||||||||||||||||||||||||||||||||
| |---|---|:---:| | ||||||||||||||||||||||||||||||||||
| | [PIAPipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/pia/pipeline_pia.py) | *Image-to-Video Generation with PIA* | | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Available checkpoints | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Motion Adapter checkpoints for PIA can be found under [](). These checkpoints are meant to work with any model based on Stable Diffusion 1.5 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Usage example | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| PIA works with a MotionAdapter checkpoint and a Stable Diffusion 1.5 model checkpoint. The MotionAdapter is a collection of Motion Modules that are responsible for adding coherent motion across image frames. These modules are applied after the Resnet and Attention blocks in the Stable Diffusion UNet. In addition to the motion modules, PIA also replaces the input convolution layer of the SD 1.5 UNet model with a 9 channel input convolution layer. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| The following example demonstrates how to use PIA to generate a video from a single image. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||
| from diffusers import ( | ||||||||||||||||||||||||||||||||||
| DDIMScheduler, | ||||||||||||||||||||||||||||||||||
| MotionAdapter, | ||||||||||||||||||||||||||||||||||
| PIAPipeline, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| from diffusers.utils import export_to_gif, load_image | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| adapter = MotionAdapter.from_pretrained("diffusers/PIA-motion-adapter") | ||||||||||||||||||||||||||||||||||
| pipe = PIAPipeline.from_pretrained("SG161222/Realistic_Vision_V6.0_B1_noVAE", motion_adapter=adapter) | ||||||||||||||||||||||||||||||||||
DN6 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| pipe.scheduler = DDIMScheduler( | ||||||||||||||||||||||||||||||||||
| clip_sample=False, | ||||||||||||||||||||||||||||||||||
| steps_offset=1, | ||||||||||||||||||||||||||||||||||
| timestep_spacing="leading", | ||||||||||||||||||||||||||||||||||
| beta_schedule="linear", | ||||||||||||||||||||||||||||||||||
| beta_start=0.00085, | ||||||||||||||||||||||||||||||||||
| beta_end=0.012, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
DN6 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| pipe.enable_model_cpu_offload() | ||||||||||||||||||||||||||||||||||
| pipe.enable_vae_slicing() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| image = load_image( | ||||||||||||||||||||||||||||||||||
| "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/pix2pix/cat_6.png?download=true" | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| image = image.resize((512, 512)) | ||||||||||||||||||||||||||||||||||
| prompt = "cat in a field" | ||||||||||||||||||||||||||||||||||
| negative_prompt = "wrong white balance, dark, sketches,worst quality,low quality" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| generator = torch.Generator("cpu").manual_seed(0) | ||||||||||||||||||||||||||||||||||
| output = pipe( | ||||||||||||||||||||||||||||||||||
| image=image, | ||||||||||||||||||||||||||||||||||
| prompt=prompt, | ||||||||||||||||||||||||||||||||||
| strength=0.75, | ||||||||||||||||||||||||||||||||||
| guidance_scale=7.5, | ||||||||||||||||||||||||||||||||||
| num_inference_steps=25, | ||||||||||||||||||||||||||||||||||
| motion_scale=2, | ||||||||||||||||||||||||||||||||||
DN6 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| generator=generator, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| frames = output.frames[0] | ||||||||||||||||||||||||||||||||||
| export_to_gif(frames, "pia-animation.gif") | ||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Here are some sample outputs: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| <table> | ||||||||||||||||||||||||||||||||||
| <tr> | ||||||||||||||||||||||||||||||||||
| <td><center> | ||||||||||||||||||||||||||||||||||
| masterpiece, bestquality, sunset. | ||||||||||||||||||||||||||||||||||
| <br> | ||||||||||||||||||||||||||||||||||
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/pia-default-output.gif" | ||||||||||||||||||||||||||||||||||
| alt="cat in a field" | ||||||||||||||||||||||||||||||||||
| style="width: 300px;" /> | ||||||||||||||||||||||||||||||||||
| </center></td> | ||||||||||||||||||||||||||||||||||
| </tr> | ||||||||||||||||||||||||||||||||||
| </table> | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| <Tip> | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| If you plan on using a scheduler that can clip samples, make sure to disable it by setting `clip_sample=False` in the scheduler as this can also have an adverse effect on generated samples. Additionally, the PIA checkpoints can be sensitive to the beta schedule of the scheduler. We recommend setting this to `linear`. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| </Tip> | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Using FreeInit | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| [FreeInit: Bridging Initialization Gap in Video Diffusion Models](https://arxiv.org/abs/2312.07537) by Tianxing Wu, Chenyang Si, Yuming Jiang, Ziqi Huang, Ziwei Liu. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| FreeInit is an effective method that improves temporal consistency and overall quality of videos generated using video-diffusion-models without any addition training. It can be applied to PIA, AnimateDiff, ModelScope, VideoCrafter and various other video generation models seamlessly at inference time, and works by iteratively refining the latent-initialization noise. More details can be found it the paper. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| The following example demonstrates the usage of FreeInit. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||
| from diffusers import ( | ||||||||||||||||||||||||||||||||||
| DDIMScheduler, | ||||||||||||||||||||||||||||||||||
| MotionAdapter, | ||||||||||||||||||||||||||||||||||
| PIAPipeline, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| from diffusers.utils import export_to_gif, load_image | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| adapter = MotionAdapter.from_pretrained("../checkpoints/pia-diffusers") | ||||||||||||||||||||||||||||||||||
DN6 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| pipe = PIAPipeline.from_pretrained("SG161222/Realistic_Vision_V6.0_B1_noVAE", motion_adapter=adapter) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # enable FreeInit | ||||||||||||||||||||||||||||||||||
| # Refer to the enable_free_init documentation for a full list of configurable parameters | ||||||||||||||||||||||||||||||||||
| pipe.enable_free_init(method="butterworth", use_fast_sampling=True) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Memory saving options | ||||||||||||||||||||||||||||||||||
| pipe.enable_model_cpu_offload() | ||||||||||||||||||||||||||||||||||
| pipe.enable_vae_slicing() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| pipe.scheduler = DDIMScheduler( | ||||||||||||||||||||||||||||||||||
| clip_sample=False, | ||||||||||||||||||||||||||||||||||
| steps_offset=1, | ||||||||||||||||||||||||||||||||||
| timestep_spacing="leading", | ||||||||||||||||||||||||||||||||||
| beta_schedule="linear", | ||||||||||||||||||||||||||||||||||
| beta_start=0.00085, | ||||||||||||||||||||||||||||||||||
| beta_end=0.012, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| pipe.scheduler = DDIMScheduler( | |
| clip_sample=False, | |
| steps_offset=1, | |
| timestep_spacing="leading", | |
| beta_schedule="linear", | |
| beta_start=0.00085, | |
| beta_end=0.012, | |
| ) | |
| pipe.scheduler = DDIMScheduler( | |
| clip_sample=False, | |
| steps_offset=1, | |
| timestep_spacing="leading", | |
| beta_schedule="linear", | |
| beta_start=0.00085, | |
| beta_end=0.012, | |
| ) |
Can't we load from a repo_id as shown before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The checkpoint only has Motion Modules. And PIA is supposed to work with any SD 1.5 UNet but is quite sensitive to the choice of beta_schedule. In my tests, anything other than linear leads to sub par results.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils import ( | ||
| DIFFUSERS_SLOW_IMPORT, | ||
| OptionalDependencyNotAvailable, | ||
| _LazyModule, | ||
| get_objects_from_module, | ||
| is_torch_available, | ||
| is_transformers_available, | ||
| ) | ||
|
|
||
|
|
||
| _dummy_objects = {} | ||
| _import_structure = {} | ||
|
|
||
| try: | ||
| if not (is_transformers_available() and is_torch_available()): | ||
| raise OptionalDependencyNotAvailable() | ||
| except OptionalDependencyNotAvailable: | ||
| from ...utils import dummy_torch_and_transformers_objects | ||
|
|
||
| _dummy_objects.update(get_objects_from_module(dummy_torch_and_transformers_objects)) | ||
| else: | ||
| _import_structure["pipeline_pia"] = ["PIAPipeline", "PIAPipelineOutput"] | ||
|
|
||
| if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT: | ||
| try: | ||
| if not (is_transformers_available() and is_torch_available()): | ||
| raise OptionalDependencyNotAvailable() | ||
| except OptionalDependencyNotAvailable: | ||
| from ...utils.dummy_torch_and_transformers_objects import * | ||
|
|
||
| else: | ||
| from .pipeline_pia import PIAPipeline, PIAPipelineOutput | ||
|
|
||
| else: | ||
| import sys | ||
|
|
||
| sys.modules[__name__] = _LazyModule( | ||
| __name__, | ||
| globals()["__file__"], | ||
| _import_structure, | ||
| module_spec=__spec__, | ||
| ) | ||
| for name, value in _dummy_objects.items(): | ||
| setattr(sys.modules[__name__], name, value) |
Uh oh!
There was an error while loading. Please reload this page.