簡體   English   中英

model.predict 來自 keras 總是返回相同的結果

[英]model.predict from keras is always returning the same result

我正在嘗試 CNN model 用於 mnist 數據集。 在訓練 model 后,model.evaluate 給出了 99% 的測試准確率。 但是當我嘗試預測一張圖像的答案時,當我調用 model.predict() 時,它總是返回相同的數組。

規范化數據:

train_images = mnist_train_images.reshape(mnist_train_images.shape[0], 28, 28, 1)
test_images = mnist_test_images.reshape(mnist_test_images.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
    
train_images = train_images.astype('float32')
test_images = test_images.astype('float32')
train_images /= 255
test_images /= 255

#converting labels to one hot encoded format
train_labels = tensorflow.keras.utils.to_categorical(mnist_train_labels, 10)
test_labels = tensorflow.keras.utils.to_categorical(mnist_test_labels, 10)

Model 結構和 Model 培訓:

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
# 64 3x3 kernels
model.add(Conv2D(64, (3, 3), activation='relu'))
# Reduce by taking the max of each 2x2 block
model.add(MaxPooling2D(pool_size=(2, 2)))
# Dropout to avoid overfitting
model.add(Dropout(0.25))
# Flatten the results to one dimension for passing into our final layer
model.add(Flatten())
# A hidden layer to learn with
model.add(Dense(128, activation='relu'))
# Another dropout
model.add(Dropout(0.5))
# Final categorization from 0-9 with softmax
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
with tensorflow.device('/device:GPU:0'):
  model.fit(train_images, train_labels,
                    batch_size=128,
                    epochs=7,
                    verbose=2,
                    validation_data=(test_images, test_labels))

現在,我有一個數字的黑白 (28,28) 圖像(實際上,它是來自 mnist 訓練數據本身的數字)。 在標准化該圖像后嘗試預測:

image = image.reshape(-1,28, 28,1)
image = image.astype('float32')
image/=255

pred_array = model.predict(image)
print(pred_array)
pred_array = np.argmax(pred_array)
print('Result: {0}'.format(pred_array))

這總是每次都給出相同的 pred_array,當然是錯誤的。 我嘗試了類似問題的答案。 例如,嘗試增加時代,還有一個答案說要做

from keras.layers.core import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D,MaxPooling2D

代替

from keras.layers import Dense, Dropout, Flatten, Conv2D,MaxPooling2D

嘗試了一切,但似乎沒有任何幫助。 也許,我對圖像的規范化是錯誤的,或者我可能犯了一些愚蠢的錯誤,因為我是處理圖像和使用 CNN 的新手。 請幫忙

我只是復制了代碼,一切正常。 我希望您不要從標准化的 train_images 加載測試圖像,因為那里的圖像已經標准化,並且您在預測之前再次對其進行標准化。 以下工作符合我的預期:

image = train_images[14]
image = image.astype('float32')
image = image.reshape(-1,28, 28,1)
image/=255
pred_array = model.predict(image)
print(pred_array)
pred_array = np.argmax(pred_array)
print('Result: {0}'.format(pred_array))  

編輯:當我復制你的代碼時,我做了一些不同的事情。 我將標准化圖像保存在不同的 Numpy 數組中,如下所示:

train_images_norm = train_images.astype('float32')
test_images_norm = test_images.astype('float32')
train_images_norm /= 255
test_images_norm /= 255
...
model.fit(train_images_norm, train_labels_norm,...)

所以現在,當我預測時,我使用原始圖像(未標准化)並在預測之前對其進行標准化。 你得到不可預知的結果的原因是你將已經標准化的圖像再次除以 255,這會產生完全不同的數字,網絡沒有訓練過。 您有兩個選擇,將原始圖像保留在不同的數組中並在預測之前對其進行規范化(我的代碼),或者如果您希望原始代碼正常工作,您可以刪除image = image.astype('float32')image /= 255之前預言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM