Python 为什么可以';在我的流程图中,我使用了一个类似于tf op的Keras模型

Python 为什么可以';在我的流程图中,我使用了一个类似于tf op的Keras模型,python,tensorflow,keras,tf.keras,Python,Tensorflow,Keras,Tf.keras,我试图在tf会话的计算流图中的某个地方使用我预先训练好的keras模型,所以我尝试了这个简化图,但仍然得到相同的错误 model = load_model('models/vtcnn3.h5') input = tf.placeholder(tf.float32,shape=(1,*x_test.shape[1:])) output = model(input) sess = tf.compat.v1.Session() guess_0 = sess.run(output, {input:

我试图在tf会话的计算流图中的某个地方使用我预先训练好的keras模型,所以我尝试了这个简化图,但仍然得到相同的错误

model = load_model('models/vtcnn3.h5')

input = tf.placeholder(tf.float32,shape=(1,*x_test.shape[1:]))
output = model(input)

sess = tf.compat.v1.Session()

guess_0 = sess.run(output, {input:x_test[0:1]} )
当会话运行时,我得到了一个很大的回溯,最终显示

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense2/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense2/kernel)
     [[node sequential/dense2/MatMul/ReadVariableOp (defined at code/soq.py:114) ]]

Errors may have originated from an input operation.
Input Source operations connected to node sequential/dense2/MatMul/ReadVariableOp:
 dense2/kernel (defined at code/soq.py:111)

Original stack trace for 'sequential/dense2/MatMul/ReadVariableOp':
  File "code/soq.py", line 114, in <module>
    output = model(input)
  File "/Users/yaba/miniconda3/envs/cs1/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 634, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
.
.
.
.
tensorflow.python.framework.errors\u impl.failedPremissionError:从容器localhost读取资源变量dense2/kernel时出错。这可能意味着变量未初始化。未找到:容器localhost不存在。(找不到资源:localhost/dense2/kernel)
[[节点顺序/dense2/MatMul/ReadVariableOp(在代码/soq.py:114处定义)]]
错误可能源于输入操作。
连接到节点sequential/dense2/MatMul/ReadVariableOp的输入源操作:
dense2/kernel(在代码/soq.py:111中定义)
“sequential/dense2/MatMul/ReadVariableOp”的原始堆栈跟踪:
文件“code/soq.py”,第114行,在
输出=模型(输入)
文件“/Users/yaba/miniconda3/envs/cs1/lib/python3.7/site packages/tensorflow/python/keras/engine/base_layer.py”,第634行,在调用中__
输出=调用(输入,*args,**kwargs)
.
.
.
.

dense2
是模型中图层的名称。当我对流程图进行其他更改时,回溯会将错误放入我的模型的不同层。

在尝试通过Keras模型运行输入之前,需要初始化变量。以下代码适用于
tensorflow==1.14.0

import tensorflow as tf

model = tf.keras.applications.ResNet50()

init_op = tf.global_variables_initializer()
with tf.compat.v1.Session() as sess:
    random_image, _ = sess.run([tf.random.normal(shape=[1, 224, 224, 3]), init_op])
    outputs = sess.run(model.output, feed_dict={model.input:random_image})

在尝试通过Keras模型运行输入之前,需要初始化变量。以下代码适用于
tensorflow==1.14.0

import tensorflow as tf

model = tf.keras.applications.ResNet50()

init_op = tf.global_variables_initializer()
with tf.compat.v1.Session() as sess:
    random_image, _ = sess.run([tf.random.normal(shape=[1, 224, 224, 3]), init_op])
    outputs = sess.run(model.output, feed_dict={model.input:random_image})

感谢Srihari Humbardwadi提供的初始化答案,它修复了我得到的错误,但现在网络的输出与使用
模型时不同。预测(…)

如果要加载Keras模型或文件并在Tensorflow图中使用,必须将
tf.Session()
设置到Keras后端,然后初始化变量,然后加载Keras模型,如下所示

sess = tf.Session()

# make sure keras has the same session as this code BEFORE initializing
tf.keras.backend.set_session(sess)

# Do this BEFORE loading a keras model
init_op = tf.global_variables_initializer()
sess.run(init_op)


model = models.load_model('models/vtcnn3.h5')


input = tf.placeholder(tf.float32,shape=(1,*x_test.shape[1:]))
output = model(input)


guess_0 = sess.run(output, feed_dict={input:x_test[0:1]} )
这个
guess\u 0
现在将与您要执行的操作相同

model = models.load_model('models/vtcnn3.h5')
guess_0 = model.predict(x_test[0:1])

感谢Srihari Humbardwadi提供的初始化答案,它修复了我得到的错误,但现在网络的输出与使用
模型时不同。预测(…)

如果要加载Keras模型或文件并在Tensorflow图中使用,必须将
tf.Session()
设置到Keras后端,然后初始化变量,然后加载Keras模型,如下所示

sess = tf.Session()

# make sure keras has the same session as this code BEFORE initializing
tf.keras.backend.set_session(sess)

# Do this BEFORE loading a keras model
init_op = tf.global_variables_initializer()
sess.run(init_op)


model = models.load_model('models/vtcnn3.h5')


input = tf.placeholder(tf.float32,shape=(1,*x_test.shape[1:]))
output = model(input)


guess_0 = sess.run(output, feed_dict={input:x_test[0:1]} )
这个
guess\u 0
现在将与您要执行的操作相同

model = models.load_model('models/vtcnn3.h5')
guess_0 = model.predict(x_test[0:1])

这成功了!!!!非常感谢你!我使用的是tensorflow v2.0,所以我必须使用兼容性选项。我做了
init\u op=tf.compat.v1.global\u variables\u initializer()
然后
sess.run(init\u op)
现在我在使用tf会话时得到了与使用
model.predict(…)
不同的结果。你也知道如何解决这个问题吗?这很有效!!!!非常感谢你!我使用的是tensorflow v2.0,所以我必须使用兼容性选项。我做了
init\u op=tf.compat.v1.global\u variables\u initializer()
然后
sess.run(init\u op)
现在我在使用tf会话时得到了与使用
model.predict(…)
不同的结果。你也知道如何解决这个问题吗?