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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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():
  49. instances.append({"image": image})
  50. result = client.infer(instances)
  51. print(result)
  52. def run_restful_classify_top1():
  53. """RESTful Client for servable resnet50 and method classify_top1"""
  54. import base64
  55. import requests
  56. import json
  57. instances = []
  58. for image in read_images():
  59. base64_data = base64.b64encode(image).decode()
  60. instances.append({"image": {"b64": base64_data}})
  61. instances_map = {"instances": instances}
  62. post_payload = json.dumps(instances_map)
  63. ip = "localhost"
  64. restful_port = 1500
  65. servable_name = "resnet50"
  66. method_name = "classify_top1"
  67. result = requests.post(f"http://{ip}:{restful_port}/model/{servable_name}:{method_name}", data=post_payload)
  68. print(result.text)
  69. if __name__ == '__main__':
  70. run_classify_top1()
  71. run_classify_top1_v1()
  72. run_classify_top5()
  73. run_restful_classify_top1()

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