栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

torchserve使用教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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) 文件,

#####待续

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/693150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号