繁体   English   中英

加载模型并将其用于在Tensorflow中训练其他模型

Loading a model and using it for training the other model in Tensorflow

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我已经在Tensorflow中将模型训练为Color_Model,并且效果很好。 我想使用这个训练有素的模型来训练另一个模型作为Motion_Model。 实际上,Color_Model的输出进入Motion_Model有助于训练Motion_Model。 但是问题是我不知道如何加载Color_Model图和设置Motion_Model图,以便Tensorflow知道它们是分开的。 我在Motion_Model中更改了权重的名称,因此它们没有任何名称冲突。

这是加载和训练代码的一部分:

with tf.Session() as sess:
  sess.run(init_op) 
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)   
  ###Loaing the color model 
  new_saver = tf.train.import_meta_graph('./Color_Model/Deep_CNN_Color_Arch16.ckpt-44.meta')

  new_saver.restore(sess,tf.train.latest_checkpoint('./Color_Model/'))
  graph = tf.get_default_graph()
  X = graph.get_tensor_by_name("X:0")
  Y = graph.get_tensor_by_name("Y:0")
  phase = graph.get_tensor_by_name("phase:0")
  A7 = graph.get_tensor_by_name("Finalo:0")
  ##########################
  ###Training phase
  for step in range(1, iterations+1):
     ###Getting the training data batch
     img = sess.run([image])    
     X_temp = img[0][:,:,:,0:8]
     Y_temp = img[0][:,:,:,8:9]
     X_temp = X_temp.astype(np.float32)/255
     Y_temp = Y_temp.astype(np.float32)/255
     ###Getting the color model result
     output = sess.run([A7], feed_dict = {X: X_temp[:,:,:,5:8], Y: Y_temp, phase: False})
     ###Training the motion model       
     _, c, outputM = sess.run([optimizer, costM, MN_out], feed_dict = {XM: X_temp[:,:,:,0:5], YM: Y_temp, phaseM: True, ZM: output})

如您所见,第一个“ sess.run”运行Color_Model以从中获取输出,第二个“ sess.run”获取此输出并将其馈送到Motion_Model进行训练。

但是,当我运行此代码时,出现以下错误:

    Traceback (most recent call last):
    File "/home/hamidreza/venv/lib/python3.5/site- 
    packages/tensorflow/python/client/session.py", line 1292, in _do_call
    return fn(*args)
    File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1277, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
    File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1367, in _call_tf_sessionrun
run_metadata)
    tensorflow.python.framework.errors_impl.NotFoundError: Key WM1 not found in checkpoint
 [[{{node save/RestoreV2}} = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1538, in restore
{self.saver_def.filename_tensor_name: save_path})
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 887, in run
run_metadata_ptr)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1110, in _run
feed_dict_tensor, options, run_metadata)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1286, in _do_run
run_metadata)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1308, in _do_call
raise type(e)(node_def, op, message)
 tensorflow.python.framework.errors_impl.NotFoundError: Key WM1 not found in checkpoint
 [[{{node save/RestoreV2}} = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

 Caused by op 'save/RestoreV2', defined at:
 File "Detection_Model1.py", line 52, in <module>
 saver = tf.train.Saver()
 File "/home/hamidreza/venv/lib/python3.5/site- 
 packages/tensorflow/python/training/saver.py", line 1094, in __init__
 self.build()
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1106, in build
 self._build(self._filename, build_save=True, build_restore=True)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1143, in _build
 build_save=build_save, build_restore=build_restore)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 787, in _build_internal
 restore_sequentially, reshape)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 406, in _AddRestoreOps
restore_sequentially)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 854, in bulk_restore
return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/ops/gen_io_ops.py", line 1466, in restore_v2
shape_and_slices=shape_and_slices, dtypes=dtypes, name=name)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
return func(*args, **kwargs)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3272, in create_op
op_def=op_def)
 File "/home/hamidreza/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1768, in __init__
self._traceback = tf_stack.extract_stack()


 NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. 
 Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

 Key WM1 not found in checkpoint
 [[{{node save/RestoreV2}} = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

我非常确定这是混合的图形,因为WM1是Motion_Model中第一层的权重,实际上错误是说它无法在引用我猜为Color_Model的检查点中找到它。 如果您能帮助我解决这个问题,我非常感谢。

问题暂未有回复.您可以查看右边的相关问题.
3 恢复tensorflow模型并将其用于单个输入

在尝试从这个repo im2latex-tensorflow获得训练模型时,我从repo中提供的ckpt文件下载了检查点文件 不太了解机器学习。只想看看输出。 经过一段谷歌搜索后,我尝试恢复模型和图形。 此后不知道该怎么办。 如何将img_input作为程序的输入并获取相应的乳 ...

6 在TensorFlow中训练量化模型

我想训练一个量化网络,即在前向传递过程中使用量化权重来计算损失,然后在后向传递过程中更新底层的全精度浮点权重。 请注意,在我的情况下,“伪量化”就足够了。 这意味着权重仍可以存储为32位浮点值,只要它们表示低位宽量化值即可。 在Pete Warden的博客文章中,他指出: 我们确实支持“伪 ...

7 在Tensorflow中重新训练模型

我有一个使用Tensorflow的简单神经网络。 这是会议: 在最后两行中,我保存了模型并将图形还原到另一个类中: 我想重新训练模型,这意味着不初始化权重,只是从停止的最后一点开始更新权重。 我怎样才能做到这一点? ...

8 如何使用Spacy训练NER并将其集成到原始模型中

我正在尝试使用Spacy训练NER和我自己的数据。 我的问题是如何将训练有素的NER整合到原始模型中? 这样可以方便地连续训练并用于我的应用。 我没有找到任何样品。 我在下面找到了一些类似的例子来训练NER,但似乎所有这些都没有保存训练好的模型并将其整合回Spacy。 有些是在内存 ...

10 在另一个模型中训练 Tensorflow 模型

我目前正在开发一个 Tensorflow 项目,我想在另一个模型 (model_2) 中使用一个模型 (model_1)。 我还想从内部使用的模型中训练权重。 我使用 tf.map_fn() 将当前数据从 model_2 输入到 model_1。 一切正常,但如果我调用 model_2.fit(x, ...

暂无
暂无

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

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