我有一个训练有素的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}
顺便说一句,如果您正在使用顺序模型,则应使用predict_classes而不是predict以获取结果。