【问题标题】:import_meta_graph fails with Data loss: not an sstable (bad magic number)import_meta_graph 因数据丢失而失败:不是 sstable(坏幻数)
【发布时间】:2018-01-25 07:59:14
【问题描述】:

上下文

我在尝试解决另一个错误时遇到了这个问题。第一个错误(原始问题)是当我尝试恢复元图时,我会得到Cannot find KeyError: "The name 'multi_rnn_cell_6' refers to an Operation not in the graph."。在尝试为该问题创建 MVCE 时,我发现了这个错误。

问题

一个简单的脚本创建一些操作,保存元图和变量,然后尝试加载图和变量失败。 问题似乎与 TF 使用的格式有关。

MVCE

import tensorflow as tf
import numpy as np
import os
import glob

class ImportIssue(object):
    def __init__(self,load=False,model_scope = 'model',checkpoint='checkpoint'):
        try:
            os.makedirs(checkpoint)
        except:
            pass

        save_file = os.path.join(checkpoint,'model')
        print("Save file: {}".format(save_file))

        graph = tf.Graph()
        with graph.as_default():
            if load:
                # load model if requested
                model_to_load = "{}.meta".format(tf.train.latest_checkpoint(checkpoint))
                print("Loading model: {}".format(model_to_load))
                rest = tf.train.import_meta_graph(model_to_load)
            else:
                # else create one
                with tf.variable_scope(model_scope):
                    inputs = tf.placeholder(shape=(None,10,10),dtype=tf.float32)
                    cell = self._build_cell(10)
                    # this cell is failing to be fond
                    #print(cell.name)
                    rnn,state = tf.nn.dynamic_rnn(cell,inputs,dtype=tf.float32)
                    train_op = self._build_training_op(inputs,rnn)

            saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES), max_to_keep=1)
            with tf.Session(graph=graph) as sess:
                if load:
                    rest.restore(sess, model_to_load)
                else:
                    sess.run(tf.global_variables_initializer())
                sess.run(train_op,feed_dict={inputs:np.random.normal(size=[3,10,10])})
                saver.save(sess, save_file)
                print("Saved model and graph")
                print("Files in checkpoint dir: {}".format(glob.glob("{}/*".format(checkpoint))))



    def _build_cell(self,size):
        with tf.variable_scope("decoder"):
            cells = []
            cells.append(tf.nn.rnn_cell.GRUCell(size,activation=tf.nn.tanh))
            for res_block_i in range(1):
                res_block = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.LSTMCell(size, use_peepholes=True) for i in range(2)])
                res_block = tf.nn.rnn_cell.ResidualWrapper(res_block)
                res_block = tf.nn.rnn_cell.DropoutWrapper(res_block, input_keep_prob = 1.0,
                        output_keep_prob = 0.5, state_keep_prob = 0.5,
                        variational_recurrent = True, dtype=tf.float32)
                cells.append(res_block)
            cell = tf.nn.rnn_cell.MultiRNNCell(cells)
            return cell

    def _build_training_op(self,inputs,rnn):
        o = tf.train.AdamOptimizer(1e-3)
        loss = tf.reduce_mean(tf.square(inputs - rnn))
        return o.minimize(loss)


if __name__ == '__main__':
    ImportIssue()
    ImportIssue(load=True)

打印

Saved model and graph
Files in checkpoint dir: ['checkpoint/model.data-00000-of-00001', 'checkpoint/model.meta', 'checkpoint/checkpoint', 'checkpoint/model.index']
Save file: checkpoint/model
Loading model: checkpoint/model.meta

错误是:

tensorflow.python.framework.errors_impl.DataLossError: Unable to open table file checkpoint/model.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?

版本

Python 3.6 Fedora 64 位 Linux TF 1.4

【问题讨论】:

    标签: python python-3.x serialization tensorflow


    【解决方案1】:

    是的,必须指定没有 .data-00000-of-00001 的检查点,该 .data-00000-of-00001 似乎添加到 V2 tf 图形保存方法中创建的所有检查点的末尾。

    【讨论】:

      【解决方案2】:

      您可能想查看issue 2676 另外,为什么不直接使用 saver.restore 功能(将立即恢复整个检查点)而不是通过元图来执行?

      【讨论】:

      • 在该问题中,用户错误地指定了配置文件。在我的 MVCE 中,我清楚地创建了一个新的元图对象,然后立即尝试加载它,因此不应该有错误的规范,并且用于编写和加载模型的版本应该是相同的。
      • 另外,使用 saver.restore,一旦图形加载到默认图形中,我将使用它来恢复变量。
      • 是的,但是您可以直接创建一个 Saver 对象,将要加载的变量列表传递给构造函数tf.train.Saver(var_names),然后只需运行saver.restore(session, path_to_checkpoint),无需通过元文件。跨度>
      【解决方案3】:

      问题出现是因为Saver.restore 正在尝试从元文件恢复。这解决了这个问题,但不幸的是,代码现在可以工作,并且 MVCE 没有重现我试图创建的原始错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多