我正在研究一個腫瘤分類問題,其中我有 4 個類別 [神經膠質瘤(1624 個樣本)、腦膜瘤(1645 個樣本)、notumor(2000 個樣本)和垂體(1757 個樣本)]。我保存的模型有問題,混淆矩陣顯示幾乎完美的結果,沒有偏見或任何東西,當我這樣做時
model.evaluate(test_set)
我得到: categorical_accuracy: 0.9872 [0.06456023454666138, 0.9871976971626282]
但是當我嘗試進行預測時,所有結果都是錯誤的(即使我故意為模型注入訓練資料來預測)我為預測撰寫了這個函式:
def predict_on_one_image(model,image_path):
img = imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = np.expand_dims(np.stack((img,)*3, axis=-1), axis=0).astype(np.float32)
p = model6.predict(img)
return {'class':p.argmax(axis=-1)[0],'probability':p}
我的標簽是:
{'glioma': 0, 'meningioma': 1, 'notumor': 2, 'pituitary': 3}
如果我嘗試從訓練集中預測影像的類別 [原始類別 = 神經膠質瘤]
predict_on_one_image(model6,'path_here/training/glioma/Te-gl_0016.jpg' )
我得到類預測為 notumor
{'class': 2, 'probability': array([[0., 0., 1., 0.]], dtype=float32)}
我懷疑它對 notumor 的偏見,因為它擁有最多的資料(類不平衡問題),但無論如何我可以得到正確的預測而不必再次運行模型?
uj5u.com熱心網友回復:
基本上,我需要以與處理影像進行訓練相同的方式處理影像進行預測(我使用 ImageDataGenerator 重新縮放它們)
我添加了這幾行代碼,它可以正確預測:
img = imread('path_here/testing/notumor/Te-no_0164.jpg')
x = asarray(img)
x = x / 255.
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model6.predict(images, batch_size=10)
print('Predicted class is :')
print(np.argmax(classes))
學分:此評論
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/508273.html