簡體   English   中英

Keras flow_from_directory 自動編碼器訓練

[英]Keras flow_from_directory autoencoder training

我正在嘗試在 Keras 中訓練自動編碼器,並且我自己的數據集組織如下:

  • 數據集:
    • 火車:
      • img1.jpg
      • ETC
    • 有效的:
    • 測試:

我已經了解了如何將 flow_from_directory 用於分類任務,其中數據集被組織在標簽和子目錄中。 在這種情況下,所有圖像都在同一個文件夾中,沒有任何 label。 執行代碼時,出現以下錯誤:“找到屬於 0 個類的 0 個圖像。”

這是我的代碼片段:

train_path = 'dataset/train/'
train_gen = train_data_gen.flow_from_directory(
    train_path,
    class_mode = 'Input',
    target_size = IMAGE_SIZE,
    color_mode = 'grayscale',
    batch_size = BS,
    seed = SEED,
    shuffle = 'Yes'
)

我該如何解決?

問題在於數據的結構。 當您將 flow_from_directory 與目錄作為 train_path = 'dataset/train/' 一起使用時,它會在該目錄中查找將是您的類的子目錄。 從您顯示的目錄結構來看,dataset/train 中沒有 class 子目錄,因此生成器不返回任何文件和類。 例如,假設您正在構建一個分類器來對狗和貓進行分類。 在您的訓練目錄中,您將有兩個子目錄,一個用於保存貓的圖像,另一個用於保存狗的圖像。 flow_from_directory 有一個參數 class_mode,描述如下

class_mode: One of "categorical", "binary", "sparse", "input", or None.
 Default: "categorical". Determines the type of label arrays that are
 returned: - "categorical" will be 2D one-hot encoded labels, - "binary" 
will be 1D binary labels, "sparse" will be 1D integer labels,
 - "input" will be images identical to input images (mainly used to 
work with autoencoders). - If None, no labels are returned 
(the generator will only yield batches of image data, 
which is useful to use with model.predict()). 
Please note that in case of class_mode None,
 the data still needs to reside in a subdirectory of directory for it to work correctly.

所以在 flow_from_directory 中設置 class_mode='input'。 這樣你就可以得到沒有任何標簽的圖像。

我解決了這個問題; 該解決方案可能對某人有用:我添加了一個存儲所有圖像的子文件夾。 所以數據集的結構是這樣的:

數據集:

  • 火車:
    • 圖片:
      • 圖片.jpg...

train_path = '數據集/訓練/'

正如@Gerry P 建議的那樣,我設置了 class_mode=None

暫無
暫無

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

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