【发布时间】:2018-05-05 12:45:19
【问题描述】:
我在尝试实施此线程中提出的回归解决方案时遇到问题。
Using Keras ImageDataGenerator in a regression model
另一个堆栈问题也有类似的问题:Tensorflow ValueError: Too many vaues to unpack (expected 2) 但我找不到适合我的解决方案。我对产量进行了this 解释,但没有任何结果。对我来说奇怪的是,前两个循环完成了,但是当输出相同时它在第三个循环中崩溃。
对于目录,文件夹标记为 0、1 和 2,分别对应于 list_of_values 中的 0.1、0.3 和 0.5。
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
height_shift_range=0.15,
shear_range=0.2)
def regression_flow_from_directory(flow_from_directory_gen, list_of_values):
for x, y in flow_from_directory_gen:
print (list_of_values[y], list_of_values,y)
yield (x, list_of_values[y])
batch_size=3
list_of_values=[0.1,0.3,0.5]
(x_train,y_train) = regression_flow_from_directory(train_datagen.flow_from_directory(
'figs/train', # this is the target directory
batch_size=batch_size,
class_mode='sparse'),
np.asarray(list_of_values))
输出
Found 9 images belonging to 3 classes.
[ 0.5 0.3 0.1] [ 0.1 0.3 0.5] [2 1 0]
[ 0.3 0.1 0.3] [ 0.1 0.3 0.5] [1 0 1]
[ 0.5 0.5 0.1] [ 0.1 0.3 0.5] [2 2 0]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-179-3cf97453bd05> in <module>()
5 batch_size=batch_size,
6 class_mode='sparse'),
----> 7 np.asarray(list_of_values))
ValueError: too many values to unpack (expected 2)
编辑:错误在于将函数regression_flow_from_directory 返回到两个变量(x_train,y_train)。仅返回 x_train 会正确通过生成器。
x_train = regression_flow_from_directory(train_datagen.flow_from_directory(
'figs/train', # this is the target directory
batch_size=batch_size,
class_mode='sparse'),
np.asarray(list_of_values))
【问题讨论】:
-
欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。
标签: python tensorflow runtime-error keras generator