Skip to content

Instantly share code, notes, and snippets.

@wronk
Created February 13, 2019 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wronk/5fe241d743572984b4943ffde7071088 to your computer and use it in GitHub Desktop.
Save wronk/5fe241d743572984b4943ffde7071088 to your computer and use it in GitHub Desktop.
TF Serving blog post: serving receiver function
import os
import os.path as op
import tensorflow as tf
HEIGHT, WIDTH, CHANNELS = 256, 256, 3
def serving_input_receiver_fn():
"""Convert string encoded images (like base64 strings) into preprocessed tensors"""
def decode_and_resize(image_str_tensor):
"""Decodes a single image string, preprocesses/resizes it, and returns a reshaped uint8 tensor."""
image = tf.image.decode_image(image_str_tensor, channels=CHANNELS,
dtype=tf.uint8)
image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])
return image
# Run preprocessing function on all images in batch
input_ph = tf.placeholder(tf.string, shape=[None], name='image_binary')
images_tensor = tf.map_fn(
decode_and_resize, input_ph, back_prop=False, dtype=tf.uint8)
# Cast to float32
images_tensor = tf.cast(images_tensor, dtype=tf.float32)
# Run Xception-specific preprocessing to scale images from [0, 255] to [-1, 1]
images_tensor = tf.subtract(tf.divide(images_tensor, 127.5), 1)
return tf.estimator.export.ServingInputReceiver(
{'input_1': images_tensor}, # The key here needs match the name of your model's first layer
{'image_bytes': input_ph}) # You can specify the key here, but this is a good default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment