重塑图像错误

时间:2018-02-11 21:38:31

标签: python numpy reshape

im=Image.open("/Users/shalinsavalia/Desktop/CNN_Numbers/Number_7.jpg")
im
im=im.resize((28, 28), Image.ANTIALIAS) # resize the image
im = np.array(im)                       # convert to an array
print(im)
im2=im/np.max(im).astype(float)         # normalise input
test_image1=np.reshape(im2, [1,784])    # reshape it to our input placeholder shape

pred=(sess.run(y_predicted,
               feed_dict={
                   x:test_image1
               }))
predicted_class=np.argmax(pred)
print "Predicted class : {}" .format(predicted_class)

结果错误是:

Traceback (most recent call last):
  File "./CNN_Numbers.py", line 238, in <module>
    test_image1=np.reshape(im2, [1,784]) # reshape it to our input placeholder shape
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 257, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 52, in _wrapfunc
    return getattr(obj, method)(*args, **kwds)
ValueError: cannot reshape array of size 3136 into shape (1,784)

1 个答案:

答案 0 :(得分:1)

我猜您的图片有3 + 1个颜色通道。你需要重塑[784,4]。