你好,我正在尝试将我的“已保存模型”(h5文件)保存为tensorflow文件。以下是我使用的代码。
import tensorflow as tf
def tensor_function(i):
tf.keras.backend.set_learning_phase(0) # Ignore dropout at inference
model = tf.keras.models.load_model('/home/ram/Downloads/AutoEncoderModels_ch2/19_hour/autoencoder_models_ram/auto_encoder_model_pos_' + str(i) + '.h5')
export_path = '/home/ram/Desktop/tensor/' + str(i)
#sess = tf.Session()
# 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})
sess.close()
for i in range(4954):
tensor_function(i)
我试图手动打开会话,使用sess = tf.session()
(也删除了with
),但是没有成功。
当我在jupyter笔记本上运行时,我得到了上述错误。当我在Linux终端上运行相同的代码时,我遇到了以下错误。
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_73/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_73/bias)
[[{{node dense_73/bias/Read/ReadVariableOp}} = ReadVariableOp[_class=["loc:@dense_73/bias"], dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_73/bias)]]
当我尝试保存一个“保存的模型文件”时,它成功运行。只有在循环中运行时才会出现问题(可能是某些会话问题)。
我尝试了此回答在SO上,但并没有太大帮助。
sess.run(tf.global_variables_initializer())
吗? - Anna Krogager