Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ClementWalter
Created September 23, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClementWalter/cbb63dd07cecb06cba7d8814284564b7 to your computer and use it in GitHub Desktop.
Save ClementWalter/cbb63dd07cecb06cba7d8814284564b7 to your computer and use it in GitHub Desktop.
Signatures to decode a base64 image and serve inference
@tf.function(input_signature=(tf.TensorSpec(shape=[None], dtype=tf.string),))
def decode(image_bytes):
"""
Takes a base64 encoded image and returns the preprocessed input tensor ready for the inference
"""
# not working on GPU if tf.__version__ < 2.3, see https://github.com/tensorflow/tensorflow/issues/28007
with tf.device("/cpu:0"):
input_tensor = tf.map_fn(
lambda x: preprocessing(tf.io.decode_jpeg(contents=tf.io.decode_base64(x), channels=3))["output_0"],
image_bytes,
dtype=tf.float32,
)
return input_tensor
@tf.function(input_signature=(tf.TensorSpec(shape=[None], dtype=tf.string),))
def decode_and_serve(image_bytes):
"""
Apply preprocessing, inference and format output for response
"""
return {
tf.saved_model.CLASSIFY_OUTPUT_SCORES: classifier(decode(image_bytes=image_bytes)),
tf.saved_model.CLASSIFY_OUTPUT_CLASSES: classifier.layers[1].columns,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment