MLOps- 吴恩达Andrew Ng Overview of the ML Lifecycle and Deployment Week1 部署深度学习模型model 实现
Posted 架构师易筋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MLOps- 吴恩达Andrew Ng Overview of the ML Lifecycle and Deployment Week1 部署深度学习模型model 实现相关的知识,希望对你有一定的参考价值。
资源
介绍
欢迎来到机器学习工程生产课程 1 的第一周。在这个未分级的实验室中,您将完成部署已经训练好的深度学习模型的过程。为此,我们将利用用户友好的库 fastAPI,它提供了一个很好的 REST API 框架。
本教程专为在您的机器上本地运行而设计。这可以通过两种方法完成:使用Python Virtual Environments或使用Docker。
这两种方法应该产生相同的结果。如果您的计算机上已经安装了 conda,我们建议您使用虚拟环境方法。如果不是这种情况,请选择 Docker 方法,因为它更容易设置。
作为一般说明,本教程中的命令旨在在终端内运行。首先,您需要将这个 repo 克隆到本地文件系统和cdweek1-ungraded-lab 目录中。
要克隆存储库,请使用以下命令:
git clone https://github.com/https-deeplearning-ai/machine-learning-engineering-for-production-public.git
安装环境操作,按照github步骤即可,下面记录server.ipynb 和 client.ipynb 的yolo3 物体识别,和网络请求的结果。
运行 server.ipynb
在默认端口 port, 8888 启动 jupyter lab
jupyter lab
yolo3-tiny 识别物体,并框出物体。
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
def detect_and_draw_box(filename, model="yolov3-tiny", confidence=0.5):
"""Detects common objects on an image and creates a new image with bounding boxes.
Args:
filename (str): Filename of the image.
model (str): Either "yolov3" or "yolov3-tiny". Defaults to "yolov3-tiny".
confidence (float, optional): Desired confidence level. Defaults to 0.5.
"""
# Images are stored under the images/ directory
img_filepath = f'images/{filename}'
# Read the image into a numpy array
img = cv2.imread(img_filepath)
# Perform the object detection
bbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)
# Print current image's filename
print(f"========================\\nImage processed: {filename}\\n")
# Print detected objects with confidence level
for l, c in zip(label, conf):
print(f"Detected object: {l} with confidence level of {c}\\n")
# Create a new image that includes the bounding boxes
output_image = draw_bbox(img, bbox, label, conf)
# Save the image in the directory images_with_boxes
cv2.imwrite(f'images_with_boxes/{filename}', output_image)
# Display the image with bounding boxes
display(Image(f'images_with_boxes/{filename}'))
confidence 默认值为0.5,识别出的效果,loyo3-tiny 速度快,但是识别的错误率高
confidence = 0.2 的效果
启动server
生成swagger链接 http://localhost:8000/docs
, 这里可以选择照片,然后执行就可以得到识别结果
client.ipynb
客户端不能在同一个服务里面访问,所以要启动一个新的服务,端口为9000,
jupyter lab --port 9000
定义:发网络请求
def response_from_server(url, image_file, verbose=True):
"""Makes a POST request to the server and returns the response.
Args:
url (str): URL that the request is sent to.
image_file (_io.BufferedReader): File to upload, should be an image.
verbose (bool): True if the status of the response should be printed. False otherwise.
Returns:
requests.models.Response: Response from the server.
"""
files = {'file': image_file, 'type': 'image/jpeg'}
headers = {'accept': 'application/json', 'Content-Type': 'multipart/form-data'}
response = requests.post(url, files=files)
status_code = response.status_code
if verbose:
msg = "Everything went well!" if status_code == 200 else "There was an error when handling the request." + str(status_code)
print(msg)
return response
调用 网络请求,并展示
image_files = [
'car2.jpg',
'clock3.jpg',
'apples.jpg'
]
for image_file in image_files:
with open(f"images/{image_file}", "rb") as image_file:
prediction = response_from_server(full_url, image_file, verbose=False)
display_image_from_response(prediction)
用Docker启动环境
上面是用Conda启动Environment
拉取镜像
docker pull deeplearningai/mlepc1w1-ugl:jupyternb
启动镜像
docker run -it --rm -p 8888:8888 -p 8000:8000 --mount type=bind,source="$(pwd)",target=/home/jovyan/work deeplearningai/mlepc1w1-ugl:jupyternb
让我们分解这个命令和它的标志:
-
-it:以交互模式运行容器并为其附加一个伪终端,以便您可以检查在容器的标准流中打印的内容。这非常重要,因为您必须复制并粘贴 Jupyter lab 的访问令牌。
-
–rm:停止后删除容器。
-
-p:允许您将计算机中的端口映射到容器中的端口。在这种情况下,我们需要一个用于 Jupyter 服务器的端口,另一个用于您将在未分级实验室中运行的服务器。
-
–mount:允许您在容器内的本地文件系统中挂载目录。这非常重要,因为如果不存在挂载,则删除容器后对文件的更改将不会保留。在这种情况下,我们将当前目录挂载week1-ungraded-lab到/home/jovyan/work容器内的目录中。
当容器开始运行时,您会看到一些信息被打印在终端中。通常,您需要进行身份验证才能使用 Jupyter 实验室,为此复制终端上显示的令牌,转到http://localhost:8888/并将其粘贴到那里。
您的终端的输出应该与下一张图像非常相似,其中突出显示了令牌以供参考:
通过身份验证后,单击/work目录,您应该会看到当前本地目录中的所有文件。查找server.ipynb文件并将其打开以开始未评分的实验。
要在完成实验后停止容器,只需按Ctrl + C两次即可。这也将删除容器。
而且……就是这样!部署深度学习模型玩得开心!😃
以上是关于MLOps- 吴恩达Andrew Ng Overview of the ML Lifecycle and Deployment Week1 部署深度学习模型model 实现的主要内容,如果未能解决你的问题,请参考以下文章
MLOps- 吴恩达Andrew Ng Selecting and Training a Model Week2 论文等资料汇总
MLOps- 吴恩达Andrew Ng Overview of the ML Lifecycle and Deployment Week1 论文等资料汇总
MLOps- 吴恩达Andrew Ng Data Definition and Baseline Week3 实验作业
MLOps- 吴恩达Andrew Ng Selecting and Training a Model Week2 实验作业
MLOps- 吴恩达Andrew Ng Overview of the ML Lifecycle and Deployment Week1 部署深度学习模型model 实现