tensorflowkerasconfusion-matrixmulticlass-classification

how to initialize y_true and y_pred for confusion_matrix and classification_report


How to initialize y_true and y_pred for confusion_matrix and classification_report? I have used flow_from_dataframe.

My code is as below:

train_set = train_datagen.flow_from_dataframe(
    train,
    path,
    x_col="image_name",
    y_col="level",
    class_mode="raw",
    color_mode="rgb",
    batch_size=32,
    target_size=(64, 64))

val_set = val_datagen.flow_from_dataframe(
    val,
    path,
    x_col="image_name",
    y_col="level",
    class_mode="raw",
    color_mode="rgb",
    batch_size=32,
    target_size=(64, 64))

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
Y_pred = model.predict(val_set)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')

print(confusion_matrix(val_set.classes, y_pred))
print('Classification Report')

class_labels = list(val_set.class_indices.keys()) 


print(classification_report(val_set.classes, y_pred, target_names=class_labels))

I get the error as AttributeError: 'DataFrameIterator' object has no attribute 'classes'.

I am trying it since a ling time. I get result for flow_from_directory but not for flow_from_dataframe.

Please guide me.


Solution

  • try this code below.NOTE in val_set = val_datagen.flow_from_dataframe( ...) set parameter shuffle=False

    errors=0
    y_pred=[]
    y_true=val_set.labels # make sure shuffle=False in generator
    classes=list(val_set.class_indices.keys()
    class_count=len(classes)
    preds=model.predict(val_set, verbose=1)
    for i, p in enumerate(preds):        
            pred_index=np.argmax(p)         
            true_index=test_gen.labels[i]  # labels are integer values        
            if pred_index != true_index: # a misclassification has occurred                                           
                errors=errors + 1
            y_pred.append(pred_index)
    tests=len(preds)
    acc=( 1-errors/tests) * 100
    msg=f'there were {errors} errors in {tests} tests for an accuracy of {acc:6.2f}'
    print(msg)
    ypred=np.array(y_pred)
    ytrue=np.array(y_true)
    cm = confusion_matrix(ytrue, ypred )
    plt.figure(figsize=(12, 8))
    sns.heatmap(cm, annot=True, vmin=0, fmt='g', cmap='Blues', cbar=False)       
    plt.xticks(np.arange(class_count)+.5, classes, rotation=90)
    plt.yticks(np.arange(class_count)+.5, classes, rotation=0)
    plt.xlabel("Predicted")
    plt.ylabel("Actual")
    plt.title("Confusion Matrix")
    plt.show()
    clr = classification_report(y_true, y_pred, target_names=classes, digits= 4) # create classification report
    print("Classification Report:\n----------------------\n", clr)