tensorflow TFRecord及样例

来源:互联网 发布:互联网办公室装修 知乎 编辑:程序博客网 时间:2024/05/29 18:01

____tz_zs学习笔记


来自实际问题中的数据往往有很多格式和属性,TFRecord 格式可以统一不同的原始数据格式,并更加有效地管理不同属性。

TFRecord 文件中的数据都是通过 tf.train.Example Protocol Buffer 的格式存储的。以下为 tf.train.Example 的数据结构:

message Example {Features features = 1;};message Features{map<string, Feature> feature = 1;};message Feature {oneof kind {BytesList bytes_list = 1;FloatList float_list = 1;Int64List int64_list = 3;}};


以下案例来源于 《TensorFlow实战Google深度学习框架》

样例一

将MNIST数据集中所有的训练数据存储到一个TFRecord文件中

# -*- coding: utf-8 -*-"""@author: tz_zs将MNIST数据集中所有的训练数据存储到一个TFRecord文件中"""import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_dataimport numpy as np# 生成整数型的属性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]))mnist = input_data.read_data_sets("/path/to/mnist/data", dtype=tf.uint8, one_hot=True)images = mnist.train.imageslabels = mnist.train.labelspixels = images.shape[1]  # 784num_examples = mnist.train.num_examples  # 55000# 输出TFRecord文件的地址filename = "/path/to/output.tfrecords"# 创建一个writer来书写TFRecord文件writer = tf.python_io.TFRecordWriter(filename)for index in range(num_examples):    # 将图像矩阵转化为一个字符串    image_raw = images[index].tostring()    '''    print(images[index])    [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0  97  96  77 118  61   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0  90 138 235 235 235 235 235     235 251 251 248 254 245 235 190  21   0   0   0   0   0   0   0   0   0       0   0 140 251 254 254 254 254 254 254 254 254 254 254 254 254 254 254     189  23   0   0   0   0   0   0   0   0   0   0 226 254 208 199 199 199     199 139  61  61  61  61  61 128 222 254 254 189  21   0   0   0   0   0       0   0   0   0  38  82  13   0   0   0   0   0   0   0   0   0   0   0      34 213 254 254 115   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0  84 254 254 234   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0  84 254 254 234   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0 106 157 254 254 243  51       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  25     117 228 228 228 253 254 254 254 254 240   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0  68 119 220 254 254 254 254 254 254 254 254     254 142   0   0   0   0   0   0   0   0   0   0   0   0   0  37 187 253     254 254 254 223 206 206  75  68 215 254 254 117   0   0   0   0   0   0       0   0   0   0   0   0 113 219 254 242 227 115  89  31   0   0   0   0     200 254 241  41   0   0   0   0   0   0   0   0   0   0   0   0 169 254     176  62   0   0   0   0   0   0   0  48 231 254 234   0   0   0   0   0       0   0   0   0   0   0   0   0  18 124   0   0   0   0   0   0   0   0       0  84 254 254 166   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0 139 254 238  57   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0 210 250 254 168   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0 242 254 239  57   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0  89 251 241  86   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   5 206 246 157   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   4 117  69   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0]    print(image_raw)    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00a`Mv=\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00Z\x8a\xeb\xeb\xeb\xeb\xeb\xeb\xfb\xfb\xf8\xfe\xf5\xeb\xbe\x15\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x8c\xfb\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe    \xfe\xfe\xbd\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe2\xfe\xd0\xc7\xc7\xc7\xc7\x8b=====\x80    \xde\xfe\xfe\xbd\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00&R\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00"\xd5\xfe\xfes\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00T\xfe\xfe\xea\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00T\xfe\xfe\xea\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00j\x9d\xfe\xfe\xf33\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x19u\xe4\xe4\xe4\xfd\xfe\xfe\xfe\xfe\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00Dw\xdc\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\x8e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00%\xbb    \xfd\xfe\xfe\xfe\xdf\xce\xceKD\xd7\xfe\xfeu\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00q\xdb\xfe\xf2    \xe3sY\x1f\x00\x00\x00\x00\xc8\xfe\xf1)\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9\xfe\xb0>\x00    \x00\x00\x00\x00\x00\x000\xe7\xfe\xea\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12|\x00\x00\x00    \x00\x00\x00\x00\x00\x00T\xfe\xfe\xa6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x8b\xfe\xee9\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\xd2\xfa\xfe\xa8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\xf2\xfe\xef9\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00Y\xfb\xf1V\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x05\xce\xf6\x9d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x04uE\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00    \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'    '''    example = tf.train.Example(features=tf.train.Features(        feature={'pixels': _int64_feature(pixels),                 'label': _int64_feature(np.argmax(labels[index])),                 'image_raw': _bytes_feature(image_raw)}))    '''    features {      feature {        key: "image_raw"        value {          bytes_list {            value: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000a`Mv=\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000Z\212\353\353\353\353\353\353\373\373\370\376\365\353\276\025\000\000\000\000\000\000\000\000\000\000\000\214\373\376\376\376\376\376\376\376\376\376\376\376\376\376\376\275\027\000\000\000\000\000\000\000\000\000\000\342\376\320\307\307\307\307\213=====\200\336\376\376\275\025\000\000\000\000\000\000\000\000\000&R\r\000\000\000\000\000\000\000\000\000\000\000\"\325\376\376s\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000T\376\376\352\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000T\376\376\352\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000j\235\376\376\3633\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\031u\344\344\344\375\376\376\376\376\360\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000Dw\334\376\376\376\376\376\376\376\376\376\216\000\000\000\000\000\000\000\000\000\000\000\000\000%\273\375\376\376\376\337\316\316KD\327\376\376u\000\000\000\000\000\000\000\000\000\000\000\000q\333\376\362\343sY\037\000\000\000\000\310\376\361)\000\000\000\000\000\000\000\000\000\000\000\000\251\376\260>\000\000\000\000\000\000\0000\347\376\352\000\000\000\000\000\000\000\000\000\000\000\000\000\022|\000\000\000\000\000\000\000\000\000T\376\376\246\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\213\376\3569\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\322\372\376\250\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\362\376\3579\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000Y\373\361V\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\005\316\366\235\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\004uE\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"          }        }      }      feature {        key: "label"        value {          int64_list {            value: 7          }        }      }      feature {        key: "pixels"        value {          int64_list {            value: 784          }        }      }    }    '''    # 写    writer.write(example.SerializeToString())writer.close()


读取TFRecord文件中的数据
# -*- coding: utf-8 -*-"""@author: tz_zs读取TFRecord文件中的数据"""import tensorflow as tfreader = tf.TFRecordReader()filename_queue = tf.train.string_input_producer(["/path/to/output.tfrecords"])_, serialized_example = reader.read(filename_queue)features = tf.parse_single_example(serialized_example,                                   features={                                       'pixels': tf.FixedLenFeature([], tf.int64),                                       'label': tf.FixedLenFeature([], tf.int64),                                       'image_raw': tf.FixedLenFeature([], tf.string)                                   })# tf.decode_raw 可以将字符串解析成图相对应的像素数组images = tf.decode_raw(features['image_raw'], tf.uint8)labels = tf.cast(features['label'], tf.int32)pixels = tf.cast(features['pixels'], tf.int32)sess = tf.Session()#  启动多线程处理输入数据coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess, coord=coord)# 每次运行读取一个样例for i in range(10):    image, label, pixel = sess.run([images, labels, pixels])    print(image)    print(label)    print(pixel)    break'''[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  97  96  77 118  61   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  90 138 235 235 235 235 235 235 251 251 248 254 245 235 190  21   0   0   0   0   0   0   0   0   0   0   0 140 251 254 254 254 254 254 254 254 254 254 254 254 254 254 254 189  23   0   0   0   0   0   0   0   0   0   0 226 254 208 199 199 199 199 139  61  61  61  61  61 128 222 254 254 189  21   0   0   0   0   0   0   0   0   0  38  82  13   0   0   0   0   0   0   0   0   0   0   0  34 213 254 254 115   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  84 254 254 234   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  84 254 254 234   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 106 157 254 254 243  51   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  25 117 228 228 228 253 254 254 254 254 240   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  68 119 220 254 254 254 254 254 254 254 254 254 142   0   0   0   0   0   0   0   0   0   0   0   0   0  37 187 253 254 254 254 223 206 206  75  68 215 254 254 117   0   0   0   0   0   0   0   0   0   0   0   0 113 219 254 242 227 115  89  31   0   0   0   0 200 254 241  41   0   0   0   0   0   0   0   0   0   0   0   0 169 254 176  62   0   0   0   0   0   0   0  48 231 254 234   0   0   0   0   0   0   0   0   0   0   0   0   0  18 124   0   0   0   0   0   0   0   0   0  84 254 254 166   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 139 254 238  57   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 210 250 254 168   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 242 254 239  57   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  89 251 241  86   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   5 206 246 157   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   4 117  69   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]7784'''

样例二

# -*- coding: utf-8 -*-"""@author: tz_zs输入文件队列 写"""import tensorflow as tf# TFRecord文件的格式def _int64_feature(value):    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))# 总共写入多少个文件num_shards = 2# 每个文件多少个数据insatances_per_shard = 2for i in range(num_shards):    filename = ('/path/to/data.tfrecords-%.5d-of-%.5d' % (i, num_shards))    writer = tf.python_io.TFRecordWriter(filename)    for j in range(insatances_per_shard):        example = tf.train.Example(features=tf.train.Features(feature={'i': _int64_feature(i), 'j': _int64_feature(j)}))        '''        print(example)        print(example.SerializeToString())                features {          feature {            key: "i"            value {              int64_list {                value: 0              }            }          }          feature {            key: "j"            value {              int64_list {                value: 0              }            }          }        }                b'\n\x18\n\n\n\x01i\x12\x05\x1a\x03\n\x01\x00\n\n\n\x01j\x12\x05\x1a\x03\n\x01\x00'        '''        writer.write(example.SerializeToString())    writer.close()


输入文件队列 读

# -*- coding: utf-8 -*-"""@author: tz_zs输入文件队列 读"""import tensorflow as tf# 正则匹配文件名files = tf.train.match_filenames_once("/path/to/data.tfrecords-*")# 创建输入队列filename_queue = tf.train.string_input_producer(files, shuffle=False)# print(filename_queue)  # <tensorflow.python.ops.data_flow_ops.FIFOQueue object at 0x00000196C9279AC8>reader = tf.TFRecordReader()_, serialized_example = reader.read(    filename_queue)  # Tensor("ReaderReadV2:0", shape=(), dtype=string),Tensor("ReaderReadV2:1", shape=(), dtype=string)features = tf.parse_single_example(serialized_example,                                   features={'i': tf.FixedLenFeature([], tf.int64),                                             'j': tf.FixedLenFeature([], tf.int64)})with tf.Session() as sess:    tf.local_variables_initializer().run()    # sess.run(files.initializer)    print(sess.run(files))    # [b'\\path\\to\\data.tfrecords-00000-of-00002'    # b'\\path\\to\\data.tfrecords-00001-of-00002']    coord = tf.train.Coordinator()    threads = tf.train.start_queue_runners(sess=sess, coord=coord)    for i in range(6):        print(sess.run([features['i'], features['j']]))    '''    [0, 0]    [0, 1]    [1, 0]    [1, 1]    [0, 0]    [0, 1]    '''    coord.request_stop()    coord.join(threads)

原创粉丝点击