2

This is a "Watson Studio" related question. I've done the following Deep-Learning tutorial/experiment assistant, successfully deployed a generated CNN model to WML(WebService). Cool!

Tutorial: Single convolution layer on MNIST data
Experiment Assistant

Next, I'd like to test if the model could identify my image( MNIST ) in deployed environment, and the questions came to my mind. What kind of input file( maybe pixel image file ) should I prepare for the model input ? How can I kick the scoring endpoint passing my image? ( I saw python code-snippet on the "Implementation" tab, but it's json example and not sure how can I pass the pixel image...)

payload_scoring = {"fields": [array_of_feature_columns], "values": [array_of_values_to_be_scored, another_array_of_values_to_be_scored]}

Any advice/suggestions highly welcomed. Thx in advance.

ishida330
  • 148
  • 1
  • 6

1 Answers1

2

The model that was trained accepts an input data that is an array of 4 dimensions i.e [<batchsize>, 28, 28, 1], where 28 refers to the height and width of the image in pixels, 1 refers to the number of channels. Currently the WML online deployment and scoring service requires the payload data in the format that matches the input format of the model. So, to predict any image with this model, you must ...

  1. convert the image to an array of [1, 28, 28, 1] dimension. Converting image to an array is explained in next section.
  2. pre-process the image data as required by the model i.e perform (a) normalize the data (b) convert the type to float
  3. pre-processed data must be be specified in json format with appropriate keys. This json doc will be the input payload for the scoring request for the model.

How to convert image to an array? There are two ways.. (using python code)

a) keras python library has a MNIST dataset that has MNIST images that are converted to 28 x 28 arrays. Using the python code below, we can use this dataset to create the scoring payload.

import numpy as np
from keras.datasets import mnist

(X, y), (X_test, y_test) = mnist.load_data()

score_payload_data = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)
score_payload_data = score_payload_data.astype("float32")/255
score_payload_data = score_payload_data[2].tolist() ## we are choosing the 2nd image in the list to predict
scoring_payload = {'values': [score_payload_data]}

b) If you have an image of size 28 x 28 pixels, we can create the scoring payload using the code below.

img_file_name = "<image file name with full path>"

from scipy import misc
img = misc.imread(img_file_name)
img_to_predict = img.reshape(img.shape[0], img.shape[1], 1)/255
img_to_predict = img_to_predict.astype("float32").tolist()
scoring_payload = {"values": [img_to_predict]}
Krsna
  • 36
  • 1
  • Hi, Krsna-san. Thank you for the perfect answer. Now I've tried your python code and succesfully generated the JSON. Then I pasted it on "test" UI tab and got the response. which might be the output of softmax , meaning the image is "1"(0.9967361092567444 confidence) Thank you very much for your hours and efforts trying to answer my question. BIG THANKS ! – ishida330 Mar 30 '18 at 10:08
  • ```{ "fields": [ "prediction" ], "values": [ [ 0.000015985766367521137, 0.9967361092567444, 1.0010278580946652e-35, 0.00039328771526925266, 1.509121871775723e-33, 3.256876188079101e-27, 0.000945420702919364, 0.0013386306818574667, 0.0005706030642613769, 7.315056458814826e-19 ] ] }``` – ishida330 Mar 30 '18 at 10:08