| 作者介绍:封磊,亚马逊云科技社区建设者,阿里云开发者社区签约作者。 | |
| --- | --- |
最近人工智能爆火啊,前有 AIGC 后有 ChatGPT,ChatGPT 倒是可以很简单的体验到,但是 AIGC 这个似乎需要一点动手能力,而且还要考虑下我们本地的环境了。恰好我最近受邀参加了亚马逊云科技`【云上探索实验室】`的活动,基于他们的 `Amazon SageMaker` 服务来做了以下三个小实验,有包括 AIGC,Deepracer 还有图像分类,整体流程体验下来比想象中好太多了,即便是一个新手也可以通过 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 来掌握下面三个实验,而且不需要您再考虑基础环境配置了,可以更加专注于业务。那么就让我们来跟着下面的实验手册和演示视频一起来操作吧!
最近我恰好受邀参与了亚马逊云科技云上探索实验室活动,利用 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 平台搭建了自己的 AIGC 应用,整个过程只用了不到20分钟。
这个动手实验室会通过三个不同类型的实验引导您如何使用[Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail),让您感受SageMaker的非凡之处。
- **[使用Amazon SageMaker构建Stable-Diffusion](https://www.bilibili.com/video/BV1c24y1E7a5/?trk=cndc-detail)** 这个实验会引导您通过 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) NoteBook 实例来构建 AIGC 环境,同时您可以体验到使用提示词来使 AI 生成图片,在后续环节中还会引导您构建前后端 Web 应用,让您直接在 Web 端也可以体验到自己的 AIGC
- **[使用 Amazon SageMaker 加速你的 Deepracer 小车](https://www.bilibili.com/video/BV1MN411T77y/?trk=cndc-detail)** 这个实验会引导您通过 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 使用开源库来加速您的 DeepRacer 小车,更加简化您的智能驾驶流程.
- **[Amazon SageMaker JumpStart 构建图像分类](https://www.bilibili.com/video/BV1m24y177VX/?trk=cndc-detail)** 通过这个实验,会引导您使用[Amazon SageMaker JumpStart](https://aws.amazon.com/cn/sagemaker/jumpstart/?trk=cndc-detail)构建图像分类,JumpStart 支持一键式部署以及对这些模型进行微调,因此您可以在部署前逐步训练和调整模型,在本实验中,您将使用 [Amazon SageMaker JumpStart](https://aws.amazon.com/cn/sagemaker/jumpstart/?trk=cndc-detail) 构建图像分类模型,并使用该模型对花卉的图像进行分类.
如果您想获取更多有关[Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail)的知识,请阅读[亚马逊 SageMaker](https://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/whatis.html?trk=cndc-detail).
# 一、使用 Amazon SageMaker 构建 Stable-Diffusion
使用 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 构建 Stable-Diffusion:https://www.bilibili.com/video/BV1c24y1E7a5?trk=cndc-detail
区域: `ap-northeast-1(东京)`
## 服务配额
Service-Quota 配额控制台:https://ap-northeast-1.console.aws.amazon.com/servicequotas/home/services/sagemaker/quotas?trk=cndc-detail
本次实验需要用到 GPU 实例,如果您使用的是个人账号,请先确认您有`ml.g4dn.xlarge` 的服务配额

### 申请提高配额
选中需要提高的服务配额,单击右上角 `请求增加配额`

更改配额值为 `1`

## 创建 Amazon SageMaker Notebook 实例
打开 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 控制台
直达链接:https://ap-northeast-1.console.aws.amazon.com/sagemaker/home?region=ap-northeast-1?trk=cndc-detail

### 创建 Notebook 实例
需要滑动左侧的服务框,找到`笔记本`–>`笔记本实例`—右上角`创建笔记本实例`

设置笔记本实例的名称、选择笔记本实例、以及存储的大小

对于权限和加密,点击`角色下拉框`,然后点击 `创建新角色`

在本次实验中需要用到 S3 存储桶来存放模型文件,在此处可以选择`任意存储桶`或者`特定的存储桶`

点击创建笔记本实例

等待大约4分钟左右,笔记本实例的状态为`InService`,我们就可以点击`JupyterLab`进行实验了

## 构建 Stable-Diffusion 环境
查看 Jupyter [Lab](https://so.csdn.net/so/search?q=Lab\\&spm=1001.2101.3001.7020) 控制台,选择最下面的 `Terminal`

在此处我们需要下载本次实验的 Notebook 代码文件
```js
cd SageMaker
wget https://static.us-east-1.prod.workshops.aws/public/73ea3a9f-37c8-4d01-ae4e-07cf6313adac/static/code/notebook-stable-diffusion-ssh-inference.ipynb
```

双击打开刚刚下载的 Notebook 文件,需要注意我们要选择内核,请选择`conda_pytorch_p39`

修改提示词,生成新图片

## 利用 Amazon Cloud 9 构建前后端 Web 应用
### 创建 Cloud 9 环境

**根据自己的需求设置名称,选择实例的大小以及实例的系统**



### 构建 Web 应用
**这里步骤就是 `下包解压装依赖,服务一跑出图片`**
```js
wget https://static.us-east-1.prod.workshops.aws/public/73ea3a9f-37c8-4d01-ae4e-07cf6313adac/static/code/SampleWebApp.zip
unzip SampleWebApp.zip
pip install Flask boto3
```
等待 Web 应用运行起来后,开始使用描述词来生成我们的图片


## 清理
**请删除对应的 Notebook 实例,以及`推理`里面的终端节点**
## 结论
在本次实验中,您可以轻松的通过[Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail)来完成本次实验,并且您无需动手添加额外代码,为考虑到用户的体验,亚马逊云科技工程师还提供了Web端应用,确实更加方便。
# 二、使用 Amazon SageMaker 加速你的 Deepracer 小车
使用 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 加速你的 Deepracer 小:https://www.bilibili.com/video/BV1MN411T77y/?trk=cndc-detail
创建 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) NoteBook 实例
需要滑动左侧的服务框,找到`笔记本`–>`笔记本实例`—右上角`创建笔记本`

设置笔记本实例的名称、选择笔记本实例、以及存储的大小

**对于权限和加密,点击`角色下拉框`,然后点击 `创建新角色`**


**等待实例服务状态为`InService`即可打开`JupyterLab`**

## 运行 Notebook 实验手册
### 克隆存储库


### 运行 Notebook

根据自己的需求设置赛道

## 查看生成的奖励函数和 ActionSpace

## 结论
在本次实验中,可以以极低的时间成本完成,无需考虑底层的环境以及线上的一个依赖库,极大的简化了用户的操作流程,同时有免费套餐覆盖本次实验内容,用户无需担心费用问题。
## 清理
请删除对应的 Notebook 实例
# 三、Amazon SageMaker JumpStart 构建图像分类
[Amazon SageMaker JumpStart](https://aws.amazon.com/cn/sagemaker/jumpstart/?trk=cndc-detail) 构建图像分类:https://www.bilibili.com/video/BV1m24y177VX?trk=cndc-detail
## 启动 Amazon SageMaker Studio
### 创建 `域名`

<!--StartFragment-->
**设置`域名名称`,`用户配置文件名称`,`执行角色`**


<!--StartFragment-->
**等待`SageMaker 域`创建成功**

<!--StartFragment-->
## 部署/微调预训练图像分类模型
### 进入到 MobileNet V2 模型页面



### 训练/部署模型

#### 训练模型


#### 部署模型
**确认模型训练完成**

**部署模型**

#### 使用微调模型对图像进行分类


**在第一个代码单元格下方插入一个新单元格以显示示例花卉图像**
```js
import boto3
from IPython.core.display import HTML
roses_jpg, sunflowers_jpg, roses2_jpg, sunflowers2_jpg = "roses.jpg", "sunflowers.jpg", "roses2.jpg", "sunflowers2.jpg"
HTML('<table><tr><td> <img src="roses.jpg" alt="roses" style="height: 250px;"/> <figcaption>roses.jpg</figcaption>'
'</td><td> <img src="sunflowers.jpg" alt="sunflowers" style="height: 250px;"/> <figcaption>sunflowers.jpg</figcaption>'
'</td><td> <img src="roses2.jpg" alt="roses" style="height: 250px;"/> <figcaption>roses2.jpg</figcaption>'
'</td><td> <img src="sunflowers2.jpg" alt="sunflowers" style="height: 250px;"/> <figcaption>sunflowers2.jpg</figcaption>'
'</td></tr></table>')
```

**插入一个新单元格并添加代码以打开图像并将它们加载到内存中**
```js
images = {}
with open(roses_jpg, 'rb') as file: images[roses_jpg] = file.read()
with open(sunflowers_jpg, 'rb') as file: images[sunflowers_jpg] = file.read()
with open(roses2_jpg, 'rb') as file: images[roses2_jpg] = file.read()
with open(sunflowers2_jpg, 'rb') as file: images[sunflowers2_jpg] = file.read()
```

**查看模型是否正确地对玫瑰和向日葵图像进行了分类**

## 清理
请删除对应的用户配置文件,域
## 结论
在本实验中,您已经部署了预训练图像分类模型来运行推理,然后在新数据集上微调预训练图像分类模型以对不属于预训练数据集的对象进行分类。借助 JumpStart,您无需代码即可完成两个模型的训练和部署
# 总结
通过本次的 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) Hands-on,即便是新手也可以根据视频和博客的引导,快速的完成本次实验,增加对 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 的一个了解。同时亚马逊云科技官方也考虑到了不同用户的水平以及经济情况,提供了很多的 workshop 以及一些免费的产品额度,极大的提高了用户使用体验,为用户节省了时间、金钱成本。虽然实验非常简单,但是从整个 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 来看,其提供了非常强大的功能,需要更加专业的实验才可以完整的体验到 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) 的强大能力。那么在最后也希望同志们可以更进一步的去了解 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail),比如通过一些亚马逊云科技官方提供的 workshop 或者研讨会来学习到更多的知识。希望这篇博文能够帮助到您,感谢阅读。
# 参考资料
1. 端到端体验 AIGC-从 SageMaker 到 Web 应用:https://catalog.us-east-1.prod.workshops.aws/workshops/3b86fa89-da3a-4e5f-8e77-b45fb11adf4a/zh-CN?trk=cndc-detail
2. [Amazon SageMaker JumpStart](https://aws.amazon.com/cn/sagemaker/jumpstart/?trk=cndc-detail):https://catalog.us-east-1.prod.workshops.aws/workshops/63069e26-921c-4ce1-9cc7-dd882ff62575/en-US/lab9?trk=cndc-detail
3. Capstone_Amazon_DeepRacer:https://github.com/dgnzlz/Capstone_AWS_DeepRacer?trk=cndc-detail