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.

ms_client.py 2.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. import grpc
  16. import numpy as np
  17. import ms_service_pb2
  18. import ms_service_pb2_grpc
  19. def run():
  20. channel = grpc.insecure_channel('localhost:50051')
  21. stub = ms_service_pb2_grpc.MSServiceStub(channel)
  22. # request = ms_service_pb2.PredictRequest()
  23. # request.name = 'haha'
  24. # response = stub.Eval(request)
  25. # print("ms client received: " + response.message)
  26. request = ms_service_pb2.PredictRequest()
  27. request.data.tensor_shape.dims.extend([32, 1, 32, 32])
  28. request.data.tensor_type = ms_service_pb2.MS_FLOAT32
  29. request.data.data = (np.ones([32, 1, 32, 32]).astype(np.float32) * 0.01).tobytes()
  30. request.label.tensor_shape.dims.extend([32])
  31. request.label.tensor_type = ms_service_pb2.MS_INT32
  32. request.label.data = np.ones([32]).astype(np.int32).tobytes()
  33. result = stub.Predict(request)
  34. #result_np = np.frombuffer(result.result.data, dtype=np.float32).reshape(result.result.tensor_shape.dims)
  35. print("ms client received: ")
  36. #print(result_np)
  37. # future_list = []
  38. # times = 1000
  39. # for i in range(times):
  40. # async_future = stub.Eval.future(request)
  41. # future_list.append(async_future)
  42. # print("async call, future list add item " + str(i));
  43. #
  44. # for i in range(len(future_list)):
  45. # async_result = future_list[i].result()
  46. # print("ms client async get result of item " + str(i))
  47. if __name__ == '__main__':
  48. run()