在回归模型中使用 Keras ImageDataGenerator [英] Using Keras ImageDataGenerator in a regression model

查看:32
本文介绍了在回归模型中使用 Keras ImageDataGenerator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用

flow_from_directory

方法

ImageDataGenerator

为回归模型生成训练数据,其中目标值可以是 1 到 -1 之间的任何浮点值.

to generate training data for a regression model, where the target value can be any float value between 1 and -1.

flow_from_directory

有一个带有描述的class_mode"参数

has a "class_mode" parameter with the descripton

class_mode:分类"、二进制"、稀疏"或无"之一.默认:分类".确定返回的标签数组的类型:分类"将是 2D 单热编码标签,二进制"将是 1D二元标签,稀疏"将是一维整数标签.

class_mode: one of "categorical", "binary", "sparse" or None. Default: "categorical". Determines the type of label arrays that are returned: "categorical" will be 2D one-hot encoded labels, "binary" will be 1D binary labels, "sparse" will be 1D integer labels.

我应该采用这些值中的哪一个?他们似乎都不适合......

Which of these values should I take? None of them seems to really fit...

推荐答案

此时(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 :) )

    这篇关于在回归模型中使用 Keras ImageDataGenerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆