Skip to content

Instantly share code, notes, and snippets.

@jeongukjae
Created January 17, 2020 06:58
Show Gist options
  • Save jeongukjae/59533224cba4979b91e38b94559e4c20 to your computer and use it in GitHub Desktop.
Save jeongukjae/59533224cba4979b91e38b94559e4c20 to your computer and use it in GitHub Desktop.
Tensorflow Serving Test (Preprocessing)
curl "http://localhost:8501/v1/models/test-model:predict" -XPOST -d '{"instances": ["test"]}' | jq
#!/bin/bash
docker run -t --rm -p 8501:8501 -v `pwd`/test-model:/models/test-model -eMODEL_NAME=test-model tensorflow/serving
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential(
[
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation="softmax"),
]
)
model.compile(
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
)
model.fit(x_train, y_train, epochs=1)
model.evaluate(x_test, y_test, verbose=2)
@tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
def serving(input_variable):
def _input_to_feature(_):
return tf.random.uniform((1, 28, 28))
input_tensor = tf.map_fn(_input_to_feature, input_variable, dtype=tf.float32)
# Predict
predictions = model(input_tensor)
return {"predictions": predictions}
tf.saved_model.save(model, "./test-model/1", signatures=serving)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment