tfrecords转np.array
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的更多相关文章
- 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 ...
- 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 ...
- np.array.all()和np.array.any()函数
np.array.all()是对np.array中所有元素进行与操作,然后结果返回True或False np.array.any()是对np.array中所有元素进行或操作,然后结果返回True或Fa ...
- Numpy np.array 相关常用操作学习笔记
1.np.array构造函数 用法:np.array([1,2,3,4,5]) 1.1 numpy array 和 python list 有什么区别? 标准Python的列表(list)中,元素本质 ...
- 判断np.array里面为空字符串的方法
#多在编译器里尝试新操作 import numpy as np for i range(100): eval1 = {"A": ''"} eval2 = {"A ...
- np.array与np.ndarray区别
(Numpy中ndarray和array的区别是什么?我在哪儿能够找到numpy中相应的实现?) 答:Well, np.array is just a convenience function to ...
- np.array()
将列表list或元组tuple转换为 ndarray 数组. numpy.array(object, dtype=None, copy=True, order=None, subok=False, n ...
- numpy 下的数据结构与数据类型的转换(np.array vs. np.asarray)
1. np.asarray -- numpy 风格的类型转换 从已有多维数组创建新的多维数组,数据类型可重新设置 >> B = np.asarray(A, dtype='int32') 2 ...
- 浮点型数据需要转化为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) ...
随机推荐
- Linux安装Oracle数据库SQLPlus客户端
安装 RPM包下载地址:https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html ...
- 解决Ajax同源政策的方法【JSONP + CORS + 服务器端解决方案】
解决Ajax同源政策的方法 使用JSONP解决同源限制问题 jsonp是json with padding的缩写,它不属于Ajax请求,但它可以模以Ajax请求.\ 步骤 1.将不同源的服务器端请求地 ...
- Java面向对象(三)—— 继承
标签: java 继承 抽象类 this super abstract 概述 多个类中存在相同的属性和行为的时候,将这些内容抽取到单独一个类中,那么多个类无需在定义这些属性和行为,只要继承那个类即可. ...
- 网易新闻App架构重构实践:DDD正走向流行
网易新闻App架构重构实践:DDD正走向流行 https://mp.weixin.qq.com/s/FdwrT_xn3CQqpWoRVBttvQ 小智 InfoQ 2020-05-14 作者 | 小智 ...
- ResponseEntity和@ResponseBody以及@ResponseStatus区别
看的迷迷糊糊的 https://www.jdon.com/springboot/responseentity.html
- Grafana部署监控docker服务
Grafana部署监控docker服务 一.使用InfluxDB+cAdvisor+Grafana配置Docker监控 1.1Docker监控组件 1.2cAdvisor: 1.3Docker监控安装 ...
- Golang之垃圾回收
本篇主要是参考了: http://legendtkl.com/2017/04/28/golang-gc/ 说是参考,但其实基本上是原封不动. GC算法简介: 1. 引用计数 引用计数的思想非常简单:每 ...
- Ajax(form表单文件上传、请求头之contentType、Ajax传递json数据、Ajax文件上传)
form表单文件上传 上菜 file_put.html <form action="" method="post" enctype="multi ...
- shell (颜色输出)
摘自https://www.cnblogs.com/t-road/p/10257296.html #!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:30-37 echo - ...
- zookper投票机制
前提:已经搭建好zookper集群 1.先开启编号为01的服务器 2.开启编号为02的服务器,状态为leader,编号为01的变成follower 3.开启编号为03的服务器,状态为follower ...