【问题标题】:AZURE REST POSTMAN issueAZURE REST POSTMAN 问题
【发布时间】:2020-03-11 20:54:01
【问题描述】:

大家好,我需要 POSTMAN 和 azureml 方面的帮助,我使用 TF/keras 构建了一个模型,并使用 Azure 机器学习服务(Learning alot)部署了它。它是一个计算机视觉模型,所以我将数据发送到 REST API(网络服务)并获得预测结果。

这是在我的 IDE(Jupyter 和 Pycharm)上运行的 score.py 文件。

# import the necessary packages
from keras.models import load_model
import tensorflow as tf
from keras.preprocessing.image import img_to_array
from PIL import Image
from PIL import ImageFile
import numpy as np
from keras import backend as K

#Azure stuff I don't know but are needed.
from azureml.contrib.services.aml_request import AMLRequest, rawhttp
from azureml.contrib.services.aml_response import AMLResponse

# manipulate files
import io
import os 
import json
import requests

#AzureML stuff to consider, checks for the registered models.
from azureml.core.model import Model
from flask import Flask
import flask

def init():
    print(" This is the init() function. ")
    global model
    #Get the path where the deployed model can be found.
    #load models
    #model = Model.get_model_path(model_name='large_model')

    #AZUREML_MODEL_DIR is an environment variable created during deployment.
    # It is the path to the model folder (./azureml-models/$MODEL_NAME/$VERSION)
    # For multiple models, it points to the folder containing all deployed models (./azureml-models)
    folder = os.getenv('AZUREML_MODEL_DIR')
    if (folder==None): #Test hors docker
        folder = "."
    model_path = os.path.join(folder, 'Autoencoders V6-LargeTraining1MLargeTraining1M.h5')
    #On charge le model Keras
    model = load_model(model_path)
    global graph
    graph = tf.get_default_graph()
    print("* Model Loaded *, this is the init() * ")

# prepares image for prediction
def prepare_image(image, target):
  #  if the image mode is not RGB, convert it
    if image.mode != "RGB":
        image = image.convert("RGB")

    # resize the input image and preprocess it
    ImageFile.LOAD_TRUNCATED_IMAGES = True #process and predict broken images.
    image = image.resize(target)
    image = img_to_array(image)/255. #normalization.
    image = np.expand_dims(image, axis=0)
    image = np.vstack([image])
    return image

##########################################################################################################################
@rawhttp
def run(request):

    try:
        if request.method == 'POST':
            reqBody = request.get_data(False)
            myImage = Image.open(io.BytesIO(reqBody))
        #prepare image for testing.
            myImage = prepare_image(myImage,target=(160,160))

            with graph.as_default():
                prediction = model.predict(myImage).tolist()
                response = {
                         "prediction":{
                                "anomaly": prediction[0][0],
                                "normal": prediction[0][1],
                                      } # JSON response 
                            } #end response
                return flask.jsonify(response) 


        else:
            return AMLResponse("bad request, use POST", 500)


    except Exception as e:
        return flask.jsonify(e)
########################################################################################################

这基本上意味着,一旦我们创建了 Web 服务并发送数据 (POST),我们必须得到响应 ({'prediction': {'anomaly': 1.0, 'normal': 0.0}})。以下代码用于部署。

from azureml.core import Workspace
ws = Workspace.from_config(path="config.json")

from azureml.core.model import Model
# Tip: When model_path is set to a directory, you can use the child_paths parameter to include
#      only some of the files from the directory
model = Model.register(model_path = "./models/Autoencoders V6-LargeTraining1MLargeTraining1M.h5",
                       model_name = "large_model",
                       description = "Large models for anomaly detection 1",
                       workspace = ws)

from azureml.core.model import InferenceConfig
from azureml.core import Environment
from azureml.core.environment import CondaDependencies

# Create the environment
myenv = Environment(name="myenv")
conda_dep = CondaDependencies(conda_dependencies_file_path="conda_dependencies.yml",_underlying_structure=None)


# Adds dependencies to PythonSection of myenv
myenv.python.conda_dependencies=conda_dep 
inference_config = InferenceConfig(entry_script="score.py",environment=myenv)

注册模型 large_model ---打印出来

from azureml.core.compute import AksCompute, ComputeTarget
from azureml.core.webservice import AciWebservice, AksWebservice, LocalWebservice
#deployment_config =  LocalWebservice.deploy_configuration()

# Use the default configuration (you can also provide parameters to customize this).
# For example, to create a dev/test cluster, use:
# prov_config = AksCompute.provisioning_configuration(cluster_purpose = AksCompute.ClusterPurpose.DEV_TEST)
prov_config = AksCompute.provisioning_configuration()

aks_name = 'adtest2'
# Create the cluster
aks_target = ComputeTarget.create(workspace = ws,
                                    name = aks_name,
                                    provisioning_configuration = prov_config)


# Wait for the create process to complete
aks_target.wait_for_completion(show_output = True)

创建.................................................. ..................................................... ..................................................... ..................................... SucceededProvisioning 操作完成,操作“Succeeded”---打印出来


from azureml.core.webservice import AksWebservice, Webservice
from azureml.core.model import Model

aks_target = AksCompute(ws,"adtest2")
#If deploying to a cluster configured for dev/test, ensure that it was created with enough
#cores and memory to handle this deployment configuration. Note that memory is also used by
#things such as dependencies and AML components.
deployment_config = AksWebservice.deploy_configuration(cpu_cores = 2, memory_gb = 4)
service = Model.deploy(ws, "scorepytest22", [model], inference_config, deployment_config, aks_target,overwrite=True)
service.wait_for_deployment(show_output = True)
print(service.state)

跑步………… 成功 AKS 服务创建操作完成,操作“成功” 健康

print(service.scoring_uri)
http://13.82.132.10:80/api/v1/service/scorepytest21/score


# call the web service end point
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}
response = requests.post(url, headers=headers, data=img)
response

prediction = json.loads(response.content.decode("ascii"))
prediction # The firt part is the test image's name, and the second part is the predicted categor

{'prediction': {'anomaly': 1.0, 'normal': 0.0}} -- 返回预测。

这显然适用于 jupyter,并且完全符合我对 score.py 文件的要求。这是我使用 POSTMAN 时的结果。

在 POSTMAN 上,我得到“OS 类型的对象错误不是 JSON 可序列化的”,请帮助我尝试了很多事情,但都失败了。

【问题讨论】:

    标签: azure tensorflow machine-learning keras


    【解决方案1】:

    大家好,我解决了这个问题,我发布了二进制图像,而 score.py 代码没有处理表单数据或正文帖子。然后我将 POST 更改为二进制,一切都按预期工作。如果您有任何问题,请告诉我。

    【讨论】:

    • 嗨@Abu,您将代码的哪一部分更改为二进制。
    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多