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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243
  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:5500')
  21. stub = ms_service_pb2_grpc.MSServiceStub(channel)
  22. request = ms_service_pb2.PredictRequest()
  23. x = request.data.add()
  24. x.tensor_shape.dims.extend([4])
  25. x.tensor_type = ms_service_pb2.MS_FLOAT32
  26. x.data = (np.ones([4]).astype(np.float32)).tobytes()
  27. y = request.data.add()
  28. y.tensor_shape.dims.extend([4])
  29. y.tensor_type = ms_service_pb2.MS_FLOAT32
  30. y.data = (np.ones([4]).astype(np.float32)).tobytes()
  31. result = stub.Predict(request)
  32. print(result)
  33. result_np = np.frombuffer(result.result[0].data, dtype=np.float32).reshape(result.result[0].tensor_shape.dims)
  34. print("ms client received: ")
  35. print(result_np)
  36. if __name__ == '__main__':
  37. run()