0
votes

I'm recently learning TensorFlow and want to import my pictures into TensorFlow to train, but I get stuck on a prob. below is my code

import tensorflow as tf

tf.device(0)

def read_and_decode(filename):
    filename_queue = tf.train.string_input_producer([filename])
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
            serialized_example,
            features={
                'label': tf.FixedLenFeature([], tf.int64),
                'img_raw': tf.FixedLenFeature([], tf.string),
            })
    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img = tf.reshape(img, [100, 100, 3])
    img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
    lbl = tf.cast(features['label'], tf.int32)
    return img, lbl

image, label = read_and_decode('/Users/Cody/PycharmProjects/TensorFlowStartUp/train.tfrecords')
img_batch, label_batch = tf.train.shuffle_batch([image, label],
                                                batch_size=5, capacity=5,
                                                min_after_dequeue=2)

x = tf.placeholder(tf.float32, [None, 30000])
y_actual = tf.placeholder(tf.float32, shape=[None, 8])
W = tf.Variable(tf.zeros([30000,8]))
b = tf.Variable(tf.zeros([8]))
y_predict = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_actual*tf.log(y_predict),reduction_indices=1))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

correct_prediction = tf.equal(tf.argmax(y_predict,1), tf.argmax(y_actual,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for i in range(100):
        batch_xs = image
        batch_ys = label
        sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys})
        if(i%10==0):
            print "accuracy:",sess.run(accuracy, feed_dict={x: image, y_actual: label})

When I run the code, I get the wrong msg as below:

Traceback (most recent call last): File "/home/hadoop/PycharmProjects/TensorFlow/Test.py", line 43, in sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys}) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 767, in run run_metadata_ptr) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 925, in _run raise TypeError('The value of a feed cannot be a tf.Tensor object. ' TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

I have no idea how to get my code right.

x = tf.placeholder(tf.float32, [None, 30000])
y_actual = tf.placeholder(tf.float32, shape=[None, 8])
W = tf.Variable(tf.zeros([30000,8]))
b = tf.Variable(tf.zeros([8]))

for x, y_actual, W, b what should I input for my situation?

really appreciated with your help

1
Instead of creating a placeholder, you should use label and image directly, since these are Tensor values.Allen Lavoie
Could you show me how to edit my codes to make it run? I have no idea how to edit the codes cause I'm just following the docs to doiamcodylee

1 Answers

0
votes
  • Your image and labelvariables are tensors
  • You can not give tensor values in you feed
  • Your feed has to be a regular python data structure like numpy array, int, etc.
  • you are using images so image variable has to be a numpy array of shape [None, 3000] which you have defined in your input tensor x = tf.placeholder(tf.float32, [None, 30000]).
  • your label variable also has to be a numpy array of shape [None, 8] which you have defined in your input tensor x = tf.placeholder(tf.float32, [None, 8])
  • In you code you want to make your image variable from tensor into a numpy array so try this: batch_xs = sess.run(image) -You also want to make your label variable from tensor into a numpy array so try this: batch_ys = sess.run(label)