MLOps- 吴恩达Andrew Ng Overview of the ML Lifecycle and Deployment Week1 部署深度学习模型model 实现作业
资源
https://github.com/https-deeplearning-ai/machine-learning-engineering-for-production-public/tree/main/course1/week1-ungraded-lab
介绍
欢迎来到机器学习工程生产课程 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_bboxdef 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/ directoryimg_filepath = f'images/{filename}'# Read the image into a numpy arrayimg = cv2.imread(img_filepath)# Perform the object detectionbbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)# Print current image's filenameprint(f"========================\nImage processed: {filename}\n")# Print detected objects with confidence levelfor 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 boxesoutput_image = draw_bbox(img, bbox, label, conf)# Save the image in the directory images_with_boxescv2.imwrite(f'images_with_boxes/{filename}', output_image)# Display the image with bounding boxesdisplay(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_codeif 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 实现作业相关推荐
- 【斯坦福公开课-机器学习】1.机器学习的动机和应用(吴恩达 Andrew Ng)
文章目录 0三个目标 0先修课程要求 基本工具 1-网址 2-邮箱 3-本系列课程链接 1机器学习的定义 1-1非正式定义 1-2正式的定义 2监督学习(Supervised Learning) 2- ...
- 第06周:吴恩达 Andrew Ng 机器学习
10 应用机器学习的建议 10.1 决定下一步做什么 依旧用房价预测的例子,假设我们使用已经正则化的线性回归的模型得到了参数,并将参数带入预测函数来预测一组新的房价,但是结果误差很大,那么为了解决问题 ...
- 第01周:吴恩达 Andrew Ng 机器学习
学习内容: 1 机器学习 1.1 机器学习定义 ①Arthur Samuel:在没有明确设置的情况下,使计算机具有学习能力的研究领域. e.g.跳棋游戏,使计算机与自己对弈上万次,使计算机学习到什么是 ...
- 机器学习(Machine Learning) - 吴恩达(Andrew Ng) 视频笔记
背景:写于20190408,大概一周前我已经看到了P47 8-4 - Model Representation II 视频链接:https://www.bilibili.com/video/av991 ...
- 网易云课堂吴恩达Andrew Ng深度学习笔记(二)
01.神经网络和深度学习 第二周 神经网络基础 先说明最简单的二分类,即可以把结果分为0和1两类,或者说是否两种的分类情况. 比如说判定图片中是否有猫,标记为有(1)或者无(0). 特征则是图片中像 ...
- 网易云课堂吴恩达Andrew Ng深度学习笔记(四)
01.神经网络和深度学习 第四周 深层神经网络 所谓深层神经网络,字面上对应于浅层神经网络,即具有2层及以上的隐藏层. 其正向传播过程一样,z[l] = w[l]a[l-1] + b[l], a[ ...
- 第08周:吴恩达 Andrew Ng 机器学习
13 聚类 聚类算法是学习的第一个无监督学习算法,它所用到的数据是不带标签的. 13.1 无监督学习 什么是无监督学习? 在无监督学习中,所有的数据不带标签,而无监督学习要做的就是将这一系列无标签的数 ...
- 第04周:吴恩达 Andrew Ng 机器学习
学习内容: 8 神经网络 8.1 为什么用神经网络 当只有两个特征时(x1.x2),使用sigmoid函数得到的结果还可以,因为可以把x1.x2的所有组合都包含到多项式中.但当很多问题含有很多特征,不 ...
- 第05周:吴恩达 Andrew Ng 机器学习
学习内容: 9 神经网络的运用 9.1 代价函数 其中: 假设训练集为m组的训练样本 用L表示神经网络的总层数,对于上图来说,L=4 用表示第L层的单元数,也就是神经元的数量(不包括这一层的偏差单元) ...
- 第07周:吴恩达 Andrew Ng 机器学习
12 支持向量机(Support Vector Machines) 12.1 优化目标 在监督学习中,许多学习算法的性能都非常类似,因此,重要的不是你该选择使用学习算法A还是学习算法B,而更重要的是, ...
最新文章
- AI2XAML's Bug(sequel)
- Java进阶篇(一)——接口、继承与多态
- [19/04/11-星期四] 多线程_并发协作(生产者/消费者模式_2种解决方案(管程法和信号灯法))...
- 第1章-什么是深度学习(笔记)
- 博客群发(2)--实现登陆
- mysql慢查询优化方案
- 如何看待不会写代码的架构师?
- linux mail使用笔记
- 称重问题 —— 哪个是假币?
- 老生常谈exec函数族
- java dateutils工具类_DateUtils 日期工具类
- JavaScript高级程序设计(读书笔记)(七)
- android toast居中显示_Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明...
- 手摸手。完成一个H5 抽奖功能
- Android studio开发app实现仿微信滑动切换界面
- php alt什么意思,img标签的alt作用是什么
- [转载]三、二、一 …… Geronimo!,第 4 部分: 模式
- 三极管PNP NPN 的判别
- openGL入门(二)用openGL绘制带有对角线的正六边形
- 二级c语言百度云,全国计算机二级C语言历年真题完整版.pdf