TF服务错误:找不到功能

时间:2020-08-04 04:45:57

标签: tensorflow tensorflow2.0 tensorflow-serving tfx

下面的

是pb文件的结构。但是,当我将JSON格式作为输入传递时,出现以下错误。您能建议我该怎么做才能调整输入以返回预测。

谢谢!:

“错误”:“ {“错误”:“名称:,功能:AA(数据类型:字符串)是必需的,但找不到。\ n \ t [[{{node ParseExample / ParseExampleV2}}]] “}”

以下是TensorFlow模型结构:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['examples'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: serving_default_examples:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: StatefulPartitionedCall_1:0
  Method name is: tensorflow/serving/predict

通话预测:

data = {
  "signature_name":"serving_default",
  "instances":[
    {
       "examples":{"b64": "ChcKFQoLcmVxdWVzdF91cmwSBgoECgJVUw==",
                   "b64": "Ch8KHQoLcmVxdWVzdF91cmwSDgoMCgpNdWx0aXNpdGVz"}
    }
  ]
}

data = json.dumps(data)
json_response = requests.post(url, data=data, headers=headers)
print(json_response.content)

错误:b'{\ n“错误”:“名称:,功能:AA(数据类型:字符串)是必需的,但找不到。\ n \ t [[{{node ParseExample / ParseExampleV2}}] ]“ \ n}'

培训师:

def get_model(show_summary=True):
    #one-hot categorical features
    num_AA = 3
    num_BB = 8
    
    input_AA = tf.keras.Input(shape=(num_AA,), name="AA_xf")
    input_BB = tf.keras.Input(shape=(num_BB,), name="BB_xf")
    
    inputs_con = tf.keras.layers.concatenate([
                   input_AA,
                   input_BB])

    dense_1 = tf.keras.layers.Dense(5, activation='relu') (inputs_con)
    output = tf.keras.layers.Dense(1, activation="sigmoid")(dense_1)

    _inputs = [
                   input_AA,
                   input_BB]
    
    model = tf.keras.models.Model(_inputs, output)
    model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.01),
              loss='binary_crossentropy', metrics=[
                                                   tf.keras.metrics.BinaryAccuracy(),
                                                   tf.keras.metrics.TruePositives(),
                                                   tf.keras.metrics.Accuracy()])
    
    if show_summary:
        model.summary()

    return model

# TFX Trainer will call this function.
def run_fn(fn_args):

    tf_transform_output = tft.TFTransformOutput(fn_args.transform_output)

    train_dataset = _input_fn(fn_args.train_files, tf_transform_output, 40)
    eval_dataset = _input_fn(fn_args.eval_files, tf_transform_output, 40)

    model = get_model()

    model.fit(
      train_dataset,
      steps_per_epoch=fn_args.train_steps,
      validation_data=eval_dataset,
      validation_steps=fn_args.eval_steps)
    
    def _get_serve_tf_examples_fn(model, tf_transform_output):

        model.tft_layer = tf_transform_output.transform_features_layer()

        @tf.function
        def serve_tf_examples_fn(serialized_tf_examples):
            """Returns the output to be used in the serving signature."""
            feature_spec = tf_transform_output.raw_feature_spec()
            feature_spec.pop(features.LABEL_KEY)
            parsed_features = tf.io.parse_example(serialized_tf_examples, feature_spec)

            transformed_features = model.tft_layer(parsed_features)
            transformed_features.pop(features.transformed_name(features.LABEL_KEY), None)

            outputs = model(transformed_features)
            return {'outputs': outputs}

        return serve_tf_examples_fn
    
      

    signatures = { "serving_default": _get_serve_tf_examples_fn(model, tf_transform_output).get_concrete_function(
                                        tf.TensorSpec(shape=[None], dtype=tf.string, name='examples'))}
                                    

    model.save(fn_args.serving_model_dir, save_format='tf', signatures=signatures)

0 个答案:

没有答案
相关问题