gpt4 book ai didi

python - CNN 模型无法做出预测

转载 作者:行者123 更新时间:2023-11-30 09:48:11 26 4
gpt4 key购买 nike

我成功训练了 CNN 模型,但是当我向模型提供图像以使其预测标签时,出现错误。

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

# load dataset
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

# interactive session
sess = tf.InteractiveSession()

# data and labels placeholder
x = 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 maps
W_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 function
conv1 = tf.nn.relu(conv2d(x_img, W_conv1) + b_conv1)
pool1 = max_pool_2x2(conv1)

# 64 filters of size 5x5
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

# second layer
conv2 = tf.nn.relu(conv2d(pool1, W_conv2) + b_conv2)
pool2 = max_pool_2x2(conv2)

# fully connected layer with 1024 neurons
W_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 neurons
prob_drop = tf.placeholder(tf.float32)
dropout = tf.nn.dropout(fully, prob_drop)

# readout layer that will return the raw values
# of our predictions
W_readout = weight_variable([1024, 10])
b_readout = bias_variable([10])

y_conv = tf.matmul(dropout, W_readout) + b_readout

# loss function
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y))

# restore the trained CNN model
saver = 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 2
two_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 label
two_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 ) } ) )

关于python - CNN 模型无法做出预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49592565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com