简体   繁体   English

收到错误:ValueError:要解包的值太多(预期为 2)

[英]Getting an error: ValueError: too many values to unpack (expected 2)

I was practicing CNN on 'intel image Classification' and I wrote a function to load the data from different folders.我在“英特尔图像分类”上练习 CNN,我写了一个 function 来从不同的文件夹加载数据。 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?但是当我调用 function 时,我收到一个 ValueError,上面写着“要解压的值太多”。知道如何解决这个问题吗?

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() x,y=data_load()

then access x[0],x[1] or in your way然后访问 x[0],x[1] 或以您的方式

Snehal R Ghungurde answered correctly. Snehal R Ghungurde 回答正确。 I thought I would elaborate.我想我会详细说明。

Your output.append((images, labels)) is producing a list of tuples, where each tuple consists of two lists.您的output.append((images, labels))正在生成一个元组列表,其中每个元组由两个列表组成。 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.我会写一个通用的 function,它执行以下操作:提供一个路径,这个 function 将读取所有图像并返回带有图像和标签的元组。

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.引发此错误是因为data_load返回两个以上的变量,并且您试图将其分配给两个。 If you add the line print(len(output)) before the return statement, you'll see that it's length is more than two.如果您在 return 语句之前添加行print(len(output)) ,您会看到它的长度大于 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误:ValueError:要解包的值太多(预期为 5) - Getting error: ValueError: too many values to unpack (expected 5) 错误:ValueError:解包的值太多(预期为 3) - Error: ValueError: too many values to unpack (expected 3) ValueError:太多的值解包(预期2) - ValueError: too many values too unpack (expected 2) 如何传递此错误“ValueError:在 Python 中解压的值太多(预期为 2)? - How Pass This Error " ValueError: too many values to unpack (expected 2) in Python? 我想解决错误“ValueError: too many values to unpack (expected 2)” - I want to resolve the error “ValueError: too many values to unpack (expected 2)” ValueError 通用 WSGI 请求错误太多值无法解包(预期 2) - ValueError generic WSGI request error too many values to unpack (expected 2) OpenAIGPTModel PyTorch 错误 - ValueError: too many values to unpack (expected 2) - OpenAIGPTModel PyTorch Error- ValueError: too many values to unpack (expected 2) ValueError:更改值时解包的值太多(预期为 2)错误 - ValueError: too many values to unpack (expected 2) error when changing a value 不断收到此错误:for name, home in reader: ValueError: too many values to unpack (expected 2) - kept getting this error: for name, home in reader: ValueError: too many values to unpack (expected 2) pysnmp-ValueError:太多值无法解包(预期4) - pysnmp - ValueError: too many values to unpack (expected 4)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM