0

I've looked through many forum sites trying to find out the solution but can't get it.

I am trying to use Tensorflow (Python 3, Win 10 64 bit) with my own set of images. When I run it, I get a ValueError. Specifically:

Traceback (most recent call last):
  File "B:\Josh\Programming\Python\imgpredict\predict.py", line 62, in <module>
    sess.run(train_step, feed_dict={imgs:batchX, lbls: batchY})
  File "C:\Users\Josh\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
    run_metadata_ptr)
  File "C:\Users\Josh\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 968, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "C:\Users\Josh\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

My code is:

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


content = [] # Where images are stored
labels_list = []


########## File opening function
with open("data/cats/files.txt") as ff:
    for line in ff:
        line = line.rstrip()
        content.append(line)
#################################

########## Labels opening function
with open("data/cats/labels.txt") as fff:
    for linee in fff:
        linee = linee.rstrip()
        labels_list.append(linee)
    labels_list = np.array(labels_list)
###############################


def create_batches(batch_size):
    images1 = []
    for img1 in content:
        thedata = cv2.imread(img1)
        thedata = tf.contrib.layers.flatten(thedata)
        images1.append(thedata)
    images1 = np.asarray(images1)

    images1 = np.array(images1)

    while(True):
        for i in range(0,298,10):
            yield(images1[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):#########################################
    for (batchX,batchY) in create_batches(10):
        for inn, imgs in enumerate(batchX):
            batchX[inn] = imgs.eval()
        sess.run(train_step, feed_dict={imgs:batchX, lbls: batchY})

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

I don't know if the error is from my images, or my labels. I've tried lot's of suggestions from other SO questions, Reddit, Google Plus, GitHub Issues, etc but to no avail. My GitHub link for the project is: https://github.com/supamonkey2000/jm-uofa

and the project folder is "imgpredict"

Any help appreciated. Thanks in advance

2 Answers 2

0

In this case, I think you are seeing this error because you are passing a tensorflow object to the feed_dict when you are running the training. It could be a tensorflow object as a result of the flattening method you used:

thedata = tf.contrib.layers.flatten(thedata)

which will return a flattened tensor (more info in the docs), that for some reason isn't being properly evaluated.

Following this answer to get past this issue you need to supply a numpy array to the feed dict. You could instead try:

thedata.flatten()

which will flatten the array to a vector. I tried it and it at least got rid of the error.

Beyond that, like Ofer Sadan pointed out, there are some fundamental issues with your approach. The most obvious one to me is that you are initializing you weight matrix to the image size (512 x 512 = 262144), but since your are loading 3 channel images (RGB color images) you end up with a flattened array three times that size (512 x 512 x 3 channels = 786432) so the training will fail anyway. Try converting to grayscale if the color isn't important to you training data(thedata = cv2.cvtColor(thedata, cv2.COLOR_BGR2GRAY).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestions, I'll be sure to try them out when I get into work today. I knew something was wrong with my 262144 value....
Perfect, this eliminated the first error! Now on to the next error... Thanks for the help :)
0

I apologize for this isn't a complete answer to the error, but I see many problems with your code that could generate it.

First, is with the create_batches function. You use a list for images1, a tensor for thedata, you append all those tensors to the list and then convert that list to a numpy array. That is very bad practice.

Second problem there - it is supposed to yield both images and labels, but the labels are not processed in that function at all and arrive from the global value. Because of that, I see no reason to assume that they even match the images when you do this:

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

After all that, it appears that your batchX is a list of tensors, so you again transform each of them to an array (with imgs.eval()). After all that god only knows what the actual shape of the arrays are now, and the error itself is usually an indication that the batchX is not of a proper "rectangular" shape to be converted from a list into an array (for example if one of the elements is an array of a certain length and the others of different length).

My suggestion, rewrite your function, simplify it, don't use tensors in it, and don't use normal lists in there too. It should return a simple numpy array of a shape that fits to sess.run(train_step, feed_dict={imgs:batchX, lbls: batchY})

1 Comment

Thanks for the suggestions, I'll fix up my code when I get into work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.