def prepare_serialized_examples(self, serialized_examples, width=50, height=50):
# set the mapping from the fields to data types in the proto
feature_map = {
'image': tf.FixedLenFeature((), tf.string, default_value=''),
'image_id': tf.FixedLenFeature((), tf.string, default_value=''),
}
features = tf.parse_example(serialized_examples, features=feature_map)
def decode_and_resize(image_str_tensor):
"""Decodes png string, resizes it and returns a uint8 tensor."""
# Output a grayscale (channels=1) image
image = tf.image.decode_png(image_str_tensor, channels=1)
# Note resize expects a batch_size, but tf_map supresses that index,
# thus we have to expand then squeeze. Resize returns float32 in the
# range [0, uint8_max]
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(
image, [height, width], align_corners=False)
image = tf.squeeze(image, squeeze_dims=[0])
image = tf.cast(image, dtype=tf.uint8)
return image
images_str_tensor = features["image"]
images = tf.map_fn(
decode_and_resize, images_str_tensor, back_prop=False, dtype=tf.uint8)
images = tf.image.convert_image_dtype(images, dtype=tf.float32)
images = tf.subtract(images, 0.5)
images = tf.multiply(images, 2.0)
image_id = features["image_id"]
return image_id, images
评论列表
文章目录