POST请求失败,将大数据发送到部署在Azure容器上的模型

问题描述 投票:0回答:1

摘要

我具有通过Azure机器学习服务SDK部署在Azure容器实例上的PyTorch模型。该模型以标准numpy格式拍摄(大)图像进行分类。

似乎,我正在服务器端达到HTTP请求大小限制。对模型的请求在尺寸为8-9mb的PNG图像中成功,而在尺寸为15mb +的图像中失败。具体来说,它失败,并显示413请求实体太大。

我假设,在部署过程中,在正在构建的Docker映像的Nginx中设置了限制。我的问题:鉴于该问题是由于HTTP请求大小限制引起的,在azureml API中是否有任何方法可以增加此限制?

部署过程

部署过程按预期成功。

from azureml.core import Workspace
from azureml.core.model import InferenceConfig, Model
from azureml.core.webservice import AciWebservice, Webservice
from azureml.exceptions import WebserviceException
from pathlib import Path

PATH = Path('/data/home/azureuser/my_project')

ws = Workspace.from_config()
model = ws.models['my_pytorch_model']

inference_config = InferenceConfig(source_directory=PATH/'src',
                                   runtime='python',
                                   entry_script='deployment/scoring/scoring.py',
                                   conda_file='deployment/environment/env.yml')

deployment_config = AciWebservice.deploy_configuration(cpu_cores=2, memory_gb=4)
aci_service_name = 'azure-model'

try:
    service = Webservice(ws, name=aci_service_name)
    if service:
        service.delete()
except WebserviceException as e:
    print()

service = Model.deploy(ws, aci_service_name, [model], inference_config, deployment_config)

service.wait_for_deployment(True)
print(service.state)

通过requests进行测试

使用请求的简单测试:

import os
import json
import numpy as np
import requests
from PIL import Image as PilImage

test_data = np.array(PilImage.open(PATH/'src/deployment/test/test_image.png')).tolist()
test_sample = json.dumps({'raw_data': 
    test_data
})
test_sample_encoded = bytes(test_sample, encoding='utf8')

headers = {
    'Content-Type': 'application/json'
}

response = requests.post(
    service.scoring_uri,
    data=test_sample_encoded,
    headers=headers,
    verify=True,
    timeout=10
)

对于较大的文件,在requests中产生以下错误:

ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))

我猜这是请求中的一个已知错误,当​​在完成数据上传之前从服务器关闭连接时。

通过pycurl进行测试

使用卷发包装,我得到了一个更可解释的答复。

import pycurl
from io import BytesIO

c = pycurl.Curl()
b = BytesIO()

c.setopt(c.URL, service.scoring_uri)
c.setopt(c.POST, True)
c.setopt(c.HTTPHEADER,['Content-Type: application/json'])
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(c.POSTFIELDS, test_sample)
c.setopt(c.VERBOSE, True)
c.perform()

out = b.getvalue()

b.close()
c.close()

print(out)

对于大文件,这会产生以下错误:

<html>
    <head>
        <title>
            413 Request Entity Too Large
        </title>
    </head>
    <body bgcolor="white">
        <center>
            <h1>
                413 Request Entity Too Large
            </h1>
        </center>
        <hr>
        <center>
                nginx/1.10.3 (Ubuntu)
        </center>
    </body>
</html>

使我相信这是Nginx配置中的问题。具体来说,我想将client_max_body_size设置为10mb。

总结问题

鉴于我确实遇到了Nginx配置问题,我可以以某种方式更改它吗?如果未使用Azure机器学习服务SDK,则可能通过覆盖/etc/nginx/nginx.conf文件?

azure-machine-learning-service
1个回答
0
投票
例如,您可以建立计分管道。例如,请参阅此笔记本:Pipeline Style Transfer

或者,如果要使用ACI Web服务,则可以将图像暂存到Blob存储,然后通过引用将图像传递到Blob位置。

© www.soinside.com 2019 - 2024. All rights reserved.