AI Painting Stable Diffusion Practice
1. Create a GPU UHost
For the creation process, please refer to Creating the First UHost.
When creating a GPU UHost, please choose “AI painting stable diffusion platform” for the image. The operation path is: Image market—>AI painting stable diffusion platform. This image provides a convenient installation of stable diffusion and comes with the built-in environment: CentOS 7.8.
Recommended types and configurations:
GPU UHost T4/T4S Minimum configuration: 8 cores 32G single GPU
GPU UHost V100S Minimum configuration: 10 cores 32G single GPU
GPU UHost P40 Minimum configuration: 8 cores 32G single GPU
Please choose 32GB or above memory, otherwise it may trigger OOM when loading the model.
Bind the EIP and allow TCP 8888 port in the external network firewall.
2. Use Stable Diffusion
2.1 Method One: Use Jupyter Notebook to Create a New LDM Environment (Recommended)
Access Jupyter by your external network IP address: http://EIP:8888
Enter the token (You can check the configuration of c.NotebookApp.token in /root/.jupyter/jupyter_notebook_config.py, and you can modify it as you wish.)
Create a new LDM.
If you use English version description, please refer to the following example:
Add and run the following sample code (take “A thriving view alongside Pearl of the Orient in Shanghai, by Van Gogh, oil painting trending on artstation HQ” as an example).
from torch import autocast
from diffusers import StableDiffusionPipeline
from IPython.display import Image
width = 512
height = 512
pipe = StableDiffusionPipeline.from_pretrained(
"/root/demo/stable-diffusion-v1-4").to("cuda")
prompt = "A thriving view alongside Pearl of the Orient in Shanghai , by Van Gogh, oil painting trending on artstation HQ"
with autocast("cuda"):
image = pipe(prompt, height, width)["sample"][0]
image.save("Van_Gogh_Style_Shanghai.png")
# Show the image in web
Image(filename = 'Van_Gogh_Style_Shanghai.png', width=width, height=height)
You can view the generated picture immediately.
If you use Chinese version description, please refer to the following example:
Add the following example code and run it (using “Vast desert, solitary smoke rising straight; long river, setting sun round, oil painting” as an example).
from diffusers import StableDiffusionPipeline
from IPython.display import Image
width = 512
height = 512
pipe = StableDiffusionPipeline.from_pretrained("IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-v0.1").to("cuda")
prompt = 'Vast desert, solitary smoke rising straight; long river, setting sun round, oil painting'
image = pipe(prompt, guidance_scale=7.5).images[0]
image.save("Oil_painting.png")
# Show the image in web
Image(filename = 'Oil_painting.png', width=width, height=height)
You can view the generated picture immediately.
- Select the code cell and click [Run]. If you see “In[*]”, that means the code is running. Please wait patiently for the image generation result.
- When adjusting the canvas size, ensure both the width and height are multiples of 8.
For a quick start, you can also directly use SD_Demos.ipynb under the demo/ directory. It comes preloaded with both English and Chinese language models, as shown below:
2.2 Method Two: Use Sample Script of Stable Diffusion
2.2.1 Switch to Conda Environment
conda activate ldm
2.2.2 Run the Sample Script
Run the script, input your expected description of the painting, and you will get the picture (Take “A valley full of flowers, by Van Gogh, oil painting trending on artstation HQ” as an example), the generated picture will be found in the directory /root/stable-diffusion/outputs/txt2img-samples/.
cd stable-diffusion
python scripts/txt2img.py --prompt "A valley full of flowers , by Van Gogh, oil painting trending on artstation HQ"
2.2.3 Check the Picture via Jupyter Page
Access Jupyter by your external network IP address: http://EIP:8888
Enter the token (You can check the configuration of c.NotebookApp.token in /root/.jupyter/jupyter_notebook_config.py, and you can modify it as you wish.)
Click to preview the picture according to the guidance.