keras flow from directory显示label index

keras imageDataGenerator封装了很多,其中有一个flow from directory函数能够直接把文件夹里面的子文件夹当作label,然后做一个分类的训练。

   rootDir='/home/eric/data/scene'
    # train_path='group0_train.txt'
    # valid_path='group0_valid.txt'
    
    trainfilepath=os.path.join(rootDir,train_path)
    trainDf=pd.read_csv(trainfilepath) #加载papa.txt,指定它的分隔符是 \t
    trainDf.rename(columns={'image':"filename",'label':'class'},inplace=True)

    validfilepath=os.path.join(rootDir,valid_path)
    validDf=pd.read_csv(validfilepath,header=None) #加载papa.txt,指定它的分隔符是 \t
    validDf.rename(columns={0:"filename",1:'class'},inplace=True)


    datagen1 = ImageDataGenerator(
        rescale=1. / 255,
        shear_range=0.2,
        zoom_range=0.2,
        rotation_range=90,
        width_shift_range=0.2,
        height_shift_range=0.2,
        horizontal_flip=True)

    datagen2 = ImageDataGenerator(rescale=1. / 255)

    # train_generator = datagen1.flow_from_directory(
    #     ptrain,
    #     target_size=(size, size),
    #     batch_size=batch,
    #     class_mode='categorical')
    train_generator = datagen1.flow_from_dataframe( dataframe=trainDf,
                                                              directory=rootDir ,
                                                              x_col="filename",
                                                              y_col="class",
                                                              subset="training",
                                                            #   classes=labels,
                                                              target_size=[size, size],
                                                              batch_size=batch,
                                                              class_mode='categorical')
    

    validation_generator = datagen2.flow_from_dataframe(
                                                        dataframe=validDf,
                                                              directory=rootDir,
                                                              x_col="filename",
                                                              y_col="class",
                                                              subset="training",
                                                            #   classes=labels,
                                                              target_size=[size, size],
                                                              batch_size=batch,
                                                              class_mode='categorical')

上面的代码有一个问题,你不知道系统怎么把label 映射为index的,所以在做predict的时候就不知道怎样对应label了,其实是可以输出的:

print(train_generator.class_indices)

这样就能够把系统的class和index对应关系以字典的形式输出了。

参考文献

[1]. Keras flow_from_directory class index. https://stackoverflow.com/questions/43813393/keras-flow-from-directory-class-index

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`flow_from_dataframe` 是 Keras 中用于从 Pandas DataFrame 中读取图像数据的方法,它可以方便地将 DataFrame 中的图像数据转换为 Keras 模型可以使用的数据流。使用 `flow_from_dataframe` 的步骤如下: 1. 导入必要的库: ```python import pandas as pd from tensorflow.keras.preprocessing.image import ImageDataGenerator ``` 2. 读取包含图像路径和标签的 Pandas DataFrame: ```python df = pd.read_csv('path_to_csv_file') ``` 其中,DataFrame 中应至少包含包含图像路径的列和标签的列。 3. 创建 ImageDataGenerator 对象,并调用 `flow_from_dataframe` 方法: ```python datagen = ImageDataGenerator(rescale=1./255) train_generator = datagen.flow_from_dataframe( dataframe=df, directory='path_to_directory_containing_images', x_col='image_path_col_name', y_col='label_col_name', target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical' ) ``` 其中,`rescale` 参数用于对图像进行缩放,`directory` 参数指定包含图像文件的目录路径,`x_col` 参数指定 DataFrame 中包含图像路径的列名,`y_col` 参数指定 DataFrame 中包含标签的列名,`target_size` 参数指定图像大小,`batch_size` 参数指定批大小,`class_mode` 参数指定标签的类型,可以是 `categorical` 或 `binary`。 4. 使用生成器训练 Keras 模型: ```python model.fit( train_generator, epochs=num_epochs, steps_per_epoch=num_train_steps, validation_data=val_generator, validation_steps=num_val_steps ) ``` 其中,`train_generator` 是通过 `flow_from_dataframe` 方法创建的训练数据生成器,`num_epochs` 指定训练的轮数,`num_train_steps` 指定训练集中的批次数,`val_generator` 是通过 `flow_from_dataframe` 方法创建的验证数据生成器,`num_val_steps` 指定验证集中的批次数。 这样,就可以使用 `flow_from_dataframe` 方法从 Pandas DataFrame 中读取图像数据并训练 Keras 模型了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值