简体   繁体   中英

Get predicted values with model.predict using ImageDataGenerator - keras 2.1.0 (deep learning)

I am trying to get all correct and incorrect predicted values (I want predict class of images)

So, my code is:

#Load the trained model
loaded_model= tf.keras.models.load_model('C:/Desktop/data/model.h5')

#ImageDataGenerator for reading data from directory
test_generator = ImageDataGenerator().flow_from_directory(
    'C:/Desktop/data/test',
    target_size=(img_width, img_height),
    batch_size=batch,
    class_mode='categorical')

#Predicting the classes of images
predictions = loaded_model.predict(test_generator)
print('predictions shape:', predictions.shape)
print('predictions:', predictions)

Output for predictions.shape is (568, 2) and for predictions :

[[4.5327284e-11 1.0000000e+00]
 [1.0000000e+00 3.6808674e-11]
 [1.3124708e-03 9.9868757e-01]
 ...
 [1.0000000e+00 2.0863072e-11]
 [9.3747419e-01 6.2525854e-02]
 [1.0000000e+00 4.2702163e-14]]

But I need to get predictions like data which can be used to confusion matrix

在此处输入图像描述

So I need to have values like:

24 predictions for class 1 was correct
 5 predictions for class 1 was incorrect
 1 prediction for class 0 was correct
 7 predictions for class 0 was incorrect

EDIT:

I am trying to use code from tutorial but I am getting an error:

AttributeError: 'DirectoryIterator' object has no attribute 'class_indicies'

My code now:

test_generator = ImageDataGenerator().flow_from_directory(
        'C:/Desktop/data/test',
        target_size=(img_width, img_height),
        batch_size=batch,
        class_mode='categorical',
        shuffle=False)

predictions = loaded_model.predict(test_generator, steps=test_generator.batch_size, verbose=1)
predicted_class_indices = np.argmax(predictions, axis=1)
print('predictions: ', predicted_class_indices)

labels = test_generator.class_indicies #here I am getting an error
labels = dict((v,k) for k,v in labels.items())
predictionss = [labels[k] for k in predicted_class_indices]
print(predictionss)

Based on your shape, I'm assuming you have 2 classes.

#Load the trained model
loaded_model= tf.keras.models.load_model('C:/Desktop/data/model.h5')

#ImageDataGenerator for reading data from directory
test_generator = ImageDataGenerator().flow_from_directory(
    'C:/Desktop/data/test',
    target_size=(img_width, img_height),
    batch_size=batch,
    class_mode='categorical')

#Predicting the classes of images
predictions = loaded_model.predict_generator(test_generator)
print('predictions shape:', predictions.shape)
print('predictions:', predictions)

# getting the labels

pred_labels = list(np.argmax(predictions, axis=-1))

# getting true labels
true_labels = test_generator.classes

# get the confusion plot

cm = sklearn.metrics.confusion_matrix(true_labels, pred_labels)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM