Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

tensorflow - Yield ValueError: Too many vaues to unpack (expected 2) in python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...