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) ...
随机推荐
- 如何安装快速 Docker 和 Docker-Compose 服务
最近由于个人在大家基于 Docker 的.企业级的CI/CD 环境,所以要安装 Docker 和 Docker-Compose ,这也算是一个学习过程,就把整个过程记录下来,便于以后查询. 测试环境 ...
- ldf和mdf文件怎么还原到sqlserver数据库
1.把mdf文件和ldf文件拷贝到数据库的默认路径C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA里:2.在sq ...
- ADB命令连接逍遥模拟器
注:打开模拟器开发者模式 ->USB调试模式 1.先进入逍遥模拟器安装目录(MEmu文件夹下),如:D:\Program Files\Microvirt\MEmu 2.在CMD下输入:adb c ...
- Azure DevOps Pipelines执行RobotFramework自动化代码
一.Azure DevOps介绍 1.什么是 Azure DevOps? Azure DevOps其实是VSTS(Visual Studio Team Service)更名后的名字.而VSTS是TFS ...
- Manachar’s Algorithm
Manachar's Algorithm Longest palindromic substring - Wikipedia https://en.wikipedia.org/wiki/Longes ...
- 能够满足这样要求的哈希算法有很多,其中比较著名并且应用广泛的一个哈希算法,那就是MurmurHash 算法。尽管这个哈希算法在 2008 年才被发明出来,但现在它已经广泛应用到 Redis、MemCache、Cassandra、HBase、Lucene 等众多著名的软件中。
能够满足这样要求的哈希算法有很多,其中比较著名并且应用广泛的一个哈希算法,那就是MurmurHash 算法.尽管这个哈希算法在 2008 年才被发明出来,但现在它已经广泛应用到 Redis.MemCa ...
- 从零开始学Java (三)基础语法
1. 基本数据类型 整数类型:byte,short,int,long 浮点数类型:float,double 字符类型:char 布尔类型:boolean java最小单位是bit,一个byte占用8个 ...
- Spring Cloud 2020.0.1 正式发布!真是头疼。。。
上一篇:Spring Cloud 2020.0.0 正式发布,全新颠覆性版本! 号外!号外!号外! Spring Cloud 2020.0.0 在去年 12 月底,赶在一年的尾巴最后几天仓促发布了,时 ...
- loj10007线段
题目描述 数轴上有 n 条线段,选取其中 k 条线段使得这 k 条线段两两没有重合部分,问 k 最大为多少. 输入格式 第一行为一个正整数 n: 在接下来的 n 行中,每行有 2 个数 a_i,b_i ...
- RocketMQ 常用消息类型
文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/dYqGd9zi2mNelsNNLIribg 消息发送示例 导入依赖: <depend ...