【问题标题】:Keras: Load dataset and autocrop relevant area of imageKeras:加载数据集并自动裁剪图像的相关区域
【发布时间】:2021-03-13 18:50:03
【问题描述】:

我正在做签名验证,我想使用 Keras/OpenCV/PIL 做很多事情,但找不到相关信息。我已经使用Keras.preprocessing.image_dataset_from_directory 加载了数据集文件夹,现在需要:

  • 从存储在数据集中的图像中裁剪签名。可能存在矩形边框(或边框的一侧),并且所有图像的边框像素都不相同。
  • 调整图像大小并注意增强签名。

示例图片:

由于我在 Keras 工作,我想使用它的功能但找不到任何功能。如何在已加载的数据集中自动裁剪/提取签名?关于图像增强,我应该在这个图像预处理阶段执行此操作,还是在我正在使用的 CNN 模型中执行此操作?我是图像处理和 Keras 的新手。

此外,由于将整个训练文件夹加载为数据集,因此标签为“正版”和“伪造”。但是,一个人的真实和伪造签名有多个,并且有多个人。如何划分数据?

【问题讨论】:

    标签: python-3.x tensorflow opencv keras conv-neural-network


    【解决方案1】:

    按如下方式组织目录

    main_dir
    -train_dir
    ``person1_fake_dir
    ```person1 fake image
    ```person1 fake image
    ---etc
    ``person1_real_dir
    ---person1 real image
    ---person1 real image
    --- etc
    --person2_fake_dir
    --- person2 fake image
    --- person2 fake image
    --- etc
    --person2_real_dir
    ---person2 real image
    ---person2 real image
    ---etc
    .
    .
    .
    --personN_fake_dir
    ---personN fake image
    ---personN fake image
    ---etc
    --personN_real_dir
    ---personN real image
    ---personN real image
    --- etc
    
    -test_dir
    same structure as train_dir but put test images here
    
    -valid_dir
    same structure as train_dir but put validation images here
    

    如果你有 N 个人,那么你将有 2 X N 个班级

    然后您可以使用 tf.keras.preprocessing.image.ImageDataGenerator().flow_from_directory() 输入您的数据。文档是here. 您不必担心裁剪图像,只需将图像大小设置为(256,256)。 下面的代码显示了您需要的其余代码

    data_gen=tf.keras.preprocessing.image.ImageDataGenerator(resize=1/255)
    train_gen=data_gen.flow_from_directory(train_dir, target_size=(224,224), color-mode='grayscale')
    valid_gen=data_gen.flow_from_directory(valid_dir, target_size=(224,224), color-mode='grayscale', shuffle=False)
    test_gen=data_gen.flow_from_directory(test_dir, target_size=(224,224), color-mode='grayscale', shuffle=False)
    model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.CategoricalCrossentropy(), metrics='accuracy')
    history=model.fit(train_gen, epochs=20, verbose=1)
    accuracy=model.evaluate (test_gen)[1]*100
    print ('Model accuracy is ', accuracy)
    

    请注意,在一般情况下,您的模型将无法区分真假。它应该适用于 1 到 N 的人。您可以尝试将所有假图像放在一个类目录中,将所有真实图像放在另一个类目录中并训练它,但我怀疑在一般情况下它不能很好地区分真假.

    【讨论】:

    • 嗨!感谢您的回答!它解决了我的大部分疑问。为了让它能够从真实的签名中找到虚假签名,我将给出一些真实签名和另一个人的虚假签名作为输入
    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2011-08-21
    • 2020-10-29
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多