-1

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)) 
Peaksandpeaks
  • 57
  • 1
  • 8
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Nov 21 '17 at 22:13

2 Answers2

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.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
  • Yup, the mistake was with trying to unpack into (x_train,y_train). Changing it to just x_train worked. `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)) ` – Peaksandpeaks Nov 21 '17 at 22:37
0
(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
Prune
  • 76,765
  • 14
  • 60
  • 81