27
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?

Harsh Panwar
  • 335
  • 1
  • 4
  • 12
  • 2
    This can be found in the documentation, there is no need to ask a question about ti: https://keras.io/preprocessing/image/#flow_from_directory – Dr. Snoopy Dec 21 '19 at 19:19
  • @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 @ https://medium.com/@vijayabhaskar96/tutorial-image-classification-with-keras-flow-from-directory-and-generators-95f75ebe5720 – Harsh Panwar Dec 21 '19 at 19:23

2 Answers2

20

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.

roschach
  • 8,390
  • 14
  • 74
  • 124
Soheil Hosseini
  • 422
  • 6
  • 10
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

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
  • 1
    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. – Soerendip Mar 31 '21 at 21:09
  • 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](https://developers.google.com/ml-kit/vision/image-labeling/label-map). 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) – Ben Butterworth Mar 31 '21 at 21:16
  • In the case of the [tensorflow lite image classifications](https://www.tensorflow.org/lite/examples/image_classification/overview), you can find it on this page: `For a full list of classes, see the labels file in the model zip.` https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_1.0_224_quant_and_labels.zip – Ben Butterworth Mar 31 '21 at 21:17
  • I'll admit, in some cases finding these labels is hard, but I've found them everytime when I go looking long enough. – Ben Butterworth Mar 31 '21 at 21:17
  • The labels can also be inferred from the folder structure `/input_dir/label_a` etc. – Soerendip Mar 31 '21 at 21:24
  • Yup thats what I meant by `generated from the directory structure` – Ben Butterworth Apr 01 '21 at 08:08
  • 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? – mountainwater Nov 09 '22 at 08:05