2016-06-23 135 views
9

我使用flow_from_directory得到訓練,從一個文件夾設置的結構如下:如何在Keras培訓多個班時獲得標籤ID?

train 
    class1 
    class2 
    class3 
    ... 

發電機被稱爲是如下:

train_generator = train_datagen.flow_from_directory( 
     train_data_dir,        
     target_size=(img_height, img_width),   
     batch_size=32,        
     class_mode='categorical') 

我沒有設置參數classes,但我期望按字母順序獲取標籤。

classes: optional list of class subdirectories (e.g. ['dogs', 'cats']). Default: None. If not provided, the list of classes will be automatically inferred (and the order of the classes, which will map to the label indices, will be alphanumeric).

但是,當我分類訓練圖像(用於檢查哪些標籤正在返回)時,我沒有得到任何特定的排序。訓練進行得很順利(準確性達到85%),並且在對來自同一班級的圖像進行分類時與輸出標籤保持一致。

如何推斷由flow_from_directory生成的標籤號碼並將它們映射到類別?

+0

這個問題是固定的一個例子[此拉請求( https://github.com/fchollet/keras/pull/3052)。 –

回答

13

可以看到哪些類對應於整數看着變量ImageDataGenerator.class_indices

下面是關於如何使用它

def build(source=None): 
     datagen = ImageDataGenerator(rescale=1./255) 
     data_generator = datagen.flow_from_directory(
     source, # this is the target directory 
     target_size=(150, 150), # all images will be resized to 150x150 
     batch_size=11, 
     class_mode='sparse') 
     class_dictionary = data_generator.class_indices 
    return data_generator, class_dictionary 
+0

太棒了!謝謝!! –

+0

我得到的錯誤:'ImageDataGenerator'對象沒有'class_indices' –

+0

的屬性它可能不言而喻,但如果你不想在這種情況下運行'build()'函數時自動返回class_dictionary,你可以做:'全球class_dictionary',然後'class_dictionary = data_generator.class_indices'這將允許您訪問全局的class_dictionary。 –