Complete online service deployment of Stable Diffusion model in 20 minutes

AIGC's AI Painting

With the development and improvement of artificial intelligence technology, AI Generated Content (AIGC, artificial intelligence automatically generates content) has brought unprecedented help to people's work and life in content creation, specifically in that it can help humans improve the efficiency of content production , enrich the diversity of content production, and provide more dynamic and interactive content.

AIGC-related technologies can give full play to their technical advantages in terms of creativity, expressiveness, iteration, dissemination, and personalization to create new forms of digital content generation and interaction. In the past two years, AIGC has continued to shine in AI painting, AI composition, AI poetry, AI writing, AI video generation, AI speech synthesis and other fields; especially the AI ​​painting that has become popular all over the Internet recently, as users, we As long as you simply enter a few keywords, a painting will be born within a few seconds.

0-1
AI painting has brought huge productivity improvements to the industry: text-generated image (AI painting) is a new production method that generates images based on text. Compared with human creators, text-generated images show low creation costs and high speed. And the advantages of easy mass production.

Recently, AI painting products from various major manufacturers have also been released one after another. For individual users and developers who like to try early adopters, the two most worthwhile AI painting products are Midjourney and Stable Diffusion; Midjourney is a commercial AI painting tool , with the help of the Discord service platform, it is popularized rapidly and has been sought after and loved by many users; while Stable Diffusion has chosen to take the road of open source, and in some respects, its drawing potential is comparable to that of Midjourney, and it is favored by the majority of open source enthusiasts favor.

At present, stable-diffusion-webui has become the hottest Stable Diffusion deployment code on GitHub. As long as there is a host or server with a graphics card (the larger the memory, the faster the image output), the code can run perfectly, and you can customize it according to your preferences. to combine various interesting visual models.

But for more individual users, a server computer with a graphics card has become a major bottleneck that prevents them from deploying personal Stable Diffusion. Do our ordinary entrepreneurs and developers have the opportunity to train and deploy AIGC in the field we are optimistic about? As for the model, the answer is yes. In the current era of cloud services, everyone has the opportunity to become a cutting-edge technology explorer. Recently, I was invited by Amazon Cloud Technology to conduct technical practice on the Amazon SageMaker platform. Before that, I had been suffering from the lack of suitable GPU servers and it was difficult to quickly deploy my own Stable Diffusion. This problem disappeared after encountering Amazon SageMaker.

A few days ago, an old classmate in Shanghai told me that the Amazon Cloud Technology China Summit will be held at the Shanghai World Expo Center on June 27-28, 2023, and asked me if I would be interested in participating together. It's a pity that my annual leave is not enough. Friends who are interested and qualified can consider participating. The opportunity is hard-won: facing the various global digital technology innovations, in this best era, it is also the most challenging In this era, we should participate in the activities of some major international companies, and we can directly experience the extraordinary cloud technology experience brought to us by the full-scale explosion of new technologies, new trends, and new opportunities. Friends of Sheniu can also seize the opportunity to properly make friends with the big guys in the circle, and the space for internship and job choices will be expanded.

Using the Amazon SageMaker platform to quickly complete the online web deployment of the AIGC model reasoning service has brought me a lot of inspiration and surprises. It turns out that AI model reasoning deployment on the cloud can be so simple, elegant and smooth. During this AIGC technology practice, I also learned a lot of useful knowledge and skills. The following blog post will take everyone on a journey of AIGC model (Stable Diffusion 2.0) web service deployment to experience how to deploy in the cloud. Implemented AI model service.

Complete online service deployment of Stable Diffusion model in 20 minutes

Meet Amazon SageMaker

Amazon SageMakeris a fully managed machine learning service: With SageMaker's capabilities, data scientists and developers can quickly and easily build and train machine learning models, then deploy directly to production-ready hosting environments. SageMaker covers the entire workflow of ML to label and prepare data, select algorithms, train models, tune and optimize models for deployment, predict and perform operations.

After more than a week of learning and practical experience, I found that this platform is simply an AI service tool tailored for us entrepreneurs and individual developers.许多AI工程项目,我们只需去构造好自己的训练集和测试集,其余的模型训练、推理、部署,Amazon SageMaker 都能够帮我们轻松完成。

The Stable Diffusion 2.0 shared in this blog post, by referring to the official technical documents, only took about 20 minutes, and I successfully built a set of smooth AI painting online services on Amazon SageMaker. Next, let us Let's unravel together.

Environment building and model inference with Amazon SageMaker

1. Create a jupyter notebook operating environment

在搜索框中搜索 SageMaker ,如下图所示

1-0

这里我们创建一个笔记本编程实例

1-1

我这里选择的配置如下:

1-2

选择角色,其他的默认即可

1-4

大概5分钟左右,实例就创建成功啦

1-5

上传刚刚下载的代码

1-5

直接打开这个代码

1-6

选择合适的conda环境

1-7

2. Run all codes with one click

这里我们直接一键运行运行所有代码即可,代码执行过程中会依次完成 Stable Diffusion 模型相关类和函数的代码定义、推理测试,并打包代码和模型,然后部署模型至Sagemaker 推理终端节点 (PS:这里的所有代码运行完毕大概需要5到10分钟左右)

1-8

The key code analysis is as follows

1. Environment preparation, code model download

检查当前 pyTorch 版本

!nvcc --version
!pip list | grep torch

安装 Stable Diffusion 代码运行额外需要的依赖库,这网速飞快

!sudo yum -y install pigz
!pip install -U pip
!pip install -U transformers==4.26.1 diffusers==0.13.1 ftfy accelerate
!pip install -U torch==1.13.1+cu117 -f https://download.pytorch.org/whl/torch_stable.html
!pip install -U sagemaker
!pip list | grep torch

下载代码和模型文件,这里直接下载Hugging Face提供的代码和模型即可

1-10

2. Configure and use the model in Notebook

直接调用 函数进行模型加载

import torch
import datetime
from diffusers import StableDiffusionPipeline
# Load stable diffusion 
pipe = StableDiffusionPipeline.from_pretrained(SD_MODEL, torch_dtype=torch.float16)

在 Cuda 上进行模型的推理,这里 Stable Diffusion V2 能够支持生成的最大图像尺寸为 768 * 768

# move Model to the GPU
torch.cuda.empty_cache()
pipe = pipe.to("cuda")

# V1 Max-H:512,Max-W:512
# V2 Max-H:768,Max-W:768

print(datetime.datetime.now())
prompts =[
    "Eiffel tower landing on the Mars",
    "a photograph of an astronaut riding a horse,van Gogh style",
]
generated_images = pipe(
    prompt=prompts,
    height=512,
    width=512,
    num_images_per_prompt=1
).images  # image here is in [PIL format](https://pillow.readthedocs.io/en/stable/)

print(f"Prompts: {prompts}\n")
print(datetime.datetime.now())

for image in generated_images:
    display(image)

友情提示 :如果报错,遇到推理时 GPU 内存不够,则可以尝试以下三种方式进行解决

  1. Try to generate a smaller resolution image
  2. Reduce the number of generated images
  3. Upgrade the model and choose a stronger GPU server

3. Deploy the model to the Sagemaker inference terminal node

我们这里直接使用 AWS 的 SageMaker Python 开发工具包部署模型刚刚已经验证能够运行成功的模型和打包好的代码。

  • Write the initialized Sagemaker code to deploy the inference endpoint
import sagemaker
import boto3

'''
# 创建 Session
'''
sess = sagemaker.Session()
# sagemaker session bucket -> used for uploading data, models and logs
# sagemaker will automatically create this bucket if it not exists
sagemaker_session_bucket=None

if sagemaker_session_bucket is None and sess is not None:
    # set to default bucket if a bucket name is not given
    sagemaker_session_bucket = sess.default_bucket()

try:
    role = sagemaker.get_execution_role()
except ValueError:
    iam = boto3.client('iam')
    role = iam.get_role(RoleName='sagemaker_execution_role')['Role']['Arn']

sess = sagemaker.Session(default_bucket=sagemaker_session_bucket)

print(f"sagemaker role arn: {
      
      role}")
print(f"sagemaker bucket: {
      
      sess.default_bucket()}")
print(f"sagemaker session region: {
      
      sess.boto_region_name}")
  • Create the inference.py script to load and reason the model
%%writefile ./$SD_MODEL/code/inference.py
import base64
import torch
from io import BytesIO
from diffusers import StableDiffusionPipeline

'''
# 加载模型到CUDA
'''
def model_fn(model_dir):
    # Load stable diffusion and move it to the GPU
    pipe = StableDiffusionPipeline.from_pretrained(model_dir, torch_dtype=torch.float16)
    pipe = pipe.to("cuda")

    return pipe

'''
# 推理方法
'''
def predict_fn(data, pipe):

    # 解析参数 get prompt & parameters
    prompt = data.pop("prompt", "")
    # set valid HP for stable diffusion
    height = data.pop("height", 512)
    width = data.pop("width", 512)
    num_inference_steps = data.pop("num_inference_steps", 50)
    guidance_scale = data.pop("guidance_scale", 7.5)
    num_images_per_prompt = data.pop("num_images_per_prompt", 1)
    # 传入参数,调用推理 run generation with parameters
    generated_images = pipe(
        prompt=prompt,
        height=height,
        width=width,
        num_inference_steps=num_inference_steps,
        guidance_scale=guidance_scale,
        num_images_per_prompt=num_images_per_prompt,
    )["images"]

    # create response
    encoded_images = []
    for image in generated_images:
        buffered = BytesIO()
        image.save(buffered, format="JPEG")
        encoded_images.append(base64.b64encode(buffered.getvalue()).decode())

    # create response
    return {
    
    "generated_images": encoded_images}

Create front-end and back-end web applications on Amazon Cloud9

AWS Cloud9 is a cloud-based integrated development environment (IDE) that requires only a browser to write, run, and debug code. Includes a code editor, debugger, and terminal, and comes pre-packaged with essential tools for common programming languages ​​like JavaScript, Python, PHP, and starts new projects without installing files or configuring a development computer.

1. Create a cloud service instance and install it in the web environment

这里我试用了 Cloud9 云服务,在查找服务处搜索 Cloud9,并点击进入Cloud9服务面板即可

2-1

点击创建环境

2-2

我这里的设置如下

2-3

其他部分配置保持默认,在页面底部点击橙色的 Create 按钮创建环境。 环境的创建将花费1-2分钟的时间。

2-4

创建成功之后,点击 open 进入服务控制台

2-5

粘贴左侧的代码,复制到控制台bash窗口进行运行,会自动下载和解压代码

cd ~/environment
wget https://static.us-east-1.prod.workshops.aws/public/73ea3a9f-37c8-4d01-ae4e-07cf6313adac/static/code/SampleWebApp.zip
unzip SampleWebApp.zip

#  在 AWS Cloud9 上安装 Flask 和 boto3
pip3 install Flask

pip3 install boto3

2-6

2. Run and start the web service, enter the image parameters and prompt words you want to generate, and call the inference service

依赖的环境安装好之后,就可以运行这个服务代码

2-7

服务启动成功之后,访问 127.0.0.1:8080 即可访问本地服务;设定 width 和 Length 参数,以及想要生成的图片描述,然后点击提交

2-8

等待 几秒钟之后,就得到了上面输入的两个 提示词对应生成的图像,看得出来效果还是非常不错的;

  • After testing, it is found that even if the prompt word input is the same each time, the output generated by the model is not fixed
  • The more accurate the prompt words entered, the easier and better the generated image effect will be
  • Based on the Amazon SageMaker service platform, it is so fast (less than half an hour after proficiency) to build a set of AI model online reasoning calls on the web side. Sure enough, good technology is the primary productivity

2-9

3. Example of text image generation

Stable Diffusion has very high requirements for the prompt words. The more detailed the prompt words you input, the better the model can understand, and the closer the generated image content will be to the expected one, the better the generated quality will be;


这里提供3组文本图像生成的示例,方便各位同学参考:

prompt word Generate image example 1 Generate image example 2
A rabbit is piloting a spaceship 5-0 5-1
Driverless car speeds through the desert $12 -----
A small cabin on top of a snowy mountain in the style of Disney, artstation $1 -----

Amazon cloud service experience summary

Through a quick review of the official tutorial, the cloud service deployment of the Stable Diffusion 2.0 project was successfully completed in 20 minutes, which made me further realize the power of AWS (Amazon Web Services):

  1. Scalability: AWS allows users to easily expand server resources according to business needs, which can help customers save costs and improve efficiency;
  2. Reliability: AWS has a continuously leading cloud service infrastructure with a highly reliable distributed architecture, which can provide stable and reliable services in the global business scope;
  3. Security: AWS provides a variety of security features and tools to effectively help users protect data and applications;
  4. Flexibility: AWS provides a variety of services and functions that can be customized to meet the specific needs of users;
  5. Reliable: AWS has established a good corporate image and service reputation in the industry, and has already become the first choice for cloud transformation of millions of enterprises and individual customers around the world.

AWS 相比其他云服务厂商,还具备以下优点:

  1. Amazon Cloud Technology provides AWS with a cloud platform with extensive global coverage and in-depth services, and more than 200 fully functional services are available;
  2. Provides a set of pay-as-you-go productivity applications built on top of AWS, enabling team users to quickly, securely, and cost-effectively check project status, collaborate on content, and communicate in real time;
  3. Provide the widest and deepest machine learning services, supporting cloud infrastructure and extensively verified algorithm models, so that every developer, data scientist and expert practitioner can use machine learning technology to efficiently cut into the landing and deployment of AI services ;
  4. Provide a full range of online development tools, users can host code faster and more efficiently and build, test and deploy applications;

More cutting-edge technology practices are waiting for everyone to explore together

Refer to the AI ​​model cloud inference deployment practical documents and video tutorials provided by the SageMaker platform to carry out real cloud service deployment operations step by step, which made me realize again the powerful productivity brought by AI technological breakthroughs in various fields, and by using Amazon SageMaker The platform's AI model service deployment greatly simplifies the difficulty for our ordinary developers to deploy AI model services, making it no longer difficult for small and medium-sized enterprises and individual developers to quickly implement AI services.

通过使用 亚马逊 SageMaker 服务平台,我们只需要一个浏览器,即可编写、运行和调试各种代码,无需进行复杂的机器适配和环境搭建,能够快速完成AI模型的推理、测试、验证部署工作。

If you also want to experience the latest and hottest Amazon cloud technology developer tools and services, just click the link below to explore and practice more interesting AI technologies with the engineers of the Amazon cloud technology team. , AI Dream Chaser

亚马逊云科技中国峰会即将举办,有兴趣的同学可以尽早关注,报名参加,和 Amazon 一起探索前沿科技

9-6

Guess you like

Origin blog.csdn.net/sinat_28442665/article/details/131116898