Using Amazon SageMaker to build a high-quality AI painting model Stable Diffusion

748e0b55f7afc1d619aa23a2ce6d17d6.gif

outside_default.png

foreword

Recently, with ChatGPT, a new generation of large-scale AI chatbots, becoming popular in the technology circle, the field of Artificial Intelligence Generated Content (AIGC) has begun to attract widespread attention from academia, industry and even ordinary users. AIGC has set off a frenzy of artificial intelligence with its unique "creativity" and creation speed beyond the reach of human beings. However, using artificial intelligence models to generate pictures, videos, etc. requires a large amount of data to train the models, and the requirements for computing power are exponentially higher than those of simple models. In order to quickly and efficiently process data sets and build generative models, cloud Training and deploying artificial intelligence models in China has become the first choice for most users and companies.

Recently, I was invited to participate in the cloud exploration laboratory activity of Amazon Cloud Technology, and created and deployed the Stable Diffusion generation model based on Amazon SageMaker to generate high-quality images, and fully experienced the comprehensive capabilities provided by Amazon SageMaker in the entire model construction process The advantages brought by advanced machine learning tools can perform machine learning tasks more quickly and efficiently, and also have many advantages such as flexibility, scalability, and ease of use. Next, let's review the entire process of generative model construction!

Cloud Discovery Lab Activities:

https://dev.amazoncloud.cn/experience?trk=cndc-detail

This article will first introduce the basic concept and development process of AIGC, and explain the current advanced image generation model Stable Diffusion, then introduce the main components of Amazon SageMaker and how to solve the pain points in the process of building artificial intelligence models, and finally use Amazon SageMaker to build Stable Diffusion model to demonstrate the advantages of Amazon SageMaker in the process of building, training and deploying artificial intelligence models.

outside_default.png

Amazon SageMaker and Machine Learning

outside_default.png

   1.1 Machine learning process

Artificial Intelligence (AI) is a systematic science and technology that studies theories, methods and applications for simulating and expanding human intelligence, which enables computers to execute corresponding strategies based on available data without explicitly programming the strategy. AI makes machines intelligent by using computer programs to simulate human behavior. The goal of artificial intelligence is to create intelligent machines that can think similarly to humans, or to extend human intelligence through artificial intelligence technology to solve practical problems. In the past few years, many artificial intelligence systems have made breakthroughs and can be applied to solve various complex problems.

Generally speaking, a complete machine learning process usually includes the following steps:

> Data collection and preparation: In the process of machine learning, data is crucial, and relevant data needs to be collected, preprocessed and cleaned to ensure the quality and consistency of training data;

> Feature engineering: perform operations such as feature selection, feature extraction, and feature transformation on data to extract useful information and convert it into a form that can be used for training models;

> Model construction and training: According to the actual application scenario, select and construct a suitable model, and use the training data to train it, which usually requires the process of model selection, hyperparameter adjustment, model construction, model training and model evaluation;

> Model verification and optimization: verify and optimize the model to ensure the accuracy and stability of the model, usually including model verification, model optimization and model adjustment, to further ensure the robustness of the model in the actual production environment

> Model deployment and monitoring: Deploy the model to the production environment, monitor and manage it to ensure the reliability and efficiency of the model, usually including the process of model deployment, model monitoring and model update.

fadc0245e43359e9cd7738081389e0e7.png

In short, the machine learning process is a very complex and challenging process that requires in-depth research and exploration of data, models, and algorithms. Usually, the complete process of a machine learning model from data collection to model deployment and application takes about 6- 18 months, and usually face the following problems:

● Machine learning model training requires a large amount of data, and the data must be cleaned and preprocessed to ensure data quality and consistency, which often takes a lot of time and effort;

● In the process of machine learning model training, it is necessary to select an appropriate model and perform operations such as hyperparameter tuning to obtain the best model performance, which usually requires multiple experiments and tests;

● Machine learning model training requires a lot of computing resources, including CPU, GPU, memory and storage space, etc., especially when dealing with large-scale data sets and complex models, it requires a lot of upfront investment, which is very important for ordinary users and small companies Not feasible.

The machine learning model training process requires the cooperation of multiple tools, a lot of time and energy for data processing, etc. Without integrated tools for the entire machine learning workflow, the development of machine learning models will be very complicated and expensive. Amazon Web Services (Amazon Web Services) is committed to building a machine learning platform for every developer and data scientist, and provides many powerful solutions for machine learning to improve the efficiency and quality of machine learning model construction, training, and deployment. Amazon cloud technology is a cloud computing service provided by Amazon. It is a flexible and scalable cloud platform that provides a large number of infrastructure, platforms and software services to help build and run various applications and services. Its services include computing, storage, database, analysis, network and security to provide cloud computing solutions with high reliability, high scalability and low cost.

Since 2018, Amazon Cloud Technology has released a series of products and services for machine learning, such as Amazon SageMaker, Amazon Machine Learning, etc., which have greatly lowered the threshold for machine learning and made it easier for users to build machine learning applications. It is getting easier and easier, which promotes the popularization and application of machine learning.

c60d906e191b5472d0a628dda2f981a6.jpeg

outside_default.png

1.2 Introduction to Amazon SageMaker

Amazon SageMaker is a fully managed machine learning service that provides data scientists, developers, and enterprises an easy way to build, train, and deploy machine learning models regardless of the complexity of the underlying infrastructure.

Amazon SageMaker provides a complete set of machine learning tools, covering basic processes such as data labeling, data processing, model training, hyperparameter tuning, model deployment, and continuous model monitoring. It also provides automatic labeling, automatic machine learning, and monitoring model training. and other high-level functions. It can reduce the cost of customer machine learning through fully managed machine learning infrastructure and support for mainstream frameworks. Amazon SageMaker makes it easier to develop high-quality models by completely removing the heavy lifting at each step of the machine learning process. Users can choose to use Amazon SageMaker's preset algorithms to quickly build and train models, or use their own algorithms and frameworks. Amazon SageMaker provides a complete set of machine learning tools to help users build, train, and deploy high-performance machine learning models, which include multiple functional components. Next, we introduce some of the main components:

> Model building

● Amazon SageMaker Studio, as the first integrated development environment (Integrated Development Environment, IDE) suitable for machine learning, Amazon SageMaker Studio includes complete functions, which can operate Notebook, create models, manage model experiments, and debug in a unified visual interface and to detect model bias;

● Amazon SageMaker Notebooks, used to speed up model building and team collaboration, solves the following two problems: 1) Users need to manage the underlying resources when using a single server to run Jupyter Notebook; 2) When sharing to other users, it is necessary to modify a series of System configuration and network permissions. Support one-click startup of Jupyter Notebook, Amazon Cloud Technology is responsible for hosting the underlying computing resources, and also supports one-click sharing of Notebooks to promote seamless collaboration among machine learning teams;

● Amazon SageMaker Autopilot, which realizes automatic model construction and optimization. Usually, when building a machine learning model, we need to spend a lot of time looking for effective algorithms to solve machine learning problems. Amazon SageMaker Autopilot can automatically check the original data and select the best algorithm parameters Aggregate, train, and tune multiple models, track model performance, and rank models based on performance, dramatically reducing the time needed to find the best model;

●Supports a variety of deep learning frameworks, including TensorFlow, PyTorch, Apache MXNet, Chainer, Keras, Gluon, Horovod, and Scikit-learn, etc. Except for the frameworks supported by default, any other frameworks can be used in Amazon SageMaker by using their own containers Train and deploy the model.

> Model training

● Amazon SageMaker Experiments to organize, track, and evaluate runs during model training. Usually, in order to get a model that can be deployed, multiple iterations and continuous tuning are required, including trying different algorithms, hyperparameters, adjusting selected features, etc. Amazon SageMaker Experiments manages by automatically capturing and storing input parameters, configurations, and results Model iteration, combined with the visual interface of Amazon SageMaker Studio, you can also browse ongoing experiments, and compare and evaluate intuitively with previous experimental results;

● Amazon SageMaker Debugger, which analyzes, detects, and alerts on issues related to machine learning. Since most machine learning processes today are opaque and take a long time to train models, resulting in an extremely time-consuming optimization process, to address this, Amazon SageMaker Debbuger automatically captures real-time metrics during training, such as confusion matrices and learned gradients etc.), it also warns of common problems and provides fix suggestions, making the training process more transparent, allowing better understanding and explanation of how the model works, and ultimately improving model accuracy;

● Significantly reduces training costs. Amazon SageMaker supports training based on managed Spot instances, reducing training costs by up to 90%. Moreover, Amazon SageMaker supports periodic automatic saving of checkpoints to prevent model restarts due to Spot instance interruptions.

> Model Deployment

● Supports one-click deployment of models, generates predictions for real-time or batch data, and can deploy models on auto-expanded instances across multiple availability zones with one-click deployment, achieving high redundancy without any infrastructure operation and maintenance operations. Amazon SageMaker can automatically manage computing instances and model deployment, and provide a secure https endpoint for API access. Applications only need to call this API interface to achieve low-latency, high-throughput reasoning;

● Amazon SageMaker Model Monitor, used to maintain the accuracy of the model after deployment, limited by the training data set, when the target variable changes over time, the model will no longer be applicable, which is often called concept drift (Concept Drift). For example, changes in the economic environment may lead to fluctuations in interest rates, thereby affecting the prediction results of the house purchase model. Amazon SageMaker Model Monitor can detect the concept drift of the deployed model and provide detailed alerts. At the same time, the model trained by Amazon SageMaker will automatically send the key Indicators to help determine the root cause of the problem, which provides an automated mechanism for some scenarios with limited training data, which facilitates continuous tuning of the model through online data, without having to postpone model deployment due to insufficient data collected or lack of automated processes;

● Integrate with Kubernetes for orchestration and management. At present, the working platform of many machine learning teams is based on the Kubernetes platform, and some existing workflow orchestration and applications are not easy to migrate. To solve these problems, Amazon SageMaker provides Kubernetes Operator to integrate with Kubernetes-based workflows;

● Amazon SageMaker Neo supports model training once and running in multiple locations. Amazon SageMaker Neo enables machine learning models to be trained once and then run on different hardware and system environments on the cloud or in edge computing nodes for inference on cloud instances and edge devices, thus running faster without Loss accuracy, which optimizes the model to run on specific hardware, providing the best available performance for a given model and hardware target, the optimized model can run up to two times faster and can consume fewer resources. In short, a series of functional components provided by Amazon SageMaker cover all aspects of the machine learning process, which can greatly improve the efficiency and quality of machine learning, and provide comprehensive support and guarantee for users to build, train and deploy models.

outside_default.png

1.3 Advantages of Amazon SageMaker

Through the above introduction, we can conclude that Amazon SageMaker has the following advantages as a comprehensive machine learning platform:

> Efficiency: Amazon SageMaker provides a series of efficient functional components, such as automatic model tuning, high-performance model training and deployment, rich monitoring and debugging tools, etc., which can significantly improve the efficiency and quality of machine learning;

> Flexibility: Amazon SageMaker provides a variety of model deployment and management methods, and users can choose the appropriate deployment method according to their needs;

> Scalability: Amazon SageMaker can easily expand computing and storage resources to cope with machine learning tasks of different sizes and complexities;

> Security: Amazon SageMaker provides a variety of security measures, including data privacy and protection, access control and encryption, etc., to ensure the security and confidentiality of user data;

> Cost-effectiveness: Amazon SageMaker provides a variety of payment models, users can choose the appropriate payment method according to their needs, and reduce the cost of machine learning tasks by using Amazon SageMaker's automatic optimization function;

In short, Amazon SageMaker has the advantages of high efficiency, flexibility, scalability, security, and cost-effectiveness. It can provide users with comprehensive machine learning support and assurance, and help users build, train, and deploy high-quality machine learning models more easily. Next, we will create and deploy a Stable Diffusion generation model based on Amazon SageMaker to generate high-quality images.

outside_default.png

AIGC 与 Stable Diffusion

outside_default.png

2.1 Entering the era of AIGC

At present, artificial intelligence models can be divided into two categories, including discriminative models (Discriminative Model) and generative models (Generative Model). The discriminative model is based on a set of input data, such as text, X-ray images, or game screens, and undergoes a series of calculations to obtain corresponding target output results, such as word translation results, diagnostic results of X-ray images, or actions to be performed at the next moment in the game. Perhaps the most familiar class of AI models, discriminative models aim to create a mapping between a set of input variables and a target output.

The generative model does not calculate scores or labels for input variables, but generates new data samples by learning the relationship between input and output. This type of model can accept vectors that have nothing to do with actual values ​​(even random vectors), Generate complex output such as text, music or images. Artificial Intelligence Generated Content (AIGC) content generally refers to the use of machine learning and natural language processing technology to allow computers to generate human-understandable text, audio, images and other content, mainly composed of deep learning algorithms and neural networks, which can be passed Learn from massive amounts of data to simulate human thinking and creativity to produce high-quality content. The image below is an image generated using the Stable Diffusion model. It can be seen that the generated image not only has high quality, but also fits the given input description well.

9eea6fb9263cb8535ecc3bb3bb062dbe.png

AIGC learns data features from raw data through machine learning methods to generate brand new, original data that remains similar to training data, rather than simply copying the original data. AIGC has made significant progress and is widely used in various fields:

> Content creation: It can assist creators to complete the creation of pictures, articles, novels, music and other content;

> Design: It can help designers in graphic design, UI design, etc.;

> Game: It can generate elements such as characters and props in the game;

> Video production: can generate special effects, animation and other content;

> Intelligent customer service: It can generate natural language dialogues to realize applications such as intelligent customer service.

AIGC can be regarded as a strategic technology in the future, which will greatly accelerate the speed of artificial intelligence to generate data, which is profoundly changing human society, promoting human creative activities, including writing, painting, programming, etc., and even promoting scientific research, such as generative science Hypotheses and scientific phenomena etc. AIGC is a rapidly developing field that will revolutionize various industries. In the future, through the continuous exploration of new algorithms and technologies by academia and industry, the quality and diversity of generated content will be further improved.

In general, the discriminative model focuses on the relationship between input and output and directly predicts the output results, while the generative model focuses on the distribution of data and generates new sample data by learning the statistical characteristics of the data. The discriminative model has promoted the development of artificial intelligence in the past few decades, and the generative model will become the key development direction of artificial intelligence in the next ten years.

outside_default.png

2.2 Stable Diffusion Introduction

The reason why AI painting has made such great progress recently can largely be attributed to the open source model Stable Diffusion, which is a text-to-image model based on latent diffusion models (Latent Diffusion Models, LDM). Training can progressively denoise random Gaussian noise to obtain data samples of interest using 512x512 images from a subset of the LAION-5B database (LAION-5B is currently the largest freely accessible multimodal dataset) Training, using this model, can generate any image including human faces. Before using Stable Diffusion to generate high-quality images, we first introduce the principle and architecture of the model. The Stable Diffusion model architecture is shown in the following figure:

98ed2c10deb5a1d4ecf72754e4766456.png

Compared with Generative Adversarial Network (GAN), the Diffusion model has a better image generation effect, but because the model is an autoregressive model that requires repeated iterative calculations, the training and reasoning costs are high. The main reason is that They operate in pixel space and are memory-intensive, especially when generating high-resolution images. Latent Diffusion reduces memory and computing costs by applying the diffusion process on a lower-dimensional latent space instead of using the actual pixel space, so Stable Diffusion introduces the Latent Diffusion method to solve the problem of expensive calculations, which can greatly reduce calculations Complexity, while generating high-quality images, Latent Diffusion mainly includes the following three components:

● Variational autoEncoder (VAE) The VAE model mainly includes two parts: the encoder and the decoder, where the encoder is used to convert the image into a low-dimensional latent representation, and the obtained low-dimensional latent representation will be used as U- Net model, while the decoder is used to convert the latent representation back to the image. During Latent Diffusion training, the encoder is used to obtain a latent representation of the image through a process of forward diffusion, which progressively uses more and more noise at each step. During inference, the denoised latent representation generated by the backdiffusion process is converted back to an image using a VAE decoder;

● U-Net U-Net also includes two parts, encoder and decoder, and both are composed of ResNet blocks. The encoder compresses the image representation into a lower resolution image representation, and the decoder decodes the lower resolution image representation back to Original higher resolution image representation. In order to prevent U-Net from losing important information when downsampling, a shortcut connection (Short-cut Connections) is usually added between the downsampling ResNet of the encoder and the upsampling ResNet of the decoder. In addition, U-Net can pass the cross-attention layer To condition its output on the text embedding, a cross-attention layer is added to the encoder and decoder parts of U-Net, usually used between ResNet blocks;

● Text-Encoder Text-Encoder is responsible for converting input prompts into an embedding space that U-Net can understand. It is usually a simple Transformer-based encoder that maps input word segmentation sequences to latent text embedding sequences. Inspired by Imagen, Stable Diffusion does not train Text-Encoder during the training process, but only uses CLIP pre-trained Text-Encoder-CLIPTextModel.

However, even though Latent Diffusion can operate on low-dimensional latent spaces, it greatly reduces memory and computational requirements compared to pixel-space diffusion models, but if high-quality photos need to be generated, the model still needs to be on a GPU above 16GB Running, specifically, building a Stable Diffusion model on a local computer will encounter the following difficulties:

> Software environment: The construction of the Stable Diffusion model requires the use of specific software and libraries. Building a software environment on a local computer may encounter problems such as version incompatibility and complex dependencies, which require a lot of time and effort to debug and solve;

> Data processing: Stable Diffusion model training needs to process a large amount of high-quality image data, and processing large amounts of data on the local computer may cause problems such as insufficient memory and slow speed;

> Computing resource limitation: Stable Diffusion model training requires a lot of computing resources, including high memory GPU and a lot of memory. If the computing resources of the local computer are insufficient, the model cannot be trained;

> Hyperparameters : The Stable Diffusion model needs to set a large number of parameters, such as diffusion coefficient, boundary conditions, learning rate, etc. The selection of these hyperparameters requires a lot of debugging, otherwise the model may not converge or the convergence speed is too slow;

> Model validation: The Stable Diffusion model requires extensive model validation and testing to ensure the correctness and reliability of the model.

To sum up, building a Stable Diffusion model needs to overcome difficulties such as computing resource limitations, software compatibility issues, data processing, and hyperparameter selection. Therefore, choosing a cloud computing platform to simplify these tasks has become a natural choice, and Amazon SageMaker, as a fully managed machine learning service, has become the first choice for building, training and deploying complex models (such as Stable Diffusion).

outside_default.png

Created with Amazon SageMaker 

Stable Diffusion Model

In this section, we will introduce the Amazon SageMaker-based Amazon SageMaker Notebook instance to test, validate the AIGC model and deploy the AIGC model to the Amazon SageMaker Inference Endpoint.

outside_default.png

3.1 Preparations

To ensure that the AIGC model can be deployed to the Amazon SageMaker Inference Endpoint, you need to ensure that you have sufficient quota. To do this, we first need to check the quota through the service quota page, enter ml.g4dn.xlarge for endpoint usage in the search box, if the second column of this quota is 0, you need to increase the quota:

9b40f99e5ce110626753ed82a29c8e9f.jpeg

To increase the quota, first select ml.g4dn.xlarge for endpoint usage, and click the "Request Quota Increase" button:

2caccaf0e6add60b00279454821d1edd.jpeg

Enter the required limit in the input box, such as "1", after filling, click the "Request" button to submit the request:

02b4339d54dab4cbebbb5047f26ac42a.png

After waiting for the quota request to pass, you can continue the experiment process.

outside_default.png

3.2 Create an instance of Amazon SageMaker Notebook

Amazon SageMaker Notebook instances are machine learning compute instances that run Jupyter Notebook applications. Amazon SageMaker is used to manage the creation of instances and related resources. We can use Jupyter Notebook in Notebook instances to prepare and process data, write code to train models, or deploy models to Amazon SageMaker, and test or validate models. Next, we'll create an Amazon SageMaker Notebook instance that will run the associated Jupyter Notebook code.

>(1) Log in to the Amazon cloud technology console, and change the current region to the Tokyo region:

a4d17af0f504a8dfc01b0b05c4a5dbba.jpeg

> (2) Search for Amazon SageMaker in the search box, and click to enter the Amazon SageMaker service:

ad28e242ba219fd30363fc0a652fa0bd.jpeg

> (3) On the left menu bar, first click the "Notebook" button, then click "Notebook Instance" to enter the notebook (Notebook) instance control panel, and click the "Create Notebook Instance" button in the upper right corner:

30b8591fcc8427b8024815289924820a.jpeg

> (4) Configure notebook instance settings. On the notebook instance creation details page, configure the basic information of the notebook instance, including the notebook instance name (such as Stable-Diffusion), notebook instance type (select the ml.g4dn.xlarge instance type, this type The instance is equipped with an NVIDIA T4 Tensor Core GPU graphics card, which provides the ability to perform floating-point calculations required by the model), platform identifier (Amazon Linux 2, Jupyter Lab 3), and volume size under "Other configurations" (recommended at least 75GB disk size , used to store machine learning models):

e355f0f2ef318d97bff22d6e85e6b53c.jpeg

> (5) Configure the notebook instance permissions and create an IAM role for the notebook instance to call services including Amazon SageMaker and S3, such as uploading models, deploying models, etc. In the IAM roles under Permissions and Encryption, click the dropdown and click Create New Role:

2d0be69080e5d3c8048b2d990f80097c.jpeg

On the configuration page, keep the default configuration and click the "Create role" button:

6117d3b1e3c193ebc66f5686fc5d1fca.png

After successfully creating the IAM role, you can get a prompt similar to the following:

259e808d3c916b6e51e9240046ea3712.jpeg

> (6) Check the configuration information, click the "Create Notebook Instance" button after confirming that it is correct, and wait for the instance to be created:

dd318d9660fe60ca23bfbbb6dd95357d.jpeg

> (7) When the notebook status changes to InService, click "Open Jupyter" to enter Jupyter Notebook:

9bb95e3a88624fd1efcb0c68891663ed.jpeg

outside_default.png

3.3 End-to-end AIGC experience

Next, we can download and save the Notebook code file, upload it to Jupyter Notebook, and then run the code directly, but the experience of writing code by hand is unparalleled. We will introduce the main content of the code file and experience AIGC end-to-end from scratch ! It should be noted that you need to ensure that the Kernel starts with conda_pytorch.

> (1) Install related libraries and perform environment configuration:

!nvcc --version
!pip list | grep torch
# 安装Notebook运行模型所需的库文件
!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

Swipe left to see more

> (2) Download the model file. We will use the Stable Diffusion V2 version, which contains a robust text generation image model, which can greatly improve the quality of image generation. For the model introduction, please refer to Github.

!curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | sudo bash
!sudo yum install git-lfs -y
# 设定模型版本的环境变量,使用 Stable Diffusion V2
SD_SPACE="stabilityai/"
SD_MODEL = "Stable-Diffusion-2-1"
# 克隆代码仓库
# Estimated time to spend 3min(V1), 8min(V2)
%cd ~/SageMaker
!printf "=======Current Path========%s\n"
!rm -rf $SD_MODEL
# !git lfs clone https://huggingface.co/$SD_SPACE$SD_MODEL -X "*.safetensors"
!mkdir $SD_MODEL
%cd $SD_MODEL
!git init
!git config core.sparseCheckout true
!echo "/*" >> .git/info/sparse-checkout
!echo "!**/*.safetensors" >> .git/info/sparse-checkout
!git remote add -f master https://huggingface.co/$SD_SPACE$SD_MODEL
!git pull master main
%cd ~/SageMaker
!printf "=======Folder========%s\n$(ls)\n"

Swipe left to see more

> (3) Configure and use the model in Notebook, first load the relevant library and model:

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

Swipe left to see more

Use the GPU to perform calculations and set hyperparameters. Some hyperparameters are as follows:

● prompt (str or List[str]): the text prompt or text list generated by the boot image;

● height (int, *optional, V2 default model can support up to 768 pixels): the height of the generated image (in pixels)

● width (int, *optional, the V2 default model can support up to 768 pixels): the width of the generated image (in pixels);

● num_inference_steps (int, *optional, the default number of denoising steps is 50): the number of denoising steps, more denoising steps usually get higher quality images at the cost of slower inference;

Guidance_scale (float, *optional, the default guidance scale is 7.5): a higher guidance scale will cause the image to be closely related to the hint, but will sacrifice image quality, and will be ignored when guidance_scale<=1;

Negative_prompt (str or List[str], *optional): text or list of text that does not lead to image generation;

● num_images_per_prompt (int, *optional, defaults to generate 1 image per prompt): the number of images generated per prompt.

torch.cuda.empty_cache()
pipe = pipe.to("cuda")


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)

Swipe left to see more

> (4) Deploy the model to the Sagemaker Inference Endpoint. After building and training the model, you can deploy the model to the terminal node to obtain predictive reasoning results:

import boto3
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}")

Swipe left to see more

Create a custom inference inference.py script:

# 为模型创建所需依赖声明的文件
%%writefile ./$SD_MODEL/code/requirements.txt
diffusers==0.13.1
transformers==4.26.1
# 编写 inference.py 脚本
%%writefile ./$SD_MODEL/code/inference.py
import base64
import torch
from io import BytesIO
from diffusers import StableDiffusionPipeline




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}

Swipe left to see more

Package the model and upload it to the S3 bucket:

!echo $(date)
!tar --exclude .git --use-compress-program=pigz -pcvf ./$SD_MODEL'.tar.gz' -C ./$SD_MODEL/ .
!echo $(date)


from sagemaker.s3 import S3Uploader


print(datetime.datetime.now())
# upload model.tar.gz to s3, Estimated time to spend 30s(V1), 1min(V2)
sd_model_uri=S3Uploader.upload(local_path=f"{SD_MODEL}.tar.gz", desired_s3_uri=f"s3://{sess.default_bucket()}/Stable-Diffusion")
print(f"=======S3 File Location========\nmodel uploaded to:\n{sd_model_uri}")


print(datetime.datetime.now())

Swipe left to see more

Deploy the model to Amazon SageMaker using HuggingFace:

huggingface_model = {}
predictor = {}


from sagemaker.huggingface.model import HuggingFaceModel
# create Hugging Face Model Class
huggingface_model[SD_MODEL] = HuggingFaceModel(
    model_data=sd_model_uri, # path to your model and script
    role=role, # iam role with permissions to create an Endpoint
    transformers_version="4.17", # transformers version used
    pytorch_version="1.10", # pytorch version used
    py_version='py38', # python version used
)


# deploy the endpoint endpoint, Estimated time to spend 8min(V2)
print(datetime.datetime.now())


predictor[SD_MODEL] = huggingface_model[SD_MODEL].deploy(
    initial_instance_count=1,
    instance_type="ml.g4dn.xlarge",
    endpoint_name=f"{SD_MODEL}-endpoint"
)


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

Swipe left to see more

Generate a custom image based on an inference endpoint:

from io import BytesIO
import base64


# helper decoder
def decode_base64_image(image_string):
    base64_image = base64.b64decode(image_string)
    buffer = BytesIO(base64_image)
    return Image.open(buffer)


response = predictor[SD_MODEL].predict(data={
    "prompt": [
        "A bird is flying in space",
        "Photos of horseback riding under the sea",
    ],
    "height" : 512,
    "width" : 512,
    "num_images_per_prompt":1
  }
)


#decode images
decoded_images = [decode_base64_image(image) for image in response["generated_images"]]


#visualize generation
for image in decoded_images:
    display(image)

Swipe left to see more

outside_default.png

3.4 Model generation effect

Next, we use the deployed model to see the effect of image generation. For example, we can get the following images using A bird is flying in space and Photos of horseback riding under the sea:

18b52561c4f82a047a4c2ada24eea2a8.png

It can be seen that even if we only give a small number of keywords, we can generate images with clear texture and high quality, and we can also use the image generation model deployed by other text tests.

outside_default.png

Amazon SageMaker experience

In the process of using Amazon SageMaker to build and deploy machine learning models, I have fully experienced the machine learning functions and tools provided by Amazon SageMaker, which can perform machine learning tasks more quickly and efficiently, and also has flexibility, scalability, and ease of use. Many advantages, in general, the experience is as follows:

● Amazon SageMaker provides complete machine learning tools that can develop, train, and deploy models more quickly. Amazon SageMaker's automation functions can also help users quickly optimize models and parameters, reducing the complexity and workload of machine learning;

● Amazon SageMaker provides an easy-to-use interactive notebook for faster exploration and processing of data, and easier sharing of code and notebooks for easier collaboration and communication;

● Amazon SageMaker provides a variety of different model deployment and management methods to meet the needs of different scenarios during use;

● The Amazon SageMaker platform provides comprehensive monitoring and debugging tools to better track and analyze model performance and ensure model stability and reliability.

outside_default.png

summary

Artificial Intelligence Generated Content ( AIGC ) has set off a frenzy of artificial intelligence with its unique "creativity" and creation speed beyond the reach of human beings. However, due to the complexity of generative model building, training and deploying AI models in the cloud has become the first choice for most AIGC users and companies. As an excellent cloud machine learning platform, Amazon SageMaker provides a wealth of functions and tools that can help us build, train, and deploy machine learning models more efficiently, and solve the problem of high computational requirements for generating models. This article mainly reviews the relevant points of creating and deploying the Stable Diffusion model based on Amazon SageMaker during the process of participating in the cloud exploration laboratory activities of Amazon Cloud Technology, and fully demonstrates the advantages of Amazon SageMaker in the process of building, training and deploying artificial intelligence models.

outside_default.png

Cloud Discovery Lab Activities

The Cloud Exploration Lab aims to let users experience the latest and hottest Amazon cloud technology developer tools and services by means of technical experiments, product experience, and case applications. The Exploration Lab on the Cloud is currently in full swing. It is carried out in the form of group guidance and hands-on experience. The official provides multiple sets of experience tutorials and quick-start materials. Participants can choose experiments of different difficulty for practical operations according to their own conditions. By submitting different forms of experience works, through this event, you can not only explore more application scenarios of machine learning and AI technology, but also meet more like-minded developers. Come and experience it together!

00c755337ff67024fcfc64e3fb40054e.png

▌Author: Pan Xiaohui

59f40ba7b90e2c267b8b911f45705356.gif

0041929cecb526c6b3ac099f84431dbc.gif

I heard, click the 4 buttons below

You will not encounter bugs!

682e2802f7f0f034eb179826c372d629.gif

Guess you like

Origin blog.csdn.net/u012365585/article/details/130279169