TensorFlow------读取二进制文件实例:

class CifarRead(object):
'''
完成读取二进制文件,写进tfrecords,读取tfrecords
:param object:
:return:
'''
def __init__(self,filelist):
# 文件列表
self.file_list = filelist # 定义读取的图片的一些属性
self.height = 32
self.width = 32
self.channel = 3
# 二进制文件每张图片的字节
self.label_bytes = 1
self.image_bytes = self.height * self.width * 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包含目标值和特征值
label_image = tf.decode_raw(value,tf.uint8)
print(label_image) # 4.分割出图片和标签数据,特征值和目标值
label = tf.slice(label_image,[0],[self.label_bytes]) image = tf.slice(label_image,[self.label_bytes],[self.image_bytes])
print('---->')
print(image) # 5. 可以对图片的特征数据进行形状的改变 [3072]-->[32,32,3]
image_reshape = tf.reshape(image,[self.height,self.width,self.channel]) print('======>')
print(label)
print('======>') # 6. 批处理数据
image_batch,label_batch = tf.train.batch([image_reshape,label],batch_size=10,num_threads=1,capacity=10) print(image_batch,label_batch) return image_batch,label_batch if __name__ == '__main__':
# 找到文件,构建列表 路径+名字 ->列表当中
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)

最新文章

  1. EF Core 数据库迁移(Migration)
  2. 常用的SQL语句
  3. c++局部变量经典举例
  4. Data Flow ->> Fuzzy Lookup & Fuzzy Grouping
  5. 使用头文件cfloat中的符号常量获知浮点类型数据的表数范围---gyy整理
  6. [POJ2234]Matches Game
  7. LINQ 101——分组、Set、转换、Element
  8. Tomcat地址栏传中文参数乱码问题处理
  9. 7篇Model View和4篇双缓冲
  10. MySQL命令行数据操作使用心得(总结版)
  11. js日期操作
  12. CentOS 7下单机部署RabbltMQ环境的操作记录
  13. QPushButton class
  14. Win下端口占用问题:OSError: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试
  15. 【分布式系列】session跨域及单点登录解决方案
  16. OpenVPN参数详解
  17. php文件处理函数
  18. Word2010去除灰色中括号标记
  19. jsp参考手册
  20. 《Python黑帽子:黑客与渗透测试编程之道》 基于GitHub的命令和控制

热门文章

  1. Django学习过程中的排错总结
  2. Linux(CentOS)下的JDK的安装和环境配置
  3. ubuntu16.04 安装composer和 laravel
  4. 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
  5. ExtJs之表单(form)
  6. 洛谷——P2813 母舰
  7. Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)
  8. 【Numpy】python机器学习包Numpy基础知识学习
  9. C++中的读入输出优化及清新脱俗的宏命令
  10. [ARC 066] Tutorial