Build and run AI image generation Stable Diffusion environment

        Stable Diffusion is a generative model based on the diffusion process, proposed by Ge et al. 2021. The model exploits a stable distribution of random variables to generate high-quality images by recursively applying a diffusion process. Compared with other generative models, Stable Diffusion has significant advantages in generating high-quality images. Specifically, the model generates images with better detail preservation and more natural appearance. Through the study of different diffusion times, Stable Diffusion can also realize the resolution of generated images by adjusting the time precision.

         In practice, Stable Diffusion is widely used in applications such as image generation, compression, restoration, and enhancement. In addition, the model can also be combined with other deep learning techniques, such as GAN and VAE, to improve the quality and diversity of generated images. In conclusion, Stable Diffusion is a very practical and potential image generation model, which will play an important role in future research and applications.

        This article mainly introduces the construction of the Stable Diffusion webui environment, and will gradually introduce parameter settings, API construction, basic principles of the model, training, deployment, etc. For specific updates, you can follow the official account below the article, or follow this column. All related articles willbe updated in " Python AIGC Large Model Training and Reasoning from Scratch ", the address is "https://blog.csdn.net/suiyingy/article/details/130169592".

1 Environment Construction

        For graphics driver, CUDA, CUDNN, Docker, Python and other environments, please refer to another blog post in this column, "Docker AIGC and other large-scale model deep learning environment construction (full and detailed version)", the address is "https://blog.csdn.net /suiyingy/article/details/130285920".

1.1 Create a Python environment

        Here use conda to create a Python 3.10 environment, the command is as follows.

conda create -n stdf python=3.10 -y
conda activate stdf

1.2 stable-diffusion-webui environment installation

        stable-diffusion-webui provides a front-end page for the Stable Diffusion model to generate images, and its official Github project address is "https://github.com/AUTOMATIC1111/stable-diffusion-webui". The official project page provides installation steps, including some automatic installation scripts. The installation is mainly done manually here, and it is verified on Ubuntu 18.04 and Ubuntu 20.04. The environment installation commands are as follows. The advantage of manual installation is to understand the error message in time and fix it.

conda activate stdf
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
pip install -r requirements_versions.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

2 start stable-diffusion-webui

        The startup command of stable-diffusion-webui is "python launch.py". After startup, the program will automatically install and download other environments it depends on. The problems and solutions during the installation process are as follows. After running successfully, we can access it through a web browser, the default port number is 7860, and the access address is "IP:7860" or "0.0.0.0:7860" or "127.0.0.1:7860" or "localhost:7860". If you need to access through other hosts, you need to let the firewall allow this port, the command is "sudo ufw allow 7860".

        After running successfully, the page is as follows. The problems and solutions during the installation process are as follows.

2.1 openclip cannot be installed

        The reason why Openclip cannot be installed may be that the corresponding resources on github cannot be obtained due to network problems, so it can be solved by replacing it with the corresponding resources on gitee. The specific modification method is to replace the line of openclip_package in the launch.py ​​file with the following content, which is located around line 234.

openclip_package = os.environ.get('OPENCLIP_PACKAGE', "git+https://gitee.com/ufhy/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b")

2.2 libGL.so

        The error message of the lack of libGL.so library is "ImportError: libGL.so.1: cannot open shared object file: No such file or directory". This error is often encountered when installing opencv, which is caused by the lack of corresponding libraries in the system itself. The solution is as follows.

apt update
apt install libgl1-mesa-glx -y

2.3 NaN

        Due to the accuracy problem, the model has a NaN error, so that the picture cannot be generated normally. This problem may occur when switching to Stable Diffusion v2.1 version model. The specific questions are as follows:

NansException: A tensor with all NaNs was produced in VAE. This could be because there's not enough precision to represent the picture. Try adding --no-half-vae commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.

        The solution is to replace commandline_args = os.environ.get('COMMANDLINE_ARGS', "") in the launch.py ​​file with the following, located around line 13.

commandline_args = os.environ.get('COMMANDLINE_ARGS', "--no-half")

3 Model Replacement

        There are multiple versions of the Stable Diffusion model, and the v1.5 version model is automatically downloaded when the launch.py ​​program is currently run. The Stable Diffusion model is stored under models/Stable-diffusion/, as shown in the figure below. We can go to the huggingface website "https://huggingface.co/" to search and download the corresponding model, and then put the model in this folder. When downloading, you only need to download the model file with the suffix .safetensors.

         For example, the download address of stable-diffusion-2-1 is "https://huggingface.co/stabilityai/stable-diffusion-2-1", click "Files and versions" on the page to see the corresponding model file . We can download only the model files with the .safetensors suffix, or download the entire content through Git LFS. For Git LFS installation and model download, please refer to "ChatGPT Flat Replacement - ChatGLM Environment Construction and Deployment Operation", the address is "https://blog.csdn.net/suiyingy/article/details/130370190".

 4 Start port modification

        As mentioned above, the default startup port of the program is 7860, and we can specify the port number through the command "python launch.py ​​--port 5800". You can also change the port number by modifying line 260 of the webui.py file, as shown below. Here you can allow public network access after directly setting share to True. It is best to set Server_name to "0.0.0.0". If it is set to "127.0.0.1", it may also cause the public network to be inaccessible.

app, local_url, share_url = shared.demo.launch(
            share=True,
            server_name='0.0.0.0',
            server_port=5900,

5 running in the background

        By default, after closing the terminal window that launched launch.py, the program exits. If you want the program to run in the background, you can use the nohup command.

        (1) Save the log to nohup.out

        "nohup python launch.py ​​&" will keep the program running in the background, and the log information will be saved to the nohup.out file.

        (2) Do not save logs

        The command to run in the background without saving the log is ": nohup python launch.py ​​> /dev/null 2>&1 &".

        (3) Close the process

        If you need to close the background process, you can query the process ID through "ps -aux | grep launch.py", and close the process through "kill -9 process ID".

        This article mainly introduces the construction of the Stable Diffusion webui environment. Later, the parameter settings, API construction, basic principles of the model, training, and deployment of Stable Diffusion will be introduced in detail. For specific updates, you can follow the official account below the article, or follow this column. All related articles will be updated in " Python AIGC Large Model Training and Reasoning from Scratch ", the address is "https://blog.csdn.net/suiyingy/article/details/130169592".

Guess you like

Origin blog.csdn.net/suiyingy/article/details/128896426