-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Add ModelEditing pipeline #2721
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ee76208
TIME first commit
bahjat-kawar 7b8ebd2
styling.
sayakpaul 16c9855
styling 2.
sayakpaul 9b5e5ab
fixes; tests
bahjat-kawar b19d460
Merge branch 'main' into time-diffusion
sayakpaul 3e81851
apply styling and doc fix.
sayakpaul a05b6a9
remove sups.
sayakpaul 61ed87e
fixes
bahjat-kawar b4272f5
Merge branch 'time-diffusion' of https://github.com/bahjat-kawar/diff…
bahjat-kawar 0094901
remove temp file
bahjat-kawar 0f43108
move augmentations to const
bahjat-kawar 0be8590
added doc entry
bahjat-kawar d0854fc
code quality
bahjat-kawar c4c69b0
customize augmentations
bahjat-kawar c449381
quality
bahjat-kawar 40d3523
quality
bahjat-kawar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
docs/source/en/api/pipelines/stable_diffusion/model_editing.mdx
This file contains hidden or 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,59 @@ | ||
| <!--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. | ||
| --> | ||
|
|
||
| # Editing Implicit Assumptions in Text-to-Image Diffusion Models | ||
|
|
||
| ## Overview | ||
|
|
||
| [Editing Implicit Assumptions in Text-to-Image Diffusion Models](https://arxiv.org/abs/2303.08084) by Hadas Orgad, Bahjat Kawar, and Yonatan Belinkov. | ||
|
|
||
| The abstract of the paper is the following: | ||
|
|
||
| *Text-to-image diffusion models often make implicit assumptions about the world when generating images. While some assumptions are useful (e.g., the sky is blue), they can also be outdated, incorrect, or reflective of social biases present in the training data. Thus, there is a need to control these assumptions without requiring explicit user input or costly re-training. In this work, we aim to edit a given implicit assumption in a pre-trained diffusion model. Our Text-to-Image Model Editing method, TIME for short, receives a pair of inputs: a "source" under-specified prompt for which the model makes an implicit assumption (e.g., "a pack of roses"), and a "destination" prompt that describes the same setting, but with a specified desired attribute (e.g., "a pack of blue roses"). TIME then updates the model's cross-attention layers, as these layers assign visual meaning to textual tokens. We edit the projection matrices in these layers such that the source prompt is projected close to the destination prompt. Our method is highly efficient, as it modifies a mere 2.2% of the model's parameters in under one second. To evaluate model editing approaches, we introduce TIMED (TIME Dataset), containing 147 source and destination prompt pairs from various domains. Our experiments (using Stable Diffusion) show that TIME is successful in model editing, generalizes well for related prompts unseen during editing, and imposes minimal effect on unrelated generations.* | ||
|
|
||
| Resources: | ||
|
|
||
| * [Project Page](https://time-diffusion.github.io/). | ||
| * [Paper](https://arxiv.org/abs/2303.08084). | ||
| * [Original Code](https://github.com/bahjat-kawar/time-diffusion). | ||
| * [Demo](https://huggingface.co/spaces/bahjat-kawar/time-diffusion). | ||
|
|
||
| ## Available Pipelines: | ||
|
|
||
| | Pipeline | Tasks | Demo | ||
| |---|---|:---:| | ||
| | [StableDiffusionModelEditingPipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_model_editing.py) | *Text-to-Image Model Editing* | [🤗 Space](https://huggingface.co/spaces/bahjat-kawar/time-diffusion)) | | ||
|
|
||
bahjat-kawar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Usage example | ||
|
|
||
| ```python | ||
| import torch | ||
| from diffusers import StableDiffusionModelEditingPipeline | ||
|
|
||
| model_ckpt = "CompVis/stable-diffusion-v1-4" | ||
| pipe = StableDiffusionModelEditingPipeline.from_pretrained(model_ckpt) | ||
|
|
||
| pipe = pipe.to("cuda") | ||
|
|
||
| source_prompt = "A pack of roses" | ||
| destination_prompt = "A pack of blue roses" | ||
| pipe.edit_model(source_prompt, destination_prompt) | ||
|
|
||
| prompt = "A field of roses" | ||
| image = pipe(prompt).images[0] | ||
| image.save("field_of_roses.png") | ||
| ``` | ||
|
|
||
| ## StableDiffusionModelEditingPipeline | ||
| [[autodoc]] StableDiffusionModelEditingPipeline | ||
| - __call__ | ||
| - all | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
After the pipeline is merged, would be nice to update the demo with
diffusers. Maybe @hysts could open a PR :)