Tensorflow:二进制文件读取分析

图片的存储,计算类型

存储:uint8(节约空间)
矩阵计算:float32(提高精度)

二进制文件读取

CIFAR-10(比赛数据)二进制数据读取

import  tensorflow as tf
import os

# 定义cifar的数据等命令行参数
FLAGS=tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('cifar_dir','./狗/',"文件的目录")


class   CifarRead(object):
    """完成读取二进制文件,写进tfrecords,读取tfrecords"""
    def  __init__(self,filelist):
        #文件列表
        self.file_list=filelist
        #定义读取图片的一些属性
        self.height=32
        self.weight=32
        self.channel=3
        #二进制文件的每张图片的字节
        self.label_bytes=1
        self.image_bytes=self.weight * self.height *self.channel
        self.bytes=self.label_bytes+self.image_bytes
    def read_and_decode(self):
        #1.构造文件队列
        file_queue=tf.train.string_input_producer(self.file_list)
        #2.构造二进制文件读取器,读取内容,每个样本的字节数
        reader=tf.FixedLengthRecordReader(self.bytes)
        key,value=reader.read(file_queue)
        #3.解码内容,二进制文件解码
        label_image=tf.decode_raw(value,tf.uint8)
        print(label_image)
        #4.分割出图片和标签数据,切出特征值和目标值
        label=tf.slice(label_image,[0],[self.label_bytes],tf.int32)
        image=tf.slice(label_image,[self.label_bytes],[self.image_bytes])
        #5.可以对图片的特征数据进行形状的改变
        image_reshape=tf.reshape(image,[self.height,self.weight,self.channel])
        #6.批处理数据
        image_batch,label_batch=tf.train.batch([image_reshape,label],batch_size=10,num_threads=1,capacity=10)


        return image_batch,label_batch

if __name__ == '__main__':
    #1.找到文件,放入列表
    file_name=os.listdir(FLAGS.cifar_dir)
    filelist=[os.path.join(FLAGS.cifar_dir,file) for file in file_name if file[-3:]=="bin"]

    cf=CifarRead(filelist)
    image_batch,label_batch=cf.read_and_decode()
    #开启会话
    with tf.Session() as sess:
        #定义一个线程协调器
        coord=tf.train.Coordinator()

        #开启读取文件的线程
        threads=tf.train.start_queue_runners(sess,coord=coord)

        # 打印读取内容
        print(sess.run([image_batch,label_batch]))

        # 回收子线程
        coord.request_stop()
        coord.join(threads)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值