torchserve使用-安装测试(一)

目录

1、安装方式

2、运行分类示例

3、运行语义分割示例

4、运行目标检测示例


pytorch1.6更新后,增加torchServe文档。官网文档地址:https://pytorch.org/serve/。

快速使用一下,直接跟踪至github。https://github.com/pytorch/serve/blob/master/README.md#serve-a-model

TorchServe是用于服务PyTorch模型的灵活易用的工具。

TorchServe架构

术语:

Frontend(前端):TorchServe的请求/响应处理组件。服务组件的这一部分既处理来自客户端的请求/响应,又管理模型的生命周期。

Model Workers(运行的模型):这些运行的模型进程负责在模型上运行实际的推断。这些是模型的实际运行实例。

Model (模型):模型可以是script_module(保存JIT的模型)或eager_mode_models。这些模型可以提供自定义的数据预处理和后处理以及任何其他模型工件(例如state_dicts)。可以从云存储或本地主机加载模型。

Plugins(插件):这些是自定义端点或authz / authn或批处理算法,可在启动时放入TorchServe。

Model Store(模型存储):这是一个目录,所有可加载模型都存在于该目录中。

1、安装方式

torchserve目前还在实验阶段,0.3版本维护不足,0.4版本仍在开发中。0.3版本还有一些遗留bug未处理,所以安装实现基本功能的0.2版本。

环境安装方式:

sudo apt-get install openjdk-11-jre-headless git wget curl -y
conda create -n serve2 python=3.7.9 -y
conda activate serve2
conda install pytorch=1.6 torchvision cudatoolkit=10.2 -y
conda install torchserve=0.2 torch-model-archiver=0.3 -c pytorch -y
# 或者
pip install torchserve==0.2 torch-model-archiver==0.3

下载代码,解压后进入该目录:

cd serve

2、运行分类示例

目录:examples/image_classifier

step1:下载权重

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

step2:模型转换

torch-model-archiver --model-name densenet161 --version 1.0 --model-file examples/image_classifier/densenet_161/model.py --serialized-file densenet161-8d451a50.pth --handler image_classifier --extra-files examples/image_classifier/index_to_name.json

step3:将模型放入存储目录

mkdir model_store
mv densenet161.mar model_store/

step4:启动serve

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

step5:测试一张图

curl http://127.0.0.1:8080/predictions/densenet161 -T examples/image_classifier/kitten.jpg

结果:

step6:测试完毕后,关闭服务、删除日志

在服务终端,ctrl+C结束。

使用torchserve --stop,关闭服务。

关闭为止:

删除logs:rm -rf logs

3、运行语义分割示例

目录:examples/image_segmenter

step1:下载权重

wget https://download.pytorch.org/models/fcn_resnet101_coco-7ecb50ca.pth

step2:模型转换

torch-model-archiver --model-name fcn_resnet_101 --version 1.0 --model-file examples/image_segmenter/fcn/model.py --serialized-file fcn_resnet101_coco-7ecb50ca.pth --handler image_segmenter --extra-files examples/image_segmenter/fcn/fcn.py,examples/image_segmenter/fcn/intermediate_layer_getter.py

step3:将模型放入存储目录

#mkdir model_store
mv fcn_resnet_101.mar model_store/

step4:启动serve

torchserve --start --model-store model_store --models fcn=fcn_resnet_101.mar

step5:测试一张图

curl http://127.0.0.1:8080/predictions/fcn -T examples/image_segmenter/fcn/persons.jpg

结果:

step6:测试完毕后,关闭服务、删除日志

在服务终端,ctrl+C结束。

使用torchserve --stop,关闭服务。

关闭为止:

删除logs:rm -rf logs

4、运行目标检测示例

目录:examples/object_detector/maskrcnn

step1:下载权重

wget https://download.pytorch.org/models/maskrcnn_resnet50_fpn_coco-bf2d0c1e.pth

step2:模型转换

torch-model-archiver --model-name maskrcnn --version 1.0 --model-file examples/object_detector/maskrcnn/model.py --serialized-file maskrcnn_resnet50_fpn_coco-bf2d0c1e.pth --handler object_detector --extra-files examples/object_detector/index_to_name.json

step3:将模型放入存储目录

#mkdir model_store
mv maskrcnn.mar model_store/

step4:启动serve

torchserve --start --model-store model_store --models maskrcnn=maskrcnn.mar

step5:测试一张图

curl http://127.0.0.1:8080/predictions/maskrcnn -T examples/object_detector/persons.jpg

结果:

step6:测试完毕后,关闭服务、删除日志

在服务终端,ctrl+C结束。

使用torchserve --stop,关闭服务。

关闭为止:

删除logs:rm -rf logs

下一节:torchserve使用-注册模型设置参数(二)

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jingbo1801

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值