简体   繁体   中英

How to use model.predict for predicting single image in tf.keras

I am trying to predict a single image using the trained model either I am getting an array of 80 values or I am getting this error:

dimension Matrix size-incompatible: In[0]: [1,19200], In[1]: [240,128]

I have tried different option available on StackOverflow but nothing worked out for me. Few of those I have mentioned here How to predict input image using trained model in Keras? Cannot predict the label for a single image with VGG19 in Keras

to load data I have used code from https://pythonprogramming.net/loading-custom-data-deep-learning-python-tensorflow-keras/

model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),
                                    tf.keras.layers.Dense(128,activation=tf.nn.relu),
                                    tf.keras.layers.Dropout(rate=0.5),
                                    tf.keras.layers.BatchNormalization(axis=1),
                                    tf.keras.layers.Dense(100,activation=tf.nn.relu),
                                    tf.keras.layers.Dropout(rate=0.5),
                                    tf.keras.layers.BatchNormalization(axis=1),
                                    tf.keras.layers.Dense(100,activation=tf.nn.relu),
                                    tf.keras.layers.Dropout(rate=0.4),
                                    tf.keras.layers.BatchNormalization(axis=1),
                                    tf.keras.layers.Dense(100,activation=tf.nn.relu),
                                    tf.keras.layers.Dropout(rate=0.2),
                                    tf.keras.layers.BatchNormalization(axis=1),
                                    tf.keras.layers.Dense(100,activation=tf.nn.relu),
                                    tf.keras.layers.Dropout(rate=0.7),
                                    tf.keras.layers.BatchNormalization(axis=1),
                                    tf.keras.layers.Dense(2,activation=tf.nn.sigmoid)])

from tensorflow.keras.optimizers import SGD
opt = SGD(lr=0.01)
model.compile(loss = "mean_squared_error", optimizer = opt, metrics=['accuracy'])

model.fit(X_train, y_train, batch_size=50,epochs=205,callbacks=[callbacks])
model.evaluate(X_test, y_test)

#First way no error multiple values
from keras.preprocessing import image
test_image = image.load_img('img', target_size=(80, 80))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
test_image = test_image.reshape(80, 80*3)   

result = model.predict(test_image/255.0, batch_size=1)
print(result)

result = model.predict_classes(test_image/255.0, batch_size=1)
print(result)

#second way, Matrix size-incompatible(Error)
img = image.load_img('img', target_size=(80, 80))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict_classes(x, batch_size=1)
print(classes)

#third way,Matrix size-incompatible(Error)
print(model.predict_classes(np.expand_dims(X_test[10], axis=0)))

first way o/p

[[0.4419657  0.5002713 ]
 [0.4379595  0.5007576 ]
 [0.43674818 0.5014705 ]
 [0.43429232 0.50493073]
 [0.43446562 0.50841236]
 [0.43417045 0.51054156]
 [0.4348069  0.51089376]
 [0.43577492 0.50624526]
 [0.43650073 0.5084632 ]
 [0.44226125 0.5106121 ]
 [0.44256458 0.51815474]
 [0.4366225  0.5247917 ]
 [0.44668812 0.5251329 ]
 [0.45352334 0.5256567 ]
 [0.4572222  0.5226744 ]
 [0.46253017 0.519516  ]
 [0.46236354 0.51812094]
 [0.4637973  0.5135511 ]
 [0.46357435 0.5091353 ]
 [0.4647084  0.50632596]
 [0.46603358 0.5004298 ]
 [0.46488768 0.49856278]
 [0.4637522  0.50335187]
 [0.4605053  0.5001269 ]
 [0.46522006 0.49863124]
 [0.46316907 0.50639075]
 [0.46407732 0.51068664]
 [0.452004   0.51411426]
 [0.4437306  0.5115358 ]
 [0.44812864 0.5081628 ]
 [0.45141432 0.50651264]
 [0.4518429  0.5081477 ]
 [0.44927847 0.49777785]
 [0.44322333 0.4825523 ]
 [0.44135702 0.47820964]
 [0.43782592 0.47925416]
 [0.43334886 0.47967055]
 [0.4303841  0.47919393]
 [0.42532465 0.48017433]
 [0.42595625 0.47586957]
 [0.4292146  0.47039127]
 [0.43103853 0.4656783 ]
 [0.43306574 0.463838  ]
 [0.4276282  0.4699353 ]
 [0.42867652 0.46581164]
 [0.43545863 0.45945364]
 [0.44277322 0.47201872]
 [0.4460439  0.4735631 ]
 [0.443609   0.47811195]
 [0.44498175 0.47373036]
 [0.44886908 0.48278013]
 [0.4429854  0.4908823 ]
 [0.44526115 0.49165127]
 [0.45446166 0.49740997]
 [0.4574405  0.49744406]
 [0.45719808 0.5041652 ]
 [0.4546386  0.50350964]
 [0.4539847  0.5072408 ]
 [0.46614394 0.5016114 ]
 [0.45871773 0.5072619 ]
 [0.4616405  0.50306535]
 [0.46992242 0.50247884]
 [0.46977502 0.50086266]
 [0.4666891  0.48775986]
 [0.46482667 0.48338565]
 [0.45363256 0.49476466]
 [0.45803532 0.49177122]
 [0.4653356  0.49367705]
 [0.46423748 0.49807605]
 [0.47541898 0.49923015]
 [0.45872727 0.5036651 ]
 [0.45934066 0.499598  ]
 [0.46240935 0.50199217]
 [0.45569527 0.5061147 ]
 [0.4612086  0.49505413]
 [0.46061015 0.49445656]
 [0.46367538 0.4839395 ]
 [0.46831584 0.4812285 ]
 [0.46474478 0.4740279 ]
 [0.46901295 0.4787915 ]]

[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1]

2nd & 3rd-way error

InvalidArgumentError:  Matrix size-incompatible: In[0]: [1,6400], In[1]: [240,128]
     [[node sequential/dense/MatMul (defined at <ipython-input-15-dfba430dbf46>:1) ]] [Op:__inference_keras_scratch_graph_1056]

It`s working for me with below code changes

IMG_SIZE = 80
img_array = cv2.imread("im.jpg" ,cv2.IMREAD_GRAYSCALE)  
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array/255.0, cmap='gray')
plt.show()

new_array=np.reshape(new_array,(80, 80, 1))
new_array=new_array.flatten()/255.0
new_array_2=np.reshape(new_array,(6400, 1))

#to predict single image
class_prob=model.predict(new_array_2.T,batch_size=1)
print(class_prob)
classifications=model.predict_classes(new_array_2.T,batch_size=1)
print(classifications) 

From the code i can see that you are trying to predict 2 class so try to change the loss function and then run it.

model_final.compile(loss = keras.losses.categorical_crossentropy, optimizer = opt, metrics=["accuracy"])

Share the output after doing this change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM