我为ImageDataGenerator创建了分类器,使用flow_from_directory创建了它,例如:

training_imGenProp = ImageDataGenerator(
    rescale=1. / 255,
    width_shift_range=0.02,
    height_shift_range=0.02,
)

training_imGen = training_imGenProp.flow_from_directory(
    'Location/to/train/images',
    target_size=(74, 448),
    batch_size=batchSize,
    class_mode='binary',
)


运行它时,在以下代码片段的最后一行中显示TypeError: unhashable type: 'numpy.ndarray'

predictions = classifier.predict_generator(testing_imGen)
predictions = (predictions >= 0.5).astype(int)
label_map = (training_imGen.class_indices)
inverted_label_map = dict((v, k) for k, v in label_map.items())  #flip k,v
predictions = [label_map[k] for k in predictions]


可能是什么原因造成的?

此外,我应如何继续从该分类器结果生成混淆矩阵?

这个:

tn, fp, fn, tp = confusion_matrix(label_map, predictions).ravel()


引发错误ValueError: Found input variables with inconsistent numbers of samples: [2,(663, 1)]

最佳答案

因此,经过长时间的摸索,问题出在最后一个列表理解之内,因为predictions是列numpy数组,例如[[1],[1],[0], ..., [0]]

因此,仅需要访问以下整数值:

textual_predictions = [s_label_map[k] for k in predictions.T[0]]


此外,创建混淆矩阵时还会发生另一个错误,该错误矩阵具有错误的输入变量:

tn, fp, fn, tp = confusion_matrix(testing_imGen.classes, predictions.T[0]).ravel()

关于python - 使用Keras ImageDataGenerator预测进行索引会产生无法散列的类型错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51682648/

10-11 22:14
查看更多