This extension aim for connecting AUTOMATIC1111 Stable Diffusion WebUI and Mikubill ControlNet Extension with segment anything and GroundingDINO to enhance Stable Diffusion/ControlNet inpainting, enhance ControlNet semantic segmentation, automate image matting and create LoRA/LyCORIS training set.
2023/04/10
: [Release] SAM extension released! You can click on the image to generate segmentation masks.2023/04/12
: [Feature] Mask expansion released by @jordan-barrett-jm! You can expand masks to overcome edge problems of SAM.2023/04/15
: [Feature] GroundingDINO support released! You can enter text prompts to generate bounding boxes and segmentation masks.2023/04/15
: [Feature] API support released by @jordan-barrett-jm!2023/04/18
: [Feature] ControlNet V1.1 inpainting support released! You can copy SAM generated masks to ControlNet to do inpainting. Note that you must update ControlNet extension to use it. ControlNet inpainting has far better performance compared to general-purposed models, and you do not need to download inpainting-specific models anymore.2023/04/24
: [Feature] Automatic segmentation support released! Functionalities with * require you to have ControlNet extension installed. This update includes support for- *ControlNet V1.1 semantic segmentation
- EditAnything un-semantic segmentation (Not tested)
- Image layout generation (single image + batch process)
- *Image masking with categories (single image + batch process)
- *Inpaint not masked for ControlNet inpainting on txt2img panel
2023/04/29
: [Feature] API has been completely refactored. You can access all features for single image process through API. API documentation has been moved to wiki.
This extension has been significantly refactored on 2023/04/24
. If you wish to revert to older version, please git checkout 724b4db
.
- Test EditAnything
- Color selection for mask region and unmask region
- Option to crop mask and separate images according to bounding boxes
- Support
Resize by
in img2img panel - Batch ControlNet inpainting
Thanks for suggestions from github issues, reddit and bilibili to make this extension better.
There are already at least two great tutorials on how to use this extension. Check out this video (Chinese) from @ThisisGameAIResearch and this video (Chinese) from @OedoSoldier. You can also check out my demo.
You should know the following before submitting an issue.
-
This extension has almost moved into maintenance phase. Although I don't think there will be huge updates in the foreseeable future, Mikubill ControlNet Extension is still fast developing, and I'm looking forward to more opportunities to connect my extension to ControlNet. Despite of this, I will continue to deal with issues, and monitor new research works to see if they are worth supporting. I welcome any community contribution and any feature requests.
-
You must use gradio>=3.23.0 and WebUI>=
22bcc7be
to use this extension. A1111 WebUI is stable, and some integrated package authors have also updated their packages (for example, if you are using the package from @Akegarasu, i.e. 秋叶整合包, it has already been updated according to this video). Also, supporting different versions of WebUI will be a huge time commitment, during which I can create many more features. Please update your WebUI and it is safe to use. I'm not planning to support some old commits of WebUI, such asa9fed7c3
. -
You are required to install CUDA Toolkit to use GroundingDINO. If your device does not have CUDA Toolkit installed, GroundingDINO will not find
_C
. Follow steps decribed here to resolve the_C
problem. DO NOT skip steps. -
It is impossible to support color inpainting at this moment, because gradio wierdly enlarge the input image which slows down your browser, or even freeze your page. I have already implemented this feature, though, but I made it invisible. Note that ControlNet v1.1 inpainting model is very strong, and you do not need to rely on the traditional inpainting anymore. ControlNet v1.1 does not support color inpainting.
-
Inpaint-Anything and EditAnything and A LOT of other popular SAM extensions have been supported. For Inpaint-Anything, you may check this issue for how to use. For EditAnything, this extension has in-theory supported, but since they only published diffusers models which probably only work for SD 2.x + diffusers package, I am unable to test at this moment. I will update once they release models in lllyasviel format. I am always open to support any other interesting applications, submit a feature request if you find another interesting one.
-
If you have a job opportunity and think I am a good fit, please feel free to send me an email.
-
If you want to sponsor me, please go to sponsor section and scan the corresponding QR code.
Download this extension to ${sd-webui}/extensions
use whatever way you like (git clone or install from UI)
Choose one or more of the models below and put them to ${sd-webui}/models/sam
. Do not change model name, otherwise this extension may fail due to a bug inside segment anything.
Three types of SAM models are available. vit_h is 2.56GB, vit_l is 1.25GB, vit_b is 375MB. I myself tested vit_h on NVIDIA 3090 Ti which is good. If you encounter VRAM problem, you should switch to smaller models.
GroundingDINO packages, GroundingDINO models and ControlNet annotator models will be automatically installed the first time you use them.
GroundingDINO has been supported in this extension. It has the following functionalities:
- You can use text prompt to automatically generate bounding boxes. You can separate different category names with
.
. SAM can convert these bounding boxes to masks - You can use point prompts with ONE bounding box to generate masks
- You can go to
Batch Process
tab to do image matting and get LoRA/LyCORIS training set
However, there are some existing problems with GroundingDINO:
- GroundingDINO will be install when you firstly use GroundingDINO features, instead of when you initiate the WebUI. Make sure that your terminal can have access to GitHub, otherwise you have to install GroundingDINO manually. GroundingDINO models will be automatically downloaded from huggingFace. If your terminal cannot visit HuggingFace, please manually download the model and put it under
${sd-webui-sam}/models/grounding-dino
. - GroundingDINO requires your device to compile C++, which might take a long time and throw tons of exceptions. If you encounter
_C
problem, it's most probably because you did not install CUDA Toolkit. Follow steps decribed here. DO NOT skip steps. Otherwise, please go to Grounded-SAM Issue Page and submit an issue there. Despite of this, you can still use this extension for point prompts->segmentation masks even if you cannot install GroundingDINO, don't worry. - If you want to use point prompts, SAM can at most accept one bounding box. This extension will check if there are multiple bounding boxes. If multiple bounding boxes, this extension will disgard all point prompts; otherwise all point prompts will be effective. You may always select one bounding box you want.
For more detail, check How to Use and Demo.
Automatic Segmentation has been supported in this extension. It has the following functionalities:
- You can use SAM to enhance semantic segmentation and copy the output to control_v11p_sd15_seg
- You can generate random segmentation and copy the output to EditAnything ControlNet
- You can generate image layout and edit them inside PhotoShop. Both single image and batch process are supported.
- You can generate masks according to category IDs. This tend to be more accurate compared to purely SAM+GroundingDINO segmentation, if what you want is a large object.
However, there are some existing problems with AutoSAM:
- You are required to install Mikubill ControlNet Extension to use functionality 1 and 4. Please do not change the directory name (
sd-webui-controlnet
). - You can observe drastic improvement if you combine
seg_ufade20k
and SAM. You may only observe some slight improvement if you combine one of theOneformer
preprocessors (seg_ofade20k
&seg_ofcoco
). - EditAnything only released SD2.1 diffusers models. Even if they release lllyasviel models, their models might not be compatible with most community-based SD1.5 models.
- Image layout generation has a pretty bad performance for anime images. I discourage you from using this functionality if you are dealing with anime images. I'm not sure about the performance for real images.
If you have previously enabled other copys while using this extension, you may want to click Uncheck all copies
at the bottom of this extension UI, to prevent other copies from affecting your current page.
- Upload your image
- Optionally add point prompts on the image. Left click for positive point prompt (black dot), right click for negative point prompt (red dot), left click any dot again to cancel the prompt. You must add point prompt if you do not wish to use GroundingDINO.
- Optionally check
Enable GroundingDINO
, select GroundingDINO model you want, write text prompt (separate different categories with.
) and pick a box threshold (I highly recommend the default setting. High threshold may result in no bounding box). You must write text prompt if you do not wish to use point prompts. - Optionally enable previewing GroundingDINO bounding box and click
Generate bounding box
. You must write text prompt to preview bounding box. After you see the boxes with number marked on the top-left corner, uncheck all the boxes you do not want. If you uncheck all boxes, you will have to add point prompts to generate masks. - Click
Preview Segmentation
button. Due to the limitation of SAM, if there are multiple bounding boxes, your point prompts will not take effect when generating masks. - Choose your favorite segmentation.
- Optionally check
Expand Mask
and specify the amount, then clickUpdate Mask
.
- You may only copy image and mask to ControlNet inpainting.
- Optionally check
ControlNet inpaint not masked
to invert mask colors and inpaint regions outside of the mask. - Select the correct ControlNet index where you are using inpainting, if you wish to use Multi-ControlNet.
- Configurate ControlNet panel. Click
Enable
, preprocessor chooseinpaint_global_harmonious
, model choosecontrol_v11p_sd15_inpaint [ebff9138]
. There is no need to upload image to the ControlNet inpainting panel. - Write your prompts, configurate A1111 panel and click
Generate
.
- Update your ControlNet (MUST) and check
Allow other script to control this extension
on your ControlNet settings. - Check
Copy to Inpaint Upload & ControlNet Inpainting
. There is no need to select ControlNet index. - Configurate ControlNet panel. Click
Enable
, preprocessor chooseinpaint_global_harmonious
, model choosecontrol_v11p_sd15_inpaint [ebff9138]
. There is no need to upload image to the ControlNet inpainting panel. - Click
Switch to Inpaint Upload
button. There is no need to upload another image or mask, just leave them blank. Write your prompts, configurate A1111 panel and clickGenerate
.
- Choose your SAM model, GroundingDINO model, text prompt, box threshold and mask expansion amount. Enter the source and destination directories of your images.
- Choose
Output per image
to configurate the number of masks per bounding box. I highly recommend 3, since some masks might be wierd. - Click/unclick several checkboxes to configurate the images you want to save. See demo for what type of images these checkboxes represent.
- Click
Start batch process
and wait. If you see "Done" below this button, you are all set.
- Install and update Mikubill ControlNet Extension before using it.
- Configurate AutoSAM tunnable parameters according to descriptions here. Use default if you cannot understand.
- Choose preprocessor.
seg_ufade20k
,seg_ofade20k
andseg_ofcoco
are from ControlNet annotators. I highly recommend one ofseg_ofade20k
andseg_ofcoco
because their performance are far better thanseg_ufade20k
. They are all compatible withcontrol_v11p_sd15_seg
. Optionally enable pixel-perfect to automatically pick the best preprocessor resolution. Configure your target width and height on txt2img/img2img default panel before preview if you wish to enable pixel perfect. Otherwise you need to manually set a preprocessor resolution.random
is for EditAnything. There is no need to set preprocessor resolution for random preprocessor since it does not contain semantic segmentation, but you need to pick an image from the AutoSeg output gallery to copy to ControlNet. 1 represents random colorization of different mask regions which is reserved for future ControlNet, 2 represents fixed colorization which can be EditAnything ControlNet control image.
- Click preview segmentation image. For semantic semgentations, you will see 4 images where the left 2 are without SAM and the right 2 are with SAM. For random preprocessor, you will see 3 images where the top-left is the blended image, the top-right is random colorized masks and the down-left is for EditAnything ControlNet.
- Check
Copy to ControlNet Segmentation
and select the correct ControlNet index where you are using ControlNet segmentation models if you wish to use Multi-ControlNet. - Configurate ControlNet panel. Click
Enable
, preprocessor choosenone
, model choosecontrol_v11p_sd15_seg [e1f51eb9]
. There is no need to upload image to the ControlNet segmentation panel. - Write your prompts, configurate A1111 panel and click
Generate
.
- For single image, simply upload image, enter output path and click generate. You will see a lot of images inside the output directory.
- For batch process, simply enter source and destination directories and click generate. You will see a lot of images inside
${destination}/{image_filename}
directory.
- Choose preprocessor similar to ControlNet step 1. This is pure semantic segmentation so there is no random preprocessor.
- Enter category IDs separated by
+
. Visit here for ade20k and here for coco to get category->id map. Note that coco jumps some numbers, so the actual ID is line_number - 21. For example, if you want bed+person, your input should be 7+12 for ade20k and 59+0 for coco. - For single image, upload image, click preview and configurate copy similar to here for txt2img and here for img2img.
- For batch process, it is similar to Batch process step 2-4.
Point prompts demo (also so-called Remove/Fill Anything)
demo.mp4
GroundingDINO demo
demo_dino.mp4
Batch process demo
Input Image | Output Image | Output Mask | Output Blend |
---|---|---|---|
Semantic segmentation demo
video1408033456.mp4
Mask by Category demo (also so-called Replace Anything)
video1941660269.mp4
Mask by Category batch demo
Input Image | Output Image | Output Mask | Output Blend |
---|---|---|---|
Disclaimer: I have not thoroughly tested this extension, so there might be bugs. Bear with me while I'm fixing them :)
If you encounter a bug, please submit an issue. Please at least provide your WebUI version, your extension version, your browser version, errors on your browser console log if there is any, error on your terminal log if there is any, to save both of our time.
I welcome any contribution. Please submit a pull request if you want to contribute
Give me a star if you like this extension!
You can sponsor me via WeChat or Alipay.
Alipay | |
---|---|
You can use this API to get the currently available SAM models.
Example:
import requests
url = "http://localhost:7861/sam/sam-model"
response = requests.get(url)
reply = response.json()
print(reply)
# Example Output:
# ["sam_vit_b_01ec64.pth", "sam_vit_h_4b8939.pth", "sam_vit_l_0b3195.pth"]
You will receive a list of SAM models that are available, which you can then utilize to set the sam_model_name
parameter for the predict API.