Tensorflow服务于古董数据集的预测

2024-10-04 01:26:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循Tensorflow排名古老的例子Tutorial: TF-Ranking for sparse features,我对使用Tensorflow服务获得预测感兴趣。我首先更改了代码并添加了以下函数

def make_serving_input_fn():
    context_feature_spec = tf.feature_column.make_parse_example_spec(context_feature_columns().values())
    example_feature_spec = tf.feature_column.make_parse_example_spec(example_feature_columns().values())
  
    feature_spec = {}
    feature_spec.update(example_feature_spec)
    feature_spec.update(context_feature_spec)
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(
        feature_spec)

此外,我还添加了一个导出:

eval_spec =  tf.estimator.EvalSpec(
          name="eval",
          input_fn=eval_input_fn,
          exporters=tf.estimator.LatestExporter(
            "latest_exporter",
            serving_input_receiver_fn=make_serving_input_fn()),
          throttle_secs=15)

为了使模型运行,我运行了以下命令:


docker run -it -p 8501:8501 --mount type=bind,source=/home/code/models/antique/export/latest_exporter/1612010189,target=/models/antique/1/ -e MODEL_NAME=antique -t tensorflow/serving

对于预测,我添加了以下代码:

def predict(instances): 
    request = predict_pb2.PredictRequest()
    t_proto = tf.make_tensor_proto(instances)
    request.inputs['examples'].CopyFrom(t_proto)
    request.model_spec.signature_name = 'predict'
    request.model_spec.name = 'antique'

    tf_serve_host ="172.17.0.3:8500"
    timeout_in_secs = 100  
    channel = insecure_channel(tf_serve_host)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    result = stub.Predict(request, timeout_in_secs)
    predictions = np.array(result.outputs['output'].float_val)

    return predictions

然而,当我试图获得预测时:

raw_dataset = tf.data.TFRecordDataset([DATA_PATH])
for e in raw_dataset.take(1):
    ELWC = input_pb2.ExampleListWithContext()
    single_example = ELWC.FromString(e.numpy())

然后:

predict([single_example.SerializeToString()])

我得到以下错误:

_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.INVALID_ARGUMENT
    details = "Could not parse example input, value: '
à)
Ý)
Ô
document_tokensÀ
½
pit
bulls
are
a
lot
of
things
-
loyal
,
friendly
,
active
-
but
they
'
re
not
    naturally

aggressive
toward
people
.
since
they
were
bred
to
fight
other
animals
,
it
was
    important
that
they
not
snap
at
their
human
handler
##s
.
you
'
ve
fallen
for
a
common
myth
.

    relevance

ž
document_input_mask†ƒ
€
Õ
document_input_ids¾»
¸eò4¡^è›Í­ó´Eòƒ*òÙóåëíÐé¤? IÖ¿ôàëäðjЊŒÐ òÙÑžØëé…PÜõáµÜßôáí†í)Õƒ¥Sôf

document_id

    3971195_0
¨ 
document_bert_encoder_outputs† ƒ 
€ ìL¿áÒa¿!~x¿Vwo>Q?°Ä¹>ïºô¾#?­¾GV¿È$w?ŸÌˆ>1}¿fÕz?®¶[¿\¬ä>|$¿K2I?Âêl?3?œ>ôv¿þ[?1üi?ƒ¶L?Ï_{?sA­>Ny|? )¿”—¾øÂ?mÂ?ülÓ>Ÿ¿ß{¿à>[¿/1_¿ªÞ
¿ÑDt¿x|x¿Fi-¿Äßr¿ÄF...TRUNCATED"
    debug_error_string = "{"created":"@1612356404.796216049","description":"Error received from peer ipv4:172.17.0.3:8500","file":"src/core/lib/surface/call.cc","file_line":1061,"grpc_message":"Could not parse example input, value: '\n\u00e0)\n\u00dd)\n\u00d4\u0002\n\u000fdocument_tokens\u0012\u00c0\u0002\n\u00bd\u0002\n\u0003pit\n\u0005bulls\n\u0003are\n\u0001a\n\u0003lot\n\u0002of\n\u0006things\n\u0001-\n\u0005loyal\n\u0001,\n\bfriendly\n\u0001,\n\u0006active\n\u0001-\n\u0003but\n\u0004they\n\u0001'\n\u0002re\n\u0003not\n\tnaturally\n\naggressive\n\u0006toward\n\u0006people\n\u0001.\n\u0005since\n\u0004they\n\u0004were\n\u0004bred\n\u0002to\n\u0005fight\n\u0005other\n\u0007animals\n\u0001,\n\u0002it\n\u0003was\n\timportant\n\u0004that\n\u0004they\n\u0003not\n\u0004snap\n\u0002at\n\u0005their\n\u0005human\n\u0007handler\n\u0003##s\n\u0001.\n\u0003you\n\u0001'\n\u0002ve\n\u0006fallen\n\u0003for\n\u0001a\n\u0006common\n\u0004myth\n\u0001.\n\u0012\n\trelevance\u0012\u0005\u001a\u0003\n\u0001\u0002\n\u009e\u0001\n\u0013document_input_mask\u0012\u0086\u0001\u001a\u0083\u0001\n\u0080\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\n\u00d5\u0001\n\u0012document_input_ids\u0012\u00be\u0001\u001a\u00bb\u0001\n\u00b8\u0001e\u00f24\u00a1^\u00e8\u000f\u008d\b\u009b\u0016\u00cd\u000f\u00ad\u0013\u00f3\u0007\u00b4E\u00f2\u0007\u0083*\u00f2\u0007\u00d9\u0018\u00f3\u0007\u00e5\u000f\u00eb\u000f\u00ed\u0007\u00d0\u0010\u00e9\u000f\u00a4?\u00a0I\u00d6\u0014\u00bf\u0010\u00f4\u0007\u00e0\u0010\u00eb\u000f\u00e4\u000f\u00f0j\u00d0\u000f\u008a\u0017\u008c\u0010\u00d0 \u00f2\u0007\u00d9\u000f\u00d1\u000f\u009e\u0014\u00d8\u000f\u00eb\u000f\u00e9\u000f\u0085P\u00dc\u000f\u00f5\u000f\u00e1\u0013\u00b5\u00dc\u0001\u00df\u000f\u00f4\u0007\u00e1\u000f\u00ed\u0007\u0086\u0012\u00ed)\u00d5\u000f\u008d\b\u0083\u0015\u00a5S\u00f4\u0007f\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\n\u001c\n\u000bdocument_id\u0012\r\n\u000b\n\t3971195_0\n\u00a8 \n\u001ddocument_bert_encoder_outputs\u0012\u0086 \u0012\u0083 \n\u0080 \u0005\u00ecL\u00bf\u00e1\u00d2a\u00bf!~x\u00bfVwo>Q\u0006\u0013?\u00b0\u00c4\u00b9>\u00ef\u00ba\u00f4\u00be#?\u00ad\u00be\u009dGV\u00bf\u00c8$w?\u009f\u00cc\u0088>1\u008d}\u00bff\u00d5z?\u00ae\u00b6[\u00bf\\u00ac\u00e4>|\u008d$\u00bfK2I?\u00c2\u00eal?3?\u009c>\u001a\u00f4v\u00bf\u00fe\u001c[?1\u00fci?\u0083\u00b6L?\u00cf_{?sA\u00ad>Ny|?\u00a0)\u007f\u00bf\u0094\u0097\u0015\u00be\u00f8\u00c2\u0011?m\u00c2\u001c?\u00fcl\u00d3>\u009f\u0014\u0005\u00bf\u00df{\u007f\u00bf\u00e0\u0017\u0015>\u0012\u0090[\u00bf/1_\u00bf\u00aa\u00de\n\u00bf\u00d1Dt\u00bfx|x\u00bfFi-\u00bf\u00c4\u00dfr\u00bf\u00c4F...TRUNCATED","grpc_status":3}"
>

谁能帮我解释一下我做错了什么


Tags: inputmakeexamplerequesttffeaturefnserving