如何让预测发生器在深度学习中进行有序预测?

2024-04-19 17:07:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在设计一个深度学习模型来分类图像,并使用下面的代码来检查预测性能。它将图像的标签与预测的类进行比较,然后返回错误谓词。在

我使用的是顺序模型,这段代码运行得很好。但是,现在它不能正常工作了。。predict_generator更改图像的顺序。(预测类和验证生成器的顺序不同)

我想按照validation_generator中图像的相同顺序进行预测,你知道我该怎么做吗??在

validation_generator = validation_datagen.flow_from_directory(
            validation_dir,
            target_size=(image_size, image_size),
            batch_size=val_batchsize,
            class_mode='categorical',
            shuffle=False)

    # Get the filenames from the generator
    fnames = validation_generator.filenames

    # Get the ground truth from generator
    ground_truth = validation_generator.classes

    # Get the label to class mapping from the generator
    label2index = validation_generator.class_indices

    # Getting the mapping from class index to class label
    idx2label = dict((v,k) for k,v in label2index.items())

    # Get the predictions from the model using the generator
    predictions = model.predict_generator(validation_generator, steps=validation_generator.samples/validation_generator.batch_size,verbose=1)
    predicted_classes = np.argmax(predictions,axis=1)

    errors = np.where(predicted_classes != ground_truth)[0]
    print("No of errors = {}/{}".format(len(errors),validation_generator.samples))

    # Show the errors
    for i in range(len(errors)):
        pred_class = np.argmax(predictions[errors[i]])
        pred_label = idx2label[pred_class]

        title = 'Original label:{}, Prediction :{}, confidence : {:.3f}'.format(
            fnames[errors[i]].split('/')[0],
            pred_label,
            predictions[errors[i]][pred_class])

        original = load_img('{}/{}'.format(validation_dir,fnames[errors[i]]))
        plt.figure(figsize=[7,7])
        plt.axis('off')
        plt.title(title)
        plt.imshow(original)
        plt.show()

型号代码

^{pr2}$

请帮帮我。。在


Tags: thefrom图像sizeget顺序pltgenerator