DSexplorer
DSexplorer

Reputation: 365

How can I load and deploy a pre-trained AWS Sagemaker XGBoost model on local machine?

I've trained a Sagemaker XGBoost model and downloaded the model.tar.gz file from S3 onto my local machine. How can I load this model for deploying it using flask?

I've tried using pickle to load the unzipped model file but it doesn't seem to work.

import sagemaker
import boto3
import os
import pickle

with open('xgboost-model', 'r') as inp:
   cls.model = pkl.load(inp)

Traceback (most recent call last): File "", line 2, in File "C:\Anaconda3\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 969: character maps to

Upvotes: 5

Views: 4444

Answers (1)

DSexplorer
DSexplorer

Reputation: 365

Figured it out! The downloaded pre-trained sagemaker model can be extracted from its tar.gz format onto the local machine. Once extracted, open the file in python in byte format and load using pickle.

file = open(model_path, 'rb')
xgb_model = pickle.loads(file.read())

Then read in the input data to be converted into xgboost DMatrix formatting without any of the independent data or headings to make predictions.

data_input = xgb.DMatrix(data.iloc[:, 1:].values)
predictions = xgb_model.predict(data_input)

Upvotes: 8

Related Questions