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 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. client = Client("localhost", 5500, "resnet50", "classify_top1")
  31. instances = []
  32. for image in read_images():
  33. instances.append({"image": image})
  34. result = client.infer(instances)
  35. print(result)
  36. def run_classify_top1_v1():
  37. """Client for servable resnet50 and method classify_top1_v1"""
  38. client = Client("localhost", 5500, "resnet50", "classify_top1_v1")
  39. instances = []
  40. for image in read_images():
  41. instances.append({"image": image})
  42. result = client.infer(instances)
  43. print(result)
  44. def run_classify_top5():
  45. """Client for servable resnet50 and method classify_top5"""
  46. client = Client("localhost", 5500, "resnet50", "classify_top5")
  47. instances = []
  48. for image in read_images(): # read multi image
  49. instances.append({"image": image}) # input `image`
  50. result = client.infer(instances)
  51. print(result)
  52. for result_item in result: # result for every image
  53. label = result_item["label"] # result `label`
  54. score = result_item["score"] # result `score`
  55. print("label result", label)
  56. print("score result", score)
  57. def run_restful_classify_top1():
  58. """RESTful Client for servable resnet50 and method classify_top1"""
  59. import base64
  60. import requests
  61. import json
  62. instances = []
  63. for image in read_images():
  64. base64_data = base64.b64encode(image).decode()
  65. instances.append({"image": {"b64": base64_data}})
  66. instances_map = {"instances": instances}
  67. post_payload = json.dumps(instances_map)
  68. ip = "localhost"
  69. restful_port = 1500
  70. servable_name = "resnet50"
  71. method_name = "classify_top1"
  72. result = requests.post(f"http://{ip}:{restful_port}/model/{servable_name}:{method_name}", data=post_payload)
  73. print(result.text)
  74. if __name__ == '__main__':
  75. run_classify_top1()
  76. run_classify_top1_v1()
  77. run_classify_top5()
  78. run_restful_classify_top1()

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