import tensorflow as tf
import numpy as np
from keras.utils import to_categorical
import sys def tfrecord2array(path_res):
imgs = []
lbls = []
# print('tfrecords_files to be transformed:', path_res)
reader = tf.TFRecordReader() filename_queue = tf.train.string_input_producer([path_res], num_epochs=1) # 从 TFRecord 读取内容并保存到 serialized_example 中
_, serialized_example = reader.read(filename_queue)
# 读取 serialized_example 的格式
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
}) # 解析从 serialized_example 读取到的内容
labels = tf.cast(features['label'], tf.int64)
images = tf.decode_raw(features['image_raw'], tf.uint8) # print('Extracting {} has just started.'.format(path_res))
with tf.Session() as sess:
# 启动多线程
sess.run(tf.local_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
while not coord.should_stop():
try:
label, img = sess.run([labels, images])
except tf.errors.OutOfRangeError:
print("Turn to next folder.")
break
img = (img > 0).astype(np.uint8).reshape(-1)
imgs.append(img)
lbls.append(label)
clock_lines = ['-', '\\', '|', '/'] sys.stdout.write(
''.join((str(np.array(lbls).shape[0]),
"-th sample in ",
path_res.split('/')[-2],
clock_lines[np.array(lbls).shape[0]//100 % 4],
'\r')))
sys.stdout.flush() coord.request_stop()
coord.join(threads)
return to_categorical(np.array(lbls), num_classes=68), np.array(imgs) def main():
imgs, labels = tfrecord2array(
r"./data_tfrecords/integers_tfrecords/test.tfrecords")
print("imgs.shape:", imgs.shape)
print("labels.shape:", labels.shape) if __name__ == '__main__':
main()

tfrecords转np.array的更多相关文章

  1. python 有关矩阵行列的存取 np.array

    初始化 a = range() a = np.array(a) a = a.reshape(,) a [[ 0  1  2  3]  [ 4  5  6  7]  [ 8  9 10 11]  [12 ...

  2. np.array转换为list,嵌套的python list转成一个一维的python list

    np.array转换为list 1 meitan = shuju.iloc[start:end, 1:2] zhengqi = shuju.iloc[start:end,2:3] print(type ...

  3. np.array.all()和np.array.any()函数

    np.array.all()是对np.array中所有元素进行与操作,然后结果返回True或False np.array.any()是对np.array中所有元素进行或操作,然后结果返回True或Fa ...

  4. Numpy np.array 相关常用操作学习笔记

    1.np.array构造函数 用法:np.array([1,2,3,4,5]) 1.1 numpy array 和 python list 有什么区别? 标准Python的列表(list)中,元素本质 ...

  5. 判断np.array里面为空字符串的方法

    #多在编译器里尝试新操作 import numpy as np for i range(100): eval1 = {"A": ''"} eval2 = {"A ...

  6. np.array与np.ndarray区别

    (Numpy中ndarray和array的区别是什么?我在哪儿能够找到numpy中相应的实现?) 答:Well, np.array is just a convenience function to ...

  7. np.array()

    将列表list或元组tuple转换为 ndarray 数组. numpy.array(object, dtype=None, copy=True, order=None, subok=False, n ...

  8. numpy 下的数据结构与数据类型的转换(np.array vs. np.asarray)

    1. np.asarray -- numpy 风格的类型转换 从已有多维数组创建新的多维数组,数据类型可重新设置 >> B = np.asarray(A, dtype='int32') 2 ...

  9. 浮点型数据需要转化为int,才能作为点,被读取abc = np.array(abc, dtype=np.int)

    import cv2 import numpy as np import matplotlib.pyplot as plt img = 'test.jpg' img = cv2.imread(img) ...

随机推荐

  1. Flask源码流程分析(一)

    Flask源码流程分析: 1.项目启动: 1.实例化Flask对象 1. 重要的加载项: * url_rule_class = Rule * url_map_class = Map * session ...

  2. uni-app开发经验分享三: Vuex实现登录和用户信息留存

    在做用户登录的过程中,其实最重要的是登录成功后的数据要怎么储存,储存到哪里,这里我分享一个利用vuex来实现用户登录和用户数据留存的方法 vuex代码如下: //引入vue和vuex import V ...

  3. 使用 IPset 进行端口敲门

    端口敲门(Port knocking)技术,即服务通过动态的添加 iptables 规则来隐藏系统开启的服务,使用自定义的一系列序列号来「敲门」,使系统开启需要访问的服务端口,才能对外访问.不使用时, ...

  4. 数位DP笔记

    数位DP 1.定义: 数位dp是一种计数用的dp,一般就是要统计一个区间[L,R]内满足一些条件数的个数.所谓数位dp,字面意思就是在数位上进行dp: 数位的含义:一个数有个位.十位.百位.千位... ...

  5. JavaWeb——B/S,C/S结构,HTTP协议

    B/S: 开发基于B/S结构项目:目前主要采用三种服务器端语言:JSP,PHP,ASP.NET. 这三种语言构成三种常用应用开发组合:JSP+Oracle组合.PHP+MySQL体系.以及ASP.NE ...

  6. 函数式编程(logging日志管理模块)

    本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四大组件记录日志 配置logging的几种方式 向日 ...

  7. 读取EXCEL文档解析工具类

    package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException ...

  8. OpenStack (云计算与openstck简介)

    云计算 什么是云计算 云计算是一种按使用量付费的模式,这种模式提供可用的,便捷的,按需的网络访问,通过互联网进入可配置的计算资源共享池(资源包括,计算,存储,应用软件和服务) 云计算的特征 易于管理: ...

  9. MyBatis逆向工程生成dao层增删改查方法解释使用(转载)

    int countByExample(BUserExample example); //根据条件查询数量 /** * 示例 * public int countByExample() { * BUse ...

  10. 最短路径问题---Floyed(弗洛伊德算法),dijkstra算法,SPFA算法

    在NOIP比赛中,如果出图论题最短路径应该是个常考点. 求解最短路径常用的算法有:Floyed算法(O(n^3)的暴力算法,在比赛中大概能过三十分) dijkstra算法 (堆优化之后是O(MlogE ...