Pytorch

Pytorch袖珍手册之十五

2021-08-31  本文已影响0人  深思海数_willschang
pytorch pocket reference

第七章 模型部署 Deploying Pytorch to Production 之一

TorchServer和TorchScript为我们提供了便捷的部署方案,不管是在云端服务器,手机端还是智能边缘设备。

在实际部署中,还是要根据应用环境权衡各个环境下的部署方案。本章节中将介绍几种不同的场景部署方案:

Pytorch部署工具和包

本章节基础示例模型:VGG16+ImageNet data的预训练模型

import numpy as np
from torchvision.models import vgg16

model = vgg16(pretrained=True)
# 查看共有多少参数量
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
params = sum([np.prod(p.size()) for p in model_parameters])
# 138357544参数需要训练学习(1亿多)
print(params)
Python API方式部署
import system
import torch

if __name__ == "__main__":
    model = MyModel()
    model = load_state_dict(torch.load(PATH))
    model.eval()
    outputs = model(inputs)
    print(outputs)
TorchScript方式

TorchScript提供了序列化和优化模型代码的方法,让Pytorch模型可以运行在非python环境的场景中。
TorchScript编译器提供了系列化,优化模型版本,使得模型可以运行在C++应用中。

TorchScript is commonly used to run PyTorch models in C++ and with any language that supports C++ bindings.
To load your TorchScript model in C++, you would use the PyTorch C++ API library called LibTorch.

有两种方式进行Pytorch模型TorchScript化:

# 简单示例
import torch

model = vgg16(pretrained=True)
example_input = torch.rand(1, 3, 224, 224)
torchscript_model = torch.jit.trace(model, example_input)
# 模型保存
torchscript_model.save("traced_vgg16_model.pt")

#  模型应用
output = torchscript_model (inputs)
import torch.nn as nn

class ControlFlowModel(nn.Module):
    def __init__(self, N):
        super(ControlFlowModel, self).__init__()
        self.fc = nn.Linear(N,100)
    
    def forward(self, input):
        if input.sum() > 0:
            output = input
        else:
            output = -input
        return output
    
model = ControlFlowModel(10)
torchcript_model = torch.jit.script(model)
torchscript_model.save("scripted_vgg16_model.pt")

在c++应用中调用示例

include <torch/script.h>
#include <iostream>
#include <memory>

int main(int argc, const char* argv[]) {
    if (argc != 2) {
        std::cerr << "usage: example-app" >> \
        "<path-to-exported-script-module>\n";
        return -1;
    }
    torch::jit::script::Module model;
    model = torch::jit::load(argv[1]);
    std::vector<torch::jit::IValue> inputs;

    inputs.push_back(torch::ones({1, 3, 224, 224}));at::Tensor output = model.forward(inputs).toTensor();
    std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
    }
}
TorchScript
TorchScript2.png
TorchServe

TorchServe是PyTorch的开源模型服务库。
AWS与Facebook合作开发了TorchServe。
借助TorchServe,您可以使用TorchScript在急切或图形模式下部署PyTorch模型,同时提供多个模型,用于A / B测试的版本生产模型,动态加载和卸载模型,以及监视详细的日志和可自定义的指标。

TorchServe易于使用。它带有一个方便的CLI,可以在本地部署,并且可以轻松打包到容器中,并可以通过Amazon SageMaker或Amazon EKS进行横向扩展。使用针对常见问题(例如图像分类,对象检测,图像分割和文本分类)的默认处理程序,您只需部署几行代码即可进行部署-无需再为初始化,预处理和后处理编写冗长的服务处理程序。TorchServe是开源的,这意味着它是完全开放的并可扩展的,可以满足您的部署需求。

TorchServe Architecture
TorchServe workflow TorchServe-1

用TorchServe来部署模型有几个步骤需要完成:

  1. 安装TorchServe工具
  2. 通过模型存档工具(the model archiver tool)对模型进行打包
  3. 模型存档完成后,运行TorchServe的Web服务
  4. Web服务启动后,即可通过APIs来请求预测,管理模型,运行监控或者获取服务日志。
conda install torchserve torch-model-archiver -c pytorch
pip install torchserve torch-model-archiver

It packages model checkpoints as well as the state_dict into a .mar file that the TorchServe server uses to serve the mode.

image.png image.png

TorchServe includes a built-in web server that is run from the command line. It wraps one or more PyTorch models in a set of REST APIs and provides controls for configuring the port, host, and logging.

image.png

The Inference API listens on port 8080 and is only accessible from localhost by default.

You can configure metrics using the Metrics API and monitor and log your models’ performance when deployed. The Metrics API listens on port 8082 and is only accessible from localhost by default, but you can change the default when configuring your TorchServe server.

其他方法: The NVIDIA Triton Inference Server is becoming morepopular and is also used to deploy AI models at scale in production.

上一篇 下一篇

猜你喜欢

热点阅读