-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tool] Add a tool to test TorchServe. (#468)
* Add `title` option in `show_result_pyplot`. * Add test_torchserver.py * Add docs about test torchserve * Update docs and result output. * Update chinese docs.
- Loading branch information
Showing
6 changed files
with
138 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
albumentations>=0.3.2 --no-binary imgaug,albumentations | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from argparse import ArgumentParser | ||
|
||
import numpy as np | ||
import requests | ||
|
||
from mmcls.apis import inference_model, init_model, show_result_pyplot | ||
|
||
|
||
def parse_args(): | ||
parser = ArgumentParser() | ||
parser.add_argument('img', help='Image file') | ||
parser.add_argument('config', help='Config file') | ||
parser.add_argument('checkpoint', help='Checkpoint file') | ||
parser.add_argument('model_name', help='The model name in the server') | ||
parser.add_argument( | ||
'--inference-addr', | ||
default='127.0.0.1:8080', | ||
help='Address and port of the inference server') | ||
parser.add_argument( | ||
'--device', default='cuda:0', help='Device used for inference') | ||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
def main(args): | ||
# Inference single image by native apis. | ||
model = init_model(args.config, args.checkpoint, device=args.device) | ||
model_result = inference_model(model, args.img) | ||
show_result_pyplot(model, args.img, model_result, title='pytorch_result') | ||
|
||
# Inference single image by torchserve engine. | ||
url = 'http://' + args.inference_addr + '/predictions/' + args.model_name | ||
with open(args.img, 'rb') as image: | ||
response = requests.post(url, image) | ||
server_result = response.json() | ||
show_result_pyplot(model, args.img, server_result, title='server_result') | ||
|
||
assert np.allclose(model_result['pred_score'], server_result['pred_score']) | ||
print('Test complete, the results of PyTorch and TorchServe are the same.') | ||
|
||
|
||
if __name__ == '__main__': | ||
args = parse_args() | ||
main(args) |