我成功培训了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:无法为Tensor'占位符:0'提供形状值(784,),其形状为'(?,784)'
所以我通过以下方式解决了这个问题......
prediction = tf.argmax(y_conv, 1)
print(sess.run(prediction, feed_dict={x:two_images[0].reshape((1, 784))}))
但现在我得到了一大堆我无法解读的错误......
InvalidArgumentError(请参阅上面的回溯):您必须使用dtype float为占位符张量'Placeholder_2'提供值 [[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]
答案 0 :(得分:1)
好的,添加的代码我可以在我的机器上测试。问题是您的广告联盟需要两个输入,一个图片和一个标签。即使你只做推理,你也必须提供一个输入,也许只有一些零?显然损失计算是错误的,但你只对预测感兴趣。所以你的sess.run行应该是:
print( sess.run( prediction, feed_dict= {
x: two_images[0].reshape((1, 784)),
y: np.zeros( shape = ( 1, 10 ), dtype = np.float32 ) } ) )