<jupyter_start><jupyter_text>Deploy trained Keras or TensorFlow models using Amazon SageMakerAmazon SageMaker makes it easier for any developer or data scientist to build, train, and deploy machine learning (ML) models. While it’s designed to alleviate the undifferentiated heavy lifting from the full life cycle of ML models, Amazon SageMaker’s capabilities can also be used independently of one another; that is, models trained in Amazon SageMaker can be optimized and deployed outside of Amazon SageMaker (or even out of the cloud on mobile or IoT devices at the edge). Conversely, Amazon SageMaker can deploy and host pre-trained models from model zoos, or other members of your team.Amazon SageMaker deploy your model to a TensorFlow Serving-based server. The server provides a super-set of the [TensorFlow Serving REST API](https://www.tensorflow.org/serving/api_rest).To Deploy a Keras/TensorFlow Model, You'll need to save your model in a TensorFlow SavedModel format (Already implemented in the script).  for More Info take a look at the ```def save_model(model, output):``` in the script  Deploy a trained ModelInstead of retraining the model,  we'll start from one of the previously trained models.<jupyter_code>import os
import sagemaker
from sagemaker import get_execution_role

sagemaker_session = sagemaker.Session()

role = get_execution_role()<jupyter_output><empty_output><jupyter_text>Configure the estimator as it was configured in notebook:  1 - Monitoring your TensorFlow scripts<jupyter_code>from sagemaker.tensorflow import TensorFlow
estimator = ...
estimator = estimator.attach(training_job_name=)  ## Configure with your previous cifar10 job name (First Completed job. You can find that name from the SageMaker console
predictor = estimator.deploy(initial_instance_count=1,instance_type='ml.m4.xlarge')<jupyter_output><empty_output><jupyter_text>Make some predictionsTo verify the that the endpoint functions properly, we generate random data in the correct shape and get a prediction.<jupyter_code># Creating fake prediction data
import numpy as np
data = np.random.randn(1, 32, 32, 3)
print("Predicted class is {}".format(np.argmax(predictor.predict(data)['predictions'])))<jupyter_output><empty_output><jupyter_text>Calculating accuracy and create a confusion matrix based on the test datasetOur endpoint works as expected, we'll now use the test dataset for predictions and calculate our model accuracy<jupyter_code>from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
datagen = ImageDataGenerator()

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

def predict(data):
    predictions = predictor.predict(data)['predictions']
    return predictions
batch_size = 128
predicted = []
actual = []
batches = 0
for data in datagen.flow(x_test,y_test,batch_size=batch_size):
    for i,prediction in enumerate(predict(data[0])):
        predicted.append(np.argmax(prediction))
        actual.append(data[1][i][0])
    batches += 1
    if batches >= len(x_test) / batch_size:
        break
from sklearn.metrics import accuracy_score, confusion_matrix

accuracy = accuracy_score(y_pred=predicted,y_true=actual)
display('Average accuracy: {}%'.format(round(accuracy*100,2)))
%matplotlib inline
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt

cm = confusion_matrix(y_pred=predicted,y_true=actual)
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
sn.set(rc={'figure.figsize':(11.7,8.27)})
sn.set(font_scale=1.4)#for label size
sn.heatmap(cm, annot=True,annot_kws={"size": 10})# font size<jupyter_output><empty_output><jupyter_text>Using this heatmap we can calculate the accuracy of each one of the labels  Cleaning upTo avoid incurring charges to your AWS account for the resources used in this tutorial you need to delete the SageMaker Endpoint:<jupyter_code>sagemaker_session.delete_endpoint(predictor.endpoint)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08020050125313283, 'fraction_numerical': 0.009523809523809525, 'mean_word_length': 6.178057553956834, 'pattern_counts': {'":': 1, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '167494', 'license': 'MIT-0', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1154, 'n_words': 414, 'path': '4_Deploying_your_TensorFlow_model.ipynb', 'repo_name': 'aws-samples/amazon-sagemaker-workshop-with-tensorflow', 'size': '3990'}