def preprocess_image_for_prediction(fixed_height: int=32, min_width: int=8):
"""
Input function to use when exporting the model for making predictions (see estimator.export_savedmodel)
:param fixed_height: height of the input image after resizing
:param min_width: minimum width of image after resizing
:return:
"""
def serving_input_fn():
# define placeholder for input image
image = tf.placeholder(dtype=tf.float32, shape=[None, None, 1])
shape = tf.shape(image)
# Assert shape is h x w x c with c = 1
ratio = tf.divide(shape[1], shape[0])
increment = CONST.DIMENSION_REDUCTION_W_POOLING
new_width = tf.cast(tf.round((ratio * fixed_height) / increment) * increment, tf.int32)
resized_image = tf.cond(new_width < tf.constant(min_width, dtype=tf.int32),
true_fn=lambda: tf.image.resize_images(image, size=(fixed_height, min_width)),
false_fn=lambda: tf.image.resize_images(image, size=(fixed_height, new_width))
)
# Features to serve
features = {'images': resized_image[None], # cast to 1 x h x w x c
'images_widths': new_width[None] # cast to tensor
}
# Inputs received
receiver_inputs = {'images': image}
return tf.estimator.export.ServingInputReceiver(features, receiver_inputs)
return serving_input_fn
评论列表
文章目录