[英]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)我正在尝试获取所有正确和不正确的预测值(我想预测图像的 class)
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
: Output 用于predictions.shape
。形状为(568, 2)
和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
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.根据您的形状,我假设您有 2 个班级。
#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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.