![](/img/trans.png)
[英]Google Cloud Run Second Flask Application - requirements.txt issue
[英]Repack inference model with requirements.txt inside source_dir without installing them during the process in SageMaker
在训练自定义 model 之后,我需要创建一个推理 model,然后部署相关端点。
当在管道的执行中,我必须注入自定义推理脚本时,将触发 model 重新打包过程。 推理model需要有requirements.txt
文件(和训练好的模型一样)。
当重新打包过程开始时,默认机器ml.m5.large
和训练图像sagemaker-scikit-learn:0.23-1-cpu-py3
被实例化。 如果推理代码文件夹中存在requirements.txt
文件,此过程将尝试安装包(虽然不是必需的,应该是对 tar.gz 的简单重新打包。)。
不幸的是,指定了特定的库版本后,它将失败。
例如:
ERROR: Ignored the following versions that require a different python version: 1.22.0 Requires-Python >=3.8; 1.22.0rc1 Requires-Python >=3.8; 1.22. 0rc2 Requires-Python >=3.8; 1.22.0rc3 Requires-Python >=3.8; 1.22.1 Requires-Python >=3.8; 1.22.2 Requires-Python >=3.8; 1.22.3 Requires-Python >=3. 8; 1.22.4 Requires-Python >=3.8; 1.23.0 Requires-Python >=3.8; 1.23.0rc1 Requires-Python >=3.8; 1.23.0rc2 Requires-Python >=3.8; 1.23. 0rc3 Requires-Python >=3.8; 1.23.1 Requires-Python >=3.8; 1.23.2 Requires-Python >=3.8; 1.23.3 Requires-Python >=3.8; 1.23.4 Requires-Python >=3.8
ERROR: Could not find a version that satisfies the requirement numpy==1.23.0
这是我正在运行的代码:
inf_img_uri = sagemaker.image_uris.retrieve(
framework='pytorch',
region=region,
image_scope='inference',
version="1.12.0",
instance_type='ml.m5.xlarge',
py_version='py38'
)
pytorch_model = Model(
image_uri=inf_img_uri,
model_data=step_train.properties.ModelArtifacts.S3ModelArtifacts,
role=role,
entry_point='inference.py',
sagemaker_session=PipelineSession(),
source_dir=os.path.join(LIB_DIR, "test"), # here is inference.py and requirements.txt
name=model_name,
)
step_create_model = ModelStep(
name="infTest",
step_args=pytorch_model.create(instance_type="ml.m5.xlarge"),
description = 'Create model for inference'
)
有什么方法可以防止模板重新打包尝试从requirements.txt
安装包?
我目前的解决方案:我省略了目录中的文件并在推理代码中使用subprocess.check_call([sys.executable, "-m", "pip", "install", package])
手动安装包。 但是我发现这种方法对于批处理推理过程是错误的(因为它每次都会执行)并且也不一致。
SageMaker SDK 将始终重新打包 tar 包以包含inference.py
脚本,然后将 tar 包重新上传到 S3。
通常,SageMaker Framework 容器将安装requirements.txt
文件中指定的包。
如果您不希望发生这种情况,您可以省略requirements.txt
文件并扩展sagemaker-scikit-learn:0.23-1-cpu-py
容器以包含所有必要的依赖项。 这样,包将被烘焙到图像中,并且每次启动批量转换作业时,都不会再次安装包。
https://docs.aws.amazon.com/sagemaker/latest/dg/prebuilt-containers-extend.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.