This is an ongoing project aims to Edit and Generate Anything in an image, powered by Segment Anything, ControlNet, BLIP2, Stable Diffusion, etc.
Any forms of contribution and suggestion are very welcomed!
2023/08/09 - Revise UI and code, fixed multiple known issues.
2023/07/25 - EditAnything is accepted by the ACM MM demo track.
2023/06/09 - Support cross-image region drag and merge, unleash creative fusion!
2023/05/24 - Support multiple high-quality character editing: clothes, haircut, colored contact lenses.
2023/05/22 - Support sketch to image by adjusting mask align strength in sketch2image.py
!
2023/05/13 - Support interactive segmentation with click operation!
2023/05/11 - Support tile model for detail refinement!
2023/05/04 - New demos of Beauty/Handsome Edit/Generation is released!
2023/05/04 - ControlNet-based inpainting model on any lora model is supported now. EditAnything can operate on any base/lord models without the requirements of inpainting model.
More update logs.
2023/05/01 - Models V0.4 based on Stable Diffusion 1.5/2.1 are released. New models are trained with more data and iterations.Model Zoo
2023/04/20 - We support the Customized editing with DreamBooth.
2023/04/17 - We support the SAM mask to semantic segmentation mask.
2023/04/17 - We support different alignment degrees bettween edited parts and the SAM mask, check it out on DEMO!
2023/04/15 - Gradio demo on Huggingface is released!
2023/04/14 - New model trained with LAION dataset is released.
2023/04/13 - Support pretrained model auto downloading and gradio in sam2image.py
.
2023/04/12 - An initial version of text-guided edit-anything is in sam2groundingdino_edit.py
(object-level) and sam2vlpart_edit.py
(part-level).
2023/04/10 - An initial version of edit-anything is in sam2edit.py
.
2023/04/10 - We transfer the pretrained model into diffusers style, the pretrained model is auto loaded when using sam2image_diffuser.py
. Now you can combine our pretrained model with different base models easily!
2023/04/09 - We released a pretrained model of StableDiffusion based ControlNet that generate images conditioned by SAM segmentation.
![image](https://private-user-images.githubusercontent.com/20515144/246580522-7f997db8-d2ea-4341-a7d7-dfe8ac5dd338.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxM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zI3VDE5MTg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWFiZTA1MjdmZGUxNWI0OGU2ZWE4ODBhMGMyMjhiNzIyMTc1ODAyNTMyZmFmNWQ1ZmRjMzgxMjAxMmVmYzE2ZmYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.8TZIO6R-N3Tr18uxV8NvendyjaqBbP__AfDIdldG7hI)
![image](https://private-user-images.githubusercontent.com/20515144/240523700-03452a0f-83ae-4257-995c-f3d8b71d4f1d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxMDgyMjcsIm5iZiI6MTcyMjEwNzkyNywicGF0aCI6Ii8yMDUxNTE0NC8yNDA1MjM3MDAtMDM0NTJhMGYtODNhZS00MjU3LTk5NWMtZjNkOGI3MWQ0ZjFkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI3VDE5MTg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTczYTU3MGI5OWExMzZmNTlmMzE4ODZjMmEyMTg5MDBmNjgyNTIyYmZlMTBmNDAxZTA3M2U5ZGFlYjU4NDM5NDcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.ryVyRXVyGCW9fdjFMXcTHmfazb85QoNFs4A1s2X1s3s)
![image](https://private-user-images.githubusercontent.com/20515144/240524159-9091e3c9-c7e1-485d-bfe6-de5b21e83814.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxMDgyMjcsIm5iZiI6MTcyMjEwNzkyNywicGF0aCI6Ii8yMDUxNTE0NC8yNDA1MjQxNTktOTA5MWUzYzktYzdlMS00ODVkLWJmZTYtZGU1YjIxZTgzODE0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI3VDE5MTg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE0NjI1YzRkMDE1OTkxMzg5ZWZhMTAyMzY5NTBjNzlmOTUwNDBhZGNjNWNhOWM5YjBjNTc4MDg4NThhNGMzNGEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.6xWuYz3YKI3xV0V4cuFbKWg9ss5MQQ8Fx9omo0PIo9c)
![image](https://private-user-images.githubusercontent.com/20515144/240525236-d9c8a136-e12c-4df4-aed0-a7c287e0ef3c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxMDgyMjcsIm5iZiI6MTcyMjEwNzkyNywicGF0aCI6Ii8yMDUxNTE0NC8yNDA1MjUyMzYtZDljOGExMzYtZTEyYy00ZGY0LWFlZDAtYTdjMjg3ZTBlZjNjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI3VDE5MTg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWViYTdiZTc0MDUyYzA0YTc1NmE1ZmFmZmNmYWNmMDU0NWQ5MmMxZTA0NjliNjFjYjczYTY4YWEwNWMyZjZlNjAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.IBb0vt3AyZwQb6BEbcYNuKKXCkPiYL2ihodJvAO_2SI)
![image](https://private-user-images.githubusercontent.com/20515144/240527011-31883059-2bbc-442c-88aa-04d3a1da7abc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxMDgyMjcsIm5iZiI6MTcyMjEwNzkyNywicGF0aCI6Ii8yMDUxNTE0NC8yNDA1MjcwMTEtMzE4ODMwNTktMmJiYy00NDJjLTg4YWEtMDRkM2ExZGE3YWJjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI3VDE5MTg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNjNjM2NTYwMjcyYjkwNTZiZDg4MDMyZWRhNzc0YjU4NTUwMzZhOGMyYzM0OTM4OGU0YTZkZDNkNWNmMjhjZTgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Yecn3K1N0ohdFonC4rN3DLkyFqa0xlZiYgdgsfxaaO8)
prompt: "a paint of a tree in the ground with a river."
More demos.
prompt: "a paint, river, mountain, sun, cloud, beautiful field."
prompt: "a man, midsplit center parting hair, HD."
prompt: "a woman, long hair, detailed facial details, photorealistic, HD, beautiful face, solo, candle, brown hair, blue eye."
Also, you could use the generated image and sam model to refine your sketch definitely!
Edit Your beauty and Generate Your beauty
EditAnything+DreamBooth: Train a customized DreamBooth Model with `tools/train_dreambooth_inpaint.py` and replace the base model in `sam2edit.py` with the trained model.Human Prompt: "A paint of spring/summer/autumn/winter field."
Text Grounding: "dog head"
Text Grounding: "bench"
Human Prompt: "esplendent sunset sky, red brick wall"
BLIP2 Prompt: "a large white and red ferry"
(1:input image; 2: segmentation mask; 3-8: generated images.)
- The human prompt and BLIP2 generated prompt build the text instruction.
- The SAM model segment the input image to generate segmentation mask without category.
- The segmentation mask and text instruction guide the image generation.
python sam2semantic.py
Highlight features:
- Pretrained ControlNet with SAM mask as condition enables the image generation with fine-grained control.
- category-unrelated SAM mask enables more forms of editing and generation.
- BLIP2 text generation enables text guidance-free control.
Create a environment
conda env create -f environment.yaml
conda activate control
Install BLIP2 and SAM
Put these models in models
folder.
# BLIP2 and SAM will be audo installed by running app.py
pip install git+https://github.com/huggingface/transformers.git
pip install git+https://github.com/facebookresearch/segment-anything.git
# For text-guided editing
pip install git+https://github.com/openai/CLIP.git
pip install git+https://github.com/facebookresearch/detectron2.git
pip install git+https://github.com/IDEA-Research/GroundingDINO.git
Download pretrained model
# Segment-anything ViT-H SAM model will be auto downloaded.
# BLIP2 model will be auto downloaded.
# Part Grounding Swin-Base Model.
wget https://github.com/Cheems-Seminar/segment-anything-and-name-it/releases/download/v1.0/swinbase_part_0a0000.pth
# Grounding DINO Model.
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha2/groundingdino_swinb_cogcoor.pth
# Get pretrained model from huggingface.
# No need to download this! But please install safetensors for reading the ckpt.
Run Demo
python app.py
# or
python editany.py
# or
python sam2image.py
# or
python sam2vlpart_edit.py
# or
python sam2groundingdino_edit.py
Model | Features | Download Path |
---|---|---|
SAM Pretrained(v0-1) | Good Nature Sense | shgao/edit-anything-v0-1-1 |
LAION Pretrained(v0-3) | Good Face | shgao/edit-anything-v0-3 |
LAION Pretrained(v0-4) | Support StableDiffusion 1.5/2.1, More training data and iterations, Good Face | shgao/edit-anything-v0-4-sd15 shgao/edit-anything-v0-4-sd21 |
- Generate training dataset with
dataset_build.py
. - Transfer stable-diffusion model with
tool_add_control_sd21.py
. - Train model with
sam_train_sd21.py
.
@InProceedings{gao2023editanything,
author = {Gao, Shanghua and Lin, Zhijie and Xie, Xingyu and Zhou, Pan and Cheng, Ming-Ming and Yan, Shuicheng},
title = {EditAnything: Empowering Unparalleled Flexibility in Image Editing and Generation},
booktitle = {Proceedings of the 31st ACM International Conference on Multimedia, Demo track},
year = {2023},
}
This project is based on:
Segment Anything, ControlNet, BLIP2, MDT, Stable Diffusion, Large-scale Unsupervised Semantic Segmentation, Grounded Segment Anything: From Objects to Parts, Grounded-Segment-Anything
Thanks for these amazing projects!