什麼是 TFRecord
PS:這段內容摘自 http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.htmlhtml
一種保存記錄的方法能夠容許你講任意的數據轉換爲TensorFlow所支持的格式, 這種方法可使TensorFlow的數據集更容易與網絡應用架構相匹配。這種建議的方法就是使用TFRecords文件,TFRecords文件包含了tf.train.Example 協議內存塊(protocol buffer)(協議內存塊包含了字段 Features)。你能夠寫一段代碼獲取你的數據, 將數據填入到Example協議內存塊(protocolbuffer),將協議內存塊序列化爲一個字符串, 而且經過tf.python_io.TFRecordWriterclass寫入到TFRecords文件。tensorflow/g3doc/how_tos/reading_data/convert_to_records.py就是這樣的一個例子。
從TFRecords文件中讀取數據, 可使用tf.TFRecordReader的tf.parse_single_example解析器。這個parse_single_example操做能夠將Example協議內存塊(protocolbuffer)解析爲張量。 MNIST的例子就使用了convert_to_records 所構建的數據。 請參看tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py, python
代碼
adjust_pic.py網絡
單純的轉換圖片大小數據結構
-
- import tensorflow as tf
-
- def resize(img_data, width, high, method=0):
- return tf.image.resize_images(img_data,[width, high], method)
pic2tfrecords.py多線程
將圖片保存成TFRecord架構
- import os.path
- import matplotlib.image as mpimg
- import tensorflow as tf
- import adjust_pic as ap
- from PIL import Image
-
-
- SAVE_PATH = 'data/dataset.tfrecords'
-
-
- 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 load_data(datafile, width, high, method=0, save=False):
- train_list = open(datafile,'r')
-
- writer = tf.python_io.TFRecordWriter(SAVE_PATH)
-
- with tf.Session() as sess:
- for line in train_list:
-
- tmp = line.strip().split(' ')
- img_path = tmp[0]
- label = int(tmp[1])
-
-
- image = tf.gfile.FastGFile(img_path, 'r').read()
-
- image = tf.image.decode_jpeg(image)
-
-
- image = tf.image.convert_image_dtype(image, dtype=tf.float32)
-
- image = ap.resize(image, width, high)
-
- image = sess.run(image)
-
-
- image_raw = image.tostring()
-
- example = tf.train.Example(features=tf.train.Features(feature={
- 'image_raw': _bytes_feature(image_raw),
- 'label': _int64_feature(label),
- }))
-
-
- writer.write(example.SerializeToString())
-
- writer.close()
-
-
- load_data('train_list.txt_bak', 224, 224)
tfrecords2data.py網站
從TFRecord中讀取並保存成圖片ui
- import tensorflow as tf
- import numpy as np
-
-
- SAVE_PATH = 'data/dataset.tfrecords'
-
-
- def load_data(width, high):
- reader = tf.TFRecordReader()
- filename_queue = tf.train.string_input_producer([SAVE_PATH])
-
-
- _, serialized_example = reader.read(filename_queue)
-
- features = tf.parse_single_example(
- serialized_example,
- features={
- 'image_raw': tf.FixedLenFeature([], tf.string),
- 'label': tf.FixedLenFeature([], tf.int64),
- })
-
-
- images = tf.decode_raw(features['image_raw'], tf.uint8)
- labels = tf.cast(features['label'], tf.int64)
-
- with tf.Session() as sess:
-
- coord = tf.train.Coordinator()
- threads = tf.train.start_queue_runners(sess=sess, coord=coord)
-
-
- for i in range(2):
-
- label, image = sess.run([labels, images])
-
-
-
-
-
-
-
-
-
-
-
-
- image = np.fromstring(image, dtype=np.float32)
-
- image = tf.reshape(image, [224, 224, 3])
-
- image = tf.image.convert_image_dtype(image, dtype=tf.uint8)
-
- image = tf.image.encode_jpeg(image)
-
- with tf.gfile.GFile('pic_%d.jpg' % label, 'wb') as f:
- f.write(sess.run(image))
-
-
- load_data(224, 224)
train_list.txt_bak 中的內容以下:編碼
image_1093.jpg 13
image_0805.jpg 10spa