在回归模型中使用Keras ImageDataGenerator [英] Using Keras ImageDataGenerator in a regression model
问题描述
我要使用
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:
-
您需要以以下方式构建目录:
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:
-
您需要按以下方式构建目录:
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 = [1st value, 2nd value, ...]
.然后按照以下方式定义生成器:
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屋!