Tensorflow 同时载入多个模型的实例讲解

 更新时间:2018年07月27日 14:41:50   作者:Icoding_F2014  
今天小编就为大家分享一篇Tensorflow 同时载入多个模型的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

有时我们希望在一个python的文件空间同时载入多个模型,例如 我们建立了10个CNN模型,然后我们又写了一个预测类Predict,这个类会从已经保存好的模型restore恢复相应的图结构以及模型参数。然后我们会创建10个Predict的对象Instance,每个Instance负责一个模型的预测。

Predict的核心为:

class Predict:
 def __init__(self....):
  创建sess
  创建恢复器tf.train.Saver
  从恢复点恢复参数:tf.train.Saver.restore(...)


 def predict(self,...):
  sess.run(output,feed_dict={输入})

如果我们直接轮流生成10个不同的Predict 对象的话,我们发现tensorflow是会报类似于下面的错误:

 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
 pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [256,512] rhs shape= [640,512]
   [[Node: save/Assign_14 = Assign[T=DT_FLOAT, _class=["loc:@fullcont/Variable"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](fullcont/Variable, save/RestoreV2_14)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "PREDICT_WITH_SPARK_DATAFLOW_WA.py", line 121, in <module>
 pre2=Predict(label=new_list[1])
 File "PREDICT_WITH_SPARK_DATAFLOW_WA.py", line 47, in __init__
 self.saver.restore(self.sess,self.ckpt.model_checkpoint_path)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1560, in restore
 {self.saver_def.filename_tensor_name: save_path})
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 895, in run
 run_metadata_ptr)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1124, in _run
 feed_dict_tensor, options, run_metadata)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
 options, run_metadata)
 File "/home/jiangminghao/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
 raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [256,512] rhs shape= [640,512]

关键就是:

Assign requires shapes of both tensors to match.意思是载入模型的时候 赋值失败。主要是因为不同对象里面的不同sess使用了同一进程空间下的相同的默认图graph。

正确的解决方法:

class Predict:
 def __init__(self....):
  self.graph=tf.Graph()#为每个类(实例)单独创建一个graph
  with self.graph.as_default():
    self.saver=tf.train.import_meta_graph(...)#创建恢复器
    #注意!恢复器必须要在新创建的图里面生成,否则会出错。
  self.sess=tf.Session(graph=self.graph)#创建新的sess
  with self.sess.as_default():
    with self.graph.as_default():
     self.saver.restore(self.sess,...)#从恢复点恢复参数

 def predict(self,...):
  sess.run(output,feed_dict={输入})

以上这篇Tensorflow 同时载入多个模型的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

最新评论