<jupyter_start><jupyter_text>Use Keras to recognize hand-written digits with `ibm-watson-machine-learning`This notebook uses the Keras machine learning framework with the Watson Machine Learning service. It contains steps and code to work with [ibm-watson-machine-learning](https://pypi.python.org/pypi/ibm-watson-machine-learning) library available in PyPI repository. It also introduces commands for getting model and training data, persisting model, deploying model and scoring it.Some familiarity with Python is helpful. This notebook uses Python 3.8.  Learning goalsThe learning goals of this notebook are:-  Download an externally trained Keras model with dataset.-  Persist an external model in Watson Machine Learning repository.-  Deploy model for online scoring using client library.-  Score sample records using client library. ContentsThis notebook contains the following parts:1.	[Setup](setup)2.	[Download externally created Keras model and data](download)3.	[Persist externally created Keras model](upload)4.	[Deploy and score in a Cloud](deploy)5.  [Clean up](cleanup)6.	[Summary and next steps](summary)  1. Set up the environmentBefore you use the sample code in this notebook, you must perform the following setup tasks:-  Create a Watson Machine Learning (WML) Service instance (a free plan is offered and information about how to create the instance can be found here).  Connection to WMLAuthenticate the Watson Machine Learning service on IBM Cloud. You need to provide platform `api_key` and instance `location`.You can use [IBM Cloud CLI](https://cloud.ibm.com/docs/cli/index.html) to retrieve platform API Key and instance location.API Key can be generated in the following way:```ibmcloud loginibmcloud iam api-key-create API_KEY_NAME```In result, get the value of `api_key` from the output.Location of your WML instance can be retrieved in the following way:```ibmcloud login --apikey API_KEY -a https://cloud.ibm.comibmcloud resource service-instance WML_INSTANCE_NAME```In result, get the value of `location` from the output. **Tip**: Your `Cloud API key` can be generated by going to the [**Users** section of the Cloud console](https://cloud.ibm.com/iam/users). From that page, click your name, scroll down to the **API Keys** section, and click **Create an IBM Cloud API key**. Give your key a name and click **Create**, then copy the created key and paste it below. You can also get a service specific url by going to the [**Endpoint URLs** section of the Watson Machine Learning docs](https://cloud.ibm.com/apidocs/machine-learning).  You can check your instance location in your  Watson Machine Learning (WML) Service instance details.You can also get service specific apikey by going to the [**Service IDs** section of the Cloud Console](https://cloud.ibm.com/iam/serviceids).  From that page, click **Create**, then copy the created key and paste it below.**Action**: Enter your `api_key` and `location` in the following cell.<jupyter_code>api_key = 'PASTE YOUR PLATFORM API KEY HERE'
location = 'PASTE YOUR INSTANCE LOCATION HERE'
wml_credentials = {
    "apikey": api_key,
    "url": 'https://' + location + '.ml.cloud.ibm.com'
}<jupyter_output><empty_output><jupyter_text>Install and import the `ibm-watson-machine-learning` package**Note:** `ibm-watson-machine-learning` documentation can be found here.<jupyter_code>!pip install -U ibm-watson-machine-learning
from ibm_watson_machine_learning import APIClient

client = APIClient(wml_credentials)<jupyter_output><empty_output><jupyter_text>Working with spacesFirst of all, you need to create a space that will be used for your work. If you do not have space already created, you can use [Deployment Spaces Dashboard](https://dataplatform.cloud.ibm.com/ml-runtime/spaces?context=cpdaas) to create one.- Click New Deployment Space- Create an empty space- Select Cloud Object Storage- Select Watson Machine Learning instance and press Create- Copy `space_id` and paste it below**Tip**: You can also use SDK to prepare the space for your work. More information can be found [here](https://github.com/IBM/watson-machine-learning-samples/blob/master/cloud/notebooks/python_sdk/instance-management/Space%20management.ipynb).**Action**: Assign space ID below<jupyter_code>space_id = 'PASTE YOUR SPACE ID HERE'<jupyter_output><empty_output><jupyter_text>You can use `list` method to print all existing spaces.<jupyter_code>client.spaces.list(limit=10)<jupyter_output><empty_output><jupyter_text>To be able to interact with all resources available in Watson Machine Learning, you need to set **space** which you will be using.<jupyter_code>client.set.default_space(space_id)<jupyter_output><empty_output><jupyter_text>2. Download externally created Keras model and dataIn this section, you will download externally created Keras models and data used for training it.<jupyter_code>import os
import wget

data_dir = 'MNIST_DATA'
if not os.path.isdir(data_dir):
    os.mkdir(data_dir)
    
model_path = os.path.join(data_dir, 'mnist_keras.h5.tgz')
if not os.path.isfile(model_path):
    wget.download("https://github.com/IBM/watson-machine-learning-samples/raw/master/cloud/models/keras/mnist_keras.h5.tgz", out=data_dir)
import os
import wget

data_dir = 'MNIST_DATA'
if not os.path.isdir(data_dir):
    os.mkdir(data_dir)
    
filename = os.path.join(data_dir, 'mnist.npz')
if not os.path.isfile(filename):
    wget.download('https://s3.amazonaws.com/img-datasets/mnist.npz', out=data_dir)  
import numpy as np

dataset = np.load(filename)
x_test = dataset['x_test']<jupyter_output><empty_output><jupyter_text>3. Persist externally created Keras model In this section, you will learn how to store your model in Watson Machine Learning repository by using the Watson Machine Learning Client.  3.1: Publish model  Publish model in Watson Machine Learning repository on Cloud. Define model name, type and software specification needed to deploy model later.<jupyter_code>sofware_spec_uid = client.software_specifications.get_id_by_name("default_py3.8")
metadata = {
            client.repository.ModelMetaNames.NAME: 'External Keras model',
            client.repository.ModelMetaNames.TYPE: 'tensorflow_2.4',
            client.repository.ModelMetaNames.SOFTWARE_SPEC_UID: sofware_spec_uid
}

published_model = client.repository.store_model(
    model=model_path,
    meta_props=metadata)<jupyter_output><empty_output><jupyter_text>3.2: Get model details<jupyter_code>import json

published_model_uid = client.repository.get_model_uid(published_model)
model_details = client.repository.get_details(published_model_uid)
print(json.dumps(model_details, indent=2))<jupyter_output>{
  "entity": {
    "software_spec": {
      "id": "2b73a275-7cbf-420b-a912-eae7f436e0bc"
    },
    "type": "keras_2.2.5"
  },
  "metadata": {
    "created_at": "2020-08-11T09:14:37.258Z",
    "id": "2b5a3f9e-2c20-4e25-8586-dff0309d512a",
    "modified_at": "2020-08-11T09:14:42.535Z",
    "name": "External Keras model",
    "owner": "IBMid-310002AFR6",
    "space_id": "968d9b1a-3cb4-4029-bc57-f53ec5e8743b"
  }
}<jupyter_text>3.3 Get all models<jupyter_code>models_details = client.repository.list_models()<jupyter_output><empty_output><jupyter_text>4. Deploy and score in a Cloud In this section you will learn how to create online scoring and to score a new data record by using the Watson Machine Learning Client.  4.1: Create model deployment  Create online deployment for published model<jupyter_code>metadata = {
    client.deployments.ConfigurationMetaNames.NAME: "Deployment of external Keras model",
    client.deployments.ConfigurationMetaNames.ONLINE: {}
}

created_deployment = client.deployments.create(published_model_uid, meta_props=metadata)<jupyter_output>#######################################################################################

Synchronous deployment creation for uid: '2b5a3f9e-2c20-4e25-8586-dff0309d512a' started

#######################################################################################


initializing.
ready


------------------------------------------------------------------------------------------------
Successfully finished deployment creation, deployment_uid='1a11f778-d430-4c41-bbba-9d116205f906'
------------------------------------------------------------------------------------------------<jupyter_text>**Note**: Here we use deployment url saved in published_model object. In next section, we show how to retrive deployment url from Watson Machine Learning instance.<jupyter_code>deployment_uid = client.deployments.get_uid(created_deployment)<jupyter_output><empty_output><jupyter_text>Now you can print an online scoring endpoint.<jupyter_code>scoring_endpoint = client.deployments.get_scoring_href(created_deployment)
print(scoring_endpoint)<jupyter_output>https://yp-qa.ml.cloud.ibm.com/ml/v4/deployments/1a11f778-d430-4c41-bbba-9d116205f906/predictions<jupyter_text>You can also list existing deployments.<jupyter_code>client.deployments.list()<jupyter_output><empty_output><jupyter_text>4.2: Get deployment details<jupyter_code>client.deployments.get_details(deployment_uid)<jupyter_output><empty_output><jupyter_text>4.3: Score You can use below method to do test scoring request against deployed model. Let's first visualize two samples from dataset, we'll use for scoring.<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
for i, image in enumerate([x_test[0], x_test[1]]):
    plt.subplot(2, 2, i + 1)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')<jupyter_output><empty_output><jupyter_text>Prepare scoring payload with records to score.<jupyter_code>score_0 = (x_test[0].ravel() / 255).tolist()
score_1 = (x_test[1].ravel() / 255).tolist()
scoring_payload = {"input_data": [{"values": [score_0, score_1]}]}<jupyter_output><empty_output><jupyter_text>Use ``client.deployments.score()`` method to run scoring.<jupyter_code>predictions = client.deployments.score(deployment_uid, scoring_payload)
print(json.dumps(predictions, indent=2))<jupyter_output>{
  "predictions": [
    {
      "fields": [
        "prediction",
        "prediction_classes",
        "probability"
      ],
      "values": [
        [
          [
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            0.0
          ],
          7,
          [
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            0.0
          ]
        ],
        [
          [
            0.0,
            0.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0
          ],
          2,
          [
            0.0,
            0.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0
          ]
        ]
      ]
    }[...]
{'fraction_non_alphanumeric': 0.1305686345526983, 'fraction_numerical': 0.02761680550525172, 'mean_word_length': 3.600166597251145, 'pattern_counts': {'":': 18, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '4660', 'license': 'Apache-2.0', 'n_tokens_mistral': 3495, 'n_tokens_neox': 3303, 'n_words': 941, 'path': 'cloud/notebooks/python_sdk/deployments/keras/Use Keras to recognize hand-written digits.ipynb', 'repo_name': 'muthukumarbala07/watson-machine-learning-samples', 'size': '11044'}