简体   繁体   English

无法使用tf.train.Saver还原模型中的变量

[英]Failed to use tf.train.Saver to restore Variables in my model

Some information of my computer: system: Windows 10. IDE: Spyder3(Anaconda/python3.5.2). 我的计算机的一些信息:系统:Windows10。IDE:Spyder3(Anaconda / python3.5.2)。 TensorFlow version: r1.1(cpu) TensorFlow版本:r1.1(cpu)

Below is my problem: 以下是我的问题:

After I trained my CNN model(training.py). 在我训练了CNN模型(training.py)之后。 I used tf.train.Saver to save model Variables. 我使用tf.train.Saver保存模型变量。 And I want to restore them in another file(testing.py) to test the testing dataset. 我想将它们还原到另一个文件(testing.py)中以测试测试数据集。 I have tried many time but alwaws failed to restore Variable. 我已经尝试了很多次,但是alwaws无法还原Variable。 For example, I tried a example(two file:save.py and restore.py): 例如,我尝试了一个示例(两个文件:save.py和restore.py):

import tensorflow as tf
w1 = tf.Variable(tf.random_normal(shape=[2]), name='w1')
w2 = tf.Variable(tf.random_normal(shape=[5]), name='w2')
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(w1))
print(sess.run(w2))
saver.save(sess, "./model")

import tensorflow as tf
w1 = tf.Variable(tf.random_normal(shape=[2]), name='w1')
w2 = tf.Variable(tf.random_normal(shape=[5]), name='w2')
saver = tf.train.Saver()
sess = tf.Session()
saver.restore(sess, "./model")
print(sess.run(w1))
print(sess.run(w2))

When I runs restore.py, it goes wrong: 当我运行restore.py时,它出错了:

INFO:tensorflow:Restoring parameters from ./model
Traceback (most recent call last):

  File "<ipython-input-2-cb3aa43151bc>", line 1, in <module>
    runfile('F:/tensorflow/saver_test/tensor_restore.py', wdir='F:/tensorflow/saver_test')

  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "F:/tensorflow/saver_test/tensor_restore.py", line 7, in <module>
    saver.restore(sess, "./model")

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1457, in restore
    {self.saver_def.filename_tensor_name: save_path})

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 778, in run
    run_metadata_ptr)

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 982, in _run
    feed_dict_string, options, run_metadata)

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1032, in _do_run
    target_list, options, run_metadata)

  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)

NotFoundError: Key w1_1 not found in checkpoint
     [[Node: save_1/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2_1/tensor_names, save_1/RestoreV2_1/shape_and_slices)]]

Caused by op 'save_1/RestoreV2_1', defined at:
  File "D:\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 223, in <module>
    main()
  File "D:\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 219, in main
    kernel.start()
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 474, in start
    ioloop.IOLoop.instance().start()
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 162, in start
    super(ZMQIOLoop, self).start()
  File "D:\Anaconda3\lib\site-packages\tornado\ioloop.py", line 887, in start
    handler_func(fd_obj, events)
  File "D:\Anaconda3\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "D:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell
    handler(stream, idents, msg)
  File "D:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request
    user_expressions, allow_stdin)
  File "D:\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "D:\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-cb3aa43151bc>", line 1, in <module>
    runfile('F:/tensorflow/saver_test/tensor_restore.py', wdir='F:/tensorflow/saver_test')
  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "D:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "F:/tensorflow/saver_test/tensor_restore.py", line 5, in <module>
    saver = tf.train.Saver()
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1056, in __init__
    self.build()
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1086, in build
    restore_sequentially=self._restore_sequentially)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 691, in build
    restore_sequentially, reshape)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 407, in _AddRestoreOps
    tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 247, in restore_op
    [spec.tensor.dtype])[0])
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 669, in restore_v2
    dtypes=dtypes, name=name)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1228, in __init__
    self._traceback = _extract_stack()

NotFoundError (see above for traceback): Key w1_1 not found in checkpoint
     [[Node: save_1/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2_1/tensor_names, save_1/RestoreV2_1/shape_and_slices)]]

I have tried some days to slove it but failed, I can't find anything wrong with my code.Anything Suggestions? 我已经尝试了好几天来解决它,但是失败了,我的代码找不到任何错误。 Thankyou!!! 谢谢!!!

After you saved your trained model, do you see a file (or files) named with .meta suffix? 保存训练好的模型后,您是否看到一个后缀名为.meta的文件? If so, at the restoring time, do not create the saver by calling: saver = tf.train.Saver(). 如果是这样,则在恢复时,不要通过调用以下命令来创建保护程序:saver = tf.train.Saver()。 Instead, try to create your saver by calling tf.train.import_meta_graph() method. 而是尝试通过调用tf.train.import_meta_graph()方法来创建您的保护程序。

It is very likely that when you were trying to create the graph from scratch, some variables were named differently so that the saved data could not be restored. 当您尝试从头开始创建图形时,很可能某些变量的名称不同,因此无法恢复已保存的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM