Tensorflow,train_step 馈送不正确

Tensorflow, train_step feed incorrect

我正在从 convnetjs 切换到 tensorflow,并且正在努力学习阅读图像和使用 tensorflow 训练 cnn 的基础知识。

我在两个文件夹中有一堆 160*120*1 的图像:train/go 和 train/no 所以我使用两个 类.

我可以通过某种方式了解 tf.train.slice_input_producer 和 sess.run(train_step.

之间的联系

我的代码:

import tensorflow as tf

def read_my_list( minId, maxId ):
    """ create list with train/no and train/go from 1 to maxid
        max maxId = 50000
    """ 

    filenames = []
    labels = []
    for num in range( minId, maxId ):

        filenames.append( "/media/boss/tensor/train/go/" + str( num ) + ".jpg" )
        labels.append( int( 1 ) )

        filenames.append( "/media/boss/tensor/train/no/" + no_go_name( num ) + ".jpg" )
        labels.append( int( 0 ) )

        # return list with all filenames
    return filenames, labels

def no_go_name( id ):

    # create string where id = 5 becomes 00005

    ret = str( id )
    while ( len( ret ) < 5 ):
      ret = "0" + ret;

    return ret;


def read_images_from_disk(input_queue):
    """Consumes a single filename and label as a ' '-delimited string.
    Args:
      filename_and_label_tensor: A scalar string tensor.
    Returns:
      Two tensors: the decoded image, and the string label.
    """
    label = input_queue[1]
    print( "read file "  )
    file_contents = tf.read_file(input_queue[0])
    example = tf.image.decode_jpeg(file_contents, channels=1)

    # do i need to set shape??????????
    example.set_shape([160, 120, 1])
    print( "file read " )
    return  example, label



# some stuff to create a cnn etc
x = tf.placeholder(tf.float32, [None, 19200])

W = tf.Variable(tf.zeros([19200, 2]))
b = tf.Variable(tf.zeros([2]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder(tf.float32, [None, 2])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)

    # get filelist and labels
    image_list, label_list = read_my_list( 1, 10 )

    # conver to tensors for input_queue
    images = tf.convert_to_tensor(image_list, dtype=tf.string)
    labels = tf.convert_to_tensor(label_list, dtype=tf.int32)

    # Makes an input queue
    input_queue = tf.train.slice_input_producer([images, labels],
                                        num_epochs=10,
                                        shuffle=True)

    image, label = read_images_from_disk(input_queue)

    for i in range(100):
        print( i )

        image_batch, label_batch = tf.train.batch([image, label],
                                          batch_size=2)


        #gives error see below                                     
        sess.run(train_step, feed_dict={x: image_batch, y_: label_batch})


    # test accuracy, unsure if something is wrong here
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    batch_xs, batch_ys = tf.train.batch([image, label],
                                      batch_size=10)

    print(sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys}))

下一行报错:

sess.run(train_step, feed_dict={x: image_batch, y_: label_batch})

这是错误:

Traceback (most recent call last):

File "detectGoNo.py", line 95, in <module>
sess.run(train_step, feed_dict={x: image_batch, y_: label_batch})

File "/home/boss/anaconda2/envs/tensor2/lib/python2.7/site-
packages/tensorflow/python/client/session.py", line 340, in run

run_metadata_ptr)

File "/home/boss/anaconda2/envs/tensor2/lib/python2.7/site-
packages/tensorflow/python/client/session.py", line 545, 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.

2016 年 2 月 6 日更新

我得到了与来自 nessuno 的解决方案、训练和验证(下面的代码)一起工作的一切 Mrry 表示管道更典型,不幸的是这个管道不起作用(下面的代码) 没有给出错误,但计算的成本保持不变,验证表明网络没有改善。

我最好的猜测是我将标签提供给培训师的方式或我使用 one_hot 功能的方式有问题。

验证部分似乎在工作,当我感觉始终带有标签 0 的图像准确度变为 100%,标签 1 准确度为 0% 和 50/50 时为 50%。当然也可以反过来,但由于训练时成本没有变化,我认为训练时出了点问题

我知道,我现在使用的模型很简单,但对于调试来说已经足够好了,工作版本在 1500 张图像中达到了 80% 的准确率。

label = tf.cast( label, tf.int64 )

label = tf.one_hot( label, 2, 0, 1 )
label = tf.cast( label, tf.float32 )

我的代码:(有效)

import tensorflow as tf
import numpy      as np
import math

IMAGE_WIDTH  = 160
IMAGE_HEIGHT = 120
IMAGE_DEPTH  = 1
IMAGE_PIXELS = IMAGE_WIDTH * IMAGE_HEIGHT
NUM_CLASSES  = 2

STEPS         = 50000
STEP_PRINT    = 100
STEP_VALIDATE = 100
LEARN_RATE    = 0.0014
DECAY_RATE    = 0.4
BATCH_SIZE    = 5

def read_my_list( minId, maxId, folder ):
    """ create list with train/no and train/go from 1 to maxid
        max maxId = 50000
    """ 

    filenames = []
    labels    = []
    #labels = np.zeros( ( ( maxId - minId ) * 2, 2 ) )
    for num in range( minId, maxId ):

        filenames.append( "/media/boss/2C260F93260F5CE8/tensor/" + folder + "/go/" + str( num ) + ".jpg" )
        #labels[ ( num - minId ) * 2 ][ 1 ] = 1
        labels.append( int( 1 ) )

        filenames.append( "/media/boss/2C260F93260F5CE8/tensor/" + folder + "/no/" + no_go_name( num ) + ".jpg" )
        #labels[ ( ( num - minId ) * 2 ) + 1 ][ 0 ] = 1
        labels.append( int( 0 ) )

        # return list with all filenames
    print( "label: " + str( len( labels ) ) )
    print( "image: " + str( len( filenames ) ) )
    return filenames, labels

def no_go_name( id ):

    # create string where id = 5 becomes 00005

    ret = str( id )
    while ( len( ret ) < 5 ):
      ret = "0" + ret;

    return ret;

# Create model
def conv_net(x):

    img_width  = IMAGE_WIDTH
    img_height = IMAGE_HEIGHT
    img_depth  = IMAGE_DEPTH

    weights    = tf.Variable( tf.random_normal( [ img_width * img_height * img_depth, NUM_CLASSES ] ) )
    biases     = tf.Variable( tf.random_normal( [ NUM_CLASSES ] ) )

    # softmax layer
    out        = tf.add( tf.matmul( x, weights ), biases )
    return out 

def read_images_from_disk(input_queue):
    """Consumes a single filename and label as a ' '-delimited string.
    Args:
      filename_and_label_tensor: A scalar string tensor.
    Returns:
      Two tensors: the decoded image, and the string label.
    """
    label = input_queue[1]
    print( "read file "  )
    file_contents = tf.read_file(input_queue[0])
    example = tf.image.decode_jpeg( file_contents, channels = 1 )

    example = tf.reshape( example, [ IMAGE_PIXELS ] )
    example.set_shape( [ IMAGE_PIXELS ] )

    example = tf.cast( example, tf.float32 )
    example = tf.cast( example, tf.float32 ) * ( 1. / 255 ) - 0.5

    label = tf.cast( label, tf.int64 )

    label = tf.one_hot( label, 2, 0, 1 )
    label = tf.cast( label, tf.float32 )

    print( "file read " )
    return  example, label

with tf.Session() as sess:

    ########################################
    # get filelist and labels for training
    image_list, label_list = read_my_list( 501, 50000, "train" )

    # create queue for training
    input_queue = tf.train.slice_input_producer( [ image_list, label_list ],
                                                num_epochs = 100,
                                                shuffle = True )

    # read files for training
    image, label = read_images_from_disk( input_queue )

    # `image_batch` and `label_batch` represent the "next" batch
    # read from the input queue.
    image_batch, label_batch = tf.train.batch( [ image, label ], batch_size = BATCH_SIZE )

    # input output placeholders

    x = tf.placeholder(tf.float32, [None, IMAGE_PIXELS])
    y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES])

    # create the network
    y = conv_net( x )

    # loss
    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( y, y_) )

    learning_rate = tf.placeholder(tf.float32, shape=[])

    # train step
    train_step   = tf.train.AdamOptimizer( 1e-3 ).minimize( cost )


    ########################################
    # get filelist and labels for validation
    image_list_test, label_list_test = read_my_list( 1, 500, "validation" )

    # create queue for validation
    input_queue_test = tf.train.slice_input_producer( [ image_list_test, label_list_test ],
                                                shuffle=True )

    # read files for validation
    image_test, label_test = read_images_from_disk( input_queue_test )

    # `image_batch_test` and `label_batch_test` represent the "next" batch
    # read from the input queue test.
    image_batch_test, label_batch_test = tf.train.batch( [ image_test, label_test ], batch_size=200 )

    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    init = tf.initialize_all_variables()
    sess.run(init)

    # N.B. You must run this function before `sess.run(train_step)` to
    # start the input pipeline.
    #tf.train.start_queue_runners(sess)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(STEPS):
        # No need to feed, because `x` and `y_` are already bound to
        # the next input batch.   
        if i % STEP_PRINT == 0:
            LEARN_RATE = LEARN_RATE * DECAY_RATE
            print( str( i ) + " " + str( LEARN_RATE ) )

        if i % STEP_VALIDATE == 0:

            imgs, lbls = sess.run([image_batch_test, label_batch_test])

            print(sess.run(accuracy, feed_dict={
                    x: imgs,
                    y_: lbls}))

        imgs, lbls = sess.run([image_batch, label_batch])

        sess.run(train_step, feed_dict={
         x: imgs,
         y_: lbls}) 
#         ,learning_rate:LEARN_RATE})      

    imgs, lbls = sess.run([image_batch_test, label_batch_test])

    print(sess.run(accuracy, feed_dict={
         x: imgs,
         y_: lbls}))

    coord.request_stop()
    coord.join(threads)

我的代码:(不工作)

with tf.Session() as sess:

    ########################################
    # get filelist and labels for training
    image_list, label_list = read_my_list( 501, 50000, "train" )

    # create queue for training
    input_queue = tf.train.slice_input_producer( [ image_list, label_list ],
                                                num_epochs = 100,
                                                shuffle = True )

    # read files for training
    image, label = read_images_from_disk( input_queue )

    # `image_batch` and `label_batch` represent the "next" batch
    # read from the input queue.
    image_batch, label_batch = tf.train.batch( [ image, label ], batch_size = BATCH_SIZE )

    x = image_batch
    y_ = label_batch

    # create the network
    y = conv_net( x )

    # loss
    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( y, y_) )

    # train step
    train_step   = tf.train.AdamOptimizer( 1e-3 ).minimize( cost )


    ########################################
    # get filelist and labels for validation
    image_list_test, label_list_test = read_my_list( 1, 500, "validation" )

    # create queue for validation
    input_queue_test = tf.train.slice_input_producer( [ image_list_test, label_list_test ],
                                                shuffle=True )

    # read files for validation
    image_test, label_test = read_images_from_disk( input_queue_test )

    # `image_batch_test` and `label_batch_test` represent the "next" batch
    # read from the input queue test.
    image_batch_test, label_batch_test = tf.train.batch( [ image_test, label_test ], batch_size=200 )

    xval = image_batch_test
    yval_ = label_batch_test

    # network for validation
    yval = conv_net( xval )

    # validate network
    correct_prediction = tf.equal( tf.argmax( yval, 1 ), tf.argmax( yval_, 1 ) )

    # calculate accuracy
    accuracy = tf.reduce_mean( tf.cast( correct_prediction, tf.float32 ) )

    # init all variables
    init = tf.initialize_all_variables()
    sess.run( init )

    # N.B. You must run this function before `sess.run(train_step)` to
    # start the input pipeline.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners( coord = coord )

    for i in range(STEPS):
        # No need to feed, because `x` and `y_` are already bound to
        # the next input batch.   
        if i % STEP_PRINT == 0:
            print( i )

        # validate accuracy
        if i % STEP_VALIDATE == 0:
            print( sess.run( accuracy ) )

        # train one step
        sess.run( train_step )  

    # validate accuracy
    print( sess.run( accuracy ) )

    coord.request_stop()
    coord.join( threads )

2016 年 6 月 10 日更新
我花了一段时间才意识到训练管道和验证管道不共享相同的权重和偏差。
现在我在单独的脚本中训练、保存模型和加载模型,效果很好。

image_batchlabel_batchtf.Tensor 个对象。

feed_dict 背后的想法是将图形外部的值传递到图形中。张量对象是图中的值。

因此,您必须在图表内部评估两个张量对象(以提取内容),然后将计算值(现在是 python 值)提供给图表。

因此,使用

评估值
imgs, lbls = image_batch.eval(), label_batch.eval()

或(更好,因为它使用单个调用)

imgs, lbls = sess.run([image_batch, label_batch])

然后提供图形,用返回值替换占位符的内容:

    sess.run(train_step, feed_dict={
         x: imgs,
         y_: lbls})

您可以用以下代码替换:

sess.run(train_step, feed_dict={x: image[i:i+batch_size], y_: label[i:i+batch_size]})

由于 , the results of tf.train.batch()image_batchlabel_batch— 是 tf.Tensor 对象,因此您不能将它们用作 feed 成子图。

使用tf.train.batch()的典型方法是用它来定义管道的输入(而不是对xy_使用tf.placeholder()),所以批处理和预取将在 TensorFlow 图中处理。这是根据需要执行批处理的程序第一部分的大致重构:

with tf.Session() as sess:

    # get filelist and labels
    image_list, label_list = read_my_list( 1, 10 )

    input_queue = tf.train.slice_input_producer([image_list, label_list],
                                                num_epochs=10,
                                                shuffle=True)

    image, label = read_images_from_disk(input_queue)

    # `image_batch` and `label_batch` represent the "next" batch
    # read from the input queue.
    image_batch, label_batch = tf.train.batch([image, label], batch_size=2)

    x = image_batch
    y_ = label_batch

    # Define your model in terms of `x` and `y_` here....
    train_step = ...

    # N.B. You must run this function after creating your graph.
    init = tf.initialize_all_variables()
    sess.run(init)

    # N.B. You must run this function before `sess.run(train_step)` to
    # start the input pipeline.
    tf.train.start_queue_runners(sess)

    for i in range(100):
        # No need to feed, because `x` and `y_` are already bound to
        # the next input batch.                                     
        sess.run(train_step)