def preprocess(image, size, max_length):
shape = tf.shape(image)
size_t = tf.constant(size, tf.float64)
height = tf.cast(shape[0], tf.float64)
width = tf.cast(shape[1], tf.float64)
cond_op = tf.less(width, height) if max_length else tf.less(height, width)
new_height, new_width = tf.cond(
cond_op, lambda: (size_t, (width * size_t) / height),
lambda: ((height * size_t) / width, size_t))
new_size = [tf.to_int32(new_height), tf.to_int32(new_width)]
resized_image = tf.image.resize_images(image, new_size)
normalised_image = resized_image - mean_pixel
return normalised_image
# max_length: Wether size dictates longest or shortest side. Default longest
评论列表
文章目录