如何将 TFRecords 转换为 numpy 数组? [英] How can I convert TFRecords into numpy arrays?
本文介绍了如何将 TFRecords 转换为 numpy 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
主要思想是将 TFRecords 转换为 numpy 数组.假设 TFRecord 存储图像.具体:
- 读取 TFRecord 文件并将每个图像转换为一个 numpy 数组.
- 将图片写入1.jpg、2.jpg等格式
- 同时将文件名和标签写入文本文件中,如下所示:<块引用>
1.jpg 22.jpg 43.jpg 5
我目前使用以下代码:
将tensorflow导入为tf导入操作系统def read_and_decode(filename_queue):阅读器 = tf.TFRecordReader()_, serialized_example = reader.read(filename_queue)特征 = tf.parse_single_example(serialized_example,# 未指定默认值,因为两个键都是必需的.特征={'image_raw': tf.FixedLenFeature([], tf.string),'标签':tf.FixedLenFeature([], tf.int64),'高度':tf.FixedLenFeature([], tf.int64),'宽度':tf.FixedLenFeature([], tf.int64),'深度':tf.FixedLenFeature([], tf.int64)})图像 = tf.decode_raw(features['image_raw'], tf.uint8)标签 = tf.cast(功能['标签'], tf.int32)高度 = tf.cast(特征['高度'], tf.int32)宽度 = tf.cast(功能['宽度'], tf.int32)深度 = tf.cast(特征['深度'],tf.int32)返回图像、标签、高度、宽度、深度使用 tf.Session() 作为 sess:filename_queue = tf.train.string_input_producer(["../data/svhn/svhn_train.tfrecords"])图像、标签、高度、宽度、深度 = read_and_decode(filename_queue)image = tf.reshape(image, tf.pack([height, width, 3]))image.set_shape([32,32,3])init_op = tf.initialize_all_variables()sess.run(init_op)打印 (image.eval())
我只是在阅读,试图为初学者获得至少一张图片.当我运行这个代码时,代码就卡住了.
解决方案
糟糕,我犯了一个愚蠢的错误.我使用了 string_input_producer 但忘记运行 queue_runners.
使用 tf.Session() 作为 sess:filename_queue = tf.train.string_input_producer(["../data/svhn/svhn_train.tfrecords"])图像、标签、高度、宽度、深度 = read_and_decode(filename_queue)image = tf.reshape(image, tf.pack([height, width, 3]))image.set_shape([32,32,3])init_op = tf.initialize_all_variables()sess.run(init_op)坐标 = tf.train.Coordinator()线程 = tf.train.start_queue_runners(coord=coord)对于我在范围内(1000):例如,l = sess.run([图像,标签])打印(例如,l)coord.request_stop()coord.join(线程)
The main idea is to convert TFRecords into numpy arrays. Assume that the TFRecord stores images. Specifically:
- Read a TFRecord File and convert each image into a numpy array.
- Write the image into 1.jpg, 2.jpg, etc.
- At the same time, write the file name and label to the text file like this:
1.jpg 2 2.jpg 4 3.jpg 5
I currently use the following code:
import tensorflow as tf
import os
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64)
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
label = tf.cast(features['label'], tf.int32)
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
depth = tf.cast(features['depth'], tf.int32)
return image, label, height, width, depth
with tf.Session() as sess:
filename_queue = tf.train.string_input_producer(["../data/svhn/svhn_train.tfrecords"])
image, label, height, width, depth = read_and_decode(filename_queue)
image = tf.reshape(image, tf.pack([height, width, 3]))
image.set_shape([32,32,3])
init_op = tf.initialize_all_variables()
sess.run(init_op)
print (image.eval())
I'm just reading trying to get at least one image for starters. The code just gets stuck when I run this.
解决方案
Oops, it was a silly mistake on my part. I used a string_input_producer but forgot to run the queue_runners.
with tf.Session() as sess:
filename_queue = tf.train.string_input_producer(["../data/svhn/svhn_train.tfrecords"])
image, label, height, width, depth = read_and_decode(filename_queue)
image = tf.reshape(image, tf.pack([height, width, 3]))
image.set_shape([32,32,3])
init_op = tf.initialize_all_variables()
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1000):
example, l = sess.run([image, label])
print (example,l)
coord.request_stop()
coord.join(threads)
这篇关于如何将 TFRecords 转换为 numpy 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文