trying to understand the flow_from_directory( class_mode= ) parameter

3,593 views
Skip to first unread message

Chris Albertson

unread,
Sep 8, 2017, 5:01:10 AM9/8/17
to Keras-users
I am trying to understand the flow_from_directory( class_mode= ) parameter.

Reading the documentation, I see that flow_from_diectory() infers class labels from the sub directory structure.  So lets assume a simple four class directory tree .../cat, .../dog, .../truck, .../person

From the above directory structure I can see how "1D one hot" or "binary" labels are generated

But what if with the above directory structure I specify class_mode = categorical?  How does it map a 4 item list to a 2D one hot encoding?

And what about mode=sparse.  How is a four item last mapped to an integer?

A the subdirectories are equivalent to a list of strings [dog, cat, truck, person] and I'm asking how this list could be mapped to a 2D array or an integer.  The 2D one-hot class_mode seem the only onethatcould work

I know, I'm missing something.

Chris Albertson

unread,
Sep 8, 2017, 2:34:50 PM9/8/17
to Keras-users


Some more information:  I tried experiment in ipython where I changed the class_mode and looked at the resulting class_indices dictionary.  (Sorry for the huge font but none of the edit tools seem to be able change it, seem to be a feature  of this forum)

In the below example I use "sparse" but I get the exact same dictionary no matter if I use categorical or binary.

In [13]: h2 = g.flow_from_directory(classes=['a', 'b', 'c', 'd', 'e'], class_mode='sparse', directory='/data')

Found 0 images belonging to 5 classes.


In [14]: h2.class_indices

Out[14]: {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}


alexand...@gmail.com

unread,
Sep 13, 2017, 7:11:52 PM9/13/17
to Keras-users
putting model = 'sparse returns the output formatted in the in the way thats required if your using 'spare_categorical_crossentropy' as a loss. In other words, it should return data thats been encoded as a 1-dimensional vector with integer labels [0,1,2,3,4,....n] where n is the number of classes, similar to the output from scikit-learn's LabelEncoder.

'categorical'  mode should return one-hot 2-dimensional output identical to what you would have gotten if you'd used pad_sequences on a [0,1,2,3,4,....n] vector like the one I mentioned above.

I think 'binary' should require a 1d vector with only labels 0 and 1. Ie similar to sparse in it the dimensionality (1d) but similar to categorical in the possible values for the integers. (eg   [0,1, 0, 1, 1, 1,0 ,...] )

Since 'sparse' and 'binary' would require the same sort of formats for the labels in the case where there are only 2 labels (and you shouldnt be using binary if there are more than 2 anyway), i imagine the they might share many of the same methods.

rushi.kh...@gmail.com

unread,
Apr 14, 2018, 8:20:31 AM4/14/18
to Keras-users

In [13]: h2 = g.flow_from_directory(classes=['a', 'b', 'c', 'd', 'e'], class_mode='sparse', directory='/data')

Found 0 images belonging to 5 classes.



have u solved this problem...if u then please tell me


Reply all
Reply to author
Forward
0 new messages