Payload data format for NeuNetS model deployments

You can deploy your NeuNetS model to your IBM Watson Machine Learning service from the NeuNetS tool. This topic describes how you need to format payload data to send to a model built in NeuNetS and then deployed to your Watson Machine Learning service.

 

You can use the NeuNetS tool to create either a text classifier or an image classifier. The required format of payload data depends on the type of classifier:

 

Text classifier payload format

The payload sent to a text classifier built by the NeuNetS tool is a dictionary containing one array labeled "values":

payload = { "values" : [ "here is a string to classify" ] }

 

Requirements and limits

  • The values array can contain only one item: a single string containing message text
  • You cannot send multiple strings to be classified at once

 

Image classifier payload format

The payload sent to an image classifier built by the NeuNetS tool is a dictionary containing one array labeled "values":

payload = { "values" : [ img_data ] }

 

Requirements and limits

  • Supported image source file formats: JPEG (.jpg) or PNG (.png)
  • You cannot send multiple images to be classified at once

 

Image data format

For an image H pixels high and W pixels wide, the image data must be a list object of length H. Each element in the list is itself a list, containing W lists of length 3 (for .jpg images) or 4 (for some .png images.)

 

Basic image example

This example demonstrates creating a payload containing data for an image in a file named my-image.jpg in the local directory:

from PIL import Image
import numpy as np
img = Image.open( 'my-image.jpg' )
img_as_arr  = np.asarray( img )
img_as_list = img_as_arr.tolist()
payload = { "values" : [ img_as_list ] }

 

Detailed image example

This example uses a sample image from the CIFAR-10 sample data set, cifar-10-automobile4.png external link. This Python code can be run in a notebook in Watson Studio.

# Install the package required to download the sample image to the notebook working directory
!pip install wget
# Copy the sample image to the notebook working directory
import os, wget
img_filename = 'cifar-10-automobile4.png'
url = 'https://github.com/pmservice/wml-sample-models/raw/master/neunets/sample-images/'
if not os.path.isfile( img_filename ): wget.download( url + img_filename )
# Open the image and view it
from PIL import Image
from IPython.display import display
img = Image.open( img_filename )
display( img )

Output:

Sample image

# Create an array object for the sample image
import numpy as np
img_as_arr = np.asarray( img )
print( len( img_as_arr ) )
print( img_as_arr.shape )
img_as_arr[0:2]

Output:

Array of image

# Create a payload for the sample image
img_as_list = img_as_arr.tolist()
payload = { "values" : [ img_as_list ] }
print( len( img_as_list ) )
print( img_as_list[0][0:3] )
print( img_as_list[1][0:3] )
print( str( payload )[0:50] + " ... " + str( payload )[-30:] )

Output:

Payload for image