python-3.x - Tensorflow 服务于 : request fails with object has no attribute 'unary_unary

标签 python-3.x tensorflow tensorflow-serving

我正在使用 TensorFlow 构建一个 CNN 文本分类器,我想在 tensorflow-serving 中加载它并使用服务 api 进行查询。当我在 grcp stub 上调用 Predict() 方法时,我收到此错误: AttributeError: 'grpc._cython.cygrpc.Channel' object has no attribute 'unary_unary'

我迄今为止所做的:
我已经成功地训练并导出了一个适合服务的模型(即,验证了签名并使用 tf.Saver 我可以成功地返回一个预测)。我也可以在 tensorflow_model_server 中加载模型而不会出错。

这是客户端代码的片段(为了便于阅读而进行了简化):

with tf.Session() as sess:
    host = FLAGS.server
    channel = grpc.insecure_channel('localhost:9001')
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'predict_text'
    request.model_spec.signature_name = 'predict_text'

    x_text = ["space"]

    # restore vocab processor
    # then create a ndarray with transform_fit using the vocabulary
    vocab = learn.preprocessing.VocabularyProcessor.restore('/some_path/model_export/1/assets/vocab')
    x = np.array(list(vocab.fit_transform(x_text)))

    # data
    temp_data = tf.contrib.util.make_tensor_proto(x, shape=[1, 15], verify_shape=True)
    request.inputs['input'].CopyFrom(tf.contrib.util.make_tensor_proto(x, shape=[1, 15], verify_shape=True))

    # get classification prediction
    result = stub.Predict(request, 5.0)

我违反规则的地方:当 pip install 不受官方支持时,我在 Python 3.5.3 中使用 tensorflow-serving-apis。各种帖子(例如:https://github.com/tensorflow/serving/issues/581)都报告说,在 Python 3 中使用 tensorflow-serving 是成功的。我已经从 pypi 下载了 tensorflow-serving-apis 包( https://pypi.python.org/pypi/tensorflow-serving-api/1.5.0)and 手动粘贴到环境中。

版本:tensorflow: 1.5.0, tensorflow-serving-apis: 1.5.0, grpcio: 1.9.0rc3, grcpio-tools: 1.9.0rcs, protobuf: 3.5.1 (所有其他依赖版本已经过验证,但不包括简洁——如果它们有实用性,很高兴添加)

环境:Linux Mint 17 Qiana; x64,Python 3.5.3

调查:
github 问题 ( https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2258 ) 表明触发此错误的历史包与 grpc beta 相关。

我缺少哪些数据或学习或实现?

最佳答案

beta_create_PredictionService_stub()已弃用。尝试这个:

from tensorflow_serving.apis import prediction_service_pb2_grpc
...
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

关于python-3.x - Tensorflow 服务于 : request fails with object has no attribute 'unary_unary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529389/

相关文章:

python - Python 3 中小数到 2 位的钱

python - 具有依赖项初始化错误 : ModuleNotFoundError or ImportError 的自定义包

python - 在列表中查找奇数并使用 lambda 函数对它们进行平方

python - python 中用于铱星 SBD 的 2 字节校验和

TensorFlow Serving 将图像作为 Cloud ML Engine 上的 base64 编码字符串

python - Tensorflow 动态填充

python - TensorFlow 版本 2 和 BatchNorm 折叠中的量化感知训练

python - 为什么 Keras/tensorflow 的 sigmoid 和交叉熵精度低?

java - 用 java 编写的 Tensorflow-serving 客户端没有给出正确的结果