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

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

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)

Related

How to load a trained BlazingText model

I have trained a text classification model using blazingText on AWS sagemaker, I can load the trained model and deploy an inference endpoint
model = bt_model.deploy(initial_instance_count=1, endpoint_name=endpoint_name, instance_type='ml.m5.xlarge', serializer=JSONSerializer())
payload = {"instances": terms}
response = model.predict(payload)
predictions = json.loads(response)
and it's working fine, now I need to load the model's bin file using an entry_point in order to do some logic before and after predictions in the input_fn and output_fn.
I extracted the bin file from the model.tar.gz and I can load it, but I get Segmentation Fault when I try to run a prediction
from gensim.models import FastText
from gensim.models.fasttext import load_facebook_model, load_facebook_vectors
model=FastText.load('model.bin')
model.predict('hello world')
As per blazingText documentations
For both supervised (text classification) and unsupervised (Word2Vec)
modes, the binaries (*.bin) produced by BlazingText can be
cross-consumed by fastText and vice versa. You can use binaries
produced by BlazingText by fastText. Likewise, you can host the model
binaries created with fastText using BlazingText.
Here is an example of how to use a model generated with BlazingText
with fastText:
#Download the model artifact from S3 aws s3 cp s3://<YOUR_S3_BUCKET>//model.tar.gz model.tar.gz
#Unzip the model archive tar -xzf model.tar.gz
#Use the model archive with fastText fasttext predict ./model.bin test.txt
but for some reason it's not working as expected

How do I save an Machine Learning ensemble of models on my PC?

I'm using Google Colab, and my runtime keeps disconnecting. Rather than training it again and again, I thought I'd store it in a PC, somehow. So that if I have to demonstrate it, I can just load that file and use .predict.
I need to save it to my PC so that I can run it with a simple .load() function if something disconnects on the platform I'm using.
If you are using python you can use pickling to save as an object
import pickle
model = LogisticRegression()
model.fit(X_train, Y_train)
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
# load the model from disk
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(X_test, Y_test)
print(result)

EEG data preprocessing with mne python

I have Physiological EEG emotion dataset named "Deap". I want to analyze and visualize the data through MNE but it has its own format.
How can I load my personal data for pre-processing, data format is (.dat)?
import pickle
with open('s01.dat', 'rb') as f:
y = pickle.load(f, encoding='latin1')
This one works for me.
Of course, the ".dat" file is in the same directory as this code.

how to get Tensorflow session from only keras .h5 file without session

The motivation behind this question is I had saved a Keras model using Matterport's MaskRCNN and in the tf.keras.callbacks.ModelCheckpoint() had very explicitly set the save_weights_only argument to False, so that the entire model would be saved (not just the weights).
Turns out there's a bug in the ModelCheckpoint() callback where it sometimes does not save the full model.
This is obviously a problem when you go to load the model after closing your TF session, as the Graph, architecture, and optimizer state are gone, making it hard (if not impossible) to reload that saved model.
Therefore, I am asking whether it is possible to somehow extract the TF session retroactively, from just the .h5 weights file, after the session has closed (resulting from, for example, your Notebook kernel crashing).
Not much code to go on, but there it is:
Given a .h5 file that was saved after each epoch of training a model in Keras, is it possible to extract the Graph session from that .h5 file, and if so, how?
I have several models saved in .h5 format but never called tf.get_session() during the saving of the model weights in h5 format.
with tf.session() as sess:
how to load this model using Tensorflow
TF 2.0 makes this a cinch, but how to solve this on Tensorflow version 1.14?
The end goal of this is to take a model saved with Keras as a .h5 file and do inference with it on Tensorflow Serving, which needs, to my knowledge, a protobuf file in .pb format.
https://medium.com/#pipidog/how-to-convert-your-keras-models-to-tensorflow-e471400b886a
I've tried keras_to_tensorflow:
https://github.com/amir-abdi/keras_to_tensorflow
The code to convert ModelCheckPoint saved in .h5 format to .pb format is shown below:
import tensorflow as tf
# The export path contains the name and the version of the model
tf.keras.backend.set_learning_phase(0) # Ignore dropout at inference
model = tf.keras.models.load_model('./model.h5')
export_path = './PlanetModel/1'
# Fetch the Keras session and save the model
# The signature definition is defined by the input and output tensors
# And stored with the default serving key
with tf.keras.backend.get_session() as sess:
tf.saved_model.simple_save(
sess,
export_path,
inputs={'input_image': model.input},
outputs={t.name:t for t in model.outputs})
For more information, please refer this article.
For other ways to do it, please refer this Stack Overflow Answer.

AttributeError occurs while running ResNet50 using Keras on Pycharm

I was using PyCharm and imported ResNet50 for image recognition of a sample image. When I run the code , the following error occured.
I was learning using an online code which needed to be completed by learners. I configured PyCharm and installed required packages that were recommended. During learning image recognition using ResNet50 , while running the code I ended up with following error. Should I custom install ResNet50 on pycharm for this to work? The instructor said the IDE will auto download ResNet50 during execution of code. Attaching python code below.
import numpy as np
from keras.preprocessing import image
from keras.applications import resnet50
model = resnet50.ResNet50
img = image.load_img("bay.jpg", target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = resnet50.preprocess_input(x)
predictions = model.predict(x)
predicted_classes = resnet50.decode_predictions(predictions, top=9)
print("This is an image of:")
for imagenet_id, name, likelihood in predicted_classes[0]:
print(" - {}: {:2f} likelihood".format(name, likelihood))
This is the resultant error that I am getting during execution.
File "/home/warlock/Downloads/Ex_Files_Building_Deep_Learning_Apps/
Exercise Files/05/image_recognition.py", line 21, in <module>
predictions = model.predict(x)
AttributeError: 'function' object has no attribute 'predict'
You have this error because ResNet50 is a fonction so you need to implement it like a fonction :
model = resnet50.ResNet50()
In order to have a resnet50 model with all default parameters

Resources