Design to deployment approach in deep learning | Keras Model | Python
Hello Learners, In this post, I am going to introduce a way, how you can approach a deep learning problem statement from design to deployment in Python using Keras deep learning module. Here I will explain to you, how you can approach building a full-stack web application, using your deep learning model.
So I think you are not clear what I am talking about right?
Now let me make It a little bit easier for you.
if you have gone through my previous tutorials, you must remember that I was creating a note before starting my code and that was please install all the required library in your system right. But now, here the concept is going to be different. Now suppose you have created your deep learning model and you want it to make available publically. How you will do that?
You must be thinking to share your code to Github and share the code with others right? Is it really a solution?
No, This is not the way, why?
I have an answer for you, suppose you have a non-technical guy, who doesn’t have knowledge of coding languages, or even suppose you have, a technical guy but he is not having all the dependencies in his system to run the code. So being an engineer you have to find the solution to resolve the issue of these two guys. But How?
Here the concept of deployment comes into the picture. The overall concept is to deploy our overall application as a package on the third party server, like on a cloud. Once you make It available on a cloud then you will get a link and you can share that link with anyone you want.
Now you don’t have to manage that app from your local system, now that will be managed by the cloud provider. There are many subscription type policies by the cloud providers, even there are many free cloud services where you can deploy your applications.
Some of the examples are, AWS, GCP, Azure, etc.
You have a small task just go and read about cloud infrastructure, you will really enjoy that.
Now you must have an idea that why I want to deploy my deep learning model.
Steps For design To Deployment Approach In Deep Learning
- Identifying The approach To your problem.
- Selecting the Right Framework
- Preparing Data For Training Your NN.
- Checking accuracy
In the above list, you can see that I am listing five steps to approach your complete deep learning problem, from design to deployment.
The very first point will be that you have to identify your problem statement. I mean Your problem may be of the classification task, regression task, or detection task, right? so on the basis of that only, you will move to the next step and selecting a framework, preparing a dataset.
Selecting the right framework, I mean to select the deep learning framework for your problem statements like PyTorch or TensorFlow.
Now the next part is and most important part is to prepare the dataset according to your problem.
After You collect the dataset, Create your model with deep learning framework TensorFlow and Keras deep learning API.
After creating your model check Its accuracy and finally the last step will be to deploy your final application on the cloud.
As you can see in the above theories I have explained to you the steps to move for deploying your deep learning application, Now It’s time to see the example.
So to see see the deployment approach, I will take code from my previous post, Diabetes prediction.
To get the complete working of code, please go through the post and come back to this post to see the deployment approach, As here we are to understand the deployment approach.
Now let’s start our Python code. At the very first, import the required libraries.
import numpy as np import pandas as pd import tensorflow as tf from keras.layers import Dense,Dropout from sklearn.model_selection import train_test_split import matplotlib as mlp import matplotlib.pyplot as plt %matplotlib inline from sklearn.preprocessing import StandardScaler
data=pd.read_csv("/content/drive/My Drive/Internship/prima-indians-diabetes.csv") data.head()
X = data.iloc[:, :-1] Y = data.iloc[:,8]
X_train_full, X_test, y_train_full, y_test = train_test_split(X, Y, random_state=42) X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, random_state=42)
from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_valid = scaler.transform(X_valid) X_test = scaler.transform(X_test)
model.compile(loss="binary_crossentropy", optimizer="SGD", metrics=['accuracy'])
model_history = model.fit(X_train, y_train, epochs=200, validation_data=(X_valid, y_valid))
y_pred = model.predict(X_new) print (y_pred)
So in the above code, I am creating a deep neural network model for binary classification to classify diabetes based on the given input features. So here I am not going to explain to you about the code how that is working because here we are to see the approach to deploy our model. If you want to see a detailed explanation about the above code just read this Diabetes prediction.
Hope you have read the blog and now we are ready to move forward.
In the above block of code, I am saving my model, I mean as we have created and compiled our model, now I am saving my model. The purpose to save our model is that we don’t want to compile our model again and again. once we will finalize our model, we will save it and after that whenever we will need it, we will load it.
ml_model = keras.models.load_model("Diabtese_model.h5")
In the above block of code, I am loading my model. Now whatever we will do in the next steps, like predicting the values, we will use ml_model only.
y_pred = ml_model.predict(X_new) print (y_pred)
In the above block of code, I am predicting the values with my created model. You can check the output above.
Now It is time to predict the values for the single observation. We want to predict the values of a single observation because when you will create your web app with this model, the user will check the output by giving the respected input values in the input field.
a = [ 2.79951672, 0.24179246, -4.01974435, 0.58212165, -0.68092235, 0.99437177, 0.28750034, 0.88979017] b = np.array(a) b = b.reshape(1, -1) y_pred_single_obs = ml_model.predict(b) round(float(y_pred_single_obs), 2)
In the above block of code, I am storing the input values into a list. If you will look closely at the values of a, you will find that length of a is 8.
Are you worried, Why I am taking the 8 values?
Let me make it clear for you if you will remember or just look at the input dimension of the above model, you will find that there are a total of 8 input feature and that is the reason I am taking it as the length of 8.
And the next thing I am changing the list into an array because mathematical operations will be performed with the help of numbers, which is stored in an array. After storing the values into an array, I am reshaping the array and finally, I am using my model to predict the values.
All right, Let me make It more clear for you. See the Python code below:
from sklearn.externals import joblib times_preg = 2.79951672 glucose= 0.24179246 BP = -4.01974435 stinmm = 0.58212165 Insuline = -0.68092235 Mass=0.99437177 age=0.28750034 diabtese_pedigree_func=0.88979017 pred_args = [times_preg,glucose,BP,stinmm,Insuline,Mass,age,diabtese_pedigree_func] pred_args_arr = np.array(pred_args) pred_args_arr = pred_args_arr.reshape(1, -1) model_prediction = ml_model.predict(pred_args_arr) round(float(model_prediction), 2)
The above code is the same as I have done before this block, the difference is that here I am assigning the numerical values to Its respective input feature as It is in our dataset. The output will be the same.
If you will see, you will find that the value is 0.68 so It will be classified as 1 because as the values will be greater than 0.5, we will take it as positive. Here one means the patient is diabetic.
Well congrats to reach here, we have created our model, saved our model, and predicted the values for a single observation.
The rest is to do the deployment step. Before moving to the deployment step, you have to create your web app in Python, serving HTML and Bootstrap as the frontend and flask as a backend.
You can use the Django framework also. Any Python web framework can be used, like Flask or Django. We are more concentrated on the Deployment approach.
Once you create your web application in Flask, You are ready to deploy your web app on docker or Heroku.
Let me give a brief description of the docker.
Docker is a platform as a service and it uses OS-Level virtualization to deliver software as packages called containers.
Overall you can say, docker, provide you to create a container, In which you can bundle your packages.
Just go and read about it.
I hope now, you have an overview that how you can approach your deep learning applications from design to deployment using the Keras TensorFlow API in Python programming.
Thank You, For Your Time.
In this post you have seen, how we can approach a deep learning problem statement from design to deployment. You can try this approach for your upcoming project, It will be very helpful to you.
I am providing you code here with the link codes.
Hope you have read the previous blog and you have already downloaded the dataset from there but still I am giving you the link code