1. import tensorflow as tf
  2. import numpy as np
  3. import skimage
  4. from skimage import data, io, color
  5. from PIL import Image
  6.  
  7. path = "1.tfrecords"
  8. img_path = '/data/test/img/1.png'
  9.  
  10. with tf.python_io.TFRecordWriter(path) as writer:
  11. # list: int or float
  12. a = 1024
  13. b = 10.24
  14.  
  15. c = [0.1, 0.2, 0.3]
  16. c = np.array(c).astype(np.float32).tobytes()
  17.  
  18. d = [[1, 2], [3, 4]]
  19. d = np.array(d).astype(np.int8).tobytes()
  20.  
  21. e = "Python"
  22. e = bytes(e, encoding='utf-8')
  23.  
  24. img = io.imread(img_path)
  25. img = img.astype(np.uint8).tobytes()
  26.  
  27. img2 = Image.open(img_path)
  28. img2 = img2.resize((256, 256))
  29. img2 = img2.tobytes()
  30.  
  31. example = tf.train.Example(features=tf.train.Features(feature={
  32. 'a': tf.train.Feature(int64_list=tf.train.Int64List(value=[a])),
  33. 'b': tf.train.Feature(float_list=tf.train.FloatList(value=[b])),
  34. 'c': tf.train.Feature(bytes_list=tf.train.BytesList(value=[c])),
  35. 'd': tf.train.Feature(bytes_list=tf.train.BytesList(value=[d])),
  36. 'e': tf.train.Feature(bytes_list=tf.train.BytesList(value=[e])),
  37. 'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img])),
  38. 'image2': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img2])),
  39.  
  40. }))
  41. writer.write(example.SerializeToString())
  42.  
  43. # 读取
  44. filename_queue = tf.train.string_input_producer([path])
  45. _, serialized_example = tf.TFRecordReader().read(filename_queue)
  46.  
  47. features = tf.parse_single_example(serialized_example,
  48. features={
  49. 'a': tf.FixedLenFeature([], tf.int64),
  50. 'b': tf.FixedLenFeature([], tf.float32),
  51. 'c': tf.FixedLenFeature([], tf.string),
  52. 'd': tf.FixedLenFeature([], tf.string),
  53. 'e': tf.FixedLenFeature([], tf.string),
  54. 'image': tf.FixedLenFeature([], tf.string),
  55. 'image2': tf.FixedLenFeature([], tf.string),
  56.  
  57. })
  58.  
  59. a = features['a'] # 返回是张量
  60. b = features['b']
  61.  
  62. c = features['c']
  63. c = tf.decode_raw(c, tf.float32)
  64.  
  65. d = features['d']
  66. d = tf.decode_raw(d, tf.int8)
  67. d = tf.reshape(d, [2, 2])
  68.  
  69. e = features['e']
  70.  
  71. img = tf.decode_raw(features['image'], tf.uint8)
  72. img = tf.reshape(img, shape=[256, 256, 3])
  73.  
  74. img2 = tf.decode_raw(features['image2'], tf.uint8)
  75. img2 = tf.reshape(img2, [256, 256,3])
  76.  
  77. with tf.Session() as sess:
  78. sess.run(tf.initialize_all_variables())
  79. tf.train.start_queue_runners(sess=sess)
  80.  
  81. print(sess.run([a, b, c, d, e]))
  82.  
  83. e = sess.run(e)
  84. print(type(e), bytes.decode(e))
  85.  
  86. img = sess.run(img)
  87. io.imshow(img)
  88.  
  89. img2 = sess.run(img2)
  90. io.imshow(img2)

tensorflow tfrecord文件存储的更多相关文章

  1. Tensorflow 读写 tfrecord 文件(Python3)

    TensorFlow笔记博客:https://blog.csdn.net/xierhacker/article/category/6511974 写入tfrecord文件 import tensorf ...

  2. Tensorflow 中(批量)读取数据的案列分析及TFRecord文件的打包与读取

    内容概要: 单一数据读取方式: 第一种:slice_input_producer() # 返回值可以直接通过 Session.run([images, labels])查看,且第一个参数必须放在列表中 ...

  3. TFRecord文件的读写

    前言在跑通了官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式,而 ...

  4. TensorFlow笔记-文件读取

    小数量数据读取 这些只用于可以完全加载到内存中的小型数据集: 1,储存在常数中 2,储存在变量中,初始化后,永远不改变它的值 使用常量 training_data = ... training_lab ...

  5. 生成TFRecord文件完整代码实例

    import os import json def get_annotation_dict(input_folder_path, word2number_dict): label_dict = {} ...

  6. AI tensorflow模型文件

    tensorflow模型可以利用tf.train.Saver类保存成文件.一个模型包含下面四个文件. meta文件 存储计算图的protobuf. data-00000-of-00001文件和inde ...

  7. 吴裕雄 python 神经网络——TensorFlow TFRecord样例程序

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  8. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:将MNIST手写图片数据写入TFRecord文件

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  9. Android数据存储之Android 6.0运行时权限下文件存储的思考

    前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...

随机推荐

  1. Chrome VSCode常用快捷键

    MAC下快捷键 Chrome快捷键: 关闭标签页:Cmd + w 新建标签页:Cmd + t 切换到指定标签页:Cmd + 数字 正向切换标签页: Ctrl + Tab 反向切换标签页: Ctrl + ...

  2. .Net dependent configuration

    error info: 解决方案:在.exe.config文件中配置Newtonsoft.Json所用版本 <runtime> <assemblyBinding xmlns=&quo ...

  3. c#操作json的两种方式

    总结一下C#操作json的两种方式,都是将对象和json格式相转. 1.JavaScriptSerializer,继承自System.Web.Script.Serialization private ...

  4. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group t1,customer t2

    ### SQL: select t1.gid,t1.gname,t1.gvalue,t1.gtype, t1.gaddress,t1.gmembers, t1.gcode,t1.gphone, t2. ...

  5. golang协程踩坑记录

    1.主线程等待多个协程执行完毕后,再执行下面的程序.golang提供了一个很好用的工具. sync.WaitGroup下面是个简单的例子. 执行结果: 2.主线程主动去结束已经启动了的多个协程.执行结 ...

  6. kotlin 委托

    委托模式是软件设计模式中的一项基本技巧.在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理. Kotlin 直接支持委托模式,更加优雅,简洁.Kotlin 通过关键 ...

  7. 基于三层架构项目下的Ado【六】

    一.基于三层架构项目下的Ado增删改查总结,提示:现在一般都是使用EF框架操作. 1. 先在model层创建出一个和你将会查询出一样类型的表,比如你将查询出的有五个字段,那么你就需要创建出一个和你查询 ...

  8. Ubuntu16.04 使用sudo cat EOF 编辑文件,提示Permission denied错误的解决办法

    一.执行命令报错 在Ubuntu16.04下,使用如下命令,修改hosts主机文件,居然提示权限错误: catty@node186:~$ sudo cat <<EOF > /etc/ ...

  9. 自学PYTHON分享 --基础1

    1.python2和python3的区别: 宏观上:python2 与 python3 区别: python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码. 2.pyt ...

  10. Python 考试练习

    1.算法复杂度分为:时间复杂度和空间复杂度 一个算法的优劣主要从算法的执行时间和所需要占用的存储空间两个方面衡量. 时间复杂度:是指执行算法所需要的计算工作量,也即算法的执行时间  (注意:是算法的执 ...