深度学习 - 如何在WML上测试MNIST教程模型?

时间:2018-03-28 04:42:26

标签: watson-studio

这是一个" Watson Studio"相关问题。我已经完成了以下深度学习教程/实验助手,成功将生成的CNN模型部署到WML(WebService)。酷!

Tutorial: Single convolution layer on MNIST data
Experiment Assistant

接下来,我想测试模型是否可以在部署环境中识别我的图像(MNIST),并且我想到了问题。 我应该为模型输入准备什么样的输入文件(可能是像素图像文件)?如何通过我的图像踢出得分端点? (我在" Implementation"标签上看到了python code-snippet,但是它的json示例并不确定如何传递像素图像...)

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

任何建议/建议都受到高度欢迎。 Thx提前。

1 个答案:

答案 0 :(得分:2)

训练的模型接受输入数据,该输入数据是4维的数组,即[<batchsize>, 28, 28, 1],其中28表示图像的高度和宽度,以像素为单位,1表示通道的数量。目前,WML在线部署和评分服务要求有效载荷数据的格式与模型的输入格式相匹配。因此,要使用此模型预测任何图像,您必须...

  1. 将图像转换为[1,28,28,1]维的数组。下一节将介绍将图像转换为数组。
  2. 按照模型的要求预处理图像数据,即执行(a)规范化数据(b)将类型转换为float
  3. 必须使用适当的密钥以json格式指定预处理数据。此json doc将是模型评分请求的输入有效负载。
  4. 如何将图像转换为数组? 有两种方式..(使用python代码)

    a)keras python库有一个MNIST数据集,其MNIST图像转换为28 x 28阵列。使用下面的python代码,我们可以使用此数据集来创建评分有效负载。

    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)如果您的图像大小为28 x 28像素,我们可以使用下面的代码创建评分有效负载。

    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]}