ForkBasetenBasetenpublished Feb 20, 2023seen 5d

basetenlabs/ControlNet

forked from lllyasviel/ControlNet

Open original ↗

Captured source

source ↗
published Feb 20, 2023seen 5dcaptured 10hhttp 200method plain

basetenlabs/ControlNet

Description: Let us control diffusion models

Language: Python

License: Apache-2.0

Stars: 1

Forks: 0

Open issues: 1

Created: 2023-02-20T22:52:20Z

Pushed: 2023-04-04T15:45:54Z

Default branch: main

Fork: yes

Parent repository: lllyasviel/ControlNet

Archived: no

README:

ControlNet

Official implementation of Adding Conditional Control to Text-to-Image Diffusion Models.

ControlNet is a neural network structure to control diffusion models by adding extra conditions.

![img](github_page/he.png)

It copys the weights of neural network blocks into a "locked" copy and a "trainable" copy.

The "trainable" one learns your condition. The "locked" one preserves your model.

Thanks to this, training with small dataset of image pairs will not destroy the production-ready diffusion models.

The "zero convolution" is 1×1 convolution with both weight and bias initialized as zeros.

Before training, all zero convolutions output zeros, and ControlNet will not cause any distortion.

No layer is trained from scratch. You are still fine-tuning. Your original model is safe.

This allows training on small-scale or even personal devices.

This is also friendly to merge/replacement/offsetting of models/weights/blocks/layers.

FAQ

Q: But wait, if the weight of a conv layer is zero, the gradient will also be zero, and the network will not learn anything. Why "zero convolution" works?

A: This is not true. [See an explanation here](docs/faq.md).

Stable Diffusion + ControlNet

By repeating the above simple structure 14 times, we can control stable diffusion in this way:

![img](github_page/sd.png)

Note that the way we connect layers is computational efficient. The original SD encoder does not need to store gradients (the locked original SD Encoder Block 1234 and Middle). The required GPU memory is not much larger than original SD, although many layers are added. Great!

Production-Ready Pretrained Models

First create a new conda environment

conda env create -f environment.yaml conda activate control

All models and detectors can be downloaded from our Hugging Face page. Make sure that SD models are put in "ControlNet/models" and detectors are put in "ControlNet/annotator/ckpts". Make sure that you download all necessary pretrained weights and detector models from that Hugging Face page, including HED edge detection model, Midas depth estimation model, Openpose, and so on.

We provide 9 Gradio apps with these models.

All test images can be found at the folder "test_imgs".

News

2023/02/20 - Implementation for non-prompt mode released. See also [Guess Mode / Non-Prompt Mode](#guess-anchor).

2023/02/12 - Now you can play with any community model by Transferring the ControlNet.

2023/02/11 - [Low VRAM mode](docs/low_vram.md) is added. Please use this mode if you are using 8GB GPU(s) or if you want larger batch size.

ControlNet with Canny Edge

Stable Diffusion 1.5 + ControlNet (using simple Canny edge detection)

python gradio_canny2image.py

The Gradio app also allows you to change the Canny edge thresholds. Just try it for more details.

Prompt: "bird" ![p](github_page/p1.png)

Prompt: "cute dog" ![p](github_page/p2.png)

ControlNet with M-LSD Lines

Stable Diffusion 1.5 + ControlNet (using simple M-LSD straight line detection)

python gradio_hough2image.py

The Gradio app also allows you to change the M-LSD thresholds. Just try it for more details.

Prompt: "room" ![p](github_page/p3.png)

Prompt: "building" ![p](github_page/p4.png)

ControlNet with HED Boundary

Stable Diffusion 1.5 + ControlNet (using soft HED Boundary)

python gradio_hed2image.py

The soft HED Boundary will preserve many details in input images, making this app suitable for recoloring and stylizing. Just try it for more details.

Prompt: "oil painting of handsome old man, masterpiece" ![p](github_page/p5.png)

Prompt: "Cyberpunk robot" ![p](github_page/p6.png)

ControlNet with User Scribbles

Stable Diffusion 1.5 + ControlNet (using Scribbles)

python gradio_scribble2image.py

Note that the UI is based on Gradio, and Gradio is somewhat difficult to customize. Right now you need to draw scribbles outside the UI (using your favorite drawing software, for example, MS Paint) and then import the scribble image to Gradio.

Prompt: "turtle" ![p](github_page/p7.png)

Prompt: "hot air balloon" ![p](github_page/p8.png)

Interactive Interface

We actually provide an interactive interface

python gradio_scribble2image_interactive.py

However, because gradio is very buggy and difficult to customize, right now, user need to first set canvas width and heights and then click "Open drawing canvas" to get a drawing area. Please do not upload image to that drawing canvas. Also, the drawing area is very small; it should be bigger. But I failed to find out how to make it larger. Again, gradio is really buggy.

The below dog sketch is drawn by me. Perhaps we should draw a better dog for showcase.

Prompt: "dog in a room" ![p](github_page/p20.png)

ControlNet with Fake Scribbles

Stable Diffusion 1.5 + ControlNet (using fake scribbles)

python gradio_fake_scribble2image.py

Sometimes we are lazy, and we do not want to draw scribbles. This script use the exactly same scribble-based model but use a simple algorithm to synthesize scribbles from input images.

Prompt: "bag" ![p](github_page/p9.png)

Prompt: "shose" (Note that "shose" is a typo; it should be "shoes". But it still seems to work.) ![p](github_page/p10.png)

ControlNet with Human Pose

Stable Diffusion 1.5 + ControlNet (using human pose)

python gradio_pose2image.py

Apparently, this model deserves a better UI to directly manipulate pose skeleton. However, again, Gradio is somewhat difficult to customize. Right now you need to input an image and then the Openpose will detect the pose for you.

Prompt: "Chief in the kitchen" ![p](github_page/p11.png)

Prompt: "An astronaut on the moon" ![p](github_page/p12.png)

ControlNet with Semantic Segmentation

Stable Diffusion 1.5 + ControlNet (using semantic segmentation)

python gradio_seg2image.py

This model use ADE20K's segmentation protocol. Again, this model deserves a better UI to directly draw the segmentations. However, again, Gradio is somewhat difficult to customize.…

Excerpt shown — open the source for the full document.