【问题标题】:Deep Learning - How can I test the MNIST tutorial model on WML?深度学习 - 如何在 WML 上测试 MNIST 教程模型?
【发布时间】:2018-09-06 15:36:06
【问题描述】:

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

Tutorial: Single convolution layer on MNIST data
Experiment Assistant

接下来,我想测试该模型是否可以在部署环境中识别我的图像(MNIST),然后我想到了这些问题。 我应该为模型输入准备什么样的输入文件(可能是像素图像文件)?如何通过我的图像踢得分端点? (我在“实现”选项卡上看到了 python code-sn-p,但它是 json 示例,不知道如何传递像素图像...)

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

非常欢迎任何意见/建议。提前谢谢。

【问题讨论】:

    标签: watson-studio


    【解决方案1】:

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

    1. 将图像转换为 [1, 28, 28, 1] 维度的数组。将图像转换为数组将在下一节中解释。
    2. 根据模型的要求预处理图像数据,即执行 (a) 规范化数据 (b) 将类型转换为浮点数
    3. 预处理数据必须以 json 格式指定,并带有适当的键。此 json 文档将作为模型评分请求的输入负载。

    如何将图像转换为数组? 有两种方法..(使用python代码)

    a) keras python 库有一个 MNIST 数据集,其中包含转换为 28 x 28 数组的 MNIST 图像。使用下面的 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]}
    

    【讨论】:

    • 嗨,奎师那圣。感谢您的完美回答。现在我已经尝试了您的 python 代码并成功生成了 JSON。然后我将它粘贴到“测试”UI 选项卡上并得到响应。这可能是 softmax 的输出,意味着图像是“1”(0.9967361092567444 置信度)非常感谢您为回答我的问题付出的时间和努力。非常感谢!
    • { "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 ] ] }
    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2018-11-04
    • 2019-06-10
    相关资源
    最近更新 更多