数据读取之TFRecords

转载自http://blog.csdn.net/u012759136/article/details/52232266 对部分代码做了一些修改

import os
import tensorflow as tf
from PIL import Image

cwd = os.getcwd()

def _int64_feature(value):
  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))



def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def create_record():
    '''
    此处我加载的数据目录如下:
    0 -- img1.jpg
         img2.jpg
         img3.jpg
         ...
    1 -- img1.jpg
         img2.jpg
         ...
    2 -- ...
    ...
    '''
    writer = tf.python_io.TFRecordWriter("train.tfrecords")
    for index, name in enumerate(classes):
        class_path = cwd + name + "/"
        for img_name in os.listdir(class_path):
            img_path = class_path + img_name
                img = Image.open(img_path)
                img = img.resize((224, 224))
            img_raw = img.tobytes() #将图片转化为原生bytes
            example = tf.train.Example(features=tf.train.Features(feature={
                "label": _int64_feature(index),
                'img_raw': _bytes_feature(img_raw)
            }))
            writer.write(example.SerializeToString())
    writer.close()

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, [224, 224, 3])
    img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
    label = tf.cast(features['label'], tf.int32)

    return img, label

if __name__ == '__main__':
    img, label = read_and_decode("train.tfrecords")

    img_batch, label_batch = tf.train.shuffle_batch([img, label],
                                                    batch_size=30, capacity=2000,
                                                    min_after_dequeue=1000,enqueue_many=True)
    #初始化所有的op
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)
	#启动队列
	coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess,coord=coord)
        for i in range(3):
            val, l= sess.run([img_batch, label_batch])
            print(val.shape, l)
	coord.request_stop()
	coord.join(threads)
	sess.close()

在源代码上做了一些修改

1.将Feature函数单独抽象出来

2.tf.train.shuffle_batch中的参数enqueue_many修改成了True,默认是False,区别在于输入[x,y,z]如果enqueue_many默认为False,则输出为[batch_size,x,y,z],如果设置为True,那么输出就是[batch_size,y,z]

3.将变量初始化修改成了新版的初始化方法(貌似这里用不到)

4.加入了tf.train.Coordinator()

猜你喜欢

转载自blog.csdn.net/u013818406/article/details/70808566