python-3.x - 我应该如何使用 mode.predict_generator 来评估混淆矩阵中的模型性能?

标签 python-3.x tensorflow machine-learning keras confusion-matrix

我正在尝试使用混淆矩阵评估常见狗和猫过滤数据集中的迁移学习模型。我的代码基于tensorflow的迁移学习教程。训练的准确率图表显示准确率高于 90%。

但是,使用生成器获取真实的 labes 并使用 model.predict_generator 获取预测数组会引发不一致的结果。首先,准确性不稳定,如果您第二次运行预测,它会改变值。其次,与单个实例上的 model.predict 相比,我通过使用 model.predict_generator 获得的预测似乎是错误的。

为了快速测试基于ImageDataGenerator的混淆矩阵,我下载了 5 张猫的图像和 5 张狗的图像。然后我从该文件夹创建了另一个生成器,并检查标签和类是否与训练相同。

两种奇怪的行为 之后,我只是使用 sklearn 指标混淆矩阵来使用 model.predict_generator 和我从生成器获得的标签作为真实标签来评估预测。

第一次运行时,我得到了 0.9 的准确率,欢呼吧!但是,如果我第二次尝试 model.predict_generator,它会抛出数组输出的其他值,并且精度会下降到 0.5。之后它就不再改变了......什么结果是正确的?为什么会改变?

我注意到你必须运行两次才能得到最终结果,但得到的结果是错误的。我编写了一些代码来单独测试每个图像,并且我的预测没有错误。那么我做错了什么?或者发电机不适用于这种情况。这有点令人困惑

代码可以在我的 github 存储库中进行破解,如果您没有 GPU,则可以在 google colaboratory 中使用代码来运行。事实上,我的小型东芝卫星在仅 2 GB 和 300 cuda 的 nvidia gpu 上运行良好

complete code at my git

代码被组织为 jupyter 笔记本,但是在这里我添加了代码 迁移学习基于https://www.tensorflow.org/tutorials/images/transfer_learning

创建生成器:

test_base_dir = '.'
test_dir = os.path.join( test_base_dir, 'test')
test_datagen_2 = ImageDataGenerator( rescale = 1.0/255. )
test_generator = test_datagen_2.flow_from_directory( test_dir,
                                                     batch_size  = 1,
                                                     class_mode  = binary', 
                                                     target_size = (image_size, image_size))

对于预测:

   filenames = test_generator.filenames
   nb_samples = len(filenames)
   y_predict = model.predict_generator(test_generator,steps = 
   nb_samples)
   y_predict

我使用 numpy 进行舍入,最终使用混淆矩阵度量


from sklearn.metrics  import confusion_matrix
cm = confusion_matrix(y_true=test_generator.labels, y_pred=y_predict_rounded)
cm

手动验证是:

def prediction(path_img):
img = image.load_img(path_img, target_size=(150,150))
x = image.img_to_array(img)
x = x/255.
x = np.expand_dims(x, axis=0)
classes = model.predict(x)
plt.imshow(img)
if classes > 0.5:
    print(path_img.split('/')[-1]+' is a dog')
else:
     print(path_img.split('/')[-1]+' is a cat')   
return classes

我按以下方式使用:

y_pred_m = []
files=[]
for filename in os.listdir(test_dir):
    file = test_dir+'/'+filename
    for item in os.listdir(file):
        file2 = file+'/'+item
        if file2.split('.')[-1]=='jpg':
            files.append(file2)

预测是:

prediction_array = [prediction(img) for img in files]

np.round(prediction_array, decimals=0)

预期结果应该是具有与训练相似的准确度水平的混淆矩阵。由于单独验证每个示例似乎预测没有错误,但是 model.predict_generate 似乎出错了。

最佳答案

问题在于默认 _flow_from_directory_ 使用 shuffle = True。如果 shuffle 变为 False,则预测是正确的。然而,即使 shuffle 为 True,使用验证数据集来评估训练似乎也是正确的。我已经更新了 git 以填充这些更改

# Flow validation images in batches of 20 using test_datagen generator
test_generator =  test_datagen_2.flow_from_directory( test_dir,
                                                  batch_size  = 1,
                                                  class_mode  = 'binary', 
                                                  target_size = (image_size, 
image_size),
                                                  shuffle = False)

关于python-3.x - 我应该如何使用 mode.predict_generator 来评估混淆矩阵中的模型性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56695299/

相关文章:

object - 如何在 tensorflow object_detection 中检查训练/评估性能

algorithm - 人类可解释的监督机器学习算法

azure - Azure 中的机器学习 : How do I publish a pipeline to the workspace once I've already built it in Python using the SDK?

python - 您可以将带有参数的函数存储在列表中,然后在 Python 中调用它们吗?

python - 如何计算一个矩阵中向量之间的欧式距离?

python - 替换嵌套循环

python - Tensorflow - 可视化预训练网络的学习过滤器

statistics - 预测泊松过程

python-3.x - 从网络摄像头 OPENCV PYTHON 移除背景

python - 如何在 Pandas 句子列中使用自动更正