将Keras模型转为TensorFlow模型时出现错误

3

你好,我正在尝试将我的“已保存模型”(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上,但并没有太大帮助。

1个回答

4

以下两种选项对我都有效:

选项1:tensor_function 的开头添加 tf.keras.backend.clear_session(),并使用 'with' 语句块:

def tensor_function(i):
    tf.keras.backend.clear_session()
    tf.keras.backend.set_learning_phase(0)  # Ignore dropout at inference

    model = ...

    export_path = 'so-test/' + str(i)

    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()

选项2:使用tf.Session()代替'with'块,但添加以下代码:sess.run(tf.global_variables_initializer())

def tensor_function(i):
    tf.keras.backend.set_learning_phase(0)  # Ignore dropout at inference

    model = ...

    export_path = 'so-test/' + str(i)
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    tf.saved_model.simple_save(
        sess,
        export_path,
        inputs={'input_image': model.input},
        outputs={t.name: t for t in model.outputs})
    sess.close()

你好,我尝试了一下。出现了这个错误:从容器中读取资源变量dense_67/kernel时出错:localhost。这可能意味着该变量未初始化。未找到:容器localhost不存在。(找不到资源:localhost/dense_67/kernel) [[{{node dense_67/kernel/Read/ReadVariableOp}} = ReadVariableOp_class=["loc:@dense_67/kernel"], dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]] - Fasty
1
你尝试过使用 sess.run(tf.global_variables_initializer()) 吗? - Anna Krogager
我已经更新了我的答案,并提供了两个不同的选项供您尝试。 - Anna Krogager

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接