目录
2、Using GRPC APIs through python client
通过脚本实现网络端访问。
1、通过http访问Using REST APIs
curl http://127.0.0.1:8080/predictions/densenet161 -T examples/image_classifier/kitten.jpg
实现相同功能:
import cv2
from PIL import Image
from io import BytesIO
def test():
filename = "examples/image_classifier/kitten.jpg"
image = Image.fromarray(cv2.imread(filename))
image2bytes = BytesIO()
image.save(image2bytes, format="PNG")
image2bytes.seek(0)
image_as_bytes = image2bytes.read()
# Send the HTTP POST request to TorchServe
import requests
req = requests.post("http://127.0.0.1:8080/predictions/densenet161", data=image_as_bytes)
if req.status_code == 200:
res = req.json()
print("ret:", res)
结果:
ret: {'tabby': 0.39039885997772217, 'tiger_cat': 0.36372312903404236,
'Egyptian_cat': 0.21934737265110016, 'lynx': 0.021873705089092255,
'snow_leopard': 0.0012753388145938516}
2、Using GRPC APIs through python client
Using GRPC APIs through python client
- Install grpc python dependencies :
pip install -U grpcio protobuf grpcio-tools
- Generate inference client using proto files
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
- Run inference using a sample client
- gRPC python client
python ts_scripts/torchserve_grpc_client.py infer densenet161 examples/image_classifier/kitten.jpg
https://github.com/pytorch/serve/blob/master/ts_scripts/torchserve_grpc_client.py
更多参数配置:
https://github.com/pytorch/serve/blob/v0.2.0/docs/batch_inference_with_ts.md
端口配置:
https://github.com/pytorch/serve/blob/v0.2.0/docs/configuration.md
上一节:torchserve使用-注册模型设置参数(二),下一节:torchserve使用-Torch Model archiver for TorchServe(四)