A thousand ways to deploy Machine learning models - A.P.I
Tags: machinelearning, python
Published: 2020-01-24 11:22:21

> Originally posted [here](https://towardsdatascience.com/a-thousand-ways-to-deploy-machine-learning-models-part-1-652b59ab92ae)


“What use is a machine learning model if you don’t deploy to production “ — Anonymous

You have done a great work building that awesome 99% accurate machine learning model but your work most of the time is not done without deploying. Most times our models will be integrated with existing web apps, mobile apps or other systems. How then do we make this happen?

I said a thousand, I guess I have just a few. I am guessing you would have found the right one for you before you get past the first two or three. Or do you think there are more? Do let me know, let’s see if we can get a thousand 😄.
Let’s start, how do we deploy Machine learning models or integrate with other systems?

### 1. Via an A.P.I

This involves making your models accessible via an Application Programming Interface (A.P.I).
First, I will be deploying a deep learning model built by [Rising Odegua](https://medium.com/u/10cf0dba197a) to classify malaria cells. The notebook can be found [here](https://github.com/risenW/Disease_classifier/blob/master/model_training_files_and_output/notebooks/malaria-cells-classification-dsn-poster.ipynb).

Then I will also deploy a simple Nigerian movie review classifier model built by [Aminu Israel](https://medium.com/u/985e503f2ec5). The notebook can be found [here](https://github.com/AminuIsrael/NLP-movie-review-model/blob/master/main.ipynb).

#### Deployment of a deep learning model via an A.P.I
After building and testing your deep learning model the next thing to do is to save your model. This can simply be done by adding this line of code.

```python
cnn_model.save('model_name.h5')
```

You can move your saved model to the folder accessible to your A.P.I code. I will be using Flask for deployment but Django, Starlette or any other python frameworks can be used. Here is what my folder structure looks like -

![folder structure](https://thepracticaldev.s3.amazonaws.com/i/3h9fam6zfmbqqw5sw8s3.png)

Coming over to our A.P.I folder (powered by Flask) first thing you will want to do is install the requirements. I saved the requirements in requirements.txt. Here is what it looks like

![requirements.text](https://thepracticaldev.s3.amazonaws.com/i/npwa43czjfhfrd3n3pi6.png)

You can install these requirements simply by running this in your terminal

```pip install -r requirements.txt```

![Import required packages](https://thepracticaldev.s3.amazonaws.com/i/vtl4wx8jfiaffc5v2mly.png)

Just as we preprocessed images before passing them to our neural network for training, we would also preprocess all input images we collect via our A.P.I endpoint.
On line 8 we converted the image collect via the A.P.I endpoint to an array. You will notice a little difference in what you would have done on a norm and that’s simply because of [Flask’s data storage](https://werkzeug.palletsprojects.com/en/0.15.x/datastructures/#werkzeug.datastructures.FileStorage). Using the code on Line 9 would do the same on a Framework like Django or if you are loading from a path on your machine.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/00wqpi25htneayov9xc8.png)

From lines 10–15 we get an RBG version of our image, resized the image to 100x100 and convert the image to a numpy array. We also scaled our image to the range [0, 1] and return a tuple containing True if no error occurs and an array with our image in it.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9zkjyr4xsq98bhackex5.png)

The image above shows the function that performs the magic.
Line 26 simply means that the function “classify_malaria_cells” would be executed when the “classify” endpoint is called.

On line 29 we are checking to see if the request contains an image file. Then we preprocess that image using the helper function we created.
The saved model can be loaded using

```python
from keras.models import load_model
# OR
# from tensorflow.keras.models import load_model
malaria_model = load_model(MODEL_PATH)
```

From lines 34 to 39, we loaded the saved model, performed prediction to determine the class for the image and get an accuracy score for the prediction. On line 40 the result from the model is saved in a python dictionary that will be sent back as a JSON response on line 60.

We end our A.P.I with:

```python
if __name__ == “__main__”:
    flask_app.run(port=8080, debug=False, threaded=False)
```

With this, we have successfully built our A.P.I and it is ready for deployment to any cloud platform.

#### Deploying to Google Cloud

1. Create a Google Cloud Account — [https://cloud.google.com/](https://cloud.google.com/)
2. Create a new Google Cloud Platform project. You can follow the steps [here](https://cloud.google.com/appengine/docs/standard/python3/building-app/creating-gcp-project)
3. Go to the root of this flask project in your terminal and run: ```python gcloud app deploy```

** I have also added a Procfile for deployment to [Heroku](https://www.heroku.com/), just follow the steps [here](https://devcenter.heroku.com/articles/getting-started-with-python) to deploy.


#### Deployment of a machine learning model via an A.P.I

This is almost the same as with the deep learning model. However, saving your models here is quite different. You could save your model using —

```python
import pickle
FILENAME = 'filename.pkl'
pickle.dump(trained_model, open(FILENAME, 'wb'))
```

or

```python
import joblib
FILENAME = 'filename.joblib'
joblib.dump(trained_model, FILENAME)
```

It is advised that **joblib** be used to save models rather than using **pickle** because of its efficiency on objects that carry large numpy arrays internally.
Just as we did with the deep learning model, save your model in a folder accessible to your Flask A.P.I code. Here is what my file structure looks like

![File structure for movie review A.P.I using Flask](https://thepracticaldev.s3.amazonaws.com/i/sn85yzo3y8x9dzvw8qk1.png)

Our requirements are a little different here.

![Requirements for running the Flask A.P.I for movie review](https://thepracticaldev.s3.amazonaws.com/i/ew9pbpqcqvtq3j2cbqh7.png)

You can install these requirements simply by running this in your terminal again.

```pip install -r requirements.txt```


Next, we would import the required modules and initialise some variables.

![import required modules](https://thepracticaldev.s3.amazonaws.com/i/k9673jz29o1p0r1tl31j.png)

![Helper function for tokenization](https://thepracticaldev.s3.amazonaws.com/i/5nr4irfolpa7fsiz776c.png)

In the helper function above, we split the sentence into words, removed the stop words loaded from the pickled file on Line 10. On line 18, a [HashingVectorizer](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.HashingVectorizer.html) is used to the tokenized words into a matrix. The output of this is a scipy.sparse matrix.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/d7i9u7ls7f9lkfaxipzj.png)

Our movie review will be in text format. In Line 24 we check to see if the required form data is been sent and then we assign a variable name to the form data on Line 25. On line 28, we opened and loaded our movie review classifier with pickle(you can achieve the same with joblib). On lines, 29–36 we passed the vectorized movie review into our movie review classifier for prediction, calculated a prediction probability score and created a python dictionary to pass out the results of our prediction. The output is then sent back as a JSON response on Lines 47.

We have successfully built a movie review classification A.P.I and it is ready for deployment to any cloud platform. I have also added a config file for deployment to both [Google Cloud](https://cloud.google.com/) and [Heroku](https://www.heroku.com/).

That’s all on model deployment via an A.P.I. You can find the codes used in this article [here](https://github.com/Emmarex/AThousandWaysToDeployModels---A.P.I).

In my next article, I will share how to deploy machine learning models to Apps with TensorFlow Lite.

Thanks for reading 👍