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. Vue项目之实现登录功能的表单验证!

    Vue项目之实现登录功能的表单验证! 步骤: 配置 Form表单验证; 1.必须给el-from组件绑定model 为表单数据对象 2 给需要验证的表单项 el-form-item 绑定 prop 属 ...

  2. JavaScript中创建对象的三种方式!

    JavaScript中创建对象的三种方式! 第一种 利用对象字面量! // 创建对象的三种方式! // 1 对象字面量. var obj = { // 对象的属性和方法! name: 'lvhang' ...

  3. Jmeter函数助手大全

    __BeanShell 入参:BeanShell语法的程序语句或者Bean Shell脚本文件 示例: ${__BeanShell(123*456,)}:返回56088: ${__BeanShell( ...

  4. selenium浏览器弹出框alert 操作

    1.简介 在WebDriver中要处理JS生成的alert.confirm以及prompt,需要 switch_to.alert() 来选取(定位)警告弹窗,在对弹窗进行关闭.输入等信息操作. 2.操 ...

  5. vue-cli快速创建项目,可视化创建

    之前学习了交互式创建,发现过程无聊,而且不方便,后面又学习了图形可视化创建,下面进行分享 1.打开cmd 2.输入vue ui,输入后会出现如下 C:\Users\12235>vue ui St ...

  6. 自导自演的面试现场,趣学MySQL的10种文件

    导读 Hi,大家好!我是白日梦!本文是MySQL专题的第 24 篇. 今天我要跟你分享的MySQL话题是:"自导自演的数据库面试现场--谈谈MySQL的10种文件" 换一种写作风格 ...

  7. java native:Java本地方法调用(jni方式)

    https://www.cnblogs.com/zh1164/p/6283831.html

  8. 【LinuxShell】free 命令详解

    前言 free命令用来显示Linux中的内存使用信息,包括空闲的.已用的物理内存,swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 命令格式 ...

  9. new() 和 make() 的区别 var arr1 = new([5]int) var arr2 [5]int

    Effective Go - The Go Programming Language https://golang.org/doc/effective_go.html#allocation_new A ...

  10. Hive 报错

    hadoop hive任务失败,原因是GC overhead limit exceeded (OOM) GC Overhead Limit Exceeded error是java.lang.OutOf ...