gpt4 book ai didi

python - PNG TO TFRecord : error : has type numpy. ndarray,但应为以下之一:字节

转载 作者:行者123 更新时间:2023-12-05 05:13:38 35 4
gpt4 key购买 nike

我正在为 tfrecord 制作一个 .png 文件。

def _bytes_feature(value):
"""Wrapper for inserting bytes features into Example proto."""
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

转换为示例文件:

def _convert_to_example(filename, image_buffer, label, text, height, width):
example = tf.train.Example(features=tf.train.Features(feature={
'image/height': _int64_feature(height),
'image/width': _int64_feature(width),
........
'image/encoded': _bytes_feature(image_buffer)})) #Error
return example

decode_png文件,不确定这部分

def decode_png(image_data):

img_str = image_data.tostring()
reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

return reconstructed_img

处理图像文件:

def _process_image(filename):

# Read the image file.
with open(filename, 'r') as f:
image_data = io.imread(f)

# Decode the RGB PNG.
image = decode_png(image_data)

# Check that image converted to RGB
assert len(image.shape) == 3
height = image.shape[0]
width = image.shape[1]
assert image.shape[2] == 3

return image_data, height, width

主要,处理图像文件批处理:

for i in files_in_shard:
filename = filenames[i]
label = labels[i]
text = texts[i]

image_buffer, height, width = _process_image(filename)

example = _convert_to_example(filename, image_buffer, label,
text, height, width)
writer.write(example.SerializeToString())
shard_counter += 1
counter += 1

但是,有一个错误是 TypeError: array([[[223, 198, 219],
[215, 185, 209],
[207, 174, 201],
...,
[230 具有 numpy.ndarray 类型,但应为以下之一:bytes

我该如何处理?任何帮助都会很棒。谢谢

最佳答案

好的,我明白了......逻辑专家

def decode_png(image_data):

img_str = image_data.tostring()
reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

return img_str


def _process_image(filename):
# Read the image file.
with open(filename, 'r') as f:
image_data = io.imread(f)

# Decode the RGB PNG.
img_str = image_data.tostring()

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

return img_str, height, width

关于python - PNG TO TFRecord : error : has type numpy. ndarray,但应为以下之一:字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53382268/

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