Python Tensorflow MNIST TFRecord

Python Tensorflow MNIST TFRecord,python,machine-learning,tensorflow,Python,Machine Learning,Tensorflow,我该如何更改教程以使用TFRecords,而不是教程从web下载的奇怪格式 我使用inception模型创建包含200x200 RGB图像的TF记录,并打算在1080Ti上进行训练,但我找不到任何关于如何加载TF记录并将其输入卷积神经网络的好例子。我做了与您打算做的类似的事情。我还使用相同的脚本来构建图像数据。我的代码用于读取数据并对其进行训练 import tensorflow as tf height = 28 width = 28 tfrecords_train_filename =

我该如何更改教程以使用TFRecords,而不是教程从web下载的奇怪格式


我使用inception模型创建包含200x200 RGB图像的TF记录,并打算在1080Ti上进行训练,但我找不到任何关于如何加载TF记录并将其输入卷积神经网络的好例子。

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

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文件进行培训的可扩展答案。它还没有考虑评估数据,因为这需要进行更多的协调。

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

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文件进行培训的可扩展答案。它还没有考虑评估数据,因为这需要进行更多的协调。

查看它的示例,说明如何使用数据从TFRecord文件和gt张量加载数据。然后,只需将这些数据作为输入传递到网络,而不是网络在同一时间获得的任何输入moment@GPhilo我的数据集可用作“图像:图像.4D大小张量[batch\u size,FLAGS.image\u size,image\u size,3]。标签:1-D整数张量[FLAGS.batch\u size]。”,但是我看不到tf.estimator.inputs有一个函数可以获取我加载的数据。tf.estimator.inputs有一个方便的函数,可以将尚未采用张量格式的数据转换为网络可以获取的数据。您需要重写
输入。\u fn
。我不熟悉这个高级API,但我认为您需要定义一个
input\u fn
,它返回一个dict
{'images':your\u image\u tensor,'labels':your\u label\u tensor}
。查看它的示例,说明如何从TFRecord文件和gt tensor加载数据。然后,只需将这些数据作为输入传递到网络,而不是网络在同一时间获得的任何输入moment@GPhilo我的数据集可用作“图像:图像.4D大小张量[batch\u size,FLAGS.image\u size,image\u size,3]。标签:1-D整数张量[FLAGS.batch\u size]。”,但是我看不到tf.estimator.inputs有一个函数可以获取我加载的数据。tf.estimator.inputs有一个方便的函数,可以将尚未采用张量格式的数据转换为网络可以获取的数据。您需要重写
输入。\u fn
。我不熟悉这个高级API,但从开始,我认为您需要定义一个
输入\u fn
,它返回一个dict
{'images':your\u image\u tensor,'labels':your\u label\u tensor}