我该如何更改MNIST教程以使用TFRecords而不是该教程从网络上下载的奇数格式?

我使用了初始模型中的build_image_data.py来创建包含200x200 RGB图像的TFRecords,并打算在1080Ti上进行训练,但是我找不到如何加载TFRecords并将其馈送到卷积神经网络的好示例。

最佳答案

我做了你打算做的类似的事情。我还使用了相同的脚本来构建图像数据。我用于读取数据和对其进行训练的代码是

import tensorflow as tf

height = 28
width = 28

tfrecords_train_filename = 'train-00000-of-00001'
tfrecords_test_filename = 'test-00000-of-00001'


def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()

    _, serialized_example = reader.read(filename_queue)

    features = tf.parse_single_example(
        serialized_example,
        features={
            'image/height': tf.FixedLenFeature([], tf.int64),
            'image/width': tf.FixedLenFeature([], tf.int64),
            'image/colorspace': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
            'image/channels': tf.FixedLenFeature([], tf.int64),
            'image/class/label': tf.FixedLenFeature([], tf.int64),
            'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
            'image/format': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
            'image/filename': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
            'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='')
        })

    image_buffer = features['image/encoded']
    image_label = tf.cast(features['image/class/label'], tf.int32)

    # Decode the jpeg
    with tf.name_scope('decode_jpeg', [image_buffer], None):
        # decode
        image = tf.image.decode_jpeg(image_buffer, channels=3)

        # and convert to single precision data type
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)
        image = tf.image.rgb_to_grayscale(image)

    image_shape = tf.stack([height, width, 1])
    image = tf.reshape(image, image_shape)

    return image, image_label


def inputs(filename, batch_size, num_epochs):
    if not num_epochs: num_epochs = None

    with tf.name_scope('input'):
        filename_queue = tf.train.string_input_producer([filename], num_epochs=None)
        image, label = read_and_decode(filename_queue)

        # Shuffle the examples and collect them into batch_size batches.
        images, sparse_labels = tf.train.shuffle_batch(
            [image, label], batch_size=batch_size, num_threads=2,
            capacity=1000 + 3 * batch_size,
            min_after_dequeue=1000)

        return images, sparse_labels

image, label = inputs(filename=tfrecords_train_filename, batch_size=200, num_epochs=None)
image = tf.reshape(image, [-1, 784])
label = tf.one_hot(label - 1, 10)

# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

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

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

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    for i in range(1000):
        img, lbl = sess.run([image, label])
        sess.run(train_step, feed_dict={x: img, y_: lbl})

    img, lbl = sess.run([image, label])
    print(sess.run(accuracy, feed_dict={x: img, y_: lbl}))

    coord.request_stop()
    coord.join(threads)


这是对mnist进行分类的超级简单模型。但是,我认为这也是如何使用TFRecord文件进行训练的可扩展答案。它尚未考虑评估数据,因为这需要进行更多的协调。

09-28 13:35