2

I have looked this up on Reddit, Stack Overflow, tech forums, documentation, GitHub issues etc etc and still can't solve this issue.

For reference, I am using Python 3 TensorFlow on Windows 10, 64 Bit.

I am trying to use my own dataset (300 pics of cats, 512x512, .png format) in Tensorflow to train it to know what a cat looks like. If this works I will train it with other animals and eventually objects.

I can't seem to figure out why I am getting the error ValueError: too many values to unpack (expected 2). The error appears in the line images,labal = create_batches(10), which points to my function create_batches (see below). I don't know what could be causing this as I am fairly new to TensorFlow. I am trying to make my own Neural Network based on the MNIST Dataset. Code below:

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}))

And the Error:

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

My GitHub link link if anyone needs it. The project folder is the "imgpredict".

phd
  • 82,685
  • 13
  • 120
  • 165

2 Answers2

2

You are yielding your results in an incorrect way:

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

which gives you one value that is yielded, but when you call you method you are expecting two values yielded:

images,labal = create_batches(10)

Either yield two values , like:

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

(uncomment) or just expect one.

Edit: You should use parentheses on both the yield and when receiving the results like this:

#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)

However this is not the way I have used the yield option; one usually iterates over your method that returns the generator, in your case it should look something like this:

#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})

You can also check this question regarding the user of yield and generators.

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
  • I modified my code, but got the same error as before. –  Jul 10 '17 at 22:14
  • could you add the modified line here or edit the question? – DarkCygnus Jul 10 '17 at 22:15
  • `yield(images[i:i+batch_size],labels_list[i:i+batch_size])` and I also edited the question –  Jul 10 '17 at 22:16
  • See, you are still grouping all that with the parentheses, do it without it like in my answer – DarkCygnus Jul 10 '17 at 22:17
  • That is, it should be literally: `yield images[i:i+batch_size], labels_list[i:i+batch_size]` – DarkCygnus Jul 10 '17 at 22:19
  • 1
    I think I see it now. You should group the yield in parentheses *and* also receive it with parentheses, editing my answer now – DarkCygnus Jul 10 '17 at 22:21
  • Unfortunately that did not fix it. Look at my comment on the other answer for my current state –  Jul 10 '17 at 22:36
  • Did it remove the "Too many values to unpack"? If it did then it was a solution for this question, if you are getting new errors you should post a new question or work through them before. – DarkCygnus Jul 10 '17 at 22:38
  • it got me partially through. It was meant to debug, not fix. Still getting the same error though my program is making it partway through the loop. I can post a new question since I *guess* its a new problem –  Jul 10 '17 at 22:39
  • I just noticed that maybe you are using the `yield` operator in a wrong way. Checking some of my codes, the way I used `yield` was rather different, I am going to edit my answer to show you how... hope it helps – DarkCygnus Jul 10 '17 at 22:40
  • Ok, do check also the link I mentioned to the other question, I believe it will greatly help you understand more about generators and the `yield` keyword – DarkCygnus Jul 10 '17 at 22:59
  • 1
    Alright! That fixed the first error, now I have one on the `sess.run(train_step, feed_dict={imgs:images, lbls: labal})` line, but that's for another question. Thanks for the help :) –  Jul 10 '17 at 23:05
  • Great I could help :) also remember to upvote if you found useful my answer. Good luck with your coding – DarkCygnus Jul 10 '17 at 23:06
  • I upvoted but my rep isn't high enough to be displayed. Thanks for the luck, I'm going to need it –  Jul 10 '17 at 23:07
0

You commented out the second return item.

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

You yield a single list to assign to images, and there's nothing left for labal. Remove that comment mark, or yield a dummy value if you're in debugging mode.


UPDATE

Separate this line and check what you're trying to return:

result = (images[i:i+batch_size],
          labels_list[i:i+batch_size])
print len(result), result
return result
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Oh... Forgot about that. I'll have to check that at home, my work shift just ended. If that fixes it I'll go insane –  Jul 10 '17 at 22:06
  • 1
    If you're entering a state of insanity, remember to bring a valid passport, so you can get home when you're done visiting. It's an interesting place to visit, but not somewhere you want to raise your kids. – Prune Jul 10 '17 at 22:08
  • Unfortunately that did not fix the issue (I remote desktopped into my system). I get the same error as before. –  Jul 10 '17 at 22:14
  • So I tested it out. Turns out my program can get through the first 3 batches (it outputs "result" to my 30th image), then it decides to stop working. Any idea why? –  Jul 10 '17 at 22:35
  • 1
    Not without the concommitant data and results, no. What I gave you is a help in debugging, not a solution. – Prune Jul 10 '17 at 22:36
  • I'm aware that it is a debugging line, and it did help me debug that my program is *partially* completing a loop, so it did help. When I get home I can post the full results of the "result" output –  Jul 10 '17 at 22:38
  • Thanks; I wanted to make sure you were clear on that part. – Prune Jul 10 '17 at 22:44
  • Thanks for the help with debugging my for loop, I have cleared up my error. I have a new error, but thats for a new question. Once again thanks :) –  Jul 10 '17 at 23:06