Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get order of class labels for Keras predict function

I have the same question as this question here in SO. However, when I tried using the probas_to_classes() utility function, **it is already missing in the current code:

"""Numpy-related utilities."""
from __future__ import absolute_import

import numpy as np


def to_categorical(y, num_classes=None):
    """Converts a class vector (integers) to binary class matrix.

    E.g. for use with categorical_crossentropy.

    # Arguments
        y: class vector to be converted into a matrix
            (integers from 0 to num_classes).
        num_classes: total number of classes.

    # Returns
        A binary matrix representation of the input.
    """
    y = np.array(y, dtype='int').ravel()
    if not num_classes:
        num_classes = np.max(y) + 1
    n = y.shape[0]
    categorical = np.zeros((n, num_classes))
    categorical[np.arange(n), y] = 1
    return categorical


def normalize(x, axis=-1, order=2):
    """Normalizes a Numpy array.

    # Arguments
        x: Numpy array to normalize.
        axis: axis along which to normalize.
        order: Normalization order (e.g. 2 for L2 norm).

    # Returns
        A normalized copy of the array.
    """
    l2 = np.atleast_1d(np.linalg.norm(x, order, axis))
    l2[l2 == 0] = 1
    return x / np.expand_dims(l2, axis)

Do you have any other alternatives in order to get the classes associated with the output of the model?

like image 861
noobalert Avatar asked Dec 19 '22 08:12

noobalert


1 Answers

As correctly presented by matias, you should use np.argmax function

But since you usually deal with inputs in batches your prediction output will most likely be a matrix. You can deal with it by applying argmax to each one individually, but I think it's better to use the axis argument.

In short:

predictions = model.predict(Input)
classes = np.argmax(predictions, axis=1)

In not so short, a runnable code you can test:

from __future__ import print_function
import keras
import numpy as np
from keras.datasets import mnist



(x_train, y_train), (x_test, y_test) = mnist.load_data()
num_classes = 10
y_test_cat = keras.utils.to_categorical(y_test, num_classes)
print(y_test)
print(np.argmax(y_test_cat,axis=1))

error = y_test-np.argmax(y_test_cat,axis=1)

all_zero = not np.any(error)

print (all_zero)

Explanation:

First all those keras and numpy imports and print function (because why not)

from __future__ import print_function
import keras
import numpy as np
from keras.datasets import mnist

Then load mnist data

(x_train, y_train), (x_test, y_test) = mnist.load_data()

After that, change your target classes to one hot encoding with to_categorical

y_test_cat = keras.utils.to_categorical(y_test, num_classes)

Then go back to the classes you need:

print(np.argmax(y_test_cat,axis=1))

In this example, y_test_cat would be the output of your model.predict() function, so that's how you'd pass it to argmax to recover your class from highest probability prediction.

Now, just to make sure our classes "predictions" are exactly the original classes (as they should be since the "predictions" were already the right classes) the error is computed. and printed

error = y_test-np.argmax(y_test_cat,axis=1)

all_zero = not np.any(error)

print (all_zero)
like image 117
maz Avatar answered Dec 20 '22 21:12

maz