微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从保存的Keras模型中获取tf.Serving的配置

如何解决从保存的Keras模型中获取tf.Serving的配置

我在Google AI平台上有一个已部署的模型,如下图所示:

enter image description here

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

当我去虚拟测试模型以查看其是否工作时,我将放入一个示例JSON请求,如下所示:

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

我正在遵循以下网址提供的示例:https://cloud.google.com/ai-platform/prediction/docs/online-predict?hl=en_US#formatting_your_input_for_online_prediction

当我将示例JSON请求输入评估器时,如下所示:

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,'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',{}]]]},{'class_name': 'Embedding','config': {'name': 'embedding','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',{'class_name': 'Flatten','config': {'name': 'flatten','data_format': 'channels_last'},'name': 'flatten','inbound_nodes': [[['embedding',{'class_name': 'Dense','config': {'name': 'dense','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,'kernel_constraint': None,'bias_constraint': None},'name': 'dense','inbound_nodes': [[['flatten','config': {'name': 'dense_1','name': 'dense_1','inbound_nodes': [[['dense','config': {'name': 'dense_2','units': 1,'activation': 'sigmoid','name': 'dense_2','inbound_nodes': [[['dense_1',{}]]]}],'input_layers': [['input_1',0]],'output_layers': [['dense_2',0]]}

我希望将要查找的某些信息包含在此处,并且我尝试使用'functional_1'input_1之类的东西作为要使用的键,但没有成功。

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

如何访问tf的元数据。知道要在JSON请求中放入什么?

解决方法

您输入的格式信息必须包含在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,38.0],[1.1200168132781982,-0.41926509141921997,5.0,-0.03403923660516739,.
   .
   .
   [-0.5574807524681091,1.0,38.0]
]}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。