Downloading and using NeuNetS models locally

The NeuNetS tool in IBM Watson Studio synthesizes a neural network and trains it on your training data without you having to design or build anything by hand. You can deploy the model NeuNetS builds as a web service in IBM Watson Machine Learning, or you can download the model to use locally. When using the model locally, you must preprocess (resize, reshape, and normalize) input data before passing the data to the model. This topics describes how you can use the NeuNetS library to perform that preprocessing.

 

NeuNetS library

The NeuNetS library simplifies using NeuNetS-built models locally. See: NeuNetS library external link

The following examples demonstrate using the NeuNetS library with sample NeuNetS models to classify text and image input.

 

Example 1: Text classifier

This example demonstrates using the NeuNetS library to work with a downloaded NeuNetS model that was trained on the UCI: SMS Spam Collection sample data set.

  • Downloaded model file name in the local, working directory: saved-sms-model.tar.gz
# Install the NeuNetS library
!pip install --upgrade git+https://github.com/pmservice/NeuNetS/#egg=neunets_processor\&subdirectory=neunets_processor
# Unpack the model built by NeuNets
!gunzip "saved-sms-model.tar.gz"
!tar -xvf "saved-sms-model.tar"
model_file = "keras_model.hdf5"
metadata_file = "metadata.json"
word_mapping_file = "word_mapping.json"
# Send a sample message to the model for classification
from neunets_processor.text import text_processor
sms_classifier = text_processor.TextProcessor( model_file, metadata_file, word_mapping_file )
result = sms_classifier.predict( [ "Hi.  Wanna meet up later?" ] )
result

Output:

Classification result

 

Example 2: Image classifier

This example demonstrates using the NeuNetS library to work with a downloaded NeuNetS model that was trained on the CIFAR-10 sample data set.

# Install the NeuNetS library
!pip install wget
# Download the sample image to the working directory and view the image
import os, wget
from PIL import Image
from IPython.display import display
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 )
img = Image.open( img_filename )
display( img )

Output:

Sample image

# Install the NeuNetS library
!pip install --upgrade git+https://github.com/pmservice/NeuNetS/#egg=neunets_processor\&subdirectory=neunets_processor
# Unpack the model built by NeuNets
!gunzip "saved-cifar-model.tar.gz"
!tar -xvf "saved-cifar-model.tar"
model_file = "keras_model.hdf5"
metadata_file = "metadata.json"
# Send the sample image to the model for classification
from neunets_processor.image import image_processor
cifar_classifier = image_processor.ImageProcessor( model_file, metadata_file )
result = cifar_classifier.predict( [ img ] )
result

Output:

Classification result

 

Sample notebooks

You can see complete examples in these notebooks: