(X,Y),(test_x,test_y)=cifar.load_data(one_hot=True)
X=X.reshape([-1,32,32,3])
test_x=test_x.reshape([-1,32,32,3])
convnet=input_data(shape=[None,32,32,3],name='input')
convnet=conv_2d(convnet,32,3,activation='relu')
convnet=max_pool_2d(convnet,2)
convnet=conv_2d(convnet,64,3,activation='relu')
convnet=max_pool_2d(convnet,2)
convnet=conv_2d(convnet,128,3,activation='relu')
convnet=conv_2d(convnet,128,3,activation='relu')
convnet=max_pool_2d(convnet,2)
convnet=fully_connected(convnet,512,activation='relu')
convnet=fully_connected(convnet,512,activation='relu')
convnet=dropout(convnet,0.8)
convnet=fully_connected(convnet,10,activation='softmax')
convnet=regression(convnet,optimizer='adam',learning_rate=0.001,loss='categorical_crossentropy')
model=tflearn.DNN(convnet)
model.fit(X,Y,n_epoch=1,validation_set=(test_x,test_y),batch_size=100,snapshot_step=1000,show_metric=True)
model.save('tflearn.model')
'''
model.load('tflearn.model')
print(model.predict(test_x[1]))
'''
When i try to predict, It is showing error: "Cannot feed value of shape (32, 32, 3) for Tensor u'input/X:0', which has shape '(?, 32, 32, 3)".
当我尝试预测时,它显示了错误:“不能为u'input/X:0'张量赋形(32、32、3)的值,它有形状(?,32岁,32岁,3)”。
Please Someone help.
请别人帮助。
2 个解决方案
#1
1
You need to use tf.expand_dims
on the input for which you want to make the prediction:
你需要使用tf。对要进行预测的输入进行expand_dims:
# 't2' is a tensor of shape [32, 32, 3]
shape(expand_dims(t2, axis=0)) ==> [1, 32, 32, 3]
#2
0
Here's the longform verison of what Frank said,
这是弗兰克说过的,
You are feeding a 3 dimensional tensor when the input accepts a four dimensional tensor.
当输入接受一个四维张量时,你正在输入一个三维张量。
The '?' in the shape corresponds to batch size which you define in the model.fit()
function. During training this is equal to 100.
“?在形状中,对应于您在model.fit()函数中定义的批处理大小。在训练中,这等于100。
When you are predicting the network still expects a 4 dimensional tensor similar to training but you are feeding just one single image which is 3 dimensional(shape = [32,32,3]). Since you are trying to predict the class of just one image, which corresponds to a shape [1,32,32,3]. So if you change your code to:
当你预测网络仍然期望一个4维张量与训练相似,但是你只提供一个3维的图像(形状=[32,32,3])。因为您试图预测一个图像的类,它对应于一个形状[1,32,32,3]。因此,如果您将代码更改为:
print(model.predict(test_x[1].reshape([1,32,32,3])))
This will print the scores for each of the ten classes.
这将打印10个班级的分数。
#1
1
You need to use tf.expand_dims
on the input for which you want to make the prediction:
你需要使用tf。对要进行预测的输入进行expand_dims:
# 't2' is a tensor of shape [32, 32, 3]
shape(expand_dims(t2, axis=0)) ==> [1, 32, 32, 3]
#2
0
Here's the longform verison of what Frank said,
这是弗兰克说过的,
You are feeding a 3 dimensional tensor when the input accepts a four dimensional tensor.
当输入接受一个四维张量时,你正在输入一个三维张量。
The '?' in the shape corresponds to batch size which you define in the model.fit()
function. During training this is equal to 100.
“?在形状中,对应于您在model.fit()函数中定义的批处理大小。在训练中,这等于100。
When you are predicting the network still expects a 4 dimensional tensor similar to training but you are feeding just one single image which is 3 dimensional(shape = [32,32,3]). Since you are trying to predict the class of just one image, which corresponds to a shape [1,32,32,3]. So if you change your code to:
当你预测网络仍然期望一个4维张量与训练相似,但是你只提供一个3维的图像(形状=[32,32,3])。因为您试图预测一个图像的类,它对应于一个形状[1,32,32,3]。因此,如果您将代码更改为:
print(model.predict(test_x[1].reshape([1,32,32,3])))
This will print the scores for each of the ten classes.
这将打印10个班级的分数。