无法将数据输入张量流图
Trouble feeding data into tensorflow graph
我使用 tutorial 中提供的脚本 mnist_3.1_convolutional_bigger_dropout.py
在 MNIST
数据集上训练了一个神经网络模型。
我想在自定义数据集上测试经过训练的模型,因此我编写了一个小脚本 predict.py
来加载经过训练的模型并将数据提供给它。我尝试了两种预处理图像的方法,使它们与 MNIST 格式兼容。
- 方法 1:将图像调整为 28x28
- 方法 2:使用提到的技术 here
这两种方法都会导致错误
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype float
predict.py
# Importing libraries
from scipy.misc import imread
import tensorflow as tf
import numpy as np
import cv2 as cv
import glob
from test import imageprepare
files = glob.glob('data2/*.*')
#print(files)
# Method 1
'''
img_data = []
for fl in files:
img = imageprepare(fl)
img = img.reshape(img.shape[0], img.shape[1], 1)
img_data.append(img)
'''
# Method 2
dig_cont = [cv.imread(fl, 0) for fl in files]
#print(len(dig_cont))
img_data = []
for i in range(len(dig_cont)):
img = cv.resize(dig_cont[i], (28, 28))
img = img.reshape(img.shape[0], img.shape[1], 1)
img_data.append(img)
print("Restoring Model ...")
sess = tf.Session()
# Step-1: Recreate the network graph. At this step only graph is created.
tf_saver = tf.train.import_meta_graph('model/model.meta')
# Step-2: Now let's load the weights saved using the restore method.
tf_saver.restore(sess, tf.train.latest_checkpoint('model'))
print("Model restored")
x = tf.get_default_graph().get_tensor_by_name('X:0')
print('x :', x.shape)
y = tf.get_default_graph().get_tensor_by_name('Y:0')
print('y :', y.shape)
dict_data = {x: img_data}
result = sess.run(y, feed_dict=dict_data)
print(result)
print(result.shape)
sess.close()
问题已解决,我忘记传递变量pkeep
的值了。我必须进行以下更改才能使其正常工作。
dict_data = {x: img_data, pkeep: 1.0}
而不是
dict_data = {x: img_data}
我使用 tutorial 中提供的脚本 mnist_3.1_convolutional_bigger_dropout.py
在 MNIST
数据集上训练了一个神经网络模型。
我想在自定义数据集上测试经过训练的模型,因此我编写了一个小脚本 predict.py
来加载经过训练的模型并将数据提供给它。我尝试了两种预处理图像的方法,使它们与 MNIST 格式兼容。
- 方法 1:将图像调整为 28x28
- 方法 2:使用提到的技术 here
这两种方法都会导致错误
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype float
predict.py
# Importing libraries
from scipy.misc import imread
import tensorflow as tf
import numpy as np
import cv2 as cv
import glob
from test import imageprepare
files = glob.glob('data2/*.*')
#print(files)
# Method 1
'''
img_data = []
for fl in files:
img = imageprepare(fl)
img = img.reshape(img.shape[0], img.shape[1], 1)
img_data.append(img)
'''
# Method 2
dig_cont = [cv.imread(fl, 0) for fl in files]
#print(len(dig_cont))
img_data = []
for i in range(len(dig_cont)):
img = cv.resize(dig_cont[i], (28, 28))
img = img.reshape(img.shape[0], img.shape[1], 1)
img_data.append(img)
print("Restoring Model ...")
sess = tf.Session()
# Step-1: Recreate the network graph. At this step only graph is created.
tf_saver = tf.train.import_meta_graph('model/model.meta')
# Step-2: Now let's load the weights saved using the restore method.
tf_saver.restore(sess, tf.train.latest_checkpoint('model'))
print("Model restored")
x = tf.get_default_graph().get_tensor_by_name('X:0')
print('x :', x.shape)
y = tf.get_default_graph().get_tensor_by_name('Y:0')
print('y :', y.shape)
dict_data = {x: img_data}
result = sess.run(y, feed_dict=dict_data)
print(result)
print(result.shape)
sess.close()
问题已解决,我忘记传递变量pkeep
的值了。我必须进行以下更改才能使其正常工作。
dict_data = {x: img_data, pkeep: 1.0}
而不是
dict_data = {x: img_data}