如何使用分割图像数据集从predict_generator输出中进行classification_report和confusion_matrix? (Keras Tensorflow)

时间:2019-05-06 15:16:10

标签: python keras deep-learning

我为ImageDataGenerator创建了一个分类器,该分类器是使用flow_from_directory创建的,用于加载我的数据集,然后执行模型和预测的训练。

我的问题是如何从classifier.predict_generator的输出中获取指标(即acc,召回,FPR等)?

如果我没记错的话,使用confusion_matrix和category_report方法会很有帮助。图像(.tif文件)位于

/data/test/image ---> RGB images 
/data/test/label ---> Binary mask images
/data/train/image ---> RGB images 
/data/train/label ---> Binary mask images

图像如下: RGB image Mask image。 Forecast_generator方法返回的图像如下: Predicted image

我已经尝试过以下代码来生成混淆矩阵,但是无法正常工作:

predicted_classes_indices = np.argmax(results,axis=1)
labels = (image_generator.class_indices)
labels = dict((v, k) for k, v in labels.items())
predictions = [labels[k] for k in predicted_classes_indices]
cm = confusion_matrix(labels, predicted_classes_indices)

所有代码:

from redeUnet import get_unet
import matplotlib.pyplot as plt
import numpy as np
import os
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.models import model_from_json
from tensorflow.python.keras.callbacks import EarlyStopping
from tensorflow.python.keras.callbacks import ModelCheckpoint

PATH_TRAIN = "..\\data\\train\\"

btSize = 4
alt = 256 # image row
larg = 256 # image col
image_folder = 'image'
mask_folder = 'label'
image_color_mode = 'rgb'
mask_color_mode = 'grayscale'
clMode =  'None' #'binary'
epocas = 5 
qtdPatience = 60

'''
Data augmentation
'''
data_gen_args = dict(featurewise_center=False,
                     samplewise_center=False,  # set each sample mean to 0
                     featurewise_std_normalization=False,  # divide inputs by std of the dataset
                     samplewise_std_normalization=False,  # divide each input by its std
                     zca_whitening=False,  # apply ZCA whitening
                     rotation_range=40,  # randomly rotate images in the range (degrees, 0 to 180)
                     zoom_range = 0.2, # Randomly zoom image 
                     width_shift_range=0.2,  # randomly shift images horizontally (fraction of total width)
                     height_shift_range=0.2,  # randomly shift images vertically (fraction of total height)
                     horizontal_flip=True,  # randomly flip images
                     vertical_flip=False,
                     rescale=1./255,
                     validation_split = 0.2)  # randomly flip images
train_image_datagen = ImageDataGenerator(**data_gen_args)
train_mask_datagen = ImageDataGenerator(**data_gen_args)

'''
DATASET prepare and load (20% Validation)
'''
# Load RGB images TRAINING
image_generator = train_image_datagen.flow_from_directory(PATH_TRAIN, 
                                                          classes = [image_folder],
                                                          class_mode = None,
                                                          color_mode = image_color_mode,
                                                          target_size = (larg, alt),
                                                          batch_size = btSize,
                                                          save_to_dir = None,
                                                          shuffle = False,
                                                          subset = 'training',
                                                          seed = 1)
# Load BINARY (Mask) images TRAINING
mask_generator = train_mask_datagen.flow_from_directory(PATH_TRAIN, 
                                                          classes = [mask_folder],
                                                          class_mode = None,
                                                          color_mode = mask_color_mode,
                                                          target_size = (larg, alt),
                                                          batch_size = btSize,
                                                          save_to_dir = None,
                                                          shuffle = False,
                                                          subset = 'training',
                                                          seed = 1)

train_generator = zip(image_generator, mask_generator)

#-------------------------------------------------
# VALIDATION images RGB
valid_image_generator = train_image_datagen.flow_from_directory(PATH_TRAIN, 
                                                          classes = [image_folder],
                                                          class_mode = None,
                                                          color_mode = image_color_mode,
                                                          target_size = (larg, alt),
                                                          batch_size = btSize,
                                                          save_to_dir = None,
                                                          shuffle = False,
                                                          subset = 'validation',
                                                          seed = 1)

# VALIDATION images BINARY (Mask)
valid_mask_generator = train_mask_datagen.flow_from_directory(PATH_TRAIN, 
                                                          classes = [mask_folder],
                                                          class_mode = None,
                                                          color_mode = mask_color_mode,
                                                          target_size = (larg, alt),
                                                          batch_size = btSize,
                                                          save_to_dir = None,
                                                          shuffle = False,
                                                          subset = 'validation',
                                                          seed = 1)
valid_generator = zip(valid_image_generator, valid_mask_generator)
#-------------------------------------------------

'''
RUN TRAINING
'''
# Get UNET
classificador = get_unet(larg, alt, 3)
classificador.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy']) #metrics = ['accuracy', minhaMetrica])

# Salvando o Modelo e Pesos (Best Model, StopEarly)
es = EarlyStopping(monitor = 'val_loss', mode = 'min', verbose = 1, patience = qtdPatience)
mc = ModelCheckpoint('best_polyp_unet_model.h5', monitor = 'val_loss', verbose = 1, save_best_only = True)


history = classificador.fit_generator(train_generator, 
                            steps_per_epoch = image_generator.n // btSize,
                            validation_data = valid_generator,
                            validation_steps = valid_image_generator.n // btSize,
                            epochs = epocas, callbacks=[es, mc])

resultados = classificador.predict_generator(valid_generator, 
                                           steps = valid_image_generator.n,
                                           verbose = 1)
#-------------------------------------------------

#HOW TO GET THE METRICS?

predicted_classes_indices = np.argmax(resultados,axis=1)
labels = (image_generator.class_indices)
labels = dict((v, k) for k, v in labels.items())
predictions = [labels[k] for k in predicted_classes_indices]
cm = confusion_matrix(ground_truth, predicted_classes)

#-------------------------------------------------

我在此行中收到一条错误消息: predictions = [labels[k] for k in predicted_classes_indices]

  

错误:无法散列的类型:'numpy.ndarray'

当我通过运行以下命令检查预测的输出变量(“ resultados”)时:resultados.shape。显示:

(480, 256, 256, 1)

通过U-net预测生成了480张图像。

但是我如何转换此信息以使其与“ confusion_matrix”或“ classification_report”匹配?我认为这比较困难,因为存在细分问题。

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

您需要扁平化您的预测和基本事实 (y):

import numpy as np
predictions_flat = predictions.flatten()
y_flat = y.flatten()

然后你可以在扁平矩阵上运行分类报告和混淆矩阵

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix


print('Train report', classification_report(y_flat, predictions_flat))
print('Train conf matrix', confusion_matrix(y_flat, predictions_flat))