打包 ML 模型

面向生产环境的机器学习模型开发

Sinan Ozdemir

Data Scientist, Entrepreneur, and Author

为何需要打包

  • 优化性能
  • 确保兼容性
  • 便于部署模型

"打包"方法

  • 序列化:简单、轻量、与语言无关
  • 环境打包:捕获完整软件环境
  • 容器化:可移植、可复现、隔离的环境
面向生产环境的机器学习模型开发

如何打包 ML 模型

  • 序列化:存储与加载 ML 模型

  • 环境打包:为 ML 模型提供一致、可复现的环境

  • 容器化:将模型、依赖和环境打包为单个"容器"

面向生产环境的机器学习模型开发

序列化 scikit-learn 模型

使用 pickle 序列化 sklearn 模型:

import pickle

model = ...  # Train the scikit-learn model

# Serialize the model to a file
with open('model.pkl', 'wb') as f:
    pickle.dump(model, f)

# Load the serialized model from the file
with open('model.pkl', 'rb') as f:
    model = pickle.load(f)

将 sklearn 模型序列化为 HDF5 格式:

import h5py
import numpy as np
from sklearn.externals import joblib

model = ...  # Train the scikit-learn model

# Serialize the model to an HDF5 file
with h5py.File('model.h5', 'w') as f:
    f.create_dataset('model_weights',
    data=joblib.dump(model))

# Load the serialized model from the HDF5 file
with h5py.File('model.h5', 'r') as f:
    model = joblib.load(f['model_weights'][:])
面向生产环境的机器学习模型开发

序列化 PyTorch 和 Tensorflow 模型

序列化 PyTorch 模型:

import torch

# Train a PyTorch model and store it in a variable
trained_model = ...

# Serialize the trained model to a file
serialized_model_path = 'model.pt'
torch.save(trained_model.state_dict(), serialized_model_path)

# Load the serialized model from a file
loaded_model = ... # Initialize the model
loaded_model.load_state_dict(
    torch.load(serialized_model_path))

序列化 Tensorflow 模型:

import tensorflow as tf

# Train a Tensorflow model
trained_model = ...

# Save the trained model to a directory 
saved_model_directory = 'model/'
tf.saved_model.save
    (trained_model, saved_model_directory)

# Load the saved model from the directory 
loaded_model = tf.saved_model.load(
    saved_model_directory)
面向生产环境的机器学习模型开发

用 Docker 打包 ML 环境

  • 确保模型运行所需的环境
  • virtualenv 等可创建一致、可复现的环境
  • Docker 容器自包含,易于部署

docker

面向生产环境的机器学习模型开发

示例 Dockerfile

# Use an existing image as the base image
FROM python:3.8-slim

# Set the working directory
WORKDIR /app

# Copy the requirements file to the image
COPY requirements.txt .

# Install the required dependencies
RUN pip install -r requirements.txt

# Copy the ML model and its dependencies to the image
COPY model/ .

# Set the entrypoint to run the model
ENTRYPOINT ["python", "run_model.py"]

<---- 使用 Python 3.8 基础镜像


<---- 设置工作目录


<---- 复制 requirements.txt 文件


<---- 安装模型依赖包


<---- 将模型复制到容器


<---- 指定容器启动方式
面向生产环境的机器学习模型开发

实验 → Docker 工作流

  1. 使用 pickle、HDF5 或 PyTorch 等格式对已训练模型进行"序列化"。

  2. 将序列化的模型、依赖和环境"容器化"

  3. 将 Docker 镜像"部署"到目标环境,如云平台

  4. 从已部署的镜像"运行"容器并运行模型

  5. 通过 API 等入口在容器内"使用"模型

Docker 工作流

面向生产环境的机器学习模型开发

Passons à la pratique !

面向生产环境的机器学习模型开发

Preparing Video For Download...