bmp图像输入的数据集只有50%准确

时间:2017-08-20 07:13:29

标签: tensorflow

我已创建此图表以尝试:

  1. 导入BMP文件并根据文件名(L / R)生成标签。
  2. 训练网络以确定左眼和右眼之间。
  3. 评估网络。
  4. 我正在使用新框架并将其作为数据集全部使用。代码运行,但我只有50%的准确率(没有学习)。

    任何人都可以检查图表是否正确,这只是我需要修复的网络吗?

    """ Routine for processing Eye Image dataset
        determines left/right eye
        Using Tensorflow API v1.3
    """
    
    from __future__ import absolute_import
    from __future__ import division
    from __future__ import print_function
    
    import os
    import fnmatch
    import tensorflow as tf
    from six.moves import xrange  # pylint: disable=redefined-builtin
    import nnLayers as nnLayer
    
    IMAGE_SIZE = 460
    SCALE_SIZE = 100
    NUM_CLASSES = 2
    IMAGE_DEPTH = 3
    
    FLAGS = tf.app.flags.FLAGS
    # Basic model parameters.
    tf.app.flags.DEFINE_integer('batch_size', 200,
                                """Number of images to process in a batch.""")
    tf.app.flags.DEFINE_integer('num_epochs', 1001,
                                """Number of images to process in a batch.""")
    tf.app.flags.DEFINE_string('train_directory', './eyeImages',
                                """directory of images to process.""")
    tf.app.flags.DEFINE_string('test_directory', './eyeTest',
                                """directory of images to process.""")
    tf.app.flags.DEFINE_string('log_dir', './logs',
                                """logging directory""")
    def _parse_function(filename, label):
        """Takes filenames and labels and returns
            one hot labels and image values"""
        #read the file
        image_string = tf.read_file(filename)
        #decode BMP file
        image_decoded = tf.image.decode_bmp(image_string)
        #resize accordingly
        image = tf.image.resize_images(image_decoded, [SCALE_SIZE, SCALE_SIZE])
        #convert label to one hot
        one_hot = tf.one_hot(label, NUM_CLASSES)
        return image, one_hot
    
    def inference(image):
        #shape image for convolution
        with tf.name_scope('input_reshape'):
            x_image = tf.reshape(image, [-1, SCALE_SIZE, SCALE_SIZE, IMAGE_DEPTH]) #infer number of images, last dimension is features
            tf.summary.image('input_images',x_image)
    
        #neural net layers
        #100x100x3 -> 50x50x32
        h_pool1 = nnLayer.conv_layer(x_image, IMAGE_DEPTH, 5, 32, 'hiddenLayer1', act=tf.nn.relu)
        #50x50x32 -> 25x25x64
        h_pool2 = nnLayer.conv_layer(h_pool1, 32, 5, 64, 'hiddenLayer2', act=tf.nn.relu)
        #25x25x64 -> 1024x2
        h_fc1 = nnLayer.fc_layer(h_pool2, 64, 25, 1024, 'fcLayer1', act=tf.nn.relu)
    
        #1024x2 ->1x2
        with tf.name_scope('final-layer'):
            with tf.name_scope('weights'):
                W_fc2 = nnLayer.weight_variable([1024,NUM_CLASSES])
            with tf.name_scope('biases'):
                b_fc2 = nnLayer.bias_variable([NUM_CLASSES])
    
        y_conv = tf.matmul(h_fc1, W_fc2) + b_fc2
    
        return y_conv
    
    def folderParser(folder):
        """output BMP file names in directory and
            label based on file name"""
        #create list of filenames in directory
        files = os.listdir(folder)
        #filter for BMP files
        bmpfiles = fnmatch.filter(files, '*.bmp')
        #create empty lists
        labels = []
        fullNames = []
        #get the length of the filename and determine left/right label
        for i in range(len(bmpfiles)):
            length = len(bmpfiles[i])
            fullNames.append(folder + '/'  + bmpfiles[i])
            if (bmpfiles[i][length-17])=='L':
                labels.append(1)
            else:
                labels.append(0)
    
        return fullNames,labels
    
    
    def main(argv=None):  # pylint: disable=unused-argument
    
        #delete the log files if present
        #if tf.gfile.Exists(FLAGS.log_dir):
        #    tf.gfile.DeleteRecursively(FLAGS.log_dir)
        #tf.gfile.MakeDirs(FLAGS.log_dir)
    
        #get file names and labels
        trainNames, trainLabels = folderParser(FLAGS.train_directory)
        testNames, testLabels = folderParser(FLAGS.test_directory)
        # create a dataset of the file names and labels
        tr_data = tf.contrib.data.Dataset.from_tensor_slices((trainNames, trainLabels))
        ts_data = tf.contrib.data.Dataset.from_tensor_slices((testNames, testLabels))
        #map the data set from file names to images
        tr_data = tr_data.map(_parse_function)
        ts_data = ts_data.map(_parse_function)
        #shuffle the images
        tr_data = tr_data.shuffle(FLAGS.batch_size*2)
        ts_data = ts_data.shuffle(FLAGS.batch_size*2)
        #create batches
        tr_data = tr_data.batch(FLAGS.batch_size)
        ts_data = ts_data.batch(FLAGS.batch_size)
        #create handle for datasets
        handle = tf.placeholder(tf.string, shape=[])
        iterator = tf.contrib.data.Iterator.from_string_handle(handle, tr_data.output_types, tr_data.output_shapes)
        next_element = iterator.get_next()
        #setup iterator
        training_iterator = tr_data.make_initializable_iterator()
        validation_iterator = ts_data.make_initializable_iterator()
        #retrieve next batch
        features, labels = iterator.get_next()
        #run network
        y_conv = inference(features)
        #determine softmax and loss function
        with tf.variable_scope('softmax_linear') as scope:
            diff = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=y_conv)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
        tf.summary.scalar('cross_entropy', cross_entropy)
        #run gradient descent
        with tf.name_scope('train'):
            training_op = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)
    
        #identify correct predictions
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(labels, 1))
        #find the accuracy of the model
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        tf.summary.scalar('accuracy', accuracy)
    
    
        with tf.Session() as sess:
            #initialization of the variables
            training_handle = sess.run(training_iterator.string_handle())
            validation_handle = sess.run(validation_iterator.string_handle())
            sess.run(tf.global_variables_initializer())
    
            #merge all the summaries and write test summaries
            merged = tf.summary.merge_all()
            train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train', sess.graph)
            test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test')
    
            #run through epochs
            for epoch in range(FLAGS.num_epochs):
                #initialize the training set for training epoch
                sess.run(training_iterator.initializer)
                if epoch % 2 ==0:
                    #initialize validation set
                    sess.run(validation_iterator.initializer)
                    #test
                    summary, acc = sess.run([merged, accuracy], feed_dict={handle: validation_handle})
                    train_writer.add_summary(summary, epoch) #write to test file
                    print('step %s, accuracy %s' % (epoch, acc))
                else:
                    #train
                    sess.run(training_op, feed_dict={handle: training_handle})
    
        #close the log files
        train_writer.close()
        test_writer.close()
    
    if __name__ == '__main__':
        tf.app.run()
    

    亚伦

1 个答案:

答案 0 :(得分:0)

答案是图像标准化:

image_std = tf.image.per_image_standardization (image_resized)

没有图像标准化,神经元就会变得饱和。立即改善结果。

感谢。