Tensorflow ValueError:要打开太多的vaues(预期2)

时间:2017-07-10 21:59:57

标签: python tensorflow runtime-error generator mnist

我已经在Reddit,Stack Overflow,技术论坛,文档,GitHub等问题上看了这些,但仍然无法解决这个问题。

作为参考,我在Windows 10,64位上使用Python 3 TensorFlow

我试图在Tensorflow中使用我自己的数据集(300张猫的图片,512x512,.png格式)来训练它以了解猫的样子。如果这样可行,我会用其他动物和最终的物体进行训练。

我似乎无法弄清楚为什么我收到错误ValueError: too many values to unpack (expected 2)。该错误显示在images,labal = create_batches(10)行中,该行指向我的函数create_batches(参见下文)。我不知道是什么导致这种情况,因为我对TensorFlow还不熟悉。我正在尝试基于MNIST数据集创建自己的神经网络。代码如下:

import tensorflow as tf
import numpy as np
import os
import sys
import cv2


content = []
labels_list = []
with open("data/cats/files.txt") as ff:
    for line in ff:
        line = line.rstrip()
        content.append(line)

with open("data/cats/labels.txt") as fff:
    for linee in fff:
        linee = linee.rstrip()
        labels_list.append(linee)

def create_batches(batch_size):
    images = []
    for img in content:
        #f = open(img,'rb')
        #thedata = f.read().decode('utf8')
        thedata = cv2.imread(img)
        thedata = tf.contrib.layers.flatten(thedata)
        images.append(thedata)
    images = np.asarray(images)

    labels =tf.convert_to_tensor(labels_list,dtype=tf.string)

    print(content)
    #print(labels_list)

    while(True):
        for i in range(0,298,10):
            yield images[i:i+batch_size],labels_list[i:i+batch_size]


imgs = tf.placeholder(dtype=tf.float32,shape=[None,262144])
lbls = tf.placeholder(dtype=tf.float32,shape=[None,10])

W = tf.Variable(tf.zeros([262144,10]))
b = tf.Variable(tf.zeros([10]))

y_ = tf.nn.softmax(tf.matmul(imgs,W) + b)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(lbls * tf.log(y_),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for i in range(10000):
    images,labal = create_batches(10)
    sess.run(train_step, feed_dict={imgs:images, lbls: labal})

correct_prediction = tf.equal(tf.argmax(y_,1),tf.argmax(lbls,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

print(sess.run(accuracy, feed_dict={imgs:content, lbls:labels_list}))

错误:

Traceback (most recent call last):
  File "B:\Josh\Programming\Python\imgpredict\predict.py", line 54, in <module>

    images,labal = create_batches(2)
ValueError: too many values to unpack (expected 2)
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
(A few hundred lines of this)
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile

我的GitHub link链接,如果有人需要它。项目文件夹是&#34; imgpredict&#34;。

2 个答案:

答案 0 :(得分:2)

您以不正确的方式产生结果:

yield(images[i:i+batch_size]) #,labels_list[i:i+batch_size])

它给你一个屈服的价值,但当你给你调用方法时,你期望得到两个值:

images,labal = create_batches(10)

要么产生两个值,例如:

yield (images[i:i+batch_size] , labels_list[i:i+batch_size])

(取消注释)或只是期待一个。

编辑:您应该在收益率和收到结果时使用括号,如下所示:

#when yielding, remember that yield returns a Generator, therefore the ()
yield (images[i:i+batch_size] , labels_list[i:i+batch_size])

#When receiving also, even though this is not correct
(images,labal) = create_batches(10)

然而这不是我使用yield选项的方式;一个人通常会迭代 over 返回生成器的方法,在你的情况下它看起来应该是这样的:

#do the training several times as you have
for i in range(10000):
    #now here you should iterate over your generator, in order to gain its benefits
    #that is you dont load the entire result set into memory
    #remember to receive with () as mentioned
    for (images, labal) in create_batches(10):
        #do whatever you want with that data
        sess.run(train_step, feed_dict={imgs:images, lbls: labal})

您还可以查看有关yield和发电机用户的this问题。

答案 1 :(得分:0)

您注释掉了第二个退货项目。

        yield(images[i:i+batch_size])    #,labels_list[i:i+batch_size])

您生成一个要分配给images的列表,labal没有任何内容。删除该注释标记,或者如果您处于调试模式,则会产生虚拟值。

<强>更新

将此行分开并检查您要返回的内容:

result = (images[i:i+batch_size],
          labels_list[i:i+batch_size])
print len(result), result
return result