从保存的Keras模型获取tf.服务的配置

2024-04-25 14:43:28 发布

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

我在谷歌人工智能平台上部署了一个模型,如下图所示:

enter image description here

这是一个使用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请求输入到evaluator时,如下所示:

enter image description here

我收到以下错误消息:

{"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输入需要的不是“values”键来进行预测

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

我最好的尝试是在本地重新加载模型,并调用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请求中


Tags: namenoneconfigtrueinputkernelclassdense
1条回答
网友
1楼 · 发布于 2024-04-25 14:43:28

输入的格式信息必须包含在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

因此,预测的输入必须符合相同的格式。同时,如果您希望在云控制台中使用模型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]
]}

相关问题 更多 >