2016-06-23 4 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

この場合、class_dictionaryが 'build()'関数の実行時に自動的に返されないようにしたい場合は、 do: 'global class_dictionary'、次に' class_dictionary = data_generator.class_indices'を実行すると、class_dictionaryにグローバルにアクセスすることができます。 –

関連する問題