我尝试使用训练好的模型预测单个图像,要么得到一个80个值的数组,要么得到以下错误:
尺寸矩阵尺寸不兼容:在[0]中:[1,19200],在[1]中:【二十四万一百二十八】
我已经尝试了不同的选择上StackOverflow,但没有为我工作。其中一些我在这里提到的How to predict input image using trained model in Keras?Cannot predict the label for a single image with VGG19 in Keras
为了加载数据,我使用了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)))
第一路出口
[[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]
第二和第三路错误
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]
2条答案
按热度按时间2lpgd9681#
它对我的工作与下面的代码更改
c8ib6hqw2#
从代码中我可以看到,你正在尝试预测2类,所以尝试改变损失函数,然后运行它。
完成此更改后共享输出。