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模型,有一个Keras的tf模型。tensorflow模型的正常使用即可,而keras模型可以这么用:定义Keras_model.py

if sys.platform=='linux':
   os.environ["CUDA_VISIBLE_DEVICES"] = "2,3"

if sys.platform=='linux':
   keras_model_file = r"/home/jmh/realtime_traffic_adversarial_attack/src/df/saved_trained_models/DF_epoch50_clusterbursts_dim2300.h5"
else:
   keras_model_file = r"E:\TempWorkStation\realtime_traffic_adversarial_attack\src\df\saved_trained_models\DF_epoch50_clusterbursts_dim2300.h5"

class df_model:
    def __init__(self):
        # 1. define tensorflow Graph and session objector for this model.
        self.df_graph = Graph()
        self.df_session = Session(graph=self.df_graph)
        keras.backend.set_learning_phase(0)
        # 2. load model
        keras.backend.set_session(self.df_session)
        with self.df_session.as_default():
            with self.df_graph.as_default():
                self.DF_model=keras.models.load_model(keras_model_file)

    # 3. prediction now!
    def predict(self,x):
        with self.df_graph.as_default():
            return self.DF_model.predict(x)
#首先创建df_model类的对象
DF_model = df_model()
#预测函数
def predictions(x):
    x = np.reshape(x,newshape=(-1,2300,1))
    probability = DF_model.predict(x)
    #print(probability)
    return np.argmax(probability,axis=1)
#预测
# test now!
if __name__ == '__main__':
    X_test, y_test=utility.LoadDataNoDef_Single_DataSet('test')
    y = predictions(X_test[:10])
    print(y)
    print(I(X_test[:10],X_test[:10]*1000.0))

在其他脚本from Keras_model import predictions 就可以使用keras训练好模型了。

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值