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. 使用EFCore连接Oracle数据库时出现的问题

    问题 System.TypeLoadException: Method 'Create' in type 'Oracle.EntityFrameworkCore.Infrastructure.Inte ...

  2. 阿里 Mock 工具正式开源,干掉市面上所有 Mock 工具!

    最近栈长注意到阿里开源了自家的 Mock 工具:TestableMock,该工具号称最轻量.简单.舒适的 Mock 测试工具,功能十分强大,媲美 PowerMock,用法比 Mockito 还要简洁, ...

  3. XShell下便捷上载/下载文件到虚拟机

    1.客户机联网后,安装 rz,sz 服务,命令如下: yum install lrzsz 2.XShell连接客户机: 2.1 上传文件:运行rz,在弹窗内选择Windows本地文件上传到客户机当前目 ...

  4. CPU处理器架构和工作原理浅析

    CPU处理器架构和工作原理浅析 http://c.biancheng.net/view/3456.html 汇编语言是学习计算机如何工作的很好的工具,它需要我们具备计算机硬件的工作知识. 基本微机设计 ...

  5. 在Ubuntu安装kubernetes

    一.安装Docker 1. 配置Docker docker安装完成后需要配置cgroup驱动为systemd来增强稳定性 sudo vim /etc/docker/daemon.json { &quo ...

  6. 某商城系统(V1.3-2020-01-10)前台命令执行漏洞

    漏洞文件: ./inc/module/upload_img.php 先跟进 del_file 函数: 在 del_file 函数中首先执行了unlink操作,然后接着进行了file_exists 判断 ...

  7. Spring听课笔记(tg)

    0. 地址:https://www.bilibili.com/video/av21335209 1.综述,Spring主要的复习要点集中在以下几点 -- Spring的整体结构,Maven依赖(环境搭 ...

  8. K8s 二、(1、kubeadm部署Kubernetes集群)

    准备工作 满足安装 Docker 项目所需的要求,比如 64 位的 Linux 操作系统.3.10 及以上的内核版本: x86 或者 ARM 架构均可: 机器之间网络互通,这是将来容器之间网络互通的前 ...

  9. Spring|SpringMVC中的注解

    文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...

  10. Django(图书管理系统)

    图书管理系统 注意事项 1.models 要创建好,规划好自己的表,以及各种表关系 2.url正则要写好 3.settings的配置 4.利用bootstarp 进行布局更漂亮哦 5.注意orm  各 ...