Search code examples
pythonopencvmachine-learningkerasone-hot-encoding

How to compare two one hot encoded lists?


I'm training a CNN on a large dataset of images with two classes, and I've done one hot encoding to my validation classes (y_test):

y_test = to_categorical(y_test, num_classes=2)

I want to compare these with the predictions my classifier makes, which I've also one hot encoded like:

y_pred = model.predict_classes(x_test)
y_pred = to_categorical(y_pred, num_classes=2)

What I want to accomplish with this comparison is to find where my classifier has made a mistake, and to save the image that's been classified incorrectly in a new folder. But I don't think I'm doing the comparison right at all:

for i in range(0, len(y_test)):
if y_pred[i].any() != y_test[i].any():
    image = x_test_copy[i]
    path = 'path'
    cv2.imwrite(os.path.join(path , str(i)+'.jpg'), image)

Does somebody know what I am doing wrong?


Solution

  • I am assuming you are using the Keras to_categorical method which computes a one-hot encoding matrix such that each row is the one-hot encoded label of a training sample. In that case, your comparison is not correct. You need to first find where the elements are not equal, then impose that if any of them are not correct, you write out the image to file.

    Therefore, first find all positions where the one-hot encoding vector do not correspond to each other: y_pred[i] != y_test[i] and only then do you impose the any method on it that checks for any elements that aren't equal (y_pred[i] != y_test[i]).any(). This means your if statement needs to change:

    for i in range(0, len(y_test)):
        if (y_pred[i] != y_test[i]).any(): # Change
            image = x_test_copy[i]
            path = 'path'
            cv2.imwrite(os.path.join(path , str(i)+'.jpg'), image)