遵循GitOps原则版本和部署模型
项目描述
MLEM帮助您打包和部署机器学习模型。它以标准格式保存ML模型,可用于各种生产场景,例如实时REST服务或批处理。
-
在任何地方运行您的ML模型:将模型作为Python包或Docker镜像包装,或将它们部署到Heroku、SageMaker或Kubernetes(即将推出更多平台)。使用单个命令透明地在平台之间切换。
-
自动将模型元数据转换为YAML:自动将Python需求和建议的输入数据包含到人类可读的、就绪的部署格式中。在任何ML框架上使用相同的元文件。
-
坚持您的训练工作流程: MLEM 不会要求您重写模型训练代码。只需在您的 Python 代码周围添加两行:一行导入库,一行保存模型。
-
以开发者为中心的体验: 当您感觉像 DevOps 一样时,请使用 CLI;当您感觉像开发者时,请使用 API。
MLEM 为什么特别呢?
使用 MLEM 而不是其他工具的主要原因是通过 GitOps 方法来管理模型生命周期。
-
Git 作为单一的事实来源: MLEM 将模型元数据写入一个纯文本文件,该文件可以与代码一起在 Git 中进行版本控制。这使 GitFlow 和其他软件开发最佳实践成为可能。
-
统一模型和软件部署: 使用用于软件更新的相同流程(分支、拉取请求等)发布模型。
-
重用现有的 Git 基础设施: 使用 GitHub 或 Gitlab 等熟悉的托管服务进行模型管理,而不是使用独立的服务。
-
UNIX 哲学: MLEM 是一个模块化工具,非常擅长解决一个问题。它与 Iterative.ai 的更大型工具集(如 DVC 和 CML)很好地集成。
使用方法
这是一段快速演示 MLEM 部署功能的巡礼。
请阅读入门指南以获取完整版本。
安装
MLEM 需要 Python 3。
$ python -m pip install mlem
要安装预发布版本
$ python -m pip install git+https://github.com/iterative/mlem
保存模型
# train.py
from mlem.api import save
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
def main():
data, y = load_iris(return_X_y=True, as_frame=True)
rf = RandomForestClassifier(
n_jobs=2,
random_state=42,
)
rf.fit(data, y)
save(
rf,
"models/rf",
sample_data=data,
)
if __name__ == "__main__":
main()
编码
查看我们拥有的内容
$ ls models/
rf
rf.mlem
$ cat rf.mlem
点击以显示 `cat` 输出
artifacts:
data:
hash: ea4f1bf769414fdacc2075ef9de73be5
size: 163651
uri: rf
model_type:
methods:
predict:
args:
- name: data
type_:
columns:
- sepal length (cm)
- sepal width (cm)
- petal length (cm)
- petal width (cm)
dtypes:
- float64
- float64
- float64
- float64
index_cols: []
type: dataframe
name: predict
returns:
dtype: int64
shape:
- null
type: ndarray
predict_proba:
args:
- name: data
type_:
columns:
- sepal length (cm)
- sepal width (cm)
- petal length (cm)
- petal width (cm)
dtypes:
- float64
- float64
- float64
- float64
index_cols: []
type: dataframe
name: predict_proba
returns:
dtype: float64
shape:
- null
- 3
type: ndarray
type: sklearn
object_type: model
requirements:
- module: sklearn
version: 1.0.2
- module: pandas
version: 1.4.1
- module: numpy
version: 1.22.3
部署模型
如果您想跟随此快速入门,您需要在 https://heroku.com 上注册,创建一个 API_KEY 并填写 HEROKU_API_KEY
环境变量(或在命令行中运行 heroku login
)。此外,您需要运行 heroku container:login
。这将使您登录到 Heroku 容器注册表。
现在我们可以使用 mlem deploy 部署模型(您需要使用不同的 app_name
,因为模型将被发布在 https://herokuapp.com 上)
$ mlem deployment run heroku app.mlem \
--model models/rf \
--app_name example-mlem-get-started-app
⏳️ Loading model from models/rf.mlem
⏳️ Loading deployment from app.mlem
🛠 Creating docker image for heroku
🛠 Building MLEM wheel file...
💼 Adding model files...
🛠 Generating dockerfile...
💼 Adding sources...
💼 Generating requirements file...
🛠 Building docker image registry.heroku.com/example-mlem-get-started-app/web...
✅ Built docker image registry.heroku.com/example-mlem-get-started-app/web
🔼 Pushing image registry.heroku.com/example-mlem-get-started-app/web to registry.heroku.com
✅ Pushed image registry.heroku.com/example-mlem-get-started-app/web to registry.heroku.com
🛠 Releasing app example-mlem-get-started-app formation
✅ Service example-mlem-get-started-app is up. You can check it out at https://example-mlem-get-started-app.herokuapp.com/
贡献
欢迎贡献!请参阅我们的贡献指南以获取更多详细信息。
感谢所有贡献者!
版权
本项目采用 Apache 许可证 2.0 版本进行分发(请参阅项目根目录中的 LICENSE 文件)。
通过向本项目提交拉取请求,您同意在 Apache 许可证 2.0 版本下向本项目许可您的贡献。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。