CNN模型无法进行预测

我成功训练了一个CNN模型,但在向模型输入图像以预测标签时遇到了错误。

这是我的模型(我使用saver.restore恢复它)…

# load datasetmnist = input_data.read_data_sets("/tmp/data/", one_hot=True)# interactive sessionsess = tf.InteractiveSession()# data and labels placeholderx = tf.placeholder(tf.float32, shape=[None, 784])y = tf.placeholder(tf.float32, shape=[None, 10])# 32 filters of size 5x5 and 32 biases,# the filters are used to create 32 feature mapsW_conv1 = weight_variable([5, 5, 1, 32])b_conv1 = bias_variable([32])x_img = tf.reshape(x, [-1, 28, 28, 1])# first layer activated using a Relu activation functionconv1 = tf.nn.relu(conv2d(x_img, W_conv1) + b_conv1)pool1 = max_pool_2x2(conv1)# 64 filters of size 5x5W_conv2 = weight_variable([5, 5, 32, 64])b_conv2 = bias_variable([64])# second layerconv2 = tf.nn.relu(conv2d(pool1, W_conv2) + b_conv2)pool2 = max_pool_2x2(conv2)# fully connected layer with 1024 neuronsW_fully = weight_variable([7 * 7 * 64, 1024])b_fully = bias_variable([1024])pool2flat = tf.reshape(pool2, [-1, 7 * 7 * 64])fully = tf.nn.relu(tf.matmul(pool2flat, W_fully) + b_fully)# dropout layer removes dead neuronsprob_drop = tf.placeholder(tf.float32)dropout = tf.nn.dropout(fully, prob_drop)# readout layer that will return the raw values# of our predictionsW_readout = weight_variable([1024, 10])b_readout = bias_variable([10])y_conv = tf.matmul(dropout, W_readout) + b_readout# loss functioncross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y))# restore the trained CNN modelsaver = tf.train.Saver()saver.restore(sess, "/tmp/model2.ckpt")

y_conv 是预测器。

该模型是在mnist数据集上训练的,现在我有一张数字的图像,我希望模型能告诉我它认为这是什么数字,并给出准确性。我尝试了以下操作…

prediction = tf.argmax(y_conv, 1)print(sess.run(prediction, feed_dict={x:two_images[0]}))

在将图像two_images[0]输入模型后,我得到了以下错误…

ValueError: Cannot feed value of shape (784,) for Tensor ‘Placeholder:0’, which has shape ‘(?, 784)’

所以我通过以下方式修复了它…

prediction = tf.argmax(y_conv, 1)print(sess.run(prediction, feed_dict={x:two_images[0].reshape((1, 784))}))

但现在我得到了一大堆无法解读的错误…

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor ‘Placeholder_2’ with dtype float [[Node: Placeholder_2 = Placeholderdtype=DT_FLOAT, shape=, _device=”/job:localhost/replica:0/task:0/device:CPU:0″]]

我不知道自己做错了什么。

编辑

这是我填充变量two_images的方式…

# extract the indices of the number 2two_idxs_list = np.where(mnist.test.labels[:, 2].astype(int) == 1)two_idxs = two_idxs_list[0][:10]# use the indices to extract the images of 2 and their corresponding labeltwo_images = mnist.test.images[two_idxs]two_labels = mnist.test.labels[two_idxs]

回答:

通过添加的代码,我能够在我的机器上进行测试。问题是你的网络期望两个输入,一个是图像,另一个是标签。即使你只是进行推断,你也必须提供一个输入,也许只是填充一些零?显然,损失计算会不准确,但你并不关心这一点,你只关心预测。所以你的sess.run行应该如下所示:

print( sess.run( prediction, feed_dict= {    x: two_images[0].reshape((1, 784)),    y: np.zeros( shape = ( 1, 10 ), dtype = np.float32 ) } ) )

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注