如何在本地环境中使用AmazonSagemaker的.joblib模型



我使用Sagemaker在AWS中创建了一个模型。我把model.joblib下载到我的机器上。我正试图用它来做一些预测。我可以加载文件:

import joblib
import mlio
import sklearn
filename=r"C:UsersbenkiDownloadsmodel.tarmodel.joblib"
loaded_model = joblib.load(filename)

然而,我不确定从这里到哪里去。我试图对"loaded_model"进行评分和预测,但我只收到错误消息,解释"loaded_model"不具有这些属性loaded_model的类型为:sagemaker_sklearn_extension.externals.automl_transformer.AutoMLTransformer

在Sagemaker Jupyter Notebook的AWS实例中,我可以用以下内容进行预测:

endpoint_name = "My_Model"  
#print(f"Note: Invoking Endpoint: {endpoint_name}")
content_type = "text/csv"                                        
accept = "text/csv"  
# create for loop to create results, one at a time
predict=[]
for sample in payloads:
response = client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType=content_type,
Body=sample
)
#print('inference complete')
inference = (response['Body'].read().decode('ascii'))
predict.append((sample,inference))

我如何使用这个joblib模型?

在这种情况下,查看确切的错误日志和整个本地推理代码可能会有所帮助。然而,在此期间,我确实有一些想法需要您检查:

  • SageMaker端点部署在完整的应用程序环境中,代码和工件在其中集成。您可能缺少SM用于设置推理的一些资源,例如转换器定义或预处理步骤。

  • 在这里您可以看到AutoMLTransformer资源的源代码。值得注意的是,它的构造函数需要访问包含fit((transform((方法的featureandtargetSklearn transformer对象。这些是你当地管道的一部分吗?

  • 在SageMaker中训练模型后,一些工件被创建并存储在AmazonS3中的用户定义的密钥路径中(提示为s3://bucket/your_artifacts_path/model.tar.gz(。解压缩时,您应该能够看到一个模型/文件夹,其中包含model.joblib工件和代码/子文件夹。在后者中,可以自动生成用于数据预处理和服务的参考Python脚本。当然,它们的某些部分可能不会在本地环境中按原样运行,但请尝试使用服务脚本作为参考,以确定如何预处理传入记录并对模型对象进行调用。

如果你发现其他东西,请告诉我!

最新更新