Using NeuNetS model deployments

You can deploy your NeuNetS model to your IBM Watson Machine Learning service from the NeuNetS tool. This topic describes how to use a model you have built in NeuNetS and then deployed to your Watson Machine Learning service.

 

You can use your deployed NeuNetS model the same way you would use any model you deploy to Watson Machine Learning as a web service. The following examples demonstrate classifying text and images using sample models built in NeuNetS and deployed to Watson Machine Learning. The sample Python code can be run in a notebook in IBM Watson Studio.

See also:

 

Example 1: Text classifier

In this example, a sample text message is sent to a deployment of the UCI: SMS Spam Collection sample NeuNetS model for classification.

# Look up the endpoint URL for your deployed model in the
# Implementation tab of your deployment details page
deployment_endpoint_url = ""
# Look up credentials for your Watson Machine Learning service
wml_credentials = {
    "instance_id" : "",
    "password"    : "",
    "url"         : "",
    "username"    : ""
}
# Create a test payload
sample_payload = { "values" : [ "Haha awesome, be there in a minute" ] }
# Use the Watson Machine Learning Python client to send the test payload to the deployment
from watson_machine_learning_client import WatsonMachineLearningAPIClient
client = WatsonMachineLearningAPIClient( wml_credentials )
result = client.deployments.score( deployment_endpoint_url, sample_payload )
result

Output:

SMS model output

import requests

# Use the Watson Machine Learning REST API to send the test payload to the deployment

# Get a bearer token
url = wml_credentials["url"] + "/v3/identity/token"
response = requests.get( url, auth=( wml_credentials["username"], wml_credentials["password"] ) )
mltoken = json.loads( response.text )["token"]

# Send sample payload
header = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + mltoken }
response = requests.post( deployment_endpoint_url, json=sample_payload, headers=header )
json.loads( response.text )

Output:

SMS model output

 

Example 2: Image classifier

In this example, a sample payload is sent to a deployment of the CIFAR-10 sample NeuNetS model for classification.

# Look up the endpoint URL for your deployed model in the
# Implementation tab of your deployment details page
deployment_endpoint_url = ""
# Look up credentials for your Watson Machine Learning service
wml_credentials = {
    "instance_id" : "",
    "password"    : "",
    "url"         : "",
    "username"    : ""
}
# Install package required to download sample images to notebook working directory
!pip install wget
# Download a test payload
import os, wget, json
car_payload_filename = "cifar-10-automobile4-sample-payload.json"
base_url = "https://raw.githubusercontent.com/pmservice/wml-sample-models/master/neunets/sample-payloads/"
car_url  = base_url + car_payload_filename
if not os.path.isfile( car_payload_filename ): wget.download( car_url )
car_payload = json.load( open( car_payload_filename ) )
print( "car_payload:\n"  + str( car_payload )[0:50]  + " ... " + str( car_payload )[-30:] + "\n" )

Output:

Downloading sample payload

# Use the Watson Machine Learning Python client to send the test payload to the deployment
from watson_machine_learning_client import WatsonMachineLearningAPIClient
client = WatsonMachineLearningAPIClient( wml_credentials )
result = client.deployments.score( deployment_endpoint_url, car_payload )
result

Output:

CIFAR-10 model output

import requests

# Use the Watson Machine Learning REST API to send the test payload to the deployment
# https://watson-ml-api.mybluemix.net

# Get a bearer token
url = wml_credentials["url"] + "/v3/identity/token"
response = requests.get( url, auth=( wml_credentials["username"], wml_credentials["password"] ) )
mltoken = json.loads( response.text )["token"]

# Send sample payload
header = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + mltoken }
response = requests.post( deployment_endpoint_url, json=car_payload, headers=header )
json.loads( response.text )

Output:

CIFAR-10 model output

 

Sample notebooks

You can see complete examples in these notebooks: