I was practicing CNN on 'intel image Classification' and I wrote a function to load the data from different folders. But when I call the function, I'am getting a ValueError which says 'Too many values to unpack'..Any idea on how to fix this?
def data_load():
datasets = ['seg_train\seg_train', 'seg_test\seg_test']
size = (150, 150)
output = []
for dataset in datasets:
directory = os.getcwd() + '/' + dataset
images = []
labels = []
for folder in os.listdir(directory):
curr_label = class_labels[folder]
for file in os.listdir(directory + '/' + folder):
img_path = directory + '/' + folder + '/' + file
curr_image = cv2.imread(img_path)
curr_image = cv2.resize(curr_image, size)
images.append(curr_image)
labels.append(curr_label)
images, labels = shuffle(images, labels)
output.append((images, labels))
return output
(X_train, y_train), (X_test, y_test) = data_load()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-36-ec0ae2384d47> in <module>
----> 1 (X_train, y_train), (X_test, y_test) = data_load()
ValueError: too many values to unpack (expected 2)
just do
x,y=data_load()
then access x[0],x[1] or in your way
Snehal R Ghungurde answered correctly. I thought I would elaborate.
Your output.append((images, labels))
is producing a list of tuples, where each tuple consists of two lists. However you are trying to unpack this list into two separate tuples.
Your current approach is to load both the training and the testing data sets at once. I would write a general function that does the following: Provided with a path, this function will read all images and return tuples with the images and labels.
It is also instructive to place variables as parameters (path, size).
def data_load(path, size=(150, 150)):
"""
Provided with a path, this function will read
all images and return tuples with the images and labels
"""
output = []
images = []
labels = []
for folder in os.listdir(directory):
curr_label = class_labels[folder]
for file in os.listdir(directory + '/' + folder):
img_path = directory + '/' + folder + '/' + file
curr_image = cv2.imread(img_path)
curr_image = cv2.resize(curr_image, size)
images.append(curr_image)
labels.append(curr_label)
images, labels = shuffle(images, labels)
output.append((images, labels))
return output
training_set = 'seg_train\seg_train'
testing_set = 'seg_test\seg_test'
x_train, y_train = data_load(training_set)
x_test, y_test = data_load(testing_set)
This error is raised because data_load
is returning more than two variables, and you are trying to assign it to two. If you add the line print(len(output))
before the return statement, you'll see that it's length is more than two.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.