【问题标题】:Getting Configurations for tf.Serving From Saved Keras Model从保存的 Keras 模型获取 tf.Serving 的配置
【发布时间】:2021-01-30 05:08:59
【问题描述】:

我在 Google AI Platform 上部署了一个模型,如下图所示:

这是一个使用 Keras 构建的模型,并使用 save_model 命令和标准选项保存。

当我去虚拟测试模型以查看它是否有效时,我将输入一个示例 JSON 请求,如下所示:

{"instances": [
  {"values": ["This is my first sentence"], "key": 1}
]}

我正在关注此 URL 中给出的示例:https://cloud.google.com/ai-platform/prediction/docs/online-predict?hl=en_US#formatting_your_input_for_online_prediction

当我像这样将示例 JSON 请求输入到评估器中时:

我收到以下错误消息:

{"error": "{\n    \"error\": \"Failed to process element: 0 key: values of 'instances' list. Error: Invalid argument: JSON object: does not have named input: values\"\n}"}

经过一番查看,问题似乎出在 tf.Serving 上,因为 JSON 输入期望的不是“值”键来进行预测。

我的问题是我不知道如何访问应该是什么。

我最好的尝试是在本地重新加载模型并调用get_config() 方法来查看那里是否有任何东西。

返回以下字典:

{'name': 'functional_1',
'layers': [{'class_name': 'InputLayer',
'config': {'batch_input_shape': (None, 1),
'dtype': 'string',
'sparse': False,
'ragged': False,
'name': 'input_1'},
'name': 'input_1',
'inbound_nodes': []},
{'class_name': 'TextVectorization',
'config': {'name': 'text_vectorization',
'trainable': True,
'dtype': 'string',
'max_tokens': 12500,
'standardize': 'lower_and_strip_punctuation',
'split': 'whitespace',
'ngrams': None,
'output_mode': 'int',
'output_sequence_length': 250,
'pad_to_max_tokens': True},
'name': 'text_vectorization',
'inbound_nodes': [[['input_1', 0, 0, {}]]]},
{'class_name': 'Embedding',
'config': {'name': 'embedding',
'trainable': True,
'batch_input_shape': (None, None),
'dtype': 'float32',
'input_dim': 12500,
'output_dim': 25,
'embeddings_initializer': {'class_name': 'RandomUniform',
 'config': {'minval': -0.05, 'maxval': 0.05, 'seed': None}},
'embeddings_regularizer': None,
'activity_regularizer': None,
'embeddings_constraint': None,
'mask_zero': False,
'input_length': None},
'name': 'embedding',
'inbound_nodes': [[['text_vectorization', 0, 0, {}]]]},
{'class_name': 'Flatten',
'config': {'name': 'flatten',
'trainable': True,
'dtype': 'float32',
'data_format': 'channels_last'},
'name': 'flatten',
'inbound_nodes': [[['embedding', 0, 0, {}]]]},
{'class_name': 'Dense',
'config': {'name': 'dense',
'trainable': True,
'dtype': 'float32',
'units': 50,
'activation': 'relu',
'use_bias': True,
'kernel_initializer': {'class_name': 'GlorotUniform',
 'config': {'seed': None}},
'bias_initializer': {'class_name': 'Zeros', 'config': {}},
'kernel_regularizer': None,
'bias_regularizer': None,
'activity_regularizer': None,
'kernel_constraint': None,
'bias_constraint': None},
'name': 'dense',
'inbound_nodes': [[['flatten', 0, 0, {}]]]},
{'class_name': 'Dense',
'config': {'name': 'dense_1',
'trainable': True,
'dtype': 'float32',
'units': 50,
'activation': 'relu',
'use_bias': True,
'kernel_initializer': {'class_name': 'GlorotUniform',
 'config': {'seed': None}},
'bias_initializer': {'class_name': 'Zeros', 'config': {}},
'kernel_regularizer': None,
'bias_regularizer': None,
'activity_regularizer': None,
'kernel_constraint': None,
'bias_constraint': None},
'name': 'dense_1',
'inbound_nodes': [[['dense', 0, 0, {}]]]},
{'class_name': 'Dense',
'config': {'name': 'dense_2',
'trainable': True,
'dtype': 'float32',
'units': 1,
'activation': 'sigmoid',
'use_bias': True,
'kernel_initializer': {'class_name': 'GlorotUniform',
 'config': {'seed': None}},
'bias_initializer': {'class_name': 'Zeros', 'config': {}},
'kernel_regularizer': None,
'bias_regularizer': None,
'activity_regularizer': None,
'kernel_constraint': None,
'bias_constraint': None},
'name': 'dense_2',
'inbound_nodes': [[['dense_1', 0, 0, {}]]]}],
'input_layers': [['input_1', 0, 0]],
'output_layers': [['dense_2', 0, 0]]}

我希望我正在寻找的一些信息会包含在这里,我已经尝试使用 'functional_1'input_1 之类的东西作为使用键,但没有成功。

我还尝试了数据集中用于X 的原始列,但没有奏效。

如何访问 tf.Serving 的元数据以了解将什么放入我的 JSON 请求中?

【问题讨论】:

    标签: python tensorflow keras google-cloud-platform google-ai-platform


    【解决方案1】:

    输入的格式信息必须包含在 Keras 模型的定义中。

    例如,在Quickstart of Training and prediction with Keras 中,使用来自the United States Census Income Dataset 的信息创建和训练模型。这个例子的代码在这个GitHub repo

    util.py内部,数据集信息已经准备好了,这里只会用到这些列:

    'age', 'workclass', 'education_num','marital_status', 'occupation', 'relationship', 'race','capital_gain', 'capital_loss', 'hours_per_week', 'native_country'
    

    然后,预处理留下这样的数据(用于训练阶段):

    -0.48454606533050537, 3.0, -0.030303768813610077, 2.0, 6.0, 0.0, 4.0, -0.1447920799255371, -0.2171318680047989, 0.6115681529045105, 38.0

    因此,预测的输入必须符合相同的格式。同时,如果您想在 Cloud Console 中使用模型 UI 中的功能测试,则需要在 JSON 对象中发送输入,如下所示:

    {"instances": [ 
       {"values": ["This is my first sentence"], "key": 1} 
    ]}
    

    然而,根据this documentation,由于每个数据实例都是浮动值的向量(在这种情况下),JSON 必须是这样的:

    {"instances": [
       [-0.48454606533050537, 3.0, -0.030303768813610077, 2.0, 6.0, 0.0, 4.0, -0.1447920799255371, -0.2171318680047989, 0.6115681529045105, 38.0],
       [1.1200168132781982, 3.0, -0.41926509141921997, 2.0, 0.0, 5.0, 4.0, -0.1447920799255371, -0.2171318680047989, -0.03403923660516739, 38.0],
       .
       .
       .
       [-0.5574807524681091, 4.0, -0.030303768813610077, 0.0, 3.0, 1.0, 4.0, -0.1447920799255371, -0.2171318680047989, -0.03403923660516739, 38.0]
    ]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 2019-01-26
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      相关资源
      最近更新 更多