gpt4 book ai didi

python - 检查 .tfrecords 文件

转载 作者:行者123 更新时间:2023-12-01 01:33:35 25 4
gpt4 key购买 nike

我使用以下代码创建了 images.tfrecoreds 文件

from PIL import Image
import numpy as np
import tensorflow as tf
import glob

images = glob.glob('E:\Projects/FYPT/vehicle/bus/*.jpg')

def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

tfrecords_filename = 'E:\Projects/FYPT/vehicle/images.tfrecords'

writer = tf.python_io.TFRecordWriter(tfrecords_filename)

original_images = []

for img_path in images:
img = np.array(Image.open(img_path))


height = img.shape[0]
width = img.shape[1]

# Put in the original images into array
# Just for future check for correctness
original_images.append((img))

img_raw = img.tostring()

example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(height),
'width': _int64_feature(width),
'image_raw': _bytes_feature(img_raw)
}))

writer.write(example.SerializeToString())

writer.close()

然后我尝试通过打印“serialized_example”的输出来检查 tf.TFRecordReader() 的输出

import tensorflow as tf
import skimage.io as io


reader = tf.TFRecordReader()
tfrecords_filename = 'E:\Projects/FYPT/vehicle/images.tfrecords'
filename_queue = tf.train.string_input_producer([tfrecords_filename],num_epochs=10)
_,serialized_example = reader.read(filename_queue)

sess= tf.Session()
print(sess.run(serialized_example))

但它给了我以下警告,并且没有给出“serialized_example”的任何输出 this是命令行的屏幕截图

我犯了什么错误,我应该如何打印“serialized_example”的输出

最佳答案

您收到该警告是因为您使用的 tf.train.string_input_ Producer() 返回一个队列,但基于 QueueRunner API 的输入管道已被弃用且不受支持在未来的版本中。

基于队列的解决方案 - 不推荐!

serialized_example 只是一个字符串对象(与使用 tf.python_io.TFRecordWriter 写入每个图像的 images.tfrecords 文件相同)例子)。

您需要解析每个示例以获取其功能。对于您的情况:

features = tf.parse_single_example(serialized_example,
features={"image_raw": tf.FixedLenFeature([], tf.string),
"height": tf.FixedLenFeature([], tf.int64) }

img_raw = tf.image.decode_jpeg(features["image_raw"])
img_height = features["height"]

# initialize global and local variables
init_op = tf.group(tf.local_variables_initializer(),
tf.global_variables_initializer())

with tf.Session() as sess:
sess.run(init_op)

# start a number of threads
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

try:
while not coord.should_stop():
img_raw_value, img_height_value = sess.run([img_raw, img_height])
print(img_raw_value.shape)
print(img_height_value)
except tf.errors.OutOfRangeError:
print("End of data")
finally:
coord.request_stop()

# wait for all threads to terminate
coord.join(threads)
sess.close()

数据集 API - 强烈推荐!

如何构建输入管道的详细说明可以参见 here: TensorFlow API .

在您的情况下,您应该像这样定义一个_parse_function:

def _parse_function(example_proto):
features={"imgage_raw": tf.FixedLenFeature([], tf.string),
"height": tf.FixedLenFeature([], tf.int64),
"width": tf.FixedLenFeature([], tf.int64)}

parsed_features = tf.parse_single_example(example_proto, features)

img_raw = tf.image.decode_jpeg(parsed_features["img_raw"])
height = parsed_features["height"]
width = parsed_features["width"]

return img_raw, height, width

然后创建一个数据集,从 TFRecord 文件中读取所有示例,并提取特征:

dataset = tf.data.TFRecordDataset([tfrecords_filename])
dataset = dataset.map(_parse_function)
# here you could batch and shuffle

iterator = dataset.make_one_shot_iterator()

next_element = iterator.get_next()

with tf.Session() as sess:
while True:
try:
val = sess.run(next_element)
print("img_raw:", val[0].shape)
print("height:", val[1])
print("width:", val[2])
except tf.errors.OutOfRangeError:
print("End of dataset")
break

我希望这会有所帮助。

关于python - 检查 .tfrecords 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52590787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com