【问题标题】:Training pictures by TensorFlowTensorFlow 训练图片
【发布时间】:2017-08-21 04:19:26
【问题描述】:

我最近在学习 TensorFlow,想将我的图片导入 TensorFlow 进行训练,但我遇到了一个问题。 下面是我的代码

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

当我运行代码时,我得到如下错误消息:

Traceback(最近一次调用最后一次):文件 “/home/hadoop/PycharmProjects/TensorFlow/Test.py”,第 43 行,在 sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys}) 文件 "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", 第 767 行,运行中 run_metadata_ptr)文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py”, 第 925 行,在 _run raise TypeError('feed 的值不能是 tf.Tensor 对象。' TypeError: feed 的值不能是 tf.Tensor 对象。 可接受的提要值包括 Python 标量、字符串、列表或 numpy ndarrays。

我不知道如何正确编写代码。

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

对于 x, y_actual, W, b 我应该针对我的情况输入什么?

非常感谢您的帮助

【问题讨论】:

  • 您应该直接使用labelimage,而不是创建占位符,因为它们是张量值。
  • 您能告诉我如何编辑我的代码以使其运行吗?我不知道如何编辑代码,因为我只是按照文档去做

标签: python tensorflow


【解决方案1】:
  • 你的imagelabel变量是张量
  • 您不能在 Feed 中提供张量值
  • 您的提要必须是常规的 Python 数据结构,例如 numpy 数组、int 等。
  • 您正在使用图像,因此 image 变量必须是您在输入张量 x = tf.placeholder(tf.float32, [None, 30000]) 中定义的形状为 [None, 3000] 的 numpy 数组。
  • 您的label 变量也必须是一个形状为[None, 8] 的numpy 数组,您已在输入张量x = tf.placeholder(tf.float32, [None, 8]) 中定义了该数组
  • 在你的代码中你想让你的image变量从张量变成一个numpy数组所以试试这个:batch_xs = sess.run(image) - 你还想把你的 label 变量从张量变成一个 numpy 数组,所以试试这个:batch_ys = sess.run(label)

【讨论】:

    猜你喜欢
    • 2016-03-09
    • 2017-11-28
    • 2017-04-06
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2018-05-22
    • 2018-11-01
    相关资源
    最近更新 更多