Python 如何使tensorflow模型保持负载?

Python 如何使tensorflow模型保持负载?,python,tensorflow,Python,Tensorflow,现在我想写两个函数: 1对于我已经培训过的加载模型 二是利用模型进行分类 但是这两个函数都需要相同的会话,所以我将会话作为一个参数,以便将其种子植入下一个函数。但是我收到了一个错误 这是我的密码。第一种方法用于加载模型,第二种方法用于使用模型预测某些内容,但我在初始化会话时遇到了一些问题 def callmodel(): with tf.Graph().as_default(): #saver = tf.train.Saver() model_path

现在我想写两个函数:

  • 1对于我已经培训过的加载模型
  • 二是利用模型进行分类
但是这两个函数都需要相同的会话,所以我将会话作为一个参数,以便将其种子植入下一个函数。但是我收到了一个错误

这是我的密码。第一种方法用于加载模型,第二种方法用于使用模型预测某些内容,但我在初始化会话时遇到了一些问题

def callmodel():
    with tf.Graph().as_default():
        #saver = tf.train.Saver()
        model_path = 'E:/MyProject/MachineLearning/callTFModel/model/'
        ckpt = tf.train.get_checkpoint_state(model_path)
        sess = tf.Session()
        saver = tf.train.import_meta_graph(ckpt.model_checkpoint_path + '.meta')
        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state(model_path)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("load model successful!")
            return sess
        else:
            print("failed to load model!")


def test_one_image(sess,test_dir):
    global p, logits
    image = Image.open(test_dir)
    image = image.resize([32, 32])
    image_array = np.array(image)
    image = tf.cast(image_array, tf.float32)
    image = tf.reshape(image, [1, 32, 32, 3])  # 调整image的形状
    p = mmodel(image, 1)
    logits = tf.nn.softmax(p)
    x = tf.placeholder(tf.float32, shape=[32, 32, 3])
    prediction = sess.run(logits, feed_dict={x: image_array})
    max_index = np.argmax(prediction)
    if max_index == 0:
        print('probability of good: %.6f' % prediction[:, 0])
    else:
        print('probability of Lack of glue: %.6f' % prediction[:, 1])



#######//test
sess=callmodel
path="c:/test/1001.jpg"
test_one_image(sess,path)

it occurs error:

 File "E:/MyProject/python/C+pythonModel/test.py", line 175, in <module>
    test_one_image(sess,path)
  File "E:/MyProject/python/C+pythonModel/test.py", line 164, in test_one_image
    prediction = sess.run(logits, feed_dict={x: image_array})
  File "D:\study\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "D:\study\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1071, in _run
    + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(32, 32, 3), dtype=float32) is not an element of this graph.
def callmodel():
使用tf.Graph()作为默认值():
#saver=tf.train.saver()
模型路径='E:/MyProject/MachineLearning/callTFModel/model/'
ckpt=tf.train.get\u checkpoint\u状态(模型路径)
sess=tf.Session()
saver=tf.train.import\u元图(ckpt.model\u checkpoint\u path+'.meta')
sess.run(tf.global\u variables\u initializer())
ckpt=tf.train.get\u checkpoint\u状态(模型路径)
如果ckpt和ckpt.model\u检查点路径:
saver.restore(sess、ckpt.model\u检查点\u路径)
打印(“加载模型成功!”)
返回sess
其他:
打印(“加载模型失败!”)
def test_one_映像(sess、test_dir):
全球p,罗吉斯酒店
image=image.open(测试目录)
image=image.resize([32,32])
图像\数组=np.数组(图像)
image=tf.cast(image\u数组,tf.float32)
图像=tf.重塑(图像[1,32,32,3])#调整形象的形状
p=mmodel(图1)
logits=tf.nn.softmax(p)
x=tf.placeholder(tf.float32,shape=[32,32,3])
prediction=sess.run(logits,feed\u dict={x:image\u array})
max_index=np.argmax(预测)
如果max_index==0:
打印('良好概率:%.6f'%prediction[:,0])
其他:
打印('缺少胶水的概率:%.6f'%prediction[:,1])
#######//试验
sess=callmodel
path=“c:/test/1001.jpg”
测试一个图像(sess,路径)
出现以下错误:
文件“E:/MyProject/python/C+pythonModel/test.py”,第175行,在
测试一个图像(sess,路径)
文件“E:/MyProject/python/C+pythonModel/test.py”,第164行,在test\u one\u图像中
prediction=sess.run(logits,feed\u dict={x:image\u array})
文件“D:\study\Anaconda3\lib\site packages\tensorflow\python\client\session.py”,第895行,正在运行
运行_元数据_ptr)
文件“D:\study\Anaconda3\lib\site packages\tensorflow\python\client\session.py”,第1071行,正在运行
+e.args[0])
TypeError:无法将feed_dict键解释为张量:张量张量(“占位符:0”,shape=(32,32,3),dtype=float32)不是此图形的元素。

问题不在于将会话用作参数,而在于如何恢复图形的输入和输出节点:当您写入时

p = mmodel(image, 1)
logits = tf.nn.softmax(p)
x = tf.placeholder(tf.float32, shape=[32, 32, 3])
您不是在恢复会话图中的相应节点,而是在创建新节点。您应改为使用:

x= sess.graph().get_tensor_by_name("your_x_placeholder_name")
logits= sess.graph().get_tensor_by_name("your_logits_placeholder_name")
然后
prediction=sess.run(logits,feed\u dict={x:image\u array})

此外,您可能需要检查
图像
图像数组
之间是否有任何错误(现在您正在重塑
图像
,而不是数组,如果使用
图像数组
,这是没有用的)