Validating the Resnet Servable

Use this notebook to validate the servable that you just created, whether using Estimator or Keras! This notebook can be used to validate that the servable you just trained works as intended by mocking part of the resnet client and feeding the input to the network.

NOTE: If you're building the ResNet servable from the Estimator API, the below validation only works if you trained ResNet with 'channels_last'. Not to fear! If the servable is validated using 'channels_last' mode, it will work with 'channels_first' mode.


In [0]:
import io
import os

from tensorflow.contrib import predictor
from PIL import Image

In [0]:
# Create a predict function from your saved model (servable)
# TODO: Use your own exported resnet serving directory and version number from the output of the last cell in
# `estimator_training_to_serving.ipynb` or `keras_training_to_serving.ipynb`.
SERVING_DIR = 'estimator_servable | keras_servable'
VERSION = ???

predict_fn = predictor.from_saved_model(os.path.join(SERVING_DIR, str(VERSION)),
                                        signature_def_key='predict')

In [0]:
TOP_K = 5  # Set this depending on how many classes the server is expected to return
NUM_CLASSES = 1001

# Load a sample jpeg, and send it to the predict function (TF serving emulator)
IMAGE_PATH='client/cat_sample.jpg'

image = Image.open(IMAGE_PATH)
encoded = io.BytesIO()
image.save(encoded, format='JPEG')
feature = encoded.getvalue()
predictions = predict_fn(
    {'images': [feature]})

# Run some test cases
classes = predictions['classes']
probabilities = predictions['probabilities']
assert len(classes[0]) == TOP_K
assert len(probabilities[0]) == TOP_K
assert max(probabilities[0]) <= 1
assert max(probabilities[0]) >= 1.0 / NUM_CLASSES
assert min(probabilities[0]) >= 0
# Print your model output for inspection
print('Model passes unit test! Here are some predictions:\n')
print(predictions)

# If this succeeds, congratulations! Your model (probably) works!
# You should however, double check whether the model was correct in its prediction too...

In [0]: