Home > other >  How do you locally load model.tar.gz file from Sagemaker?
How do you locally load model.tar.gz file from Sagemaker?

Time:11-28

I'm new to Sagemaker and I trained a classifier model with the built in XGBoost. It saved a "Model.tar.gz" at an S3. I downloaded the file because I was planning to deploy the model else where. So to experiment, I started loading the file locally first. I tried this code.

import pickle as pkl
import tarfile

t = tarfile.open('model.tar.gz', 'r:gz')
t.extractall()

model = pkl.load('xgboost-model', 'rb')

But it's only giving me this error

XGBoostError: [13:32:18] /opt/concourse/worker/volumes/live/7a2b9f41-3287-451b-6691-43e9a6c0910f/volume/xgboost-split_1619728204606/work/src/learner.cc:922: Check failed: header == serialisation_header_: 
If you are loading a serialized model (like pickle in Python) generated by older
XGBoost, please export the model by calling `Booster.save_model` from that version
 first, then load it back in current version.  There's a simple script for helping
the process.

So I tried using the Booster.save_model function at sagemaker notebook but it doesnt work nor does pickling the trained model work.

I also tried this code

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

but it's giving me this error

XGBoostError: std::bad_alloc

Any help would be greatly appreciated.

CodePudding user response:

found the answer to my question. Apparently, the sagemaker environment is using an old build of XGBoost, around version 0.9. As the XGboost team make constant upgrades and changes to their library, AWS was unable to keep up with it.

That said I was able to run my code below by downgrading the XGBoost library on my environment from 1.7 to 0.9 and it works like a charm.

t = tarfile.open('model.tar.gz', 'r:gz')
t.extractall()
model = pkl.load('xgboost-model', 'rb')
  • Related