在tensorflow中,TFRecord格式的文件是可以将样本和标签放在一起,是在模型之前的一个预处理步骤,这种方式可以很大的提高效率和节约运行的内存,这种格式对数据是不进行压缩的。image.tfrecord也可以是image.tfrecords,格式写入和读取要一样就好了
1.写入
import tensorflow as tf;
image_raw_data = tf.gfile.FastGFile('/home/penglu/Desktop/11.jpg').read()
image = tf.image.decode_jpeg(image_raw_data)
a = image.eval(session=tf.Session())
a = a.tostring()
label = 1
writer = tf.python_io.TFRecordWriter('/home/penglu/Desktop/image.tfrecord')
example = tf.train.Example(features=tf.train.Features(feature={
'label':tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
'image':tf.train.Feature(bytes_list=tf.train.BytesList(value=[a]))
}))
writer.write(example.SerializeToString())
writer.close()
import tensorflow as tf
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(['/home/penglu/Desktop/image.tfrecord'])
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, features={
'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64)
})
img = tf.decode_raw(features['image'], tf.uint8)
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
print sess.run(label)
coord.request_stop()
coord.join(threads)
输出:
1