Tensorflow使用TFRecord构建自己的数据集并读取

2024-04-03 01:48

本文主要是介绍Tensorflow使用TFRecord构建自己的数据集并读取,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Tensorflow使用TFRecord构建自己的数据集并读取

参考文章:

http://blog.csdn.net/freedom098/article/details/56011858 
还有 优酷上kevin大神的视频

目标:1、将自己的数据集以TFRecord格式存储。

          2、从TFRecord中读取数据,并使用画图工具,以图片形式展现。


以一个图片为例:


一、将图片存储TFRecod

# 生成整数型的属性
def _int64_feature(value):if not isinstance(value,list):value=[value]return tf.train.Feature(int64_list=tf.train.Int64List(value=value))#生成字符串型的属性
def _byte_feature(value):return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
#将图片存储到tfrecord中
def convert_to_tfrecord(images, labels, save_dir, name):#从图片路径读取图片编码成tfrecord'''''convert all images and labels to one tfrecord file. Args: images: list of image directories, string type labels: list of labels, int type save_dir: the directory to save tfrecord file, e.g.: '/home/folder1/' name: the name of tfrecord file, string type, e.g.: 'train' Return: no return Note: converting needs some time, be patient... ''' filename = (save_dir + name + '.tfrecords')  n_samples = len(labels) #判断 image的样本数量和label是否相同if np.shape(images)[0] != n_samples:  raise ValueError('Images size %d does not match label size %d.' %(images.shape[0], n_samples))  writer = tf.python_io.TFRecordWriter(filename)  print('\nTransform start......')  for i in range(len(images)):  try: image_raw_data = tf.gfile.FastGFile(images[i],'r').read()img_data = tf.image.decode_png(image_raw_data)label = int(labels[i])  example = tf.train.Example(features=tf.train.Features(feature={  'label':int64_feature(label),  'image_raw': bytes_feature(image_raw)}))  writer.write(example.SerializeToString())  except IOError as e:  print('Could not read:', images[i])  print('error: %s' %e)  print('Skip it!\n')  writer.close() 

二、读取数据,并绘图
# read the data from tfrecoder
def read_and_decode(tfrecords_file):  '''''read and decode tfrecord file, generate (image, label) batches Args: tfrecords_file: the directory of tfrecord file batch_size: number of images in each batch Returns: image: 4D tensor - [batch_size, width, height, channel] label: 1D tensor - [batch_size] '''  # make an input queue from the tfrecord file  filename_queue = tf.train.string_input_producer([tfrecords_file])  reader = tf.TFRecordReader()  _, serialized_example = reader.read(filename_queue)  
#解析读入的样例img_features = tf.parse_single_example(  serialized_example,  features={  'label': tf.FixedLenFeature([], tf.int64),  'image_raw': tf.FixedLenFeature([], tf.string),  })  
#将字符串解析成相应的数组image = tf.decode_raw(img_features['image_raw'], tf.uint8)  
#转化成图片的格式image = tf.reshape(image, [465, 315,3])sess = tf.Session()coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess,coord=coord)image , label = sess.run([image,label])print imageplt.imshow(image)plt.show()sess.close()

read_and_decode('/home/tensor/Desktop/tia.tfrecords')


三、结果



这篇关于Tensorflow使用TFRecord构建自己的数据集并读取的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/871610

相关文章

Python在二进制文件中进行数据搜索的实战指南

《Python在二进制文件中进行数据搜索的实战指南》在二进制文件中搜索特定数据是编程中常见的任务,尤其在日志分析、程序调试和二进制数据处理中尤为重要,下面我们就来看看如何使用Python实现这一功能吧... 目录简介1. 二进制文件搜索概述2. python二进制模式文件读取(rb)2.1 二进制模式与文本

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

Springboot配置文件相关语法及读取方式详解

《Springboot配置文件相关语法及读取方式详解》本文主要介绍了SpringBoot中的两种配置文件形式,即.properties文件和.yml/.yaml文件,详细讲解了这两种文件的语法和读取方... 目录配置文件的形式语法1、key-value形式2、数组形式读取方式1、通过@value注解2、通过

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.