栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

002、torchserve调用官方库模型

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

002、torchserve调用官方库模型

前言

在PyTorch 环境下生成模型 .pt 格式,放到指定目录下,再转换为 .mar 格式,利用模型预测

1、本地生成 .pt 模型
#scripted mode
from torchvision import models
import torch
model = models.resnet18(pretrained=True)
sm = torch.jit.script(model)
sm.save("/Users/hb/serve/model-archiver/resnet-18.pt")
2、转换为 .mar 格式
#  model-archiver
pip install .
torch-model-archiver -f --model-name resnet-18 
--version 1.0 
--serialized-file resnet-18.pt 
--handler image_classifier 
--export-path model-store/

可能会执行报错,把 全部删了就能运行成功

torch-model-archiver -f --model-name resnet-18  --version 1.0 --serialized-file resnet-18.pt --handler image_classifier --export-path model-store/

3、 docker 启动 torchserve
docker run --rm -it -p 8080:8080 -p 8081:8081 --name mar -v $(pwd)/model-store:/home/model-server/model-store -v $(pwd)/examples:/home/model-server/examples  pytorch/torchserve:latest
4、注册模型
curl --location --request POST 'http://localhost:8081/models' 
--header 'Content-Type: application/json' 
--data-raw '{
    "url": "resnet-18.mar",
    "batch_size": 64,
    "initial_workers": 1
}'

代码可能会报错,格式不对
https://code.bizseer.com/puruokun/torch-serving/-/tree/master/
从这里修改的代码,可以用;复制到其他地方,再次复制使用就会出现问题。

5、预测
  • 下载图片
    curl -O https://s3.amazonaws.com/model-server/inputs/kitten.jpg

  • 预测

 curl 'http://127.0.0.1:8080/predictions/resnet-18' -T kitten.jpg

6、删除没有注册并且没用的模型(可选)

curl --location --request DELETE ‘http://localhost:8081/models/shape_cnn’

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

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

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