今日小结——20190427(TF图像数据处理)

TFRecord输入数据样式

tensorflow提供了一种统一的格式来储存数据,这就是TFRecord,TFRecord文件中的数据都是通过tf.train.Example Protacol Buffer的格式来储存的,底层定义中包含了一个从属性名称到取值的字典,比如把一张解码前的图像储存为一个字符串,图像所对应的类别编号存为整数列表。

下面学习了tensorflow怎么吧输入图像数据转化为TFRecord数据

首先设置生成整数型和字符串型的属性的属性:

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]))

定义保存属性,labels,images,pixels:

images = mnist.train.images
labels = mnist.train.labels
pixels = images.shape[1]

将图像矩阵转化为字符串,吧所有信息写入Example Protocol Buffer这个数据结构中,然后吧一个example写入TFRecord文件

def _make_example(pixels, label, image):
    image_raw = image.tostring()
    example = tf.train.Example(features=tf.train.Features(feature={
        'pixels': _int64_feature(pixels),
        'label': _int64_feature(np.argmax(label)),
        'image_raw': _bytes_feature(image_raw)
    }))
with tf.python_io.TFRecordWriter("output.tfrecords") as writer:
    for index in range(num_examples):
        example = _make_example(pixels, labels[index], images[index])
        writer.write(example.SerializeToString())
print("TFRecord训练文件已保存。")

 以上程序将图像数据储存在TFRecord中,下面学习怎么从TFRecord中读出数据

首先需要创建一个reader读取TFRecord中的样例,并创建一个队列来维护输入文件列表,读文件样例也可以通过read_up_to一次性读取多个样例

reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(["output.tfrecords"])
_,serialized_example = reader.read(filename_queue)

 解析样例,tensorflow有两种属性解析的方法,一种是用tf.FixedLenFeature,这种方法解析的结果是一个Tensor,另一种方法是通过tf.VarLenFeature,这种方法解析得到的结果是SparseTensor,用于处理稀疏数据。

PS:这里解析数据的格式要和上面程序写入数据的格式一致!!!

然后通过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])

 明儿玩去咯!!放几天假!!!

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值