【问题标题】:Validation Generator with Previously Split Data Keras具有先前拆分数据的验证生成器 Keras
【发布时间】:2021-06-12 07:34:12
【问题描述】:

我有三个数据框用于 ML 程序的训练、验证和测试。它们是从读取 csv 的 pandas 数据帧中分离出来的。以下是该文件的示例:

id,label
904797024fe2c8ebe4c12f54baf34c62c05ec1ff,1
0ad0a93569e96a95ed1e777b983452e9dbd445f9,0

ID 是不带扩展名的图像的文件名,.tif

以前,我将训练和验证数据放在同一个数据帧中,但为了避免差异,我将这些部分分成两个数据帧。

这是我之前的代码:

train_datagen = ImageDataGenerator(rescale = 1./255,
                                  validation_split = 0.1)

test_datagen = ImageDataGenerator(rescale = 1./255)

train_val_path = "../input/train/"

train_generator = train_datagen.flow_from_dataframe(
                dataframe = df_train_val,
                directory = train_val_path,
                x_col = "id",
                y_col = "label",
                subset = "training",
                target_size = (96, 96),
                batch_size = 32,
                class_mode="binary",
                validate_filenames=False
                )

validation_generator = train_datagen.flow_from_dataframe(dataframe = df_train_val,                                                     directory = train_val_path,
                                                        x_col = "id",
                                                        y_col = "label",
                                                        subset = "validation",
                                                        target_size = (96, 96),
                                                        batch_size = 32,
                                                        class_mode="binary",
                                                        validate_filenames=False
                                                        )

正如您在第一行中看到的,ImageDataGenerator 的验证拆分为 0.1。如果我已经进行了拆分,我将如何调整此代码以使其正常工作?

【问题讨论】:

    标签: python pandas machine-learning keras


    【解决方案1】:

    默认情况下,验证拆分为 0.0,即不采集样本进行验证。

    train_datagen = ImageDataGenerator(rescale = 1./255)
    
    test_datagen = ImageDataGenerator(rescale = 1./255) # Not required
    
    train_val_path = "../input/train/"
    
    train_generator = train_datagen.flow_from_dataframe(
                    dataframe = df_train_val,
                    directory = train_val_path,
                    x_col = "id",
                    y_col = "label",
                    subset = None,         # Because validation split is not specified
                    target_size = (96, 96),
                    batch_size = 32,
                    class_mode="binary",
                    validate_filenames=False
                    )
    
    validation_generator = train_datagen.flow_from_dataframe(dataframe = df_train_val,   # Pass test dataframe                                                     directory = train_val_path,
                                                            x_col = "id",
                                                            y_col = "label",
                                                            subset = None,       # Because validation split is not specified
                                                            target_size = (96, 96),
                                                            batch_size = 32,
                                                            class_mode="binary",
                                                            validate_filenames=False
                                                            )
    

    【讨论】:

    • 感谢您的回答!澄清一下,验证生成器会将正确的值传递给 fit 函数,即使它没有“验证”子集,也没有指定验证拆分百分比,对吧?
    • 我假设您有两个单独的数据框用于训练和验证。所以对于 val_generator 传递 val_df 和对于 train_generator 传递 train_df。两个生成器的数据框不能相同,否则它们将生成相同的值。
    猜你喜欢
    • 2019-09-28
    • 2019-03-14
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2016-04-28
    相关资源
    最近更新 更多