FastAI课程项目:基于BentoML与Kubernetes的宠物分类模型部署指南
course-v3 The 3rd edition of course.fast.ai 项目地址: https://gitcode.com/gh_mirrors/co/course-v3
前言
在完成FastAI深度学习课程中的宠物分类模型训练后,如何将模型部署到生产环境是许多开发者面临的下一个挑战。本文将详细介绍如何利用BentoML框架将FastAI训练好的宠物分类模型打包,并通过Kubernetes实现容器化部署,构建一个可扩展的REST API服务。
准备工作
环境要求
-
Kubernetes环境:
- 本地开发推荐使用minikube搭建单节点集群
- 生产环境可选择云服务商提供的托管Kubernetes服务
-
Docker环境:
- 需要安装Docker引擎并配置好容器镜像仓库账户
- 确保本地可以构建和推送Docker镜像
-
Python环境:
- Python 3.6及以上版本
- 安装必要包:
pip install bentoml fastai==1.0.57 torch==1.4.0 torchvision==0.5.0
模型服务开发
创建BentoML服务
BentoML是一个专门为机器学习模型服务化设计的框架。下面我们定义一个宠物分类服务:
from bentoml import BentoService, api, env, artifacts
from bentoml.artifact import FastaiModelArtifact
from bentoml.handlers import FastaiImageHandler
@artifacts([FastaiModelArtifact('pet_classifier')])
@env(auto_pip_dependencies=True)
class PetClassification(BentoService):
@api(FastaiImageHandler)
def predict(self, image):
result = self.artifacts.pet_classifier.predict(image)
return str(result)
这段代码定义了一个包含预测接口的模型服务,使用FastAI的ImageHandler处理图片输入。
训练并打包模型
使用FastAI课程中的宠物分类模型训练代码:
from fastai.vision import *
# 数据准备和模型训练代码...
learn = create_cnn(data, models.resnet50, metrics=error_rate)
learn.fit_one_cycle(8)
learn.unfreeze()
learn.fit_one_cycle(3, max_lr=slice(1e-6,1e-4))
# 打包模型
from pet_classification import PetClassification
service = PetClassification()
service.pack('pet_classifier', learn)
service.save()
执行后会生成一个包含模型、代码和依赖的SavedBundle,这是部署的基本单元。
本地测试
启动本地服务
bentoml serve PetClassification:latest
测试API接口
curl -i --request POST --header "Content-Type: multipart/form-data" \
-F "image=@/path/to/test_image.jpg" localhost:5000/predict
Kubernetes部署
构建Docker镜像
- 获取SavedBundle路径
- 构建并推送镜像
saved_path=$(bentoml get PetClassifier:latest -q | jq -r ".uri.uri")
docker build -t your-container-registry/pet-classifier $saved_path
docker push your-container-registry/pet-classifier
创建Kubernetes部署文件
pet-classifier.yaml
示例:
apiVersion: v1
kind: Service
metadata:
name: pet-classifier
spec:
ports:
- port: 5000
targetPort: 5000
selector:
app: pet-classifier
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pet-classifier
spec:
selector:
matchLabels:
app: pet-classifier
template:
metadata:
labels:
app: pet-classifier
spec:
containers:
- image: your-container-registry/pet-classifier
name: pet-classifier
ports:
- containerPort: 5000
部署到集群
kubectl apply -f pet-classifier.yaml
验证部署
kubectl get svc pet-classifier
监控配置
为模型服务添加Prometheus监控:
# 在Deployment的template.metadata中添加
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "5000"
生产环境建议
- 资源限制:为容器设置合理的CPU/内存限制
- 自动扩缩:配置HPA根据负载自动调整实例数
- 日志收集:集成ELK或类似日志系统
- 金丝雀发布:新版本先小流量验证
总结
通过本文,我们完成了从FastAI模型训练到BentoML服务打包,再到Kubernetes部署的完整流程。这种方案具有以下优势:
- 标准化:BentoML提供了统一的模型服务化规范
- 可扩展:Kubernetes集群可以轻松应对流量变化
- 可观测:内置监控指标便于运维
希望本指南能帮助你顺利将FastAI课程中的模型投入实际应用。
course-v3 The 3rd edition of course.fast.ai 项目地址: https://gitcode.com/gh_mirrors/co/course-v3
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考