我有一个图像(JPEG或PNG)作为字节缓冲区(可从互联网读取),这是我之前将其放在tf.train.Example
中的方式:
record = tf.train.Example(features=tf.train.Features(feature={
'image/encoded': dataset_util.bytes_feature(image_bytes)
# there are more features but they're not relevant
}))
但是,对于我的用例而言,图像太大了,因此我想在将它们放入tf.train.Example
之前或之后(以最简单的为准)调整它们的大小。
这就是我要尝试的:
# predeclared
# - image_bytes
# - image_format
# - height
# - width
# resize image
if image_format == b'jpeg':
image = tf.image.decode_jpeg(image_bytes, None, tf.float32)
elif image_format == b'png':
image = tf.image.decode_png(image_bytes, None, tf.float32)
image = tf.image.resize_images(image, (int(height), int(width)))
image = tf.image.convert_image_dtype(image, tf.uint8)
record = tf.train.Example(features=tf.train.Features(feature={
'image/encoded': dataset_util.bytes_feature(tf.image.encode_jpeg(image))
# there are more features but they're not relevant
}))
我怀疑这是正确的,直到我实际尝试将其放入tf.train.Example
为止,此时它告诉我TypeError: <tf.Tensor 'EncodeJpeg:0' shape=() dtype=string> has type Tensor, but expected one of: bytes
。我试图弄清楚如何将Tensor
放入BytesList
或类似的文件中,但是我还没有找到任何相关的文档。我怀疑可能有更好的方法来处理整个过程。
如何正确执行此操作?
答案 0 :(得分:2)
您可以在编码之前调整大小。
def int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
从字符串转换并调整大小
image = numpy.fromstring(byte_arr).reshape((height, width, channels))
image_raw = image.tostring()
然后序列化为tfrecords文件
writer = tf.python_io.TFRecordWriter(tfr_name)
example = tf.train.Example(features=tf.train.Features(feature{'height':int64_feature(height),
'width': int64_feature(width),
'channels': int64_feature(channels),
'image_raw': bytes_feature(image_raw),
writer.write(example.SerializeToString())
writer.close()