我已经使用tutorial中提供的脚本MNIST
在mnist_3.1_convolutional_bigger_dropout.py
数据集上训练了神经网络模型。
我想在自定义数据集上测试经过训练的模型,因此我编写了一个小脚本predict.py
,该脚本加载了经过训练的模型并将数据提供给它。我尝试了2种预处理图像的方法,以使其与MNIST格式兼容。
这两种方法均会导致错误
InvalidArgumentError(请参阅上面的回溯):您必须使用dtype float输入占位符张量“ Placeholder_2”的值
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()
答案 0 :(得分:0)
问题已解决,我忘记传递变量pkeep
的值。我必须进行以下更改才能使其正常工作。
dict_data = {x: img_data, pkeep: 1.0}
代替
dict_data = {x: img_data}