且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在回归模型中使用 Keras ImageDataGenerator

更新时间:2023-12-02 19:24:40

此时(2017 年 1 月 21 日最新版本的 Keras)flow_from_directory 只能以下列方式工作:

At this moment (newest version of Keras from January 21st 2017) the flow_from_directory could only work in a following manner:

  1. 您需要按以下方式构建目录:

  1. You need to have a directories structured in a following manner:

directory with images
    1st label
        1st picture from 1st label
        2nd picture from 1st label
        3rd picture from 1st label
        ...
    2nd label
        1st picture from 2nd label
        2nd picture from 2nd label
        3rd picture from 2nd label
        ...
    ...

  • flow_from_directory(picture, label) 格式返回固定大小的批次.
  • flow_from_directory returns batches of a fixed size in a format of (picture, label).
  • 如您所见,它只能用于分类案例,并且文档中提供的所有选项仅指定将类提供给分类器的方式.但是,有一个巧妙的技巧可以使 flow_from_directory 对回归任务有用:

    So as you can see it could only be used for a classification case and all options provided in a documentation specify only a way in which the class is provided to your classifier. But, there is a neat hack which could make a flow_from_directory useful for a regression task:

    1. 您需要按以下方式构建目录:

    1. You need to structure your directory in a following manner:

    directory with images
        1st value (e.g. -0.95423)
            1st picture from 1st value
            2nd picture from 1st value
            3rd picture from 1st value
            ...
        2nd value (e.g. - 0.9143242)
            1st picture from 2nd value
            2nd picture from 2nd value
            3rd picture from 2nd value
            ...
       ...
    

  • 您还需要有一个列表list_of_values = [第一个值,第二个值,...].然后您的生成器按以下方式定义:

  • You also need to have a list list_of_values = [1st value, 2nd value, ...]. Then your generator is defined in a following manner:

    def regression_flow_from_directory(flow_from_directory_gen, list_of_values):
        for x, y in flow_from_directory_gen:
            yield x, list_of_values[y]
    

  • 对于 flow_from_directory_gen 来说,拥有一个 class_mode='sparse' 来完成这项工作至关重要.当然这有点麻烦,但它有效(我使用了这个解决方案:))

    And it's crucial for a flow_from_directory_gen to have a class_mode='sparse' to make this work. Of course this is a little bit cumbersome but it works (I used this solution :) )