Search code examples
pythonmachine-learningdeep-learningconv-neural-networkone-hot-encoding

How to convert 2D array one hot encoding to class name label?


I have just trained a CNN model for rock-paper-scissors image classification with Tensorflow. If I try to predict a class of an image, it gives output like [[1. 0. 0.]]. How to convert it to a class name label? I use ImageDataGenerator() method with class_mode="categorical" argument.


Solution

  • You need to decode the output. A basic one can be as follows:

    import tensorflow as tf
    
    BATCH_SIZE = 3
    NUM_CLASSES = 3
    
    one_hot_encoded = tf.constant([[0, 1, 0],
                                   [1, 0, 0],
                                   [0, 0, 1]])
    
    # Compute the argmax across the columns.
    decoded = tf.argmax(one_hot_encoded, axis=1)