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))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…