簡體   English   中英

我應該如何使用 mode.predict_generator 來評估混淆矩陣中的模型性能?

[英]How should I use mode.predict_generator to evaluate model performance in a Confusion Matrix?

我正在嘗試使用混淆矩陣評估常見的狗和貓過濾數據集中的遷移學習模型。 我的代碼基於 tensorflow 的遷移學習教程。 訓練准確度圖顯示准確度超過 90%。

然而,使用生成器來獲取真實的標簽和使用 model.predict_generator 來獲取預測數組會拋出不一致的結果。 首先,准確性不穩定,如果您第二次運行預測,它會改變值。 其次,與單個實例上的 model.predict 相比,我通過使用 model.predict_generator 得到的預測似乎是錯誤的。

為了快速測試基於ImageDataGenerator的混淆矩陣,我下載了 5 張貓圖像和 5 張狗圖像。 然后我從文件夾中創建了另一個生成器並檢查標簽和類是否與訓練相同。

兩種奇怪的行為之后,我只是使用 sklearn 指標混淆矩陣來評估使用 model.predict_generator 和我從生成器中獲得的標簽作為真實標簽的預測。

在第一次運行時,我得到了 0.9 的准確率並說歡呼!。 但是,如果我第二次嘗試 model.predict_generator 並且它會為數組輸出和精度下降到 0.5 拋出其他值。 之后它不再改變......什么結果是正確的? 為什么會改變?

我一直注意到你必須運行兩次才能得到最終結果,但得到的結果是錯誤的。 我寫了一些代碼來單獨測試每個圖像,我的預測沒有錯。 那么我做錯了什么? 或者發電機不適用於這種情況。 這有點令人困惑

代碼可以在我的 github 存儲庫中進行修改,如果您沒有 gpu,可以在 google colaboratory 中使用以運行。 事實上,在我的小東芝衛星上,只有 2 GB 和 300 cuda 的 nvidia gpu 運行良好

在我的 git 上完成代碼

代碼組織為 jupyter 筆記本,但在這里我添加代碼遷移學習基於https://www.tensorflow.org/tutorials/images/transfer_learning

創建生成器:

test_base_dir = '.'
test_dir = os.path.join( test_base_dir, 'test')
test_datagen_2 = ImageDataGenerator( rescale = 1.0/255. )
test_generator = test_datagen_2.flow_from_directory( test_dir,
                                                     batch_size  = 1,
                                                     class_mode  = binary', 
                                                     target_size = (image_size, image_size))

對於預測:

   filenames = test_generator.filenames
   nb_samples = len(filenames)
   y_predict = model.predict_generator(test_generator,steps = 
   nb_samples)
   y_predict

我使用 numpy 最終使用混淆矩陣度量


from sklearn.metrics  import confusion_matrix
cm = confusion_matrix(y_true=test_generator.labels, y_pred=y_predict_rounded)
cm

手動驗證改為:

def prediction(path_img):
img = image.load_img(path_img, target_size=(150,150))
x = image.img_to_array(img)
x = x/255.
x = np.expand_dims(x, axis=0)
classes = model.predict(x)
plt.imshow(img)
if classes > 0.5:
    print(path_img.split('/')[-1]+' is a dog')
else:
     print(path_img.split('/')[-1]+' is a cat')   
return classes

我通過以下方式使用:

y_pred_m = []
files=[]
for filename in os.listdir(test_dir):
    file = test_dir+'/'+filename
    for item in os.listdir(file):
        file2 = file+'/'+item
        if file2.split('.')[-1]=='jpg':
            files.append(file2)

預測如下:

prediction_array = [prediction(img) for img in files]

np.round(prediction_array, decimals=0)

預期的結果應該是具有與訓練相似的准確度級別的混淆矩陣。 由於單獨驗證每個示例似乎沒有預測錯誤,但是 model.predict_generate 似乎出錯了。

問題是默認_flow_from_directory_使用 shuffle = True 如果 shuffle 為 False,則預測是正確的。 然而,使用驗證數據集來評估訓練似乎是正確的,即使 shuffle 為 True。 我已經更新了 git 以填充這些更改

# Flow validation images in batches of 20 using test_datagen generator
test_generator =  test_datagen_2.flow_from_directory( test_dir,
                                                  batch_size  = 1,
                                                  class_mode  = 'binary', 
                                                  target_size = (image_size, 
image_size),
                                                  shuffle = False)

暫無
暫無

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

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