使用时出错tf.saved_模型.simple\u save()

2024-04-26 05:04:41 发布

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

我尝试在我的机器上托管一个图像分类模型,我试图实现本文中给出的步骤Medium serving ml models

我使用的代码片段是:

import tensorflow as tf


# The export path contains the name and the version of the model
tf.keras.backend.set_learning_phase(0) # Ignore dropout at inference
model = tf.keras.models.load_model('./model_new.hdf5')
export_path = './model/1'

# Fetch the Keras session and save the model
# The signature definition is defined by the input and output tensors
# And stored with the default serving key
with tf.keras.backend.get_session() as sess:
    tf.saved_model.simple_save(
       sess,
       export_path,
       inputs={'input_image': model.input},
       outputs={t.name:t for t in model.outputs})

如上文所述。我的模型存储在模型中_新建.hdf5文件,但我收到以下错误消息。在

NameError: name 'tf' is not defined

排队

model = tf.keras.models.load_model('./model_new.hdf5')

这是正确的使用方法吗tf.saved_模型.simple_save()?在


Tags: andthepathname模型inputmodelmodels
1条回答
网友
1楼 · 发布于 2024-04-26 05:04:41

这是加载模型时出错,而不是使用tf.saved_model.simple_save()。加载Keras模型时,需要处理自定义对象或自定义层。您可以通过传递一个custom_objectsdict来完成此操作,该dict包含tf,在您的示例中:

import tensorflow as tf
model = keras.models.load_model('model_new.hdf5', custom_objects={'tf': tf})

相关问题 更多 >