torchserve使用教程

torchserve使用教程,第1张

torchserve使用教程

官方文档地址:

https://github.com/pytorch/serve/blob/master/docs/README.mdhttps://github.com/pytorch/serve/blob/master/docs/README.md

下载项目:

git clone GitHub - pytorch/serve: Model Serving on PyTorch

------Serving quick start---------------------------------------------

注:还需要安装jdk11

(1)安装依赖项

 到源码根目录下运行:安装GPU的依赖

python ./ts_scripts/install_dependencies.py --cuda=cu102

(2)安装torchserve, torch-model-archiver and torch-workflow-archiver

使用pip安装:

pip install torchserve torch-model-archiver torch-workflow-archiver

 (3)源码安装:

若要对源码进行开发,安装方式如下:

NOTE: This script uninstalls existing torchserve, torch-model-archiver and torch-workflow-archiver installations

python ./ts_scripts/install_dependencies.py --environment=dev
python ./ts_scripts/install_from_src.py
Store a Model

新建一个文件夹

mkdir model_store

下载官方示例模型

wget https://download.pytorch.org/models/densenet161-8d451a50.pth

 Archive the model (参数解析后续详解)

--serialized-file:pth文件名
--export-path  :pth文件夹路径
torch-model-archiver --model-name densenet161 --version 1.0 --model-file ./serve/examples/image_classifier/densenet_161/model.py --serialized-file densenet161-8d451a50.pth --export-path model_store --extra-files ./serve/examples/image_classifier/index_to_name.json --handler image_classifier

运行完成后在model_store下会生成一个.mar的模型

Start TorchServe to serve the model启动服务

 --model-store:mar文件夹路径

(其余参数后续详解)

torchserve --start --ncs --model-store model_store --models densenet161.mar

注:其中可能会有报错,确保jdk11环境;其中还会有python报错,缺少哪些环境就pip安装。

Get predictions from a model 输入图片进行预测

(1)python程序调用的方式:

pip install -U grpcio protobuf grpcio-tools
python -m grpc_tools.protoc --proto_path=frontend/server/src/main/resources/proto/ --python_out=ts_scripts --grpc_python_out=ts_scripts frontend/server/src/main/resources/proto/inference.proto frontend/server/src/main/resources/proto/management.proto
python ts_scripts/torchserve_grpc_client.py infer densenet161 examples/image_classifier/kitten.jpg

(2)REST API

最后是图片:

curl http://127.0.0.1:8080/predictions/densenet161 -T kitten_small.jpg

return返回示例

[
  {
    "tiger_cat": 0.46933549642562866
  },
  {
    "tabby": 0.4633878469467163
  },
  {
    "Egyptian_cat": 0.06456148624420166
  },
  {
    "lynx": 0.0012828214094042778
  },
  {
    "plastic_bag": 0.00023323034110944718
  }
]

------------Torch Model archiver for TorchServe----------------

torchserve需要一个.mar文件,转换自pytorch的pth文件或torchscript(jit的pt) 文件,

#####待续

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5689517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存