Search code examples
pythontensorflowscikit-learnconfusion-matrix

TensorFlow: Image cannot be converted to float when creating confusion matrix


I am trying to create a confusion matrix in TensorFlow but I am getting a

TypeError: Image data cannot be converted to float.

The images are predicted accurately but now I want to show the confusion matrix using matplotlib. I tried converting to to np.array() but the error is still the same.

I am following the official documentation for confusion matrix from scikit-learn. https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.tight_layout()


if result[0][0]>0.85:
    predictions.append(result[0][0])
elif result[0][1]>0.85:
    predictions.append(result[0][1])
elif result[0][2]>0.85:
    predictions.append(result[0][2])
elif result[0][3]>0.85:
    predictions.append(result[0][3])
elif result[0][4]>0.85:
    predictions.append(result[0][4])
elif result[0][5]>0.85:
    predictions.append(result[0][5])

class_names = ['Up', 'Down', 'Left', 'Right', 'Forward', 'Backward']

# label_list contains the filename e.g. hand1.jpg, hand2.jpg....

# Compute confusion matrix
cnf_matrix = tf.confusion_matrix(label_list,predictions,num_classes=6)
np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plt.figure()

# ERROR HERE
plot_confusion_matrix(cnf_matrix, classes=class_names,title='Confusion matrix, without normalization')

# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,title='Normalized confusion matrix')

plt.show()

Solution

  • I have not tested it in my PC. Your description is a little bit ambiguous for me (the line of error, etc.), but the main difference of your code and the documentation you linked is confusion_matrix(). Just try to go with confusion_matrix() of sckit-learn instead of confusion_matrix() of tensorflow (at the link, the former is used). In my opinion, it is the easiest way you can go.

    EDIT: Make your predictions like this:

    for i in range(6):
        if result[0][i] > 0.85:
            predictions.append(i)
            continue
    

    Then your predictions will not be continuous ones. Here your predictions should be integers since you are predicting class labels.