keras 如何从custom_data_generator获取混淆矩阵y_true

y3bcpkx1  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(124)

如何从custom_data_generator获取?
我知道通过model.predict可以得到y_pred数据,但我不知道如何得到y_true。
我发现了一些文章,但他们介绍了Keras的ImageDataGenerator

data_generator = CustomDataGenerator(image_folders, label_folders, **params)
model = unet()
model_checkpoint = ModelCheckpoint('test1.hdf5', monitor='loss',verbose=1, save_best_only=True)
train_history=model.fit_generator(data_generator, steps_per_epoch=200,epochs=30,callbacks=[model_checkpoint])

y_true = []  
y_pred = []  
y_pred = model.predict(train_history)
y_pred_cm = np.argmax(y_pred, axis=1)

cal(y_true, y_pred_cm)

个字符

hfyxw5xn

hfyxw5xn1#

从所提供的代码片段来看,y_true似乎是在CustomDataGenerator类中生成的。CustomDataGenerator类的get_data方法生成X和y数据批次,其中y表示基础真值标签。
若要访问y_true数据,可以修改CustomDataGenerator类以将标签存储在单独的列表中。

from keras.utils import to_categorical

class CustomDataGenerator(Sequence):
    def __init__(self, image_folders, label_folders, **params):
        # Initialize the generator

    def get_data(self, batch):
        # Generate X and y data for a batch

    def __getitem__(self, index):
        # Get a batch of data

    def on_epoch_end(self):
        # Execute operations at the end of each epoch

    def generate_y_true(self):
        y_true = []
        for i in range(len(self.image_folders)):
            # Generate the ground truth labels for each image folder
            label_folder = self.label_folders[i]
            # Generate the labels based on your desired logic
            # Here, 'to_categorical' is used assuming the labels are categorical
            labels = to_categorical(generate_labels(label_folder), num_classes=self.n_classes)
            y_true.append(labels)
        return np.concatenate(y_true, axis=0)

字符串
在这个修改后的CustomDataGenerator类中,generate_y_true方法为所有图像文件夹生成真实值标签。它假设存在一个函数generate_labels,该函数根据提供的label_folder生成标签。您可以使用自己的逻辑替换generate_labels以生成地面实况标签。
要获得y_true,可以在训练模型后调用generate_y_true方法:

y_true = data_generator.generate_y_true()

相关问题