如何获得由keras训练模型预测的类别的分类输出?

问题描述 投票:0回答:1

我有一个训练有素的CNN模型,该模型应该预测以下5种情绪之一:愤怒,厌恶,恐惧,中立或悲伤。我正在使用

ImageDataGenerator(rescale = 1./255,
                      shear_range=0.1,
                      zoom_range=0.1,
                      horizontal_flip=True)

low_from_directory('../large-dataset-copy/training/',
                            color_mode='grayscale',
                            target_size=(48,48),
                            class_mode = 'categorical',
                            batch_size = 32)

用于读取和转换图像。

训练后,我不知道如何提取模型预测的对应类。我使用下面的代码读取和转换图像:

def test_data(img_path):
    img_array = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
    new_img = cv2.resize(img_array,(48,48))
    return new_img.reshape(-1,48,48,1)

img = test_data('Abdullah_Gul_0006.jpg')
emotionClassifier = k.models.load_model('moedel.h5',compile=False)
emotion = emotionClassifier.predict(img)
print(emotion)

这是我得到的输出always[[0。 1. 0. 0. 0。]]

也来自[[print(test_data.class_indices)的输出为{'anger':0,'disgust':1,'fear':2,'neutral':3,'sadness':4}

keras label cnn
1个回答
0
投票
由于在flow_from_directory方法中将类模式设置为“类别”,结果将以2D一键编码标签数组的形式返回。您得到的结果[0,1,0,0,0]表示图片标签是“令人反感的”,因为它是class_indices列表中的第二个。

顺便说一句,如果您正在使用顺序模型,则应使用predict_classes而不是predict以获取结果。

© www.soinside.com 2019 - 2024. All rights reserved.