1

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.

1 Answers1

0

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)
Gerry P
  • 7,662
  • 3
  • 10
  • 20
  • After using this code, again I got the error 'DataFrameIterator' object has no attribute 'class_indices'. – Sheetal Nagar Dec 05 '22 at 09:34
  • I assumed you used the ImageDataGenerator to create train_datagen and val_datagen. Is that what you used? Another solution is to produce the classes variable directly from your dataframe. – Gerry P Dec 05 '22 at 14:01
  • Yes. I used ImageDataGenerator as below. – Sheetal Nagar Dec 05 '22 at 14:22
  • ```train_datagen = ImageDataGenerator(rescale=1/255, shear_range=0.2, zoom_range = 0.2, horizontal_flip = True, rotation_range = 40, width_shift_range = 0.2, height_shift_range = 0.2) val_datagen = ImageDataGenerator(rescale = 1/255)``` – Sheetal Nagar Dec 05 '22 at 14:23
  • I think the issue may be with the version of tensorflow. I think they may have changed the image data generator code so that class_indices is no longer available.. I know the intend to depreciate the image data generator in future version of tensorflow. – Gerry P Dec 05 '22 at 19:44
  • I use Google Colab for this implementation. – Sheetal Nagar Dec 06 '22 at 07:18