def test_int_scalar_tensor_to_np_scalar_back_to_tensor(self, shape): tp_kwargs = {"int64_val": [1], "dtype": DT_INT64, "tensor_shape": shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto
def Predict(self, request, context): self.logger.info( "Received inference request: {}".format(request)[:256]) numpy_outputs = {} try: numpy_request_inputs: Dict[str] = { k: tensor_proto_to_np(t) for k, t in request.inputs.items() } numpy_outputs: Dict[str] = self.executable(**numpy_request_inputs) # If TensorProto is returned, than pass it. If Numpy is returned, cast it to TensorProto tensor_proto_outputs: Dict[str, Tensor] = { k: (v if isinstance(v, Tensor) else np_to_tensor_proto(v)) for k, v in numpy_outputs.items() } result = PredictResponse(outputs=tensor_proto_outputs) self.logger.info("Answer: {}".format(result)[:256]) return result except ValueError as e: self.logger.exception( "Could not convert numpy output ({}) to tensor proto. {}". format(numpy_outputs, e)) context.abort(grpc.StatusCode.OUT_OF_RANGE, repr(e)) except Exception as ex: self.logger.exception( "Function {} failed to handle request".format( self.signature.signature_name)) context.abort(grpc.StatusCode.INTERNAL, repr(ex))
def test_half_dtype_conversion(self, dtype): tensor_shape = TensorShape(dims=[3,1]) tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: np.array([1.10, 2.20, 3.30], dtype=np.float16).view(np.uint16), "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto
def predict_resp_to_dict_np(response: PredictResponse) -> Dict[str, np.array]: """ Transform PredictResponse into a Dictionary with Numpy arrays/scalars :param response: PredictResponse proto message returned from the runtime :return: Dictionary with Numpy arrays """ output_tensors_dict = dict() for tensor_name, tensor_proto in response.outputs.items(): output_tensors_dict[tensor_name] = tensor_proto_to_np(tensor_proto) return output_tensors_dict
def test_tensor_to_np_array_to_tensor_float(self, dtype): tensor_shape = TensorShape(dims=[3, 1]) tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: [1.10, 2.20, 3.30], "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto
def test_np_scalar_to_tensor_to_np(self, np_dtype): x = np.array([1.0], dtype=np_dtype)[0] tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored
def test_complex_scalar_to_tensor_to_np(self, dt): x = np.array([-1 - 1j], dtype=dt)[0] tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored
def test_complex_np_to_tensor_to_np(self, dt): x = np.array([-1 - 1j, -1 + 1j, +1 - 1j, +1 + 1j], dtype=dt) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored)
def test_str_scalar_to_tensor_and_back(self): x = np.str("a") tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored
def test_str_np_to_tensor_to_np(self): x = np.array(["a", "b", "c"], dtype=np.str) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored)
def test_bool_scalar_to_tensor_and_back(self): x = np.bool() tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored
def test_bool_np_to_tensor_to_np(self): x = np.array([True, False, True], dtype=np.bool) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored)
def test_unsupported_np_to_tensor_to_np(self, np_dtype): x = np.array([1.0, 2.0, 3.0], dtype=np_dtype) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored)