River Tam
River Tam

Reputation: 3216

How to resize image to put into tf.train.Example

I have an image (JPEG or PNG) as a byte buffer (read from the internet), and this is the way I was putting it in a tf.train.Example before:

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
}))

However, for my usecase, the images are too big, so I'd like to resize them either before I put them in the tf.train.Example or just after (whichever is easiest).

Here's what I'm trying:

# 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
}))

I suspect this is valid right up until I actually try to put it in the tf.train.Example, at which point it tells me TypeError: <tf.Tensor 'EncodeJpeg:0' shape=() dtype=string> has type Tensor, but expected one of: bytes. I've tried figuring out how to get the Tensor into a BytesList or something like it, but I haven't been able to find any documentation for this. I suspect there may be a better way to approach the entire process however.

How can I do this the right way?

Upvotes: 0

Views: 553

Answers (1)

Sharky
Sharky

Reputation: 4533

You can resize prior to encoding.

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]))

Convert from string and resize

image = numpy.fromstring(byte_arr).reshape((height, width, channels))
image_raw = image.tostring()

Then serialize as tfrecords file

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()

Upvotes: 2

Related Questions