从 AWS SageMaker Pipeline 训练组件中的训练脚本将自定义文件上传到 s3

Upload custom file to s3 from training script in training component of AWS SageMaker Pipeline

我是 Sagmaker 的新手,我已经从 SageMaker notebook 创建了一个管道,其中包含训练和部署组件。 在训练脚本中,我们可以通过SM_MODEL_DIR将模型上传到s3。但是现在,我想将分类报告上传到s3。我试过这段代码。但它表明这不是一个合适的 s3 存储桶。

df_classification_report = pd.DataFrame(class_report).transpose()
classification_report_file_name = os.path.join(args.output_data_dir,
                                               f"{args.eval_model_name}_classification_report.csv")
df_classification_report.to_csv(classification_report_file_name)
# instantiate S3 client and upload to s3

# save classification report to s3
s3 = boto3.resource('s3')
print(f"classification_report is being uploaded to s3- {args.model_dir}")
s3.meta.client.upload_file(classification_report_file_name, args.model_dir,
                            f"{args.eval_model_name}_classification_report.csv")

错误

Invalid bucket name "/opt/ml/output/data": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:(s3|s3-object-lambda):[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"

有人可以帮忙吗?非常感谢您提供的任何帮助。

SageMaker 培训作业将压缩位于 /opt/ml/model(即 SM_MODEL_DIR 的值)中的任何文件,并自动将其上传到 S3。您可以考虑将文件保存到 SM_MODEL_DIR(您的分类报告将因此上传到模型 tar 球中的 S3)。

upload_file() 函数要求您传递 S3 存储桶。 您还可以查看在代码中手动指定一个 S3 存储桶以将文件上传到。

s3.meta.client.upload_file(classification_report_file_name, <YourS3Bucket>,
                            f"{args.eval_model_name}_classification_report.csv")

您可以将报告等非模型工件保存到 output_data_dir。参见 here

parser.add_argument("--output_data_dir", type=str, 
default=os.environ.get('SM_OUTPUT_DATA_DIR'),                              
     help="Directory to save output data artifacts.")

如果您希望将工件与模型文件打包在一起,请按照@Marc 的回答进行操作。对于与特定模型相关的报告,这可能有意义,尽管在模型注册表中捕获它对我来说更有意义。
请注意,如果您将模型部署到端点(可能会混淆推理运行时模型加载代码),这些额外的工件将被保留。