无法访问安装在 Azure ML 中的 python 个包

Unable to access python packages installed in Azure ML

我正在尝试将预训练的 ML 模型(另存为 .h5 文件)部署到 Azure ML。我创建了一个 AKS 集群并尝试部署模型,如下所示:

from azureml.core import Workspace
from azureml.core.model import Model

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

from azureml.core.webservice import AksWebservice, LocalWebservice
from azureml.core.compute import ComputeTarget

workspace = Workspace.from_config(path="config.json")

env = Environment.get(workspace, name='AzureML-TensorFlow-1.13-GPU')

# Installing packages present in my requirements file
with open('requirements.txt') as f:
    dependencies = f.readlines()
dependencies = [x.strip() for x in dependencies if '# ' not in x]
dependencies.append("azureml-defaults>=1.0.45")

env.python.conda_dependencies = CondaDependencies.create(conda_packages=dependencies)

# Including the source folder so that all helper scripts are included in my deployment
inference_config = InferenceConfig(entry_script='app.py', environment=env, source_directory='./ProcessImage')

aks_target = ComputeTarget(workspace=workspace, name='sketch-ppt-vm')

# Deployment with suitable config
deployment_config = AksWebservice.deploy_configuration(cpu_cores=4, memory_gb=32)
model = Model(workspace, 'sketch-inference')
service = Model.deploy(workspace, "process-sketch-dev", [model], inference_config, deployment_config, deployment_target=aks_target, overwrite=True)
service.wait_for_deployment(show_output = True)
print(service.state)

我的主要入口脚本需要一些额外的帮助脚本,我通过在推理配置中提及源文件夹来包含这些脚本。

我原以为我添加的帮助程序脚本应该能够访问在部署期间设置环境时安装的包,但我收到 ModuleNotFoundError。

这是错误输出,以及我在执行入口脚本时打印的几个环境变量:

    AZUREML_MODEL_DIR ----  azureml-models/sketch-inference/1
    PYTHONPATH ----  /azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages:/var/azureml-server:
    PATH ----  /azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/bin:/opt/miniconda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/intel/compilers_and_libraries/linux/mpi/bin64
    Exception in worker process
    Traceback (most recent call last):
    File "/azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
        worker.init_process()
    File "/azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
        self.load_wsgi()
    File "/azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
        self.wsgi = self.app.wsgi()
    File "/azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
        self.callable = self.load()
    File "/azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
        return self.load_wsgiapp()
    File "/azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
        return util.import_app(self.app_uri)
    File "/azureml-envs/azureml_6dc005c11e151f8d9427c0c6091a1bb9/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
        __import__(module)
    File "/var/azureml-server/wsgi.py", line 1, in <module>
        import create_app
    File "/var/azureml-server/create_app.py", line 3, in <module>
        from app import main
    File "/var/azureml-server/app.py", line 32, in <module>
        from aml_blueprint import AMLBlueprint
    File "/var/azureml-server/aml_blueprint.py", line 25, in <module>
        import main
    File "/var/azureml-app/main.py", line 12, in <module>
        driver_module_spec.loader.exec_module(driver_module)
    File "/structure/azureml-app/ProcessImage/app.py", line 16, in <module>
        from ProcessImage.samples.coco.inference import run as infer
    File "/var/azureml-app/ProcessImage/samples/coco/inference.py", line 1, in <module>
        import skimage.io
    ModuleNotFoundError: No module named 'skimage'

与此相关的现有答案没有多大帮助。我相信必须有一种更简单的方法来解决这个问题,因为 AzureML 专门提供了通过提供 requirements.txt 文件或单独安装的 pip/conda 包来设置环境的功能。

我在这里错过了什么?请帮忙。

所以,经过反复试验,创建一个新环境然后添加包解决了我的问题。我仍然不清楚为什么当我尝试使用 Environment.from_pip_requirements() 时这不起作用。阅读这方面的详细答案会很有趣。

我的主要任务是推理 - 给定图像的目标检测,我们的团队开发了自己的模型。我想要两种类型的导入:

1.标准 python 包(通过 pip 安装)
这是通过创建 conda 依赖项并将其添加到 env 对象(第 2 步)

解决的

2。 Methods/vars 来自帮助程序脚本(如果您在模型推理期间有 pre/post 处理要完成):
这是通过在 InferenceConfig(步骤 3)

中提及 source_directory 来完成的

这是我更新的脚本,它结合了环境创建、推理和部署配置以及使用工作区中的现有计算(通过门户创建)。

from azureml.core import Workspace
from azureml.core.model import Model
from azureml.core.environment import Environment, DEFAULT_GPU_IMAGE
from azureml.core.conda_dependencies import CondaDependencies
from azureml.core.model import InferenceConfig
from azureml.core.webservice import AksWebservice, LocalWebservice
from azureml.core.compute import ComputeTarget


# 1. Instantiate the workspace
workspace = Workspace.from_config(path="config.json")

# 2. Setup the environment
env = Environment('sketchenv')
with open('requirements.txt') as f: # Fetch all dependencies as a list
    dependencies = f.readlines()
dependencies = [x.strip() for x in dependencies if '# ' not in x]
env.docker.base_image = DEFAULT_GPU_IMAGE
env.python.conda_dependencies = CondaDependencies.create(conda_packages=['numpy==1.17.4', 'Cython'], pip_packages=dependencies)

# 3. Inference Config
inference_config = InferenceConfig(entry_script='app.py', environment=env, source_directory='./ProcessImage')

# 4. Compute target (using existing cluster from the workspacke)
aks_target = ComputeTarget(workspace=workspace, name='sketch-ppt-vm')

# 5. Deployment config
deployment_config = AksWebservice.deploy_configuration(cpu_cores=6, memory_gb=100)

# 6. Model deployment
model = Model(workspace, 'sketch-inference') # Registered model (which contains model files/folders)
service = Model.deploy(workspace, "process-sketch-dev", [model], inference_config, deployment_config, deployment_target=aks_target, overwrite=True)
service.wait_for_deployment(show_output = True)
print(service.state)