问题
I have an issue trying to implement the regression solution proposed in this thread.
Using Keras ImageDataGenerator in a regression model
Another stack question had a similar issue: Tensorflow ValueError: Too many vaues to unpack (expected 2) but I couldnt find a solution that would work in my case. I went through this explanation for yield without any result. What is odd to me is that the first two loops complete but it crashes on the third when the outputs are identical.
For the directory, the folders are labeled 0, 1, and 2 corresponding to the 0.1, 0.3, and 0.5, respectively in the list_of_values.
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))
output
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)
EDIT: the Error was in returning the function regression_flow_from_directory to two variables (x_train, y_train). Returning only to x_train passes the generator correctly.
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))
回答1:
The error has nothing to do with np.asarray. The function regression_flow_from_directory contains a yield statement. Therefore when you call it you get, not a tuple of yielded values, but a generator object. That's just one object, which you are trying to unpack into a two-element tuple. That's the reason for the error message.
回答2:
(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))
The problem appears to be that your routine regression_flow_from_directory returns more than two values. You have a pair on the left of that assignment, so you must have exactly two values on the right. Try printing your actual return value, rather than the components. For instance:
result = regression_flow_from_directory(...)
print (result)
(x,y) = result
You'll see the problem: you have to iterate through regression_flow_from_directory with those arguments.
Trivial example of the principal:
>>> (x, y) = 1, 2
>>> x
1
>>> y
2
>>> (x, y) = 1, 2, 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
来源:https://stackoverflow.com/questions/47423512/yield-valueerror-too-many-vaues-to-unpack-expected-2-in-python