You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Client for resnet50"""
  16. import os
  17. from mindspore_serving.client import Client
  18. def read_images():
  19. """Read images for directory test_image"""
  20. images_buffer = []
  21. for path, _, file_list in os.walk("./test_image/"):
  22. for file_name in file_list:
  23. image_file = os.path.join(path, file_name)
  24. print(image_file)
  25. with open(image_file, "rb") as fp:
  26. images_buffer.append(fp.read())
  27. return images_buffer
  28. def run_classify_top1():
  29. """Client for servable resnet50 and method classify_top1"""
  30. print("run_classify_top1-----------")
  31. client = Client("localhost", 5500, "resnet50", "classify_top1")
  32. instances = []
  33. for image in read_images():
  34. instances.append({"image": image})
  35. result = client.infer(instances)
  36. print(result)
  37. def run_classify_top1_v1():
  38. """Client for servable resnet50 and method classify_top1_v1"""
  39. print("run_classify_top1_v1-----------")
  40. client = Client("localhost", 5500, "resnet50", "classify_top1_v1")
  41. instances = []
  42. for image in read_images():
  43. instances.append({"image": image})
  44. result = client.infer(instances)
  45. print(result)
  46. def run_classify_top5():
  47. """Client for servable resnet50 and method classify_top5"""
  48. print("run_classify_top5-----------")
  49. client = Client("localhost", 5500, "resnet50", "classify_top5")
  50. instances = []
  51. for image in read_images(): # read multi image
  52. instances.append({"image": image}) # input `image`
  53. result = client.infer(instances)
  54. print(result)
  55. for result_item in result: # result for every image
  56. label = result_item["label"] # result `label`
  57. score = result_item["score"] # result `score`
  58. print("label result:", label)
  59. print("score result:", score)
  60. def run_classify_top5_async():
  61. """Client for servable resnet50 and method classify_top5"""
  62. print("run_classify_top5_async-----------")
  63. client = Client("localhost", 5500, "resnet50", "classify_top5")
  64. instances = []
  65. for image in read_images(): # read multi image
  66. instances.append({"image": image}) # input `image`
  67. result_future = client.infer_async(instances)
  68. result = result_future.result()
  69. print(result)
  70. for result_item in result: # result for every image
  71. label = result_item["label"] # result `label`
  72. score = result_item["score"] # result `score`
  73. print("label result:", label)
  74. print("score result:", score)
  75. def run_restful_classify_top1():
  76. """RESTful Client for servable resnet50 and method classify_top1"""
  77. print("run_restful_classify_top1-----------")
  78. import base64
  79. import requests
  80. import json
  81. instances = []
  82. for image in read_images():
  83. base64_data = base64.b64encode(image).decode()
  84. instances.append({"image": {"b64": base64_data}})
  85. instances_map = {"instances": instances}
  86. post_payload = json.dumps(instances_map)
  87. ip = "localhost"
  88. restful_port = 1500
  89. servable_name = "resnet50"
  90. method_name = "classify_top1"
  91. result = requests.post(f"http://{ip}:{restful_port}/model/{servable_name}:{method_name}", data=post_payload)
  92. print(result.text)
  93. if __name__ == '__main__':
  94. run_classify_top1()
  95. run_classify_top1_v1()
  96. run_classify_top5()
  97. run_restful_classify_top1()
  98. run_classify_top5_async()

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.