TensorFlow TFRecord
生活随笔
收集整理的這篇文章主要介紹了
TensorFlow TFRecord
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
把mnist數(shù)據(jù)集另存為TFRecord格式
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as npdef _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]))mnist = input_data.read_data_sets("MNIST_data/", dtype=tf.uint8, one_hot=True)images = mnist.train.images print(images.shape) pixels = images.shape[1] #圖像分辯率28*28=784 labels = mnist.train.labelsfilename = "MNIST_data/output.tfrecords" writer = tf.python_io.TFRecordWriter(filename)num = mnist.train.num_examples print(num) for i in range(num):example = tf.train.Example(features=tf.train.Features(feature={'pixels':_int64_feature(pixels),'label':_int64_feature(np.argmax(labels[i])),'image_raw':_bytes_feature(images[i].tostring())}))writer.write(example.SerializeToString()) writer.close()讀TFRecord格式 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as np#隊列 queue = tf.train.string_input_producer(["MNIST_data/output.tfrecords"])reader = tf.TFRecordReader() _, serialized_example = reader.read(queue)#解析數(shù)據(jù),對應(yīng)寫入格式 features = tf.parse_single_example(serialized_example,features={'image_raw':tf.FixedLenFeature([], tf.string),'pixels':tf.FixedLenFeature([], tf.int64),'label':tf.FixedLenFeature([], tf.int64)} )images = tf.decode_raw(features['image_raw'], tf.uint8) labels = tf.cast(features['label'], tf.int32) pixels = tf.cast(features['pixels'], tf.int32)with tf.Session() as sess:coord = tf.train.Coordinator()#啟動多線程輸入數(shù)據(jù)threads = tf.train.start_queue_runners(sess=sess, coord=coord)for i in range(2):image, label, pixel = sess.run([images, labels, pixels])print(label)print(image.shape)print(pixel)
總結(jié)
以上是生活随笔為你收集整理的TensorFlow TFRecord的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CFA将人工智能列入考试内容折射啥
- 下一篇: windows安装XGBoost