Image 将图像馈送到Theano会引发匹配错误
我正在尝试一个非常基本的无逻辑回归模型的例子,在训练网络之后,我想测试一些图像,看看它们是如何分类的。有关培训和测试的代码,请访问。事实上,我试图修改的唯一部分是predict()函数,如下所示:Image 将图像馈送到Theano会引发匹配错误,image,theano,predict,Image,Theano,Predict,我正在尝试一个非常基本的无逻辑回归模型的例子,在训练网络之后,我想测试一些图像,看看它们是如何分类的。有关培训和测试的代码,请访问。事实上,我试图修改的唯一部分是predict()函数,如下所示: def predict(): """ An example of how to load a trained model and use it to predict labels. """ # load the saved model classifi
def predict():
"""
An example of how to load a trained model and use it
to predict labels.
"""
# load the saved model
classifier = cPickle.load(open('best_model.pkl'))
# compile a predictor function
predict_model = theano.function(
inputs=[classifier.input],
outputs=classifier.y_pred)
# We can test it on some examples from test test
#dataset='mnist.pkl.gz'
#datasets = load_data(dataset)
#test_set_x, test_set_y = datasets[2]
#test_set_x = test_set_x.get_value()
img = Image.open('index.png','r')
shared_x = theano.shared(numpy.asarray(img,dtype=theano.config.floatX))
predicted_values = predict_model(shared_x.get_value())
print ("Predicted values for the first 10 examples in test set:")
print predicted_values
我遵循了这里的一些提示,但显然我遇到了问题,因为我得到的是:
ValueError:形状不匹配:x有28列(和28行),但y有784行
行(和10列)应用导致错误的节点:Dot22(x,W)
输入类型:[TensorType(浮点64,矩阵),TensorType(浮点64,
矩阵]输入形状:[(28,28),(784,10)]输入步幅:[(224,
8) ,(80,8)]输入值:[“未显示”、“未显示”]
那么,将图像(在我的例子中是28x28)馈送到Theano进行预测的正确方法是什么呢?模型需要成批的展平图像作为输入
输入需要是一个矩阵,每个图像有一行。共有784列,是28x28像素图像的展平(或重塑)版本(请注意,28*28=784)
而不是
img = Image.open('index.png','r')
shared_x = theano.shared(numpy.asarray(img,dtype=theano.config.floatX))
predicted_values = predict_model(shared_x.get_value())
使用
无需使用共享变量,因为您只需将它包含的numpy数组传递给Theano函数。模型需要成批的展平图像作为输入
输入需要是一个矩阵,每个图像有一行。共有784列,是28x28像素图像的展平(或重塑)版本(请注意,28*28=784)
而不是
img = Image.open('index.png','r')
shared_x = theano.shared(numpy.asarray(img,dtype=theano.config.floatX))
predicted_values = predict_model(shared_x.get_value())
使用
不需要使用共享变量,因为您只是将它包含的numpy数组传递给Theano函数