what does class_mode parameter in Keras image_gen.flow_from_directory() signify?
Asked Answered
A

2

29
train_image_gen = image_gen.flow_from_directory('/Users/harshpanwar/Desktop/Folder/train',
                                               target_size=image_shape[:2],
                                               batch_size=batch_size,
                                               class_mode='binary')

In the above code snippet what does class_mode='binary' signify. I think it is for the number of categories of images. I am using this code for training a image recognition classifier in Keras to classify between 2 different categories like dog and cat. So if class_mode='binary' is for signifying two categories how do we make it for three or more?

Ayr answered 21/12, 2019 at 19:11 Comment(2)
This can be found in the documentation, there is no need to ask a question about ti: keras.io/preprocessing/image/#flow_from_directoryIwo
@MatiasValdenegro Well I tried before but wasn't able to get it. Thank you for redirecting to the correct link. And also I found a better explaination @ medium.com/@vijayabhaskar96/…Ayr
C
21

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_generator()). 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.

Chattanooga answered 17/5, 2020 at 23:34 Comment(1)
Hello, when you cite something, please give it's source too. > keras.io/api/preprocessing/imageSmallpox
C
17

Say you have N classes in your dataset. If you have 4 labels, dog (index 0), cat (1), donkey (2) and human (3), N would be 4.

Class modes:

  • "categorical": 2D output (aka. list of numbers of length N), [0, 0, 1, 0], which is a one-hot encoding (only one number is 1/ "hot") representing the donkey. This is for mutually exclusive labels. A dog cannot be a cat, a human is not a dog.
  • "binary": 1D output (aka. 1 number), which is either 0, 1, 2, 3 ... N. It is called this because it is binary if there are only two classes (IMHO this is a bad reason), source. I suggest using "binary" just for single label classification, because it documents-in-code, your intention.
  • "sparse": After digging in the code, this is the same as "binary". The logic is done with elif self.class_mode in {'binary', 'sparse'}:, and the class_mode is not used after that. I suggest using "sparse" for multilabel classification though, again because it documents-in-code, your intention.
  • "input": The label is literally the image again. So the label for an image of the dog, is the same dog picture array. If I knew more about autoencoders I might have been able to explain further.
  • None: No labels, therefore not useful for training, but for inference/ prediction.

The TensorFlow documentation is here but I think it should go into more depth for 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.


Sparse is the same as binary?:

As you can see in my search results, sparse is only checked twice (line 2 and 4 in search results). I believe the intention of "sparse" is for multi-label classification, and "binary" is designed for single-label classification (Hot-dog vs. No hotdog), but currently there is no difference, since the behaviour is the same: enter image description here

Commutative answered 31/3, 2021 at 7:14 Comment(7)
I like your answer better as the accepted answer, but both answers miss to explain where the labels come from. Is it from the file names or the folder structure etc.Wiring
Labels must be provided with the dataset or if the model is pre-trained, it comes with the model documentation. For example, the labels can be in a text file. Another example is MLKit's built-in labelling model, the labels are here. I had to scrape that website to get a json of index to label text. They can also be generated from the directory structure (e.g. 1 folder for cat, 1 folder for dog)Commutative
In the case of the tensorflow lite image classifications, you can find it on this page: For a full list of classes, see the labels file in the model zip. storage.googleapis.com/download.tensorflow.org/models/tflite/…Commutative
I'll admit, in some cases finding these labels is hard, but I've found them everytime when I go looking long enough.Commutative
The labels can also be inferred from the folder structure /input_dir/label_a etc.Wiring
Yup thats what I meant by generated from the directory structure 😇Commutative
I know this is an old thread - but what about a situation where you want to label an image with age AND gender i.e. multi-output, how can we consolidate that situation with the way folders save dogs/cats in separate folders. Is the only way to make individual age bins or is there another way?Ascetic

© 2022 - 2024 — McMap. All rights reserved.