您现在的位置是:热点 >>正文
在 Kubernetes 上部署一个深度学习模型
热点0034人已围观
简介...
了解如何使用 Kubermatic Kubernetes 平台来部署、扩展与管理图像识别预测的深度学习模型。
随着企业增加了对人工智能(AI)、机器学习(ML)与深度学习(DL)的使用,出现了一个关键问题:如何将机器学习的开发进行规模化与产业化?这些讨论经常聚焦于机器学习模型本身;然而,模型仅仅只是完整解决方案的其中一环。为了达到生产环境的应用和规模,模型的开发过程必须还包括一个可以说明开发前后关键活动以及可公用部署的可重复过程。
本文演示了如何使用 Kubermatic Kubernetes 平台 对图像识别预测的深度学习模型进行部署、扩展与管理。
Kubermatic Kubernetes 平台是一个生产级的开源 Kubernetes 集群管理工具,提供灵活性和自动化,与机器学习/深度学习工作流程整合,具有完整的集群生命周期管理。
开始
这个例子部署了一个用于图像识别的深度学习模型。它使用了 CIFAR-10 数据集,包含了 60,000 张分属 10 个类别的 32x32 彩色图,同时使用了 Apache MXNet 的 Gluon 与 NVIDIA GPU 进行加速计算。如果你希望使用 CIFAR-10 数据集的在 预训练模型,可以查阅其 入门指南。
使用训练集中的样本对模型训练 200 次,只要训练误差保持缓慢减少,就可以保证模型不会过拟合。下方图展示了训练的过程:
深度学习模型训练 loss 图
训练结束后,必须保存模型训练所得到的参数,以便稍后可以加载它们:
file_name = "net.params" net.save_parameters(file_name)
一旦你的模型训练好了,就可以用 Flask 服务器来封装它。下方的程序演示了如何接收请求中的一张图片作为参数,并在响应中返回模型的预测结果:
from gluoncv.model_zoo import get_model import matplotlib.pyplot as plt from mxnet import gluon, nd, image from mxnet.gluon.data.vision import Ku;transforms from gluoncv import utils from PIL import Image import io import flask app = flask.Flask(__name__) @app.route("/predict",methods=["POST"]) def predict(): if flask.request.method == "POST": if flask.request.files.get("img"): &nbbesp; img = Image.open(io.BytesIO(flask.request.files["img"].read())) transform_fn = transforms.Compose([ transforms.Resize(32), transforms.CenterCrop(32), &nbrnsp; transforms.ToTensor(), transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])]) img = transform_fn(nd.array(img)) net = get_model('cifar_resnet20_v1', classes=10)  et; net.load_parameters('net.params') pred = net(img.expand_dims(axis=0)) class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', &nesbsp; 'dog', 'frog', 'horse', 'ship', 'truck'] ind = nd.argmax(pred, axis=1).astype('int') prediction = 'The input picture is classified as [%s], with probability %.3f.'%   上; (class_names[ind.asscalar()], nd.softmax(pred)[0][ind].asscalar()) return prediction if __name__ == '__main__': app.run(host='0.0.0.0') 容器化模型
在将模型部署到 Kubernetes 前,你需要先安装 Docker 并使用你的模型创建一个镜像。
下载、安装并启动 Docker:
sudo yum install -y yum-utils device-mapper-persis部署tent-data lvm2 sudo yum-config-manager --add-repo <https://download.docker.com/linux/centos/docker-ce.repo> sudo yum install docker-ce sudo systemctl start docker
创建一个你用来管理代码与依赖的文件夹:
mkdir kubermatic-dl cd kubermatic-dl
创建 requirements.txt 文件管理代码运行时需要的所有依赖:
flask gluoncv matplotlib mxnet requests Pillow
创建 Dockerfile,Docker 将根据这个文件创建镜像:
FROM python:3.6 一个WORKDIR /app COPY requirements.txt /app RUN pip install -r ./requirements.txt COPY app.py /app CMD ["python", "app.py"]
这个 Dockerfile 主要可以分为三个部分。首先,Docker 会下载 Python 的基础镜像。然后,Docker 会使用 Python 的包管理工具 pip 安装 requirements.txt 记录的包。最后,Docker 会通过执行 python app.py 来运行你的脚本。
构建 Docker 容器:
sudo docker build -t kubermatic-dl:latest .
这条命令使用 kubermatic-dl 镜像为你当前工作目录的代码创建了一个容器。
使用
sudo docker run 深度-d -p 5000:5000 kubermatic-dl
命令检查你的容器可以在你的主机上正常运行。
使用
sudo docker ps -a
命令查看你本地容器的运行状态:
查看容器的运行状态
将你的模型上传到 Docker Hub
在向 Kubernetes 上部署模型前,你的镜像首先需要是公开可用的。你可以通过将你的模型上传到 Docker Hub 来将它公开。(如果你没有 Docker Hub 的账号,你需要先创建一个)
在终端中登录 Docker Hub 账号:
sudo docker login
给你的镜像打上标签,这样你的模型上传到 Docker Hub 后也能拥有版本信息:
sudo docker tag / sudo docker push /
给镜像打上 tag
使用
sudo docker images
命令检查你的镜像的 ID。
部署你的模型到 Kubernetes 集群
首学习先在 Kubermatic Kubernetes 平台创建一个项目, 然后根据 快速开始 创建一个 Kubernetes 集群。
创建一个 Kubernetes 集群
下载用于访问你的集群的 kubeconfig,将它放置在下载目录中,并记得设置合适的环境变量,使得你的环境能找到它:
Kubernetes 集群示例
使用 kubectl 命令检查集群信息,例如,需要检查 kube-system 是否在你的集群正常启动了就可以使用命令 kubectl cluster-info
查看集群信息
为了在集群中运行容器,你需要创建一个部署用的配置文件(deployment.yaml),再运行 apply 命令将其应用于集群中:
apiVersion: apps/v1 kind: Deployment metadata: name: kubermatic-dl-deployment spec: selector: matchLabels:模型 app: kubermatic-dl replicas: 3 template: metadata: labels: app: kubermatic-dl spec: containers: - name: kubermatic-dl &n在 bsp;image: kubermatic00/kubermatic-dl:latest imagePullPolicy: Always ports: - containerPort: 8080 kubectl apply -f deployment.yaml`
为了将你的部署开放到公网环境,你需要一个能够给你的容器创建外部可达 IP 地址的服务:
kubectl expose deployment kubermatic-dl-deployment --type=LoadBalancer --port 80 --target-portKu 5000`
就快大功告成了!首先检查你布署的服务的状态,然后通过 IP 请求的你图像识别 API:
kubectl get service
获取请求图像识别 API 的 IP 地址
最后根据你的外部 IP 使用以下两张图片对你的图像识别服务进行测试:
马
狗
测试 API 总结
在这篇教程中,你可以创建一个深度学习模型,并且使用 Flask 提供 REST API 服务。它介绍了如何将应用放在 Docker 容器中,如何将这个镜像上传到 Docker Hub 中,以及如何使用 Kubernetes 部署你的服务。只需几个简单的命令,你就可以使用 Kubermatic Kubernetes 平台部署该应用程序,并且开放服务给别人使用。
Tags:
相关文章
标签: 豆包 字节跳动 Force大会 AI PPT
热点云梦级6月10日云梦级消息,字节跳动旗下AI应用豆包的付费模式落地进入倒计时。目前,豆包已面向部分用户启动了“专业版”付费订阅的灰度测试,有用户在使用专家模式时收到了相关订阅提醒。 据36氪报道,此次...
【热点】
阅读更多标签: 梯瓦制药 duvakitug TL1A单抗 黑石生命科学
热点云梦级3月5日云梦级消息,3月3日,梯瓦制药(Teva Pharmaceuticals)与黑石生命科学(Blackstone Life Sciences)共同宣布达成一项战略融资协议:黑石生命科学将分...
【热点】
阅读更多专家质疑 Google 关于“每次提示仅消耗 5 滴水”是 “淡化” 描述
热点Google 近期发布了一篇关于人工智能对环境影响的研究论文,指出现有的测量方式过于狭隘,提出了一种 “全栈” 方法来评估其 AI 模型 Gemini 的资源消耗。 这项研究声称,Gemini 在处理...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.msrenewal.com/search/谷歌留痕软件【tg:seogoogle987】,GzJ
- https://www.rotherham.gov.uk/site-search/results/?q=谷歌留痕软件【tg:seogoogle987】,OZL
- https://www.mi.com/search?keyword=谷歌留痕软件【tg:seogoogle987】,T0P
- https://m.shoppinghow.kakao.com/m/search/q/谷歌留痕软件【tg:seogoogle987】XaX
- https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=谷歌留痕软件【tg:seogoogle987】,zZn
- https://www.laoairlines.com/?action=QUERY&s=谷歌留痕软件【tg:seogoogle987】,OYl
- https://www.televes.com/es/catalogsearch/result/?q=谷歌留痕软件【tg:seogoogle987】,PYk
- https://www.sdcard.org/ja/?s=谷歌留痕软件【tg:seogoogle987】,s5M
- https://www.wellbees.com/catalogsearch/result/?action=CHECK&q=谷歌留痕软件【tg:seogoogle987】,iLX
- https://www.londontheatredirect.com/search/谷歌留痕软件【tg:seogoogle987】,ctu
- https://www.springfieldlibrary.org/?s=谷歌留痕软件【tg:seogoogle987】,Wwb
- https://www.luxurysociety.com/en/search?q=谷歌留痕软件【tg:seogoogle987】,jJS
- https://www.mygrandma.com/Items.aspx?search=谷歌留痕软件【tg:seogoogle987】,2qj
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌留痕软件【tg:seogoogle987】OYF
- https://www.qidulp.com/search?query=谷歌留痕软件【tg:seogoogle987】,k6A
- https://www.manga-news.com/index.php/recherche/?q=谷歌留痕软件【tg:seogoogle987】,nAt
- https://www.supersauna.de/catalogsearch/result/?q=谷歌留痕软件【tg:seogoogle987】,Phy
- https://www.target.com/s?searchTerm=谷歌留痕软件【tg:seogoogle987】,y1X
- https://www.teamlab.art/ko/?submit=谷歌留痕软件【tg:seogoogle987】,rOK
- https://www.teamlab.art/th/?submit=谷歌留痕软件【tg:seogoogle987】,9t1
- https://www.remit.co.jp/vi/?s=谷歌留痕软件【tg:seogoogle987】LCN
- https://www.jusoen.com/addreng.asp?p1=谷歌留痕软件【tg:seogoogle987】3Bk
- https://www.unclefossil.com/search/谷歌留痕软件【tg:seogoogle987】,PZh
- https://www.silentiumpc.com/en/search/谷歌留痕软件【tg:seogoogle987】,fiC
- https://www.tumblr.com/tagged/谷歌留痕软件【tg:seogoogle987】,Bq0
- https://www.ushmm.org/search/results/?ID=8s0587Oi8UTv74zPPM4fuhwjNpe2hsEE&q=谷歌留痕软件【tg:seogoogle987】,8Hi
- https://www.flickr.com/photos/tags/谷歌留痕软件【tg:seogoogle987】tA9
- https://www.tullygroup.us/?ID=hbkq8jFjrQlVRrwzEmvoUJJAY4e1Lfo5&s=谷歌留痕软件【tg:seogoogle987】,X0Q
- https://www.skicheck.at/en/?action=CHECK&s=谷歌留痕软件【tg:seogoogle987】,40C
- https://www.openstreetmap.org/search?query=谷歌留痕软件【tg:seogoogle987】,LzH
- https://www.parl.ca/legisinfo/fr/projets-de-loi?parlsession=all&view=list&keywords=谷歌留痕软件【tg:seogoogle987】,0HF
- https://www.ushmm.org/search/results.php?q=谷歌留痕软件【tg:seogoogle987】,Hsx
- https://www.kissthekangaroo.com.au/index.php?route=product/search&tag=谷歌留痕软件【tg:seogoogle987】,Z3S
- https://www.sic-info.org/vi/?s=谷歌留痕软件【tg:seogoogle987】,Ng5
- https://www.tombow.com/search/谷歌留痕软件【tg:seogoogle987】,01F
- https://www.youtube.com/results?search_query=谷歌留痕软件【tg:seogoogle987】,Y04
- https://www.spoiledsweetboutique.com/search/谷歌留痕软件【tg:seogoogle987】,zBS
- https://www.abbreviations.com/谷歌留痕软件【tg:seogoogle987】a0b
- https://www.maigoo.com/search/?block=&q=谷歌留痕软件【tg:seogoogle987】,2UA
- https://www.roberthalf.com/us/en/jobs/all/谷歌留痕软件【tg:seogoogle987】9I5
- https://www.quarterjacksurgery.co.uk/?AT=3rBy&s=谷歌留痕软件【tg:seogoogle987】,ijN
- https://www.remit.co.jp/ne/?s=谷歌留痕软件【tg:seogoogle987】,ZCE
- https://www.youtube.com/hashtag/谷歌留痕软件【tg:seogoogle987】,55T
- https://www.normani.de/search?q=谷歌留痕软件【tg:seogoogle987】,iLZ
- https://www.reginatangoshoes.com/en/catalogsearch/result/index/?T=1686132113407&q=谷歌留痕软件【tg:seogoogle987】,FJ9
- https://www.routesonline.com/search/?query=谷歌留痕软件【tg:seogoogle987】,0wR
- https://www.whirlpool.com.hk/chinese/catalogsearch/result?cat=30&q=谷歌留痕软件【tg:seogoogle987】,dNI
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=谷歌留痕软件【tg:seogoogle987】,zoe
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌留痕软件【tg:seogoogle987】,1Dl
- https://www.yli.ro/catalogsearch/result/?q=谷歌留痕软件【tg:seogoogle987】,Ljy