Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 52 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,108 +1,111 @@
# Update v0.6: Added support for weighted prompts (based on the code from @lstein's [repo](https://github.com/lstein/stable-diffusion))
<h1 align="center">Optimized Stable Diffusion</h1>
<p align="center">
<img src="https://img.shields.io/github/last-commit/basujindal/stable-diffusion?logo=Python&logoColor=green&style=for-the-badge"/>
<img src="https://img.shields.io/github/issues/basujindal/stable-diffusion?logo=GitHub&style=for-the-badge"/>
<img src="https://img.shields.io/github/stars/basujindal/stable-diffusion?logo=GitHub&style=for-the-badge"/>
</p>

- You can now use weighted prompts to put relative emphasis on certain words.
eg. `--prompt tabby cat:0.25 white duck:0.75 hybrid`.
- The number followed by the colon represents the weight given to the words before the colon.
The weights can be fractions or integers.

# Optimized Stable Diffusion (Sort of)
This repo is a modified version of the Stable Diffusion repo, optimized to use less VRAM than the original by sacrificing inference speed.

This repo is a modified version of the Stable Diffusion repo, optimized to use lesser VRAM than the original by sacrificing on inference speed.
To achieve this, the stable diffusion model is fragmented into four parts which are sent to the GPU only when needed. After the calculation is done, they are moved back to the CPU. This allows us to run a bigger model while requiring less VRAM.

img2img to generate new image based on a prior image and prompt
<h1 align="center">Installation</h1>

- `optimized_img2img.py` Generate images using CLI
- `img2img_gradio.py` Generate images using gradio GUI
All the modified files are in the [optimizedSD](optimizedSD) folder, so if you have already cloned the original repository you can just download and copy this folder into the original instead of cloning the entire repo. You can also clone this repo and follow the same installation steps as the original (mainly creating the conda environment and placing the weights at the specified location).

txt2img to generate an image based only on a prompt
The only drawback is higher inference time which is still orders of magnitude faster than inference on CPU.

- `optimized_txt2img.py` Generate images using CLI
- `txt2img_gradio.py` Generate images using gradio GUI
<h1 align="center">Usage</h1>

### img2img
## img2img

- It can generate _512x512 images from a prior image and prompt on a 4GB VRAM GPU in under 20 seconds per image_ (RTX 2060 in my case).
- `img2img` can generate _512x512 images from a prior image and prompt on a 4GB VRAM GPU in under 20 seconds per image_ on an RTX 2060.

- The maximum size that can fit on 6GB GPU (RTX 2060) is around 576x768.

- For example, the following command will generate 20 512x512 images:

`python optimizedSD/optimized_img2img.py --prompt "Austrian alps" --init-img ~/sketch-mountains-input.jpg --strength 0.8 --n_iter 2 --n_samples 5 --H 576 --W 768`

### txt2img
## txt2img

- It can generate _512x512 images from a prompt on a 4GB VRAM GPU in under 25 seconds per image_ (RTX 2060 in my case).
- `txt2img` can generate _512x512 images from a prompt on a 4GB VRAM GPU in under 25 seconds per image_ on an RTX 2060.

- For example, the following command will generate 20 512x512 images:

`python optimizedSD/optimized_txt2img.py --prompt "Cyberpunk style image of a Telsa car reflection in rain" --H 512 --W 512 --seed 27 --n_iter 2 --n_samples 10 --ddim_steps 50`

---

## Optional arguments

### `--turbo` (Faster inference)

- If generating small batch of images (~ 1 to 4), use this argument to generate a single 512x512 image in under 25 seconds (on RTX 2060, excluding the time to load the model) using using only around 1GB of extra VRAM (4.3 GB instead of 3.3GB)
<h1 align="center">Arguments</h1>
## `--turbo`

**Increases inference speed at the cost of extra VRAM usage.**

- If generating small batch of images (~ 1 to 4), use this argument to generate an image or images in under 25 seconds (based on an RTX 2060, excluding the time to load the model) using around 1GB of extra VRAM (3.3 GB -> 4.3 GB usage on average)

## `--seed`

### `--seed` (Seed)
**Seed for image generation**, can be used to reproduce previously generated images. Defaults to a random seed if unspecified.

- The code will give the seed number along with each generated image. To generate the same image again, just specify the seed using `--seed` argument. Also, images will be saved with its seed number as its name.
- The code will give the seed number along with each generated image. To generate the same image again, just specify the seed using `--seed` argument. Images are saved with its seed number as its name by default.

- eg. If the seed number for an image is `1234` and it's the 55th image in the folder, the image name will be named `seed_1234_00055.png`. If no seed is given as an argument, a random initial seed will be choosen.
- For example if the seed number for an image is `1234` and it's the 55th image in the folder, the image name will be named `seed_1234_00055.png`.

### `--n_samples` (batch size)
## `--n_samples`

**Batch size/amount of images to generate at once.**

- To get the lowest inference time per image, use the maximum batch size `--n_samples` that can fit on the GPU. Inference time per image will reduce on increasing the batch size, but the required VRAM will also increase.

- If you get a CUDA out of memory error, try reducing the batch size `--n_samples`. If it doesn't work, the other option is to reduce the image width `--W` or height `--H` or both.

### `--n_iter`
## `--n_iter`

- Equivalent to running the script n_iter number of times. Only difference is that the model is loaded only once per n_iter iterations. Unlike `n_samples`, reducing it doesn't have an effect on VRAM required or inference time.
**Run *x* amount of times**

### `--H` & `--W` (Height & Width)
- Functions similarly to `--n_samples`, the only difference is that the model is loaded only once per n_iter iterations, (unlike `n_samples`) meaning reducing it doesn't have an effect on VRAM required or inference time.

- Both height and width should be a multiple of 64
## `--H` & `--W`

### `--precision autocast` or `--precision full` (Full or Mixed Precision)
**Height & width of the generated image.**

- Mixed Precision is enabled by default. If you don't have a GPU with tensor cores (RTX 10xx series), you may not be able use mixed precision. Use `--precision full` argument to disable it.
- Both height and width should be a multiple of 64.

### `--unet_bs`
## `--precision autocast` or `--precision full`

- Batch size for the unet model. Takes up a lot of extra RAM for very little improvement in inference time. unet_bs > 1 is not recommended!
**Whether or not to use `full` or `mixed` precision**

- Should be a multiple of 2x(n_samples)
- Mixed Precision is enabled by default. If you don't have a GPU with tensor cores (any GTX 10 series card), you may not be able use mixed precision. Use the `--precision full` argument to disable it.

### Gradio for Graphical User Interface
## `--unet_bs`

- You can also use gradio interface for img2img & txt2img instead of the CLI. Just activate the conda env and install the latest version of gradio using `pip install gradio` .
**Batch size for the unet model**

- Run img2img using `python optimizedSD/img2img_gradio.py` and txt2img using `python optimizedSD/img2img_gradio.py`.
- Takes up a lot of extra RAM for **very little improvement** in inference time. `unet_bs` > 1 is not recommended!

-- img2img_gradio.py has the feature to crop images. Look for the pen symbol in the image box after selecting the image.
- Should generally be a multiple of 2x(n_samples)

### Weighted Prompts
<h1 align="center">Using the Gradio GUI</h1>

- The prompts can also be weighted to put relative emphasis on certain words.
eg. `--prompt tabby cat:0.25 white duck:0.75 hybrid`.
- You can also use the built-in gradio interface for `img2img` & `txt2img` instead of the command line interface. Activate the conda environment and install the latest version of gradio using `pip install gradio`,

- The number followed by the colon represents the weight given to the words before the colon.The weights can be both fractions or integers.
- Run img2img using `python3 optimizedSD/img2img_gradio.py` and txt2img using `python3 optimizedSD/img2img_gradio.py`.

### Installation
- img2img_gradio.py has the feature to crop images. Look for the pen symbol in the image box after selecting the image.

- All the modified files are in the [optimizedSD](optimizedSD) folder, so if you have already cloned the original repo, you can just download and copy this folder into the orignal repo instead of cloning the entire repo. You can also clone this repo and follow the same installation steps as the original repo(mainly creating the conda env and placing the weights at the specified location).
<h1 align="center">Weighted Prompts</h1>

---
- Prompts can also be weighted to put relative emphasis on certain words.
eg. `--prompt tabby cat:0.25 white duck:0.75 hybrid`.

- To achieve this, the stable diffusion model is fragmented into four parts which are sent to the GPU only when needed. After the calculation is done, they are moved back to the CPU. This allows us to run a bigger model on a lower VRAM.
- The number followed by the colon represents the weight given to the words before the colon.The weights can be both fractions or integers.

- The only drawback is higher inference time which is still an order of magnitude faster than inference on CPU.

## Changelog

- v0.6: Added support for using weighted prompts. (based on @lstein [repo](https://github.com/lstein/stable-diffusion))
- v0.6: Added support for using weighted prompts. (based on @lstein's [repo](https://github.com/lstein/stable-diffusion))
- v0.5: Added support for using gradio interface.
- v0.4: Added support for specifying image seed.
- v0.3: Added support for using mixed precision.
Expand Down
Loading