python - 无法将数据输入张量流图
问题描述
我已经使用本教程中提供MNIST
的脚本在数据集上训练了一个神经网络模型。mnist_3.1_convolutional_bigger_dropout.py
我想在自定义数据集上测试经过训练的模型,因此我编写了一个小脚predict.py
本来加载经过训练的模型并将数据提供给它。我尝试了 2 种预处理图像的方法,以便它们与 MNIST 格式兼容。
- 方法 1:将图像大小调整为 28x28
- 方法2 :使用这里提到的技术
这两种方法都会导致错误
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()
解决方案
问题已解决,我忘了传递变量的值pkeep
。我必须进行以下更改才能使其正常工作。
dict_data = {x: img_data, pkeep: 1.0}
代替
dict_data = {x: img_data}
推荐阅读
- c++ - 扫描密文文档,猜测密钥,计算分布
- python - 如何在熊猫中创建具有最大功能的列
- javascript - 使每个类的效果单独工作(Javascript)
- java - Java 模块 module-info.java 重复
- flutter - BlocBuilder 构建器函数仅被调用一次
- javascript - 使用 Ajax 在 WordPress 前端上传多个文件
- c - 对齐数据的更快 memcpy
- machine-learning - 随着更多数据可用,您如何改进机器学习模型?
- oracle - 在 oracle 中创建物化视图时出错 - ORA-12052
- regex - Powershell Regex 新电子邮件标准