2021-06-12 21:45:45 +08:00
# Model Serving
In order to serve an `MMClassification` model with [`TorchServe` ](https://pytorch.org/serve/ ), you can follow the steps:
## 1. Convert model from MMClassification to TorchServe
```shell
python tools/deployment/mmcls2torchserve.py ${CONFIG_FILE} ${CHECKPOINT_FILE} \
--output-folder ${MODEL_STORE} \
--model-name ${MODEL_NAME}
```
2021-10-14 17:56:32 +08:00
```{note}
${MODEL_STORE} needs to be an absolute path to a folder.
```
Example:
```shell
python tools/deployment/mmcls2torchserve.py \
configs/resnet/resnet18_b32x8_imagenet.py \
checkpoints/resnet18_8xb32_in1k_20210831-fbbb1da6.pth \
--output-folder ./checkpoints \
--model-name resnet18_in1k
```
2021-06-12 21:45:45 +08:00
## 2. Build `mmcls-serve` docker image
```shell
docker build -t mmcls-serve:latest docker/serve/
```
## 3. Run `mmcls-serve`
Check the official docs for [running TorchServe with docker ](https://github.com/pytorch/serve/blob/master/docker/README.md#running-torchserve-in-a-production-docker-environment ).
2021-06-30 20:53:09 +08:00
In order to run in GPU, you need to install [nvidia-docker ](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html ). You can omit the `--gpus` argument in order to run in GPU.
2021-06-12 21:45:45 +08:00
Example:
```shell
docker run --rm \
--cpus 8 \
--gpus device=0 \
-p8080:8080 -p8081:8081 -p8082:8082 \
2021-10-14 17:56:32 +08:00
--mount type=bind,source=`realpath ./checkpoints`,target=/home/model-server/model-store \
2021-06-12 21:45:45 +08:00
mmcls-serve:latest
```
2021-10-14 17:56:32 +08:00
```{note}
`realpath ./checkpoints` points to the absolute path of "./checkpoints", and you can replace it with the absolute path where you store torchserve models.
```
2021-06-30 20:53:09 +08:00
[Read the docs ](https://github.com/pytorch/serve/blob/master/docs/rest_api.md ) about the Inference (8080), Management (8081) and Metrics (8082) APis
2021-06-12 21:45:45 +08:00
## 4. Test deployment
```shell
2021-10-14 17:56:32 +08:00
curl http://127.0.0.1:8080/predictions/${MODEL_NAME} -T demo/demo.JPEG
2021-06-12 21:45:45 +08:00
```
2021-10-13 14:33:07 +08:00
You should obtain a response similar to:
2021-06-12 21:45:45 +08:00
```json
{
2021-10-14 17:56:32 +08:00
"pred_label": 58,
"pred_score": 0.38102269172668457,
"pred_class": "water snake"
2021-06-12 21:45:45 +08:00
}
```
2021-10-14 17:56:32 +08:00
And you can use `test_torchserver.py` to compare result of TorchServe and PyTorch, and visualize them.
```shell
python tools/deployment/test_torchserver.py ${IMAGE_FILE} ${CONFIG_FILE} ${CHECKPOINT_FILE} ${MODEL_NAME}
[--inference-addr ${INFERENCE_ADDR}] [--device ${DEVICE}]
```
Example:
```shell
python tools/deployment/test_torchserver.py \
demo/demo.JPEG \
configs/resnet/resnet18_b32x8_imagenet.py \
checkpoints/resnet18_8xb32_in1k_20210831-fbbb1da6.pth \
resnet18_in1k
```