-
Notifications
You must be signed in to change notification settings - Fork 910
Qwen image edit #814
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
Qwen image edit #814
Conversation
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.
Summary of Changes
Hello @mi804, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces comprehensive support for the Qwen-Image-Edit
model, significantly expanding the project's capabilities to include image editing. The core QwenImagePipeline
has been upgraded with new components and logic to process input images alongside text prompts, allowing for precise modifications. This involves changes to how prompts are embedded and how image latents are handled within the Diffusion Transformer (DiT) model. Additionally, the PR includes new example scripts and updated documentation to facilitate the use and training of the Qwen-Image-Edit
model, making it readily accessible for users to generate and edit images.
Highlights
- Integration of Qwen-Image-Edit Model: Full support for the new
Qwen-Image-Edit
model has been integrated, enabling advanced image editing capabilities within the existingQwenImagePipeline
. - Enhanced Pipeline Functionality: The
QwenImagePipeline
now features a newQwenImageUnit_EditImageEmbedder
for processing input images for editing. TheQwenImageUnit_PromptEmbedder
has been updated to adapt prompt templates and tokenization based on whether an edit image is provided, enhancing conditional image generation. - DiT Model Adaptations: The underlying DiT model in
qwen_image_dit.py
has been adapted to handleedit_latents
by concatenating them with image latents for processing, ensuring seamless integration of editing inputs. Thepos_index
andneg_index
ranges were also expanded. - Comprehensive Training and Inference Examples: New Python scripts and shell scripts have been added to provide comprehensive examples for both inference (standard and low VRAM) and training (full and LoRA) of the
Qwen-Image-Edit
model, along with updated documentation in both English and ChineseREADME.md
files.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request introduces support for the Qwen-Image-Edit model, including new pipeline components, training scripts, and examples. The changes are generally well-implemented. My review includes a critical fix for a validation script that was not loading the trained model weights, a suggestion to improve code robustness by replacing an assert
statement with a proper exception, and a clarification for a potentially confusing line of code to enhance maintainability.
tokenizer_config=None, | ||
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"), | ||
) | ||
state_dict = load_state_dict("models/train/Qwen-Image-Edit_full/epoch-1.safetensors") |
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 loaded state_dict
from the trained checkpoint is not being applied to the model. The validation script is currently running inference with the pre-trained base model, not the fine-tuned one. You should load the state dict into the pipe.dit
model component to correctly validate the trained weights.
state_dict = load_state_dict("models/train/Qwen-Image-Edit_full/epoch-1.safetensors") | |
state_dict = load_state_dict("models/train/Qwen-Image-Edit_full/epoch-1.safetensors") | |
pipe.dit.load_state_dict(state_dict) |
else: | ||
assert False, "QwenImagePipeline requires either tokenizer or processor to be loaded." |
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.
Using assert
for runtime checks is not recommended as assertions can be disabled with the -O
(optimize) Python flag, which could lead to unexpected behavior. It's better to raise an explicit exception like ValueError
for such checks to ensure the program terminates correctly when a required component is missing.
else: | |
assert False, "QwenImagePipeline requires either tokenizer or processor to be loaded." | |
else: | |
raise ValueError("QwenImagePipeline requires either tokenizer or processor to be loaded.") |
diffsynth/pipelines/qwen_image.py
Outdated
|
||
|
||
if edit_latents is not None: | ||
img_shapes[0] = (img_shapes[0][0] + edit_latents.shape[0], img_shapes[0][1], img_shapes[0][2]) |
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.
This line is a bit confusing. It appears to be using the first dimension of img_shapes
(which typically represents batch size) to indicate the number of concatenated image parts (original latents + edit latents). This is then used as a "frame" dimension for positional embeddings. While this works under the assumption that the batch size is 1, adding a comment to explain this logic would greatly improve code clarity and maintainability for other developers.
img_shapes[0] = (img_shapes[0][0] + edit_latents.shape[0], img_shapes[0][1], img_shapes[0][2]) | |
# The first dimension of img_shapes is used as the "frame" dimension for RoPE, | |
# representing the number of concatenated latents (original + edit). | |
img_shapes[0] = (img_shapes[0][0] + edit_latents.shape[0], img_shapes[0][1], img_shapes[0][2]) |
No description provided.