当前位置:网站首页>002. Torchserve calls the official library model

002. Torchserve calls the official library model

2022-06-13 12:11:00 nsq1101

Preface

stay PyTorch Generate model in environment .pt Format , Put it in the specified directory , Re convert to .mar Format , Prediction by model

1、 Locally generated .pt Model

#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、 Convert to .mar Format

#  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/

An error may be reported , hold \ Delete all of them and they will run successfully

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 start-up 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、 Registration model

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
}'

The code may report an error , Wrong format
https://code.bizseer.com/puruokun/torch-serving/-/tree/master/
Code modified from here , It can be used ; Copy to other places , Copying and using again will cause problems .

5、 forecast

  • Download the pictures
    curl -O https://s3.amazonaws.com/model-server/inputs/kitten.jpg

  • forecast

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

6、 Delete unregistered and useless models ( Optional )

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

原网站

版权声明
本文为[nsq1101]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131205314682.html