首页 > 解决方案 > 无法将数据输入张量流图

问题描述

我已经使用本教程中提供MNIST的脚本在数据集上训练了一个神经网络模型。mnist_3.1_convolutional_bigger_dropout.py

我想在自定义数据集上测试经过训练的模型,因此我编写了一个小脚predict.py本来加载经过训练的模型并将数据提供给它。我尝试了 2 种预处理图像的方法,以便它们与 MNIST 格式兼容。

这两种方法都会导致错误

InvalidArgumentError(参见上面的回溯):您必须使用 dtype float 为占位符张量“Placeholder_2”提供一个值

预测.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()

标签: pythontensorflowmnist

解决方案


问题已解决,我忘了传递变量的值pkeep。我必须进行以下更改才能使其正常工作。

dict_data = {x: img_data, pkeep: 1.0}

代替

dict_data = {x: img_data}

推荐阅读