尝试序列化成功生成的 SageMaker 模型时,是什么导致 unpickling 堆栈下溢?

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

我目前正在致力于在 Amazon Sagemaker 中建立管道。为此,我设置了一个 xgboost-estimator 并在我的数据集上对其进行了训练。训练作业按预期运行,新训练的模型将保存到指定的输出存储桶中。稍后我想重新导入模型,这是通过从输出存储桶中获取 model.tar.gz、提取模型并通过 pickle 序列化二进制文件来完成的。

# download the model artifact from AWS S3
!aws s3 cp s3://my-bucket/output/sagemaker-xgboost-2021-09-06-12-19-41-306/output/model.tar.gz .

# opens the downloaded model artifact and loads it as 'model' variable
model_path = "model.tar.gz"
with tarfile.open(model_path) as tar:
    tar.extractall(path=".")

model = pkl.load(open("xgboost-model", "rb"))

每当我尝试调整它时,我都会收到一个 unpickling 堆栈下溢:

---------------------------------------------------------------------------
UnpicklingError                           Traceback (most recent call last)
<ipython-input-9-b88a7424f790> in <module>
     10     tar.extractall(path=".")
     11 
---> 12 model = pkl.load(open("xgboost-model", "rb"))
     13 

UnpicklingError: unpickling stack underflow

到目前为止,我重新训练了模型,以查看不同的模型文件是否发生错误,并且确实发生了。我还下载了 model.tar.gz 并通过gunzip 对其进行了验证。当正确提取二进制文件 xgboost-model 时,我无法对其进行 pickle。我在 stackoverflow 上发现的每次错误都指向损坏的文件,但这个错误是由 SageMaker 直接生成的,我确实注意到对其执行任何转换,但从 model.tar.gz 中提取它。参考文档和不同的教程,重新加载这样的模型似乎是一个非常常见的用例。 在本地,我收到与下载的文件相同的错误。我尝试直接进入 pickle 进行调试,但无法理解它的意义。完整的错误堆栈如下所示:

Exception has occurred: UnpicklingError       (note: full exception trace is shown but execution is paused at: _run_module_as_main)
unpickling stack underflow
  File "/sagemaker_model.py", line 10, in <module>
    model = pkl.load(open('xgboost-model', 'rb'))
  File "/usr/local/Cellar/[email protected]/3.9.1_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/[email protected]/3.9.1_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/Cellar/[email protected]/3.9.1_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/local/Cellar/[email protected]/3.9.1_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/[email protected]/3.9.1_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main (Current frame)
    return _run_code(code, main_globals, None,

什么可能导致此问题以及在此过程中的哪一步我可以应用更改来修复或解决该问题。

python-3.x pickle amazon-sagemaker
3个回答
7
投票

最新的 XGBoost 版本似乎改变了这个过程。这对

1.3.x
有效:

import xgboost as xgb

model = xgb.Booster()
model.load_model('xgboost-model')

3
投票

问题根源在于 xgboost 框架使用的模型版本。从 1.3.0 开始,默认输出从 pickle 更改为 json,并且 sagemaker 文档似乎没有相应更新。所以如果你想通过

阅读模型
    tar.extractall(path=".")

model = pkl.load(open("xgboost-model", "rb"))

如 sagemaker 文档中所述,您需要导入旧版本的 XGBOOST 框架,例如1.2.1.


1
投票

读完@Imoe41的答案后,我也想为这个问题做出贡献。问题是,如果您单击错误中的链接,您会看到(https://xgboost.readthedocs.io/en/latest/tutorials/ saving_model.html),从 xgboost 1.0 版本开始,模型是保存在json中,在1.0版本之前,模型保存在pickle中。 我在2020年用sagemaker训练了xgboost模型,使用的是0.90的xgboost版本。然而,在我的笔记本中,xgboost 软件包版本是 1.5.1。

解决方案:

  1. 检查已安装的xgboost版本

将 xgboost 导入为 xgb print(xgb.版本)

  1. 如果版本高于1.0,则需要降级。为了降级xgboost,您还需要降级其他软件包。
pip install scipy==1.4.1
pip install shap==0.37.0
pip install xgboost==0.90.0
  1. 将模型加载为pickle
import pickle as pkl
import tarfile
t = tarfile.open('model.tar.gz', 'r:gz')
t.extractall()
model = pkl.load(open("xgboost-model", 'rb'))
© www.soinside.com 2019 - 2024. All rights reserved.